Photonics

Photonics

Topics related to Lumerical and more.

Exporting a large number of data sets into TXT from varFDTD

    • Matt Wilson
      Subscriber

      Hi,

       

      I have used a varFDTD frequency monitor to get a plot of the electric field distribution in a disk in 2D (2D Z-normal to be specific). I have ran this simulation over a large number of source wavelength values (I have 501 plots). I want to now export these plots as TXT files. It allows you to do this in the E visualiser individually but I was hoping to not have to do it that way for all 501 plots. I am having trouble automating this through scripting. I can't see anything in the documentation for the scripting language. I essentially want to export the TXT information from the monitor for each value of wavelength that I have used. Can anyone advise? For reference, I have pasted my script below:

      monitorName = "E_field";

      data = getresult(monitorName, "E");

      wavelengths = data.lambda * 10^9;

      # Loop over each wavelength value

      for (i=500; i<=size(wavelengths); i=i+1){

       

      fileName = "data" + num2str(i) + ".txt";

      #write(fileName, num2str(data.E), "overwrite");

      savedata(fileName, data);

      }

      Thanks in advance!

    • Guilin Sun
      Ansys Employee

      Simple methods to save data into a file are:


      matlabsave in .mat format;matlabsave - Script command

      savedata in ldf format savedata - Script command

      If you really want to save data in txt format, you will need to specify the data, eg

      write(fileName, num2str(data.E(i,j,k,m)), "overwrite");

      or one line have all the data along one axis, say x (which has fewer data):

      write(fileName, num2str(data.E(1:nx,j,k,m)),"overwrite");

      where

      x=data.x;

      nx=length(x)

      I strongly suggest to use the standard, simple methods to save and load the data instead of txt format.

       

      • Matt Wilson
        Subscriber

         

        That didn't work for me. Basically what I am after is a script that will give me the same file that I would get if I clicked this button in the visualizer for each value of wavelength in the simualtion. I need it in txt so that I can export to a custom python script for analysis. 

    • Matt Wilson
      Subscriber



      That didn't work for me. Basically what I am after is a script that will give me the same file that I would get if I clicked this button in the visualizer for each value of wavelength in the simualtion. I need it in txt so that I can export to a custom python script for analysis. 

    • Guilin Sun
      Ansys Employee

      Unfortunately the output button has its own data format and information. Usually you will need to use "write" to output data into txt file.

      Visualizer: https://optics.ansys.com/hc/en-us/articles/360037222234-Using-the-data-visualizer-and-figure-windows

       

      • Matt Wilson
        Subscriber

        Okay. Do you have any advice then for scripting? How can I properly extract the electric field data to txt for each wavelegnth slice? I am having trouble doing this.

    • Guilin Sun
      Ansys Employee

      You know how to use "write" to save data. Next, you will choose what data and style you want to save. For example, the txt file from the output in Visualizer contains

      x;

      y;

      z;

      f;

      E field data. It can be point by point, or more than one points. What ever you save, you will know it when you import it to Python.

      note that E is a 4D matrix from the monitor: x,y,z,f.

      once you know this, please refer to my first reply for the script.

       

       

      • Matt Wilson
        Subscriber

        Hi, sorry to be a pain with this haha. So I have rewritten my script as per your suggestion. This is what I now have:

        Ex = real(getdata("E_field", "Ex"));

        Ey = real(getdata("E_field", "Ey"));

         

        x = getdata("E_field", "x");

        y = getdata("E_field", "y");

        z = getdata("E_field", "z");

        f = getdata("E_field", "f");

         

        x_max = length(x);

        y_max = length(y);

        z_max = length(z);

         

        lambda = (c/f) * 10^9;

         

        for (m=1:5){

        fileName = num2str(lambda(m)) + "nm.txt";

        E = sqrt(Ex(1:x_max, 1:y_max, 1:z_max, 1:m)^2 + Ey(1:x_max, 1:y_max, 1:z_max, 1:m)^2);

        write(fileName, num2str(E(1:x_max, 1:y_max)), "overwrite");

        }

        It prints the E field data into a text file but it only seems to be doing it for the first data set (i.e. not iterating through the different wavelength values). Can you suggest where I could fix this?

      • Matt Wilson
        Subscriber

        Apologies. I tweaked my code and it is now iterating through the range of wavelength values. I apear to have a seperate issue now though. I think there is a problem with how I am calculating the E field data from the Ex and Ey componants:

        Ex = real(getdata("E_field", "Ex"));

        Ey = real(getdata("E_field", "Ey"));

         

        E_data = sqrt(Ex^2 + Ey^2);

         

        x = getdata("E_field", "x");

        y = getdata("E_field", "y");

        z = getdata("E_field", "z");

        f = getdata("E_field", "f");

         

        x_max = length(x);

        y_max = length(y);

        z_max = length(z);

         

        lambda = (c/f) * 10^9;

         

        for (m=1:5){

        fileName = num2str(lambda(m)) + "nm.txt";

        E = E_data(1:x_max, 1:y_max, 1:z_max, m);

        write(fileName, num2str(E(1:x_max, 1:y_max)), "overwrite");

        }

        The E field that is returned by my code differs from the E field that is seen in the monitor. I am pretty certain it is a misunderstanding on my end of how the E field is calculated from the raw data in the monitor. Can you advise how to fix this?

    • Guilin Sun
      Ansys Employee

      I guess it is because of num2str(E(1:x_max, 1:y_max)). Please try to use point to point so you will know how the data is written. 

       

      • Matt Wilson
        Subscriber

        Hi Guilin, the script now iterates through the for loop properly. I seem to be having issues with the values of E. I think it is due to a misunderstanding of how it is calculated from the raw data. Can you advise?

         

        Ex = real(getdata("E_field", "Ex"));

        Ey = real(getdata("E_field", "Ey"));

        E_data = sqrt(Ex^2 + Ey^2);

        This is how I am currently calculating it.

    • Guilin Sun
      Ansys Employee

      Ex,Ey are complex fields;

      what you calculated is the real part. Usually people do not use the real part, since it does not give information of the magnitude. You should save complax number, and use Python or other language to process the data as you wish.

       

       

Viewing 6 reply threads
  • The topic ‘Exporting a large number of data sets into TXT from varFDTD’ is closed to new replies.