Tutorial example demonstrating how to use StabFem for a time-stepping calculation (ADVANCED)

This tutorial script is designed as a support for chapter 4 of the StabFem manual available here

We show how to compute the solution of a time-dependant through time-stepping and how to import, plot, and organise the results in a database.

This example is based on the "Thermal Conduction example in the FreeFem manual".

In a previous example called SCRIPT_ThermalConduction_Basic.m the basic usage was demonstrated.

In this second example we show more advanced options : * Using database manager to monitor several time-stepping simulations, * Using non-interactive mode to launch very long simulations and check results in a second step.

The mesh generator is Mesh_ThermalConduction.edp and the freefem solver is TimeStepper_ThermalConduction.edp

Contents

Initialization

close all;
addpath([fileparts(fileparts(pwd)) '/SOURCES_MATLAB']);

SF_Start('verbosity',4);
% higest levels of verbosity results in more messages.
% recommended values are 2 (minimal notice) and 4 (notice + freefem output). 6 and more is debug mode.

SF_DataBase('open','./WORK/');
% this is to initialize the database management in a folder called 'WORK'.
% NB you may use 'create' instead of 'open' to clean up
NOTICE  - Initialization already done
WARNING - Detected verbosity = 4 in Publish mode ; not recommended ! switching to 2 instead

Chapter 1 : creating mesh

ffmesh = SF_Mesh('Mesh_ThermalConduction.edp');

Plot this mesh

figure; SF_Plot(ffmesh);

Chapter 2 : Launching a simple time-stepping simulation

Run1 = SF_TS_Launch('TimeStepper_ThermalConduction.edp','Mesh',ffmesh,'Options',{'Niter',100,'alpha',.1},'wait',true);

Contrary to the basic example where DataBae management was disabled, the output results are not written in the current working folder, but in a subfolder 'TimeStepping/Run1' of the 'Database' folder.

Let's check this :

dir('WORK/TimeStepping/RUN000001')

% Now importing the contents just as in previous basic example.
[stats,snapshots] = SF_TS_Check(Run1);
.                       Snapshot_00000040.txt   Snapshot_00000100.ff2m  
..                      Snapshot_00000050.ff2m  Snapshot_00000100.txt   
.meshfilename           Snapshot_00000050.txt   TS.status               
Simulation.log          Snapshot_00000060.ff2m  TimeStats.ff2m          
Snapshot_00000010.ff2m  Snapshot_00000060.txt   TimeStats.txt           
Snapshot_00000010.txt   Snapshot_00000070.ff2m  freefemwarning.txt      
Snapshot_00000020.ff2m  Snapshot_00000070.txt   mesh.ff2m               
Snapshot_00000020.txt   Snapshot_00000080.ff2m  mesh.msh                
Snapshot_00000030.ff2m  Snapshot_00000080.txt   mesh_connectivity.ff2m  
Snapshot_00000030.txt   Snapshot_00000090.ff2m  
Snapshot_00000040.ff2m  Snapshot_00000090.txt   

#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000001/ : 
 Simulation completed

 Time-stepping simulation started 23-Mar-2023 12:16:33
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : 
 Argument string             :  -Niter 100 -alpha 0.1 
 -> Found time series spaning from iter = 1 to 100 ; time = 0.1 to 10
 -> Found 10 Snapshots in this folder. Importing them....
 Simulation completed
 
 

Plotting a few snapshots;

figure;
subplot(3,1,1); SF_Plot(snapshots(1),'T','title','Field at t=1','contour','on');
subplot(3,1,2); SF_Plot(snapshots(2),'T','title','Field at t=2','contour','on');
subplot(3,1,3); SF_Plot(snapshots(4),'T','title','Field at t=4','contour','on');

Plotting time-series of T(0.5,3)

figure; plot(stats.t,stats.u3);xlabel('t');ylabel('T(3,.5)');

Chapter 3 : Starting a second time-stepping in a new folder, with a diffent value of physical parameter alpha

Run2 = SF_TS_Launch('TimeStepper_ThermalConduction.edp','Mesh',ffmesh,'Init',snapshots(end),'wait',true,'Options',{'Niter',100,'alpha',.25});
stats2 = SF_TS_Check(Run2);
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000002/ : 
 Simulation completed

 Time-stepping simulation started 23-Mar-2023 12:16:45
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : ./WORK/TimeStepping/RUN000001/Snapshot_00000100.txt
 Argument string             :  -Niter 100 -alpha 0.25  -i0 100 -t0 10
 -> Found time series spaning from iter = 101 to 200 ; time = 10.1 to 20
 -> Found 10 Snapshots in this folder.
#################################################################################
 
.... SNAPSHOTS FOUND IN DIRECTORY ./WORK/TimeStepping/RUN000002/
     (couples of .txt/.ff2m files )
 
Total : 10 files. Displaying only first and last ones
Name                     | Type         | Date                 | alpha      | t          | it        
Snapshot_00000110.txt    | DNSField     | 23-mars-2023 12:16:46 | 0.25       | 11         | 110       
Snapshot_00000120.txt    | DNSField     | 23-mars-2023 12:16:46 | 0.25       | 12         | 120       
(skipping intermediate)    |
Snapshot_00000190.txt    | DNSField     | 23-mars-2023 12:16:47 | 0.25       | 19         | 190       
Snapshot_00000200.txt    | DNSField     | 23-mars-2023 12:16:47 | 0.25       | 20         | 200       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',2,200)"
 Simulation completed
 
 

Chapter 3bis : Restarting the first simualtion, doing 100 more time steps, writing results in same folder

Run3 = SF_TS_Launch('TimeStepper_ThermalConduction.edp','Mesh',ffmesh,'Init',snapshots(end),'wait',true,'Options',{'Niter',100,'alpha',.1},'samefolder',true);
stats3 = SF_TS_Check(Run3);
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000001/ : 
 Simulation completed

 Time-stepping simulation started 23-Mar-2023 12:16:33
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : 
 Argument string             :  -Niter 100 -alpha 0.1 

 Time-stepping simulation RESTARTED 23-Mar-2023 12:16:50
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : ./WORK/TimeStepping/RUN000001/Snapshot_00000100.txt
 Argument string             :  -Niter 100 -alpha 0.1  -i0 100 -t0 10
 -> Found time series spaning from iter = 1 to 200 ; time = 0.1 to 20
 -> Found 20 Snapshots in this folder.
#################################################################################
 
.... SNAPSHOTS FOUND IN DIRECTORY ./WORK/TimeStepping/RUN000001/
     (couples of .txt/.ff2m files )
 
Total : 20 files. Displaying only first and last ones
Name                     | Type         | Date                 | alpha      | t          | it        
Snapshot_00000010.txt    | DNSField     | 23-mars-2023 12:16:35 | 0.1        | 1          | 10        
Snapshot_00000020.txt    | DNSField     | 23-mars-2023 12:16:35 | 0.1        | 2          | 20        
(skipping intermediate)    |
Snapshot_00000190.txt    | DNSField     | 23-mars-2023 12:16:54 | 0.1        | 19         | 190       
Snapshot_00000200.txt    | DNSField     | 23-mars-2023 12:16:54 | 0.1        | 20         | 200       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',1,200)"
 Simulation completed
 
 

Chapter 4 : lanching a simulation in non-interactive mode

Run4 = SF_TS_Launch('TimeStepper_ThermalConduction.edp','Mesh',ffmesh,'Options',{'Niter',100,'alpha',.1},'wait',false);

In this way the simulation will be launched as a remote job, so that you can continue to work in Matlab/Octave.

Here the simulations will take less than 10 seconds. For demonstration, let's wait for 10 seconds and check the status of the simulation:

pause(10);
[stats4,snapshots4] = SF_TS_Check(Run4);
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000003/ : 
 Simulation completed

 Time-stepping simulation started 23-Mar-2023 12:16:55
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : 
 Argument string             :  -Niter 100 -alpha 0.1 
 -> Found time series spaning from iter = 1 to 100 ; time = 0.1 to 10
 -> Found 10 Snapshots in this folder. Importing them....
 Simulation completed
 
 

Chapter 5 : how to stop a simulation.

Suppose you lauch a simulation in the way :

Run5 = SF_TS_Launch('TimeStepper_ThermalConduction.edp','Mesh',ffmesh,'Options',{'Niter',1e4,'alpha',.1},'wait',false);

% ... and then fter 1 second realize OOPS TOO MANY TIME STEPS ! Here is the emergency stop procedure :
pause(1);
SF_TS_Stop(Run5);

%now checking what has been done :
pause(1);
[stats5,snapshots5] = SF_TS_Check(Run5);
 Simulation still running
WARNING - Signal has been sent through file TS.status 
WARNING - Simulation will normally stop after current time step. Please use no SF_TS_Check to get the results.
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000004/ : 
 Simulation status could not be determined

 Time-stepping simulation started 23-Mar-2023 12:17:09
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : 
 Argument string             :  -Niter 10000 -alpha 0.1 
 -> Found time series spaning from iter = 1 to 234 ; time = 0.1 to 23.4
 -> Found 24 Snapshots in this folder. Importing them....
 Simulation status could not be determined
 
 

Checking the whole content

ss = SF_TS_Check

% plot
figure;
plot(ss(1).t,ss(1).u3,'r',ss(2).t,ss(2).u3);
legend('run 1 : alpha = .1','run 2 : alpha = 0.25 restarted from run 1')

% [[PUBLISH]] -> this tag is here to tell the git runner to automatically generate a web page from this script


%
fclose(fopen('SCRIPT_ThermalConduction_Advanced.success','w+'));
#################################################################################
.... Checking for simulation results in folder ./WORK/TimeStepping
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000001/ : 
 Simulation completed

 Time-stepping simulation started 23-Mar-2023 12:16:33
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : 
 Argument string             :  -Niter 100 -alpha 0.1 

 Time-stepping simulation RESTARTED 23-Mar-2023 12:16:50
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : ./WORK/TimeStepping/RUN000001/Snapshot_00000100.txt
 Argument string             :  -Niter 100 -alpha 0.1  -i0 100 -t0 10
 -> Found time series spaning from iter = 1 to 200 ; time = 0.1 to 20
 -> Found 20 Snapshots in this folder.
#################################################################################
 
.... SNAPSHOTS FOUND IN DIRECTORY ./WORK/TimeStepping/RUN000001/
     (couples of .txt/.ff2m files )
 
Total : 20 files. Displaying only first and last ones
Name                     | Type         | Date                 | alpha      | t          | it        
Snapshot_00000010.txt    | DNSField     | 23-mars-2023 12:16:35 | 0.1        | 1          | 10        
Snapshot_00000020.txt    | DNSField     | 23-mars-2023 12:16:35 | 0.1        | 2          | 20        
(skipping intermediate)    |
Snapshot_00000190.txt    | DNSField     | 23-mars-2023 12:16:54 | 0.1        | 19         | 190       
Snapshot_00000200.txt    | DNSField     | 23-mars-2023 12:16:54 | 0.1        | 20         | 200       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',1,200)"
 Simulation completed
 
 
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000002/ : 
 Simulation completed

 Time-stepping simulation started 23-Mar-2023 12:16:45
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : ./WORK/TimeStepping/RUN000001/Snapshot_00000100.txt
 Argument string             :  -Niter 100 -alpha 0.25  -i0 100 -t0 10
 -> Found time series spaning from iter = 101 to 200 ; time = 10.1 to 20
 -> Found 10 Snapshots in this folder.
#################################################################################
 
.... SNAPSHOTS FOUND IN DIRECTORY ./WORK/TimeStepping/RUN000002/
     (couples of .txt/.ff2m files )
 
Total : 10 files. Displaying only first and last ones
Name                     | Type         | Date                 | alpha      | t          | it        
Snapshot_00000110.txt    | DNSField     | 23-mars-2023 12:16:46 | 0.25       | 11         | 110       
Snapshot_00000120.txt    | DNSField     | 23-mars-2023 12:16:46 | 0.25       | 12         | 120       
(skipping intermediate)    |
Snapshot_00000190.txt    | DNSField     | 23-mars-2023 12:16:47 | 0.25       | 19         | 190       
Snapshot_00000200.txt    | DNSField     | 23-mars-2023 12:16:47 | 0.25       | 20         | 200       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',2,200)"
 Simulation completed
 
 
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000003/ : 
 Simulation completed

 Time-stepping simulation started 23-Mar-2023 12:16:55
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : 
 Argument string             :  -Niter 100 -alpha 0.1 
 -> Found time series spaning from iter = 1 to 100 ; time = 0.1 to 10
 -> Found 10 Snapshots in this folder.
#################################################################################
 
.... SNAPSHOTS FOUND IN DIRECTORY ./WORK/TimeStepping/RUN000003/
     (couples of .txt/.ff2m files )
 
Total : 10 files. Displaying only first and last ones
Name                     | Type         | Date                 | alpha      | t          | it        
Snapshot_00000010.txt    | DNSField     | 23-mars-2023 12:16:57 | 0.1        | 1          | 10        
Snapshot_00000020.txt    | DNSField     | 23-mars-2023 12:16:57 | 0.1        | 2          | 20        
(skipping intermediate)    |
Snapshot_00000090.txt    | DNSField     | 23-mars-2023 12:16:58 | 0.1        | 9          | 90        
Snapshot_00000100.txt    | DNSField     | 23-mars-2023 12:16:58 | 0.1        | 10         | 100       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',3,100)"
 Simulation completed
 
 
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000004/ : 
 Simulation status could not be determined

 Time-stepping simulation started 23-Mar-2023 12:17:09
 Solver                      : TimeStepper_ThermalConduction.edp
 Mesh file name              : ./WORK/MESHES/FFMESH_000001.msh
 Initial condition file name : 
 Argument string             :  -Niter 10000 -alpha 0.1 
 -> Found time series spaning from iter = 1 to 234 ; time = 0.1 to 23.4
 -> Found 24 Snapshots in this folder.
#################################################################################
 
.... SNAPSHOTS FOUND IN DIRECTORY ./WORK/TimeStepping/RUN000004/
     (couples of .txt/.ff2m files )
 
Total : 24 files. Displaying only first and last ones
Name                     | Type         | Date                 | alpha      | t          | it        
Snapshot_00000010.txt    | DNSField     | 23-mars-2023 12:17:10 | 0.1        | 1          | 10        
Snapshot_00000020.txt    | DNSField     | 23-mars-2023 12:17:10 | 0.1        | 2          | 20        
(skipping intermediate)    |
Snapshot_00000230.txt    | DNSField     | 23-mars-2023 12:17:12 | 0.1        | 23         | 230       
Snapshot_00000234.txt    | DNSField     | 23-mars-2023 12:17:12 | 0.1        | 23.4       | 234       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',4,234)"
 Simulation status could not be determined
 
 
 

ss = 

  1x4 struct array with fields:

    filename
    DataDescription
    datatype
    datastoragemode
    datadescriptors
    t
    u3
    u6
    iter
    status