This example shows how to visualize 3D data from Lumerical FDTD in MATLAB.
The MATLAB function scatter3() allows visualization of data, in this case, E-field values, at points in 3D space. In this example, a single frequency Gaussian beam is focused in free space and the area around the focal point is imaged. Occlusion is a major problem for such plots, thus, we threshold the number points that are displayed, focusing only on the volume of interest. In the resulting figure, we can clearly see the focal point of the Gaussian beam.
First, download and run the simulation usr_matlab_simple_beam.fsp . Since there are no structures, the simulation should run fairly quickly. Next, run the script usr_script_3d_scatter.lsf . It picks out points over the specified threshold value and writes them to e_field.txt .
m_name = "monitor2"; thresh = 1.3; # set threshold, fields above this value will be shown mon=pinch(getelectric(m_name),4,1); # picks the first freq 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(monthresh); 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)); 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));
Next, open up Matlab and set the working directory to where e_field.txt was saved. Running the following code, contained within usr_matlab_script_3d_scatter.m will generate the interactive 3D plot seen above. Note that the axis limits should be set to the same values for a correct spatial representation. The image above is stretched out in the x and y directions whereas in reality, the beam is spatially more narrow.
% simple matlab script that plots single frequency 3D monitor data e_field=dlmread('e_field.txt','\t'); % tab delimited 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(a*1e6,b*1e6,c*1e6,size,e); xlabel('x-axis (um)'); ylabel('y-axis (um)'); zlabel('z-axis (um)');
See also
MATLAB - Lumerical integrations
Creating 2D image plots with MATLAB