# How to save 3D data in the temperature monitor and do 3D plotting?

Hi, I finished a simulation on photothermal heating with 3D temperature monitor and wanted to do 3D plotting in matlab. I wanted to make a similar graph below, but there were some problems in getting data from T monitor and doing the Device-matlab interconnection.

The script below was from a 3D plotting example for E-field in KB, https://kb.lumerical.com/en/index.html?ref_scripts_tutorial_matlab.html. Problems are in brackets in bold.

m_name = “monitor2”;
thresh = 1.3; # set threshold, fields above this value will be shown (How to set this value in T field)

mon=pinch(getelectric(m_name),4,1); # picks the first freq (How to script for T field, “gettemperature”?)
xmon=getdata(m_name,“x”);
ymon=getdata(m_name,“y”);
zmon=getdata(m_name,“z”);
n=size(xmon);
n=n(1);

# setup empty arrays

s=sum(mon>thresh);
x=matrix(1,s);
y=matrix(1,s);
z=matrix(1,s);
ef=matrix(1,s);

# get positions of (x,y,z) over threshold value

t=1; #counter
for(i=1:n) {
for(j=1:n) {
for(k=1:n) {
if(abs(mon(i,j,k))>thresh) {
x(t)=xmon(i);
y(t)=ymon(j);
z(t)=zmon(k);
ef(t)=abs(mon(i,j,k)); (How to script for T in this line? )
t=t+1;
}
}
}
}

# write data to a txt file to be imported into Matlab

f=“e_field.txt”;
del(f);
write(f,num2str(x));
write(f,num2str(y));
write(f,num2str(z));
write(f,num2str(ef));

% simple matlab script that plots single frequency 3D monitor data
a=e_field(1,:); % x
b=e_field(2,:); % y
c=e_field(3,:); % z
e=e_field(4,:); % e-field value
size=25; % rendering size of plotted points

% plot the data and label
scatter3(a1e6,b1e6,c*1e6,size,e); ( Still “scatter”?)
xlabel(‘x-axis (um)’);
ylabel(‘y-axis (um)’);
zlabel(‘z-axis (um)’);

1 Like

Hi @mcliang1991, the mesh in DEVICE is quite different from the one in FDTD. DEVICE uses a finite-element mesh whereas in FDTD the mesh is rectangular. As a result, the format of 3D spatial data is very different in DEVICE and when plotting the data in Matlab, you have to use a different command.

The good news is that there is no need for any data conditioning. Once you are done with your HEAT simulation, the results are available either in the solver region or in the monitors. In either case, you can read the data using the “getresult” script command. The data will be in a format that we call “unstructured dataset”. The dataset will have elements named x, y, z, and elements. These matrices (x,y,z) contain the information about the x,y,z coordinates of the vertices and “elements” is the connectivity matrix of the finite-element mesh. You can use the following script commands to read these matrices along with the temperature data from a temperature monitor (for example):

data = getresult("HEAT::temp_monitor_name","temperature"); # result from monitor x = data.x; y = data.y; z = data.z; elements = data.elements;
T = data.T; # reading the temperature

matlabsave("temp_data.mat",x,y,z,elements,T); # save the data in a file

Once you are in Matlab, you can use the command “trisurf” to create a surface plot of the spatial data. For example for the above mentioned data the command in Matlab will be,

load('temp_data.mat'); trisurf(elements,x,y,z,T);

Once the figure opens, you can use the “Jet” color map to get a color map similar to the one used in DEVICE.

4 Likes

I just tried your script and got the matlab 3D image, but the image was different from that in Device, especially on the srface, is it because of the non-uniform mesh in Device? And how to do the interpolation to avoid this mismatch?

1 Like

Hi @mcliang1991, Yes I can see that the surface does look somewhat rough in the Matlab plot. I am not sure what exactly is responsible for that. I will discuss this with my colleagues and get back to you. Unfortunately I am travelling right now so my response might be a bit delayed. My apologies for that in advance.

1 Like

Hi @mcliang1991, I have talked with my colleague and it looks like that the “trisurf” command was a poor choice since that particular command is used to create a surface plot not a volume plot. For our purpose the “tetramesh” command would be more appropriate. This command takes the connectivity matrix (elements), vertex matrix (x,y,z) and the spatial data (temperature) to create a volume plot. However, the temperature data needs to be defined for each element rather than for each vertex. Therefore you will need to calculate the average temperature over each tetrahedral element. To do that, you can simply take the average of the temperatures at the four corners of a tetrahedron as follows:

load('temp_data.mat'); T_ele = 0.25*(T(elements(:,1))+T(elements(:,2))+T(elements(:,3))+T(elements(:,4))); tetramesh(elements,[x,y,z],T_ele);

NOTE: I have tried this command for a 3D temperature plot from this example (https://kb.lumerical.com/en/index.html?sp_photothermal-heating-in-plasmo.html). However I noticed that when the number of elements is very large the plotting in Matlab was very slow.

3 Likes

Thank you foryour assistance so far!
I have just finished the 3D plotting although the Matlab was very slow during the process!

1 Like

Hi @mcliang1991, there was a typo in my script (line 2 where I was calculating the average temperature). I have corrected it so please use the updated version in your script.