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 16-Jul-2021 00:34:44
 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....

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 16-Jul-2021 00:34:49
 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     | 16-juil.-2021 00:34:49 | 0.25       | 11         | 110       
Snapshot_00000120.txt    | DNSField     | 16-juil.-2021 00:34:50 | 0.25       | 12         | 120       
(skipping intermediate)    |
Snapshot_00000190.txt    | DNSField     | 16-juil.-2021 00:34:50 | 0.25       | 19         | 190       
Snapshot_00000200.txt    | DNSField     | 16-juil.-2021 00:34:50 | 0.25       | 20         | 200       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',2,200)"

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);
workdir =

    '/TimeStepping/RUN000001/'

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

 Time-stepping simulation started 16-Jul-2021 00:34:44
 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 16-Jul-2021 00:34:51
 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     | 16-juil.-2021 00:34:44 | 0.1        | 1          | 10        
Snapshot_00000020.txt    | DNSField     | 16-juil.-2021 00:34:44 | 0.1        | 2          | 20        
(skipping intermediate)    |
Snapshot_00000190.txt    | DNSField     | 16-juil.-2021 00:34:52 | 0.1        | 19         | 190       
Snapshot_00000200.txt    | DNSField     | 16-juil.-2021 00:34:52 | 0.1        | 20         | 200       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',1,200)"

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);
ans =

    './WORK/TimeStepping/RUN000003/TS.status'

WARNING -  Could not detect if simulation is correctly running. Your solver should write "running" in a file "TS.status"

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 16-Jul-2021 00:34:52
 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....

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);
ans =

    './WORK/TimeStepping/RUN000004/TS.status'

WARNING -  Could not detect if simulation is correctly running. Your solver should write "running" in a file "TS.status"
 Simulation still running
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 stopped

 Time-stepping simulation started 16-Jul-2021 00:35:03
 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 135 ; time = 0.1 to 13.5
 -> Found 14 Snapshots in this folder. Importing them....

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 16-Jul-2021 00:34:44
 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 16-Jul-2021 00:34:51
 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     | 16-juil.-2021 00:34:44 | 0.1        | 1          | 10        
Snapshot_00000020.txt    | DNSField     | 16-juil.-2021 00:34:44 | 0.1        | 2          | 20        
(skipping intermediate)    |
Snapshot_00000190.txt    | DNSField     | 16-juil.-2021 00:34:52 | 0.1        | 19         | 190       
Snapshot_00000200.txt    | DNSField     | 16-juil.-2021 00:34:52 | 0.1        | 20         | 200       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',1,200)"
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000002/ : 
 Simulation completed

 Time-stepping simulation started 16-Jul-2021 00:34:49
 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     | 16-juil.-2021 00:34:49 | 0.25       | 11         | 110       
Snapshot_00000120.txt    | DNSField     | 16-juil.-2021 00:34:50 | 0.25       | 12         | 120       
(skipping intermediate)    |
Snapshot_00000190.txt    | DNSField     | 16-juil.-2021 00:34:50 | 0.25       | 19         | 190       
Snapshot_00000200.txt    | DNSField     | 16-juil.-2021 00:34:50 | 0.25       | 20         | 200       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',2,200)"
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000003/ : 
 Simulation completed

 Time-stepping simulation started 16-Jul-2021 00:34:52
 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     | 16-juil.-2021 00:34:53 | 0.1        | 1          | 10        
Snapshot_00000020.txt    | DNSField     | 16-juil.-2021 00:34:53 | 0.1        | 2          | 20        
(skipping intermediate)    |
Snapshot_00000090.txt    | DNSField     | 16-juil.-2021 00:34:54 | 0.1        | 9          | 90        
Snapshot_00000100.txt    | DNSField     | 16-juil.-2021 00:34:54 | 0.1        | 10         | 100       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',3,100)"
WARNING -  This run does not have the same metadata as previous ones
#################################################################################
.... CONTENT OF  ./WORK/TimeStepping/RUN000004/ : 
 Simulation stopped

 Time-stepping simulation started 16-Jul-2021 00:35:03
 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 135 ; time = 0.1 to 13.5
 -> Found 14 Snapshots in this folder.
#################################################################################
 
.... SNAPSHOTS FOUND IN DIRECTORY ./WORK/TimeStepping/RUN000004/
     (couples of .txt/.ff2m files )
 
Total : 14 files. Displaying only first and last ones
Name                     | Type         | Date                 | alpha      | t          | it        
Snapshot_00000010.txt    | DNSField     | 16-juil.-2021 00:35:04 | 0.1        | 1          | 10        
Snapshot_00000020.txt    | DNSField     | 16-juil.-2021 00:35:04 | 0.1        | 2          | 20        
(skipping intermediate)    |
Snapshot_00000130.txt    | DNSField     | 16-juil.-2021 00:35:05 | 0.1        | 13         | 130       
Snapshot_00000135.txt    | DNSField     | 16-juil.-2021 00:35:05 | 0.1        | 13.5       | 135       
 
 To import one snapshot, use SF_Load. For instance to import last one : "Snap = SF_Load('TimeStepping',4,135)"
WARNING -  This run does not have the same metadata as previous ones

ss = 

  1x2 struct array with fields:

    filename
    DataDescription
    datatype
    datastoragemode
    datadescriptors
    mesh
    t
    u3
    u6
    iter
    status