OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
FuelCell::ApplicationCore::TransientBase Class Referenceabstract

This is a base class for transient solvers. More...

#include <transient_base.h>

Inheritance diagram for FuelCell::ApplicationCore::TransientBase:
Inheritance graph
[legend]
Collaboration diagram for FuelCell::ApplicationCore::TransientBase:
Collaboration graph
[legend]

Public Member Functions

 TransientBase (ApplicationBase &app)
 Constructor, receiving an application with transient model and a data object. More...
 
virtual void declare_parameters (ParameterHandler &param)
 Declare input parameters in section "Transient" of parameter file. More...
 
virtual void initialize (ParameterHandler &param)
 Read the parameters of TransientBase. More...
 
virtual void solve (FuelCell::ApplicationCore::FEVector &u, const FuelCell::ApplicationCore::FEVectors &in_vectors)=0
 The actual transient solver. More...
 
- Public Member Functions inherited from FuelCell::ApplicationCore::ApplicationWrapper
 ApplicationWrapper (ApplicationBase &app)
 Constructor for a derived application. More...
 
 ~ApplicationWrapper ()
 Destructor. More...
 
virtual void remesh ()
 Generate the next mesh depending on the mesh generation parameters. More...
 
virtual void init_vector (FEVector &dst) const
 Initialize vector to problem size. More...
 
virtual double residual (FEVector &dst, const FEVectors &src, bool apply_boundaries=true)
 Compute residual of src and store it into dst. More...
 
virtual void Tsolve (FEVector &dst, const FEVectors &src)
 Solve the dual system assembled with right hand side rhs and return the result in start. More...
 
virtual double estimate (const FEVectors &src)
 Estimate cell-wise errors. More...
 
virtual double evaluate (const FEVectors &src)
 Evaluate a functional. More...
 
virtual void grid_out (const std::string &filename) const
 
virtual void data_out (const std::string &filename, const FEVectors &src)
 Write data in the format specified by the ParameterHandler. More...
 
virtual std::string id () const
 Return a unique identification string for this application. More...
 
virtual void notify (const Event &reason)
 Add a reason for assembling. More...
 
- Public Member Functions inherited from FuelCell::ApplicationCore::ApplicationBase
 ApplicationBase (boost::shared_ptr< ApplicationData > data=boost::shared_ptr< ApplicationData >())
 Constructor for an application. More...
 
 ApplicationBase (const ApplicationBase &other)
 Copy constructor. More...
 
virtual ~ApplicationBase ()
 Virtual destructor. More...
 
void print_parameters_to_file (ParameterHandler &param, const std::string &file_name, const ParameterHandler::OutputStyle &style)
 Print default parameters for the application to a file. More...
 
virtual void start_vector (FEVector &dst, std::string) const
 Initialize vector to problem size. More...
 
virtual void grid_out (const std::string &)
 Write the mesh in the format specified by the ParameterHandler. More...
 
boost::shared_ptr
< ApplicationData
get_data ()
 Get access to the protected variable data. More...
 
const boost::shared_ptr
< ApplicationData
get_data () const
 Get read-only access to the protected variable data. More...
 
virtual void clear ()
 All true in notifications. More...
 
virtual void clear_events ()
 All false in notifications. More...
 
virtual unsigned int get_solution_index ()
 Returns solution index. More...
 

Protected Member Functions

void debug_output (const FEVector &sol)
 Function used to output any necessary information at each time layer for debugging purposes. More...
 
void responses_output ()
 Function used to output boundary responses computed by application. More...
 
void error_output (const double &err)
 Function used to output the solution error on each layer. More...
 
void tau_output ()
 Function used to output time step on each time layer. More...
 
void initialize_u_old (const FEVector u, const double n_ref, const FEVector old_solution, const bool force=true)
 If we are inside the adaptive refinement loop, then we receive the old solution from class since it was transferred to a finer mesh and has now a different size. More...
 
void ats_Richardson (FEVector &u, const FEVector u1, const FEVector u2, int scheme_order, bool &solution_accepted)
 This routine estimates solution error and changes the time step using Richardson extrapolation. More...
 
bool file_exists (const std::string &file)
 
- Protected Member Functions inherited from FuelCell::ApplicationCore::ApplicationWrapper
SmartPointer< ApplicationBaseget_wrapped_application ()
 Gain access to the inner application. More...
 
- Protected Member Functions inherited from FuelCell::ApplicationCore::ApplicationBase
void print_caller_name (const std::string &caller_name) const
 Print caller name. More...
 

Protected Attributes

bool print_last_layer_only
 If this variable is set to true, then only the last time layer data will be printed. More...
 
bool debug_solution
 Print updated solution after each step into file solution_NNNNN.dat. More...
 
unsigned int debug
 Write debug output to FcstUtilities::log; the higher the number, the more output. More...
 
unsigned int step
 Number of the current time layer. More...
 
std::string ats_method
 Decision varible for selecting the adaptive time stepping method. More...
 
double tau
 Initial time step size. More...
 
double max_tau
 Maximal time step size. More...
 
double min_tau
 Minimal time step size. More...
 
int max_ats_it
 Maximal number of adaptive time step iterations used to resolve each time layer. More...
 
FEVector u_old
 A vector to store the solution from the previous time layer in. More...
 
int ar_freq
 If adaptive refinement is selected, then it will be performed at each ar_freq-th layer. More...
 
double total_time
 Total time of simulation in seconds. More...
 
double time
 Current time of simulation. More...
 
double abs_err_estimate
 
double rel_err_estimate
 
double abs_error_tolerance
 
double rel_error_tolerance
 
bool output_error
 
bool output_tau
 
std::ofstream responses_outfile
 Variable used in printing responses data into a file. More...
 
std::ofstream error_outfile
 Variable used in printing solution error data into a file. More...
 
std::ofstream tau_outfile
 Variable used in printing time step into a file. More...
 
const std::string fname_err ="Solution_error.dat"
 File name for solution error output. More...
 
const std::string fname_tau ="Time_step.dat"
 File name for solution error output. More...
 
std::vector< double > all_time_values
 A vector of all time values. More...
 
bool everything_is_fine =true
 
unsigned int n_ref
 
- Protected Attributes inherited from FuelCell::ApplicationCore::ApplicationWrapper
SmartPointer< ApplicationBaseapp
 Pointer to the application this one depends upon. More...
 
- Protected Attributes inherited from FuelCell::ApplicationCore::ApplicationBase
boost::shared_ptr
< ApplicationData
data
 Object for auxiliary data. More...
 
Event notifications
 Accumulate reasons for assembling here. More...
 

Private Member Functions

const std::string get_time_stepping_method_names () const
 This function returns the string of names for the adaptive time stepping methods. More...
 

Private Attributes

std::string fname
 File name for debug output. More...
 
std::ofstream streamOut
 Variable for streaming to file. More...
 
double precaution_factor
 Precaution factor in optimal time step computation. More...
 

Detailed Description

This is a base class for transient solvers.

All it does is sets some basic parameters common for all numerical methods implementing marching in time (such as time step and its value's limits, debug level for output, etc.) and outputs solution to a .dat file if required.

Default parameters in data input file are as follows:

subsection Transient
set Initial time step, [s] = 0.1
set Maximal time step, [s] = 1
set Minimal time step, [s] = 0.01
set Debug level = 0
set Debug solution = false
end
Author
Aslan Kosakian, 2015

Constructor & Destructor Documentation

FuelCell::ApplicationCore::TransientBase::TransientBase ( ApplicationBase app)

Constructor, receiving an application with transient model and a data object.

Member Function Documentation

void FuelCell::ApplicationCore::TransientBase::ats_Richardson ( FEVector u,
const FEVector  u1,
const FEVector  u2,
int  scheme_order,
bool &  solution_accepted 
)
protected

This routine estimates solution error and changes the time step using Richardson extrapolation.

Assuming $ m $-th order method,

\[ u^*=u_1+c\tau^m+\mathcal{O}\left(\tau^{m+1}\right), \]

\[ u^*=u_2+c\frac{\tau^m}{2^m}+\mathcal{O}\left(\tau^{m+1}\right), \]

where $ u^* $ is the exact solution, $ u_1 $ is the solution obtained from previous time layer with a single time step of the size $ \tau $ and $ u_2 $ is the solution obtained from the same previous time layer with two time steps of the size $ \tau/2 $. From these two relations, we can find constant $ c $:

\[ c = \frac{u_2-u_1}{\tau^m}\frac{2^m}{2^m-1}. \]

Decision on time step change is made based on the absolute and relative difference between solutions $ u_1 $ and $ u_2 $ in some norm, here - in the l2 norm defined as L2 norm of the solution divided by the square root of number of DoFs minus one. Suppose that we want to find such step $ \hat{\tau} $ that the solution $ u_2 $ (computed with that new time step) satisfies $ \lVert u^* - u_2 \rVert \approx \mathrm{TOL}_\mathrm{abs} $, where $ \mathrm{TOL}_\mathrm{abs} $ is absolute solution error tolerance prescribed by the user.

Then, given

\[ u^*=u_2+c\frac{\hat{\tau}^m}{2^m}+\mathcal{O}\left(\hat{\tau}^{m+1}\right), \]

we can assume

\[ \lVert c \rVert \frac{\hat{\tau}^m}{2^m} \approx \mathrm{TOL}_\mathrm{abs}, \]

from where we get

\[ \hat{\tau}^m \approx \frac{\mathrm{TOL}_\mathrm{abs} \tau^m \left( 2^m-1 \right)}{\lVert u_2 - u_1 \rVert}. \]

In practice, a precaution factor $ \omega $ is introduced:

\[ \hat{\tau} = \omega \tau \sqrt[m]{ \frac{\mathrm{TOL}_\mathrm{abs} \left( 2^m-1 \right)}{\lVert u_2 - u_1 \rVert} }. \]

For most of the cases, value $ \omega=0.9 $ can be safely used [1].

To satisfy the relative error tolerance $ \mathrm{TOL}_\mathrm{rel} $ as well, we want

\[ \frac{\lVert u^* - u_2 \rVert}{\lVert u_2 \rVert} \approx \frac{\lVert c \rVert }{\lVert u_2 \rVert}\frac{\hat{\tau}^m}{2^m} \approx \mathrm{TOL}_\mathrm{rel}. \]

In this case, we get

\[ \hat{\tau} = \omega \tau \sqrt[m]{ \frac{\mathrm{TOL}_\mathrm{rel} \lVert u_2 \rVert \left( 2^m-1 \right)}{\lVert u_2 - u_1 \rVert} }. \]

The resulting time step is chosen as minimum between the two values obtained based on absolute and relative error estimation.

The resulting solution

\[ u^{n+1}=u_2+\frac{u_2-u_1}{2^m-1}+\mathcal{O}\left(\tau^{m+1}\right) \]

has accuracy of order $ m+1 $.

Note
This implementation is guaranteed to work only with Euler methods $(\theta=0,~\theta=1)$ and Crank-Nicolson scheme $(\theta=0.5)$. It is done this way because otherwise the user has to perform a numerical analysis of the scheme and provide the order of it in the parameter file. If a different value of $ \theta $ is specified, a constant time step of initial value will be used.

References

  1. Faragó, István, Ágnes Havasi, and Zahari Zlatev. "Efficient implementation of stable Richardson extrapolation algorithms." Computers & Mathematics with Applications 60.8 (2010): 2309-2325.
Note
In [1], the ratio under the square root in the equation for the optimal step size is mistakenly flipped over. It makes sense to use smaller step if $ \mathrm{ERROR}_n>\mathrm{TOL} $ in the paper and larger step if $ \mathrm{ERROR}_n<\mathrm{TOL} $.
void FuelCell::ApplicationCore::TransientBase::debug_output ( const FEVector sol)
protected

Function used to output any necessary information at each time layer for debugging purposes.

virtual void FuelCell::ApplicationCore::TransientBase::declare_parameters ( ParameterHandler &  param)
virtual

Declare input parameters in section "Transient" of parameter file.

Reimplemented from FuelCell::ApplicationCore::ApplicationWrapper.

Reimplemented in FuelCell::ApplicationCore::ThetaScheme.

void FuelCell::ApplicationCore::TransientBase::error_output ( const double &  err)
protected

Function used to output the solution error on each layer.

Data can be used to plot solution error vs. time and see if there were any oscillations.

This is the most convenient way to check stability of the numerical scheme (after theoretical analysis, of course, if it is not too hard to do). If the plot is not monotonous, then you might need to use a smaller time step, a finer mesh, or try adaptive time stepping algorithms.

bool FuelCell::ApplicationCore::TransientBase::file_exists ( const std::string &  file)
inlineprotected
const std::string FuelCell::ApplicationCore::TransientBase::get_time_stepping_method_names ( ) const
inlineprivate

This function returns the string of names for the adaptive time stepping methods.

virtual void FuelCell::ApplicationCore::TransientBase::initialize ( ParameterHandler &  param)
virtual

Read the parameters of TransientBase.

Those are general parameters for transient loop, e.g. time step.

Reimplemented from FuelCell::ApplicationCore::ApplicationWrapper.

Reimplemented in FuelCell::ApplicationCore::ThetaScheme.

void FuelCell::ApplicationCore::TransientBase::initialize_u_old ( const FEVector  u,
const double  n_ref,
const FEVector  old_solution,
const bool  force = true 
)
protected

If we are inside the adaptive refinement loop, then we receive the old solution from class since it was transferred to a finer mesh and has now a different size.

The transferred solution will be stored in the local variable u_old. If no adaptive refinement is used, then u_old is initialized with u. Variable force is used in adaptive time stepping algorithm. Forced initialization, i.e., initialization with force=true, sets u_old to u, which is previous iterate of the model - it can be different from the old solution u^n in transient sense. For example, when we do two half-steps in Richardson extrapolation, the first half-step uses the old_solution solution from class and the second half-step - the old solution in a global sense, u, which was obtained from the first half-step.

void FuelCell::ApplicationCore::TransientBase::responses_output ( )
protected

Function used to output boundary responses computed by application.

virtual void FuelCell::ApplicationCore::TransientBase::solve ( FuelCell::ApplicationCore::FEVector u,
const FuelCell::ApplicationCore::FEVectors in_vectors 
)
pure virtual

The actual transient solver.

Performs loop over time.

This function's definition is used in classes derived from TransientBase (such as ThetaScheme), but not in current class itself.

Reimplemented from FuelCell::ApplicationCore::ApplicationWrapper.

Implemented in FuelCell::ApplicationCore::ThetaScheme.

void FuelCell::ApplicationCore::TransientBase::tau_output ( )
protected

Function used to output time step on each time layer.

Data can be used to analyze the adaptive time stepping algorithm that was used in the simulation.

Member Data Documentation

double FuelCell::ApplicationCore::TransientBase::abs_err_estimate
protected
double FuelCell::ApplicationCore::TransientBase::abs_error_tolerance
protected
std::vector<double> FuelCell::ApplicationCore::TransientBase::all_time_values
protected

A vector of all time values.

Used in the funciton.

int FuelCell::ApplicationCore::TransientBase::ar_freq
protected

If adaptive refinement is selected, then it will be performed at each ar_freq-th layer.

std::string FuelCell::ApplicationCore::TransientBase::ats_method
protected

Decision varible for selecting the adaptive time stepping method.

This variable is set in the input file in:

* subsection Transient
* set Adaptive stepping algorithm =
* end
*

Possible options are:

  • None: Constant time step will be used.
  • Richardson: Time stepping algorithm based on Richardson extrapolation will be used.
unsigned int FuelCell::ApplicationCore::TransientBase::debug
protected

Write debug output to FcstUtilities::log; the higher the number, the more output.

bool FuelCell::ApplicationCore::TransientBase::debug_solution
protected

Print updated solution after each step into file solution_NNNNN.dat.

std::ofstream FuelCell::ApplicationCore::TransientBase::error_outfile
protected

Variable used in printing solution error data into a file.

bool FuelCell::ApplicationCore::TransientBase::everything_is_fine =true
protected
std::string FuelCell::ApplicationCore::TransientBase::fname
private

File name for debug output.

const std::string FuelCell::ApplicationCore::TransientBase::fname_err ="Solution_error.dat"
protected

File name for solution error output.

const std::string FuelCell::ApplicationCore::TransientBase::fname_tau ="Time_step.dat"
protected

File name for solution error output.

int FuelCell::ApplicationCore::TransientBase::max_ats_it
protected

Maximal number of adaptive time step iterations used to resolve each time layer.

No convergence error will be thrown if the number of iterations exceeds this value.

double FuelCell::ApplicationCore::TransientBase::max_tau
protected

Maximal time step size.

Used in adaptive time stepping.

double FuelCell::ApplicationCore::TransientBase::min_tau
protected

Minimal time step size.

Used in adaptive time stepping.

unsigned int FuelCell::ApplicationCore::TransientBase::n_ref
protected
bool FuelCell::ApplicationCore::TransientBase::output_error
protected
bool FuelCell::ApplicationCore::TransientBase::output_tau
protected
double FuelCell::ApplicationCore::TransientBase::precaution_factor
private

Precaution factor in optimal time step computation.

bool FuelCell::ApplicationCore::TransientBase::print_last_layer_only
protected

If this variable is set to true, then only the last time layer data will be printed.

double FuelCell::ApplicationCore::TransientBase::rel_err_estimate
protected
double FuelCell::ApplicationCore::TransientBase::rel_error_tolerance
protected
std::ofstream FuelCell::ApplicationCore::TransientBase::responses_outfile
protected

Variable used in printing responses data into a file.

unsigned int FuelCell::ApplicationCore::TransientBase::step
protected

Number of the current time layer.

Time layer 0 contains initial solution.

std::ofstream FuelCell::ApplicationCore::TransientBase::streamOut
private

Variable for streaming to file.

double FuelCell::ApplicationCore::TransientBase::tau
protected

Initial time step size.

Note
Can be modified by the transient solver if an adaptive time stepping algorithm is used.
std::ofstream FuelCell::ApplicationCore::TransientBase::tau_outfile
protected

Variable used in printing time step into a file.

double FuelCell::ApplicationCore::TransientBase::time
protected

Current time of simulation.

double FuelCell::ApplicationCore::TransientBase::total_time
protected

Total time of simulation in seconds.

FEVector FuelCell::ApplicationCore::TransientBase::u_old
protected

A vector to store the solution from the previous time layer in.


The documentation for this class was generated from the following file: