action functions
|
Please note that the library has not been "officially" released. While we continue to work on the documentation, these web pages are likely to contain broken links and documents in draft form. Please send an email to if you wish to be informed of the library's "official" release. |
Simulations of time-dependent problem can be very time consuming and it is important to be able to restart simulations, e.g. to continue a run after a system crash, etc. We shall illustrate oomph-lib's dump/restart capabilities by re-visiting the 2D unsteady heat equation discussed in a previous example.
Solve
in the square domain
and initial conditions
where the functions |
As before, we consider the unforced case,
, and choose boundary and initial conditions that are consistent with the exact solution
where
and
are constants, controlling the decay rate of the solution and its spatial orientation, respectively.
The figure below shows a plot of computed and exact solution at a control node as a function of time. The solid lines represent quantitites computed during the original simulation; the dashed line shows the corresponding data from a second simulation that was restarted with the restart file generated at timestep 23 of the original simulation.
Time evolution of the computed and exact solutions at a control node, the global error norm and the norm of the solution. Solid lines: original simulation; dashed lines: restarted simulation.
Most of the driver code for this example is identical to that discussed in the previous example, therefore we only discuss the modifications required to enable the dump and restart operations:
and generate the initial conditions as in the previous exampledoc_solution(...).set_initial_condition() so that the initial conditions are read from the restart file if a restart file was specified on the command line.ExactSolnForUnsteadyHeat that stores the problem parameters is identical to that in the previous example.
CommandLineArgs
//=======start_of_main==================================================== /// \short Driver code for unsteady heat equation with option for /// restart from disk: Only a single command line argument is allowed. /// If specified it is interpreted as the name of the restart file. //======================================================================== int main(int argc, char* argv[]) { // Store command line arguments CommandLineArgs::setup(argc,argv);
The rest of the main function is identical to that in the previous example.
/// \short Dump problem to disk to allow for restart. void dump_it(ofstream& dump_file); /// \short Read problem for restart from specified restart file. void restart(ifstream& restart_file);
CommandLineArgs, as only a single command line argument is allowed. If a command line argument is provided, it is interpreted as the name of the restart file. We try to open the file and, if successful, pass the input stream to the restart(...) function, discussed below. If no command line arguments are specified, we generate the initial conditions, essentially as in the previous example. The only difference is that in the current version of the code, we moved the specification and initialisation of the timestep from the main function into set_initial_condition(). This is because in a restarted simulation, the value of dt must be consistent with that used in the original simulation. If the simulation is restarted, the generic Problem::read(...) function, called by restart(...), automatically initialises the previous timestep; otherwise we have to perform the initialisation ourselves.
//======================start_of_set_initial_condition==================== /// \short Set initial condition: Assign previous and current values /// from exact solution or from restart file. //======================================================================== template<class ELEMENT> void UnsteadyHeatProblem<ELEMENT>::set_initial_condition() { // Pointer to restart file ifstream* restart_file_pt=0; // Restart? //--------- // Restart file specified via command line [all programs have at least // a single command line argument: their name. Ignore this here.] if (CommandLineArgs::Argc==1) { cout << "No restart -- setting IC from exact solution" << std::endl; } else if (CommandLineArgs::Argc==2) { // Open restart file restart_file_pt= new ifstream(CommandLineArgs::Argv[1],ios_base::in); if (restart_file_pt!=0) { cout << "Have opened " << CommandLineArgs::Argv[1] << " for restart. " << std::endl; } else { std::ostringstream error_stream; error_stream << "ERROR while trying to open " << CommandLineArgs::Argv[1] << " for restart." << std::endl; throw OomphLibError( error_stream.str(), "UnsteadyHeatProblem<ELEMENT>::set_initial_condition()", OOMPH_EXCEPTION_LOCATION); } } // More than one command line argument? else { std::ostringstream error_stream; error_stream << "Can only specify one input file\n" << "You specified the following command line arguments:\n"; //Fix this CommandLineArgs::output(); throw OomphLibError( error_stream.str(), "UnsteadyHeatProblem<ELEMENT>::set_initial_condition()", OOMPH_EXCEPTION_LOCATION); } // Read restart data: //------------------- if (restart_file_pt!=0) { // Read the problem data from the restart file restart(*restart_file_pt); } // Assign initial condition from exact solution //--------------------------------------------- else { // Choose timestep double dt=0.01; // Initialise timestep -- also sets the weights for all timesteppers // in the problem. initialise_dt(dt); // Backup time in global Time object double backed_up_time=time_pt()->time(); // Past history fo needs to be established for t=time0-deltat, ... // Then provide current values (at t=time0) which will also form // the initial guess for the first solve at t=time0+deltat // Vector of exact solution value Vector<double> soln(1); Vector<double> x(2); //Find number of nodes in mesh unsigned num_nod = mesh_pt()->nnode(); // Set continuous times at previous timesteps int nprev_steps=time_stepper_pt()->nprev_values(); Vector<double> prev_time(nprev_steps+1); for (int itime=nprev_steps;itime>=0;itime--) { prev_time[itime]=time_pt()->time(unsigned(itime)); } // Loop over current & previous timesteps for (int itime=nprev_steps;itime>=0;itime--) { // Continuous time double time=prev_time[itime]; cout << "setting IC at time =" << time << std::endl; // Loop over the nodes to set initial guess everywhere for (unsigned n=0;n<num_nod;n++) { // Get nodal coordinates x[0]=mesh_pt()->node_pt(n)->x(0); x[1]=mesh_pt()->node_pt(n)->x(1); // Get intial solution ExactSolnForUnsteadyHeat::get_exact_u(time,x,soln); // Assign solution mesh_pt()->node_pt(n)->set_value(itime,0,soln[0]); // Loop over coordinate directions: Previous position = present position for (unsigned i=0;i<2;i++) { mesh_pt()->node_pt(n)->x(itime,i)=x[i]; } } } // Reset backed up time for global timestepper time_pt()->time()=backed_up_time; } } // end of set_initial_condition
doc_solution(...) is identical to that in the previous example, apart from the addition of a call to the new dump_it(...) function, discussed below.
// Write restart file sprintf(filename,"%s/restart%i.dat",doc_info.directory().c_str(), doc_info.number()); some_file.open(filename); dump_it(some_file); some_file.close();
Problem::dump(...) function writes the generic Problem data in ASCII format to the specified output file. The content of the file can therefore be inspected and, if necessary, manipulated before a restart. However, the specific content of the file is generally of little interest -- it is written in a format that can be read by the corresponding function Problem::read(...).Briefly, the dump file contains:
Problem::time_pt()->time();dt, stored in the Problem's Time object, and their values.Data objects in the Problem, as well as the present and previous coordinates of all Nodes.
In the present problem, the generic Problem::dump(...) function is sufficient to record the current state of the simulation, therefore no additional information needs to be added to the dump file. The section Comments and Exercises below contains an exercise that illustrates how to customise the dump function to record additional parameters; the demo code with spatial adaptivity provides another example of a customised dump/restart function.
//=====start_of_dump_it=================================================== /// Dump the solution to disk to allow for restart //======================================================================== template<class ELEMENT> void UnsteadyHeatProblem<ELEMENT>::dump_it(ofstream& dump_file) { // Call generic dump() Problem::dump(dump_file); } // end of dump_it
Problem::dump(...) function, it can be read back with the generic Problem::read(...) function. If any additional data had been recorded in the restart file, additional read statements could be added here; see Comments and Exercises.
//=================start_of_restart======================================= /// Read solution from disk for restart //======================================================================== template<class ELEMENT> void UnsteadyHeatProblem<ELEMENT>::restart(ifstream& restart_file) { // Read the generic problem data from restart file Problem::read(restart_file); } // end of restart
Problem::dump(...) and Problem::read(...) functions write/read the generic data that is common to all oomph-lib problems. Occasionally, it is necessary to record additional data to re-create the system's state when the simulations is restarted. We will explore this in more detail in another example. Here we provide a brief exercise that illustrates the general idea and addresses a shortcoming of the driver code: Currently the program computes the same number of timesteps, regardless of whether or not the simulation was restarted. If a simulation is restarted, the computation therefore continues past 
for loop over the fixed number of timesteps in the main function to a while loop that checks if the continuous time, accessible via Problem::time_pt()->time(), has reached or exceeded
. This works because the Problem::dump(...) and Problem::read(...) functions dump and restore the value of the continuous time.dump_it(...) and restart(...) so that they write/read the current label for the output files to/from the restart file. Problem::dump(...) and Problem::read(...); just make sure you do it in the same order in both functions. (ii) The easiest way to make the label, currently stored in the DocInfo object in the main function, accessible to all member functions in the Problem is to make the DocInfo object a private data member of the Problem class.
1.4.7