-
-
March 5, 2024 at 4:52 pm
Matt Wilson
SubscriberHi,
Â
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! -
March 5, 2024 at 8:49 pm
Guilin Sun
Ansys EmployeeSimple methods to save data into a file are:
matlabsave in .mat format;matlabsave - Script commandsavedata 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.
Â
-
March 6, 2024 at 2:23 pm
Matt Wilson
Subscriber
-
-
March 6, 2024 at 12:18 pm
Matt Wilson
Subscriber -
March 6, 2024 at 4:16 pm
Guilin Sun
Ansys EmployeeUnfortunately 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
Â
-
March 6, 2024 at 9:13 pm
Matt Wilson
SubscriberOkay. 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.
-
-
March 6, 2024 at 10:25 pm
Guilin Sun
Ansys EmployeeYou 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.
Â
Â
-
March 7, 2024 at 11:16 am
Matt Wilson
SubscriberHi, 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? -
March 7, 2024 at 12:14 pm
Matt Wilson
SubscriberApologies. 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?
-
-
March 7, 2024 at 4:08 pm
Guilin Sun
Ansys EmployeeI 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.Â
Â
-
March 7, 2024 at 4:16 pm
Matt Wilson
SubscriberHi 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.
-
-
March 8, 2024 at 1:52 am
Guilin Sun
Ansys EmployeeEx,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.
Â
Â
-
- The topic ‘Exporting a large number of data sets into TXT from varFDTD’ is closed to new replies.
-
3029
-
971
-
858
-
841
-
777
© 2025 Copyright ANSYS, Inc. All rights reserved.