How to plot, save, calculate, manipulate, ... everything you want during the simulation is running independent from the detectors

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to plot, save, calculate, manipulate, ... everything you want during the simulation is running independent from the detectors

tinolang
Administrator
There is a way to plot, save, calculate, ... everything you want during the simulation using the inloopFun property in obj.simProp  

Here an example:

obj = chi3D();

obj('inloopFun','myfun'); %the string in obj.simProp.inloopFun defines an external function which will be called during each loop of the simulation


and here an example for myfun.m:
function myfun()

% evalin('caller', 'who') %you can figure out which internal variables are used in the code if you set a break here during calling your simulation and execute evalin('caller', 'who')

k=evalin('caller', 'k');disp(k) %k is an integer control variable of the simulation loop, e.g. for k=1:N,...,end

obj=evalin('caller', 'obj'); % current chi3D object within the loop
E_ftfxfy=evalin('caller', 'Eftfxfy_o'); % ordinary complex field after k loops 


if(~mod(k,10)) % to speed up the simulation

    [I_freq,I_lam,Phase,GD,GDD]=obj.getSpectrum(E_ftfxfy,'alpha',obj.beamProp.alpha_x_pulse2) ; % alpha is the propagation angle of the spectral components of interest. Only needed for phase, GD, GDD,...
    figure(111)

    yyaxis left
    plot(obj.constVect.lambda,I_lam./max(I_lam))
    xlim([450e-9 550e-9])
    ylabel('Spectral Intensity (norm.)')
    xlabel('Wavelength (nm)')

    yyaxis right
    plot(obj.constVect.lambda,GDD*1e30)
    ylabel('GDD (fs2)')
    
end
Tino Lang
Reply | Threaded
Open this post in threaded view
|

Re: How to plot, save, calculate, manipulate, ... everything you want during the simulation is running independent from the detectors

Peter
Hej everyone, here is an example for saving data calculated within the myfun function.
It is important to define persistent variables, otherwise these variables exist only within one function call.

Cheers
Peter

main program
% runs standard example of chi3D
% executes every iteration a function

obj = chi3D();
obj('inloopFun','myfun');
[Eftfxfy_o,Eftfxfy_e]=run(obj,{'pulse1'});

function called in run methode
% performs every n-th step a calculation & saves result

function myfun

n= 10;                                                % reduction factor
persistent i k myData
k=evalin('caller', 'k');
% disp(k);
% k is an integer control variable of the simulation loop, e.g. for k=1:N,...,end
obj=evalin('caller', 'obj');                     % current chi3D object within the loop
E_ftfxfy=evalin('caller', 'Eftfxfy_o');      % ordinary complex field after k loops

if isempty(k)                                       % initialize array
    k=1;
    myData=zeros(obj.constVect.Nt,obj.simProp.Nz/n);
end

if(~mod(k,n))                                     % to speed up the simulation
    [I_freq]=obj.getSpectrum(E_ftfxfy,'alpha',obj.beamProp.alpha_x_pulse2) ;
    figure(10)                                       % use plot function only if necessary
    plot(I_freq)
    i=k/n
    myData(:,i)=I_freq;
end

if i==obj.simProp.Nz/n
    h.userdata=myData;
    save('myData.mat','myData');
end
Reply | Threaded
Open this post in threaded view
|

Re: How to plot, save, calculate, manipulate, ... everything you want during the simulation is running independent from the detectors

tinolang
Administrator
Great! Thank you Peter

just one comment:

better use "if isempty(myData) " to acquire the memory for myData instead of "if isempty(k)" since k will never be empty.

Tino Lang