INMOST
A toolkit for distributed mathematical modeling
inmost_model.h
1 #ifndef INMOST_MODEL_INCLUDED
2 #define INMOST_MODEL_INCLUDED
3 
4 #include "inmost_common.h"
5 #include "inmost_autodiff.h"
6 #include "inmost_residual.h"
7 #include "inmost_operator.h"
8 
9 #if defined(USE_AUTODIFF) && defined(USE_MESH) && defined(USE_SOLVER)
10 
11 namespace INMOST
12 {
13 
14  class Model;
15  class AbstractOperator;
16 
21  {
22  public:
24  virtual bool PrepareEntries(Model& P) = 0;
26  virtual bool Initialize(Model& P) = 0;
28  virtual bool SetupCoupling(Model& P) { return true; }
31  virtual bool PrepareIterations() { return true; }
33  virtual bool FillResidual(Residual& R) const = 0;
35  virtual bool UpdateSolution(const Sparse::Vector& sol, double alpha) = 0;
37  virtual bool UpdateTimeStep() = 0;
39  virtual bool SetTimeStep(double dt) = 0;
41  virtual bool SetTime(double t) = 0;
43  virtual bool RestoreTimeStep() = 0;
45  virtual double UpdateMultiplier(const Sparse::Vector& sol) const { (void)sol; return 1; }
47  virtual double AdjustTimeStep(double dt) const { return dt; }
48  /*
52  virtual void Adaptation(Mesh& m) const { (void)m; }
53 
54  virtual void PrepareAdaptation(Mesh& m) { (void)m; }
55 
56  //new hanging node was introduced, no connectivity at function call
57  virtual void NewNode(Cell& c, Node& n, Storage::reference_array cell_hanging_nodes) { (void)c; (void)n; (void)cell_hanging_nodes; }
58  virtual void NewNode(Face& f, Node& n, Storage::reference_array face_hanging_nodes) { (void)f; (void)n; (void)face_hanging_nodes; }
59  virtual void NewNode(Edge& e, Node& n) { (void)e; (void)n; }
60  //new hanging edge was introduced, no connectivity at function call
61  virtual void NewEdge(Cell& c, Edge& e) { (void)c; (void)e; }
62  virtual void NewEdge(Face& f, Edge& e) { (void)f; (void)e; }
63  //new hanging face was introduced, no connectivity at function call
64  virtual void NewFace(Cell& c, Face& f) { (void)c; (void)f; }
65  //element was refined into new elements
66  virtual void CellRefinement(Cell& old_cell, ElementArray<Cell>& new_cells, ElementSet& new_cell_set) { (void)old_cell; (void)new_cells; (void)new_cell_set; }
67  virtual void FaceRefinement(Face& old_face, ElementArray<Face>& new_faces) { (void)old_face; (void)new_faces; }
68  virtual void EdgeRefinement(Edge& old_edge, ElementArray<Edge>& new_edges) { (void)old_edge; (void)new_edges; }
69  //old elements were connected into an old element
70  virtual void CellCoarsening(ElementArray<Cell>& old_cells, Cell& new_cell, ElementSet& old_cells_set) { (void)old_cells; (void)new_cell; (void)old_cells_set; }
71  virtual void FaceCoarsening(ElementArray<Face>& old_faces, Face& new_face) { (void)old_faces; (void)new_face; }
72  virtual void EdgeCoarsening(ElementArray<Edge>& old_edges, Edge& new_edge) { (void)old_edges; (void)new_edge; }
73  */
75  virtual void PrepareOutput() {}
77  virtual void FinishOutput() {}
78  };
79 
84  {
85  public:
87  virtual bool Initialize(Model& P) = 0;
89  virtual bool SetupCoupling(Model& P) { return true; }
91  virtual bool FillResidual(Residual& R) const = 0;
92 
96 
99  virtual bool PrepareIterations() { return true; }
101  virtual bool UpdateTimeStep() { return true; }
103  virtual bool SetTimeStep(double dt) { return true; }
105  virtual bool SetTime(double t) { return true; }
107  virtual bool RestoreTimeStep() { return true; }
109  virtual double UpdateMultiplier(const Sparse::Vector& sol) const { (void)sol; return 1; }
111  virtual double AdjustTimeStep(double dt) const { return dt; }
112  };
113 
117  {
118  public:
120  virtual bool Initialize(Model& P) = 0;
122  virtual bool SetupCoupling(Model& P) { return true; }
126  virtual variable Evaluate(const Storage& e) const = 0;
127 
131 
134  virtual bool PrepareIterations() { return true; }
136  virtual bool UpdateTimeStep() { return true; }
138  virtual bool SetTimeStep(double dt) { return true; }
140  virtual bool SetTime(double t) { return true; }
142  virtual bool RestoreTimeStep() { return true; }
144  virtual double UpdateMultiplier(const Sparse::Vector& sol) const { (void)sol; return 1; }
146  virtual double AdjustTimeStep(double dt) const { return dt; }
147  };
148 
152  {
153  public:
155  virtual bool Initialize(Model& P) = 0;
157  virtual bool SetupCoupling(Model& P) { return true; }
161  virtual vMatrix Evaluate(const Storage& e) const = 0;
162 
166 
169  virtual bool PrepareIterations() { return true; }
171  virtual bool UpdateTimeStep() { return true; }
173  virtual bool SetTimeStep(double dt) { return true; }
175  virtual bool SetTime(double t) { return true; }
177  virtual bool RestoreTimeStep() { return true; }
179  virtual double UpdateMultiplier(const Sparse::Vector& sol) const { (void)sol; return 1; }
181  virtual double AdjustTimeStep(double dt) const { return dt; }
182  };
183 
185  class Model
186  {
187  Automatizator & aut;
188  //todo: decide later on how it should be stored
189  std::vector< std::pair<std::string, AbstractSubModel *> > SubModels;
190  std::vector< std::pair<std::string, AbstractEntry *> > Entries;
191  std::vector< std::pair<std::string, Mesh *> > Meshes;
192  std::map< AbstractSubModel*, std::vector< std::pair<std::string, AbstractCouplingTerm*> > > CouplingTerms;
193  //std::map< AbstractSubModel*, std::vector< std::pair<std::string, AbstractEntry*> > > SubModelEntries;
194  std::vector< std::pair<std::string, AbstractScalarFunction*> > ScalarFunctions;
195  std::vector< std::pair<std::string, AbstractMatrixFunction*> > MatrixFunctions;
196  std::vector< std::pair<std::string, AbstractOperator *> > Operators;
197  std::map< std::string, bool> activeEntries, activeSubModels;
198 
199  bool initialized;
200  bool setentries;
201  public:
202  Model(Automatizator & aut) : aut(aut), initialized(false), setentries(false) {}
203  //todo:
204  //Model(const Model & b) aut(b.aut) {}
205  //todo:
206  //Model & operator =(Model const & b);
207  virtual ~Model() {}
208  void ActivateSubModel(std::string name) { activeSubModels[name] = true; }
209  void DeactivateSubModel(std::string name) { activeSubModels[name] = false; }
210  void ToggleEntryState();
211  void ActivateEntry(std::string name) { activeEntries[name] = true; }
212  void DeactivateEntry(std::string name) { activeEntries[name] = false; }
217  void AddEntry(std::string name, AbstractEntry& entry);
222  void AddAfterEntry(std::string name, AbstractEntry& entry, std::string after);
227  void AddFirstEntry(std::string name, AbstractEntry& entry);
234  void AddMesh(std::string name, Mesh & m);
239  void AddSubModel(std::string name, AbstractSubModel & submodel);
246  void AddCouplingTerm(std::string submodel, std::string name, AbstractCouplingTerm& term);
249  void AddScalarFunction(std::string name, AbstractScalarFunction& func);
252  void AddMatrixFunction(std::string name, AbstractMatrixFunction& func);
255  void AddOperator(std::string name, AbstractOperator & op);
257  Automatizator & GetAutomatizator() {return aut;}
259  const Automatizator & GetAutomatizator() const {return aut;}
261  Mesh * GetMesh(std::string);
263  const Mesh * GetMesh(std::string) const;
265  std::vector<std::string> GetMeshesNames() const;
267  AbstractEntry * GetEntry(std::string name);
269  const AbstractEntry * GetEntry(std::string name) const;
271  std::vector<std::string> GetEntriesNames() const;
273  AbstractSubModel * GetSubModel(std::string name);
275  const AbstractSubModel * GetSubModel(std::string name) const;
277  std::string GetSubModelName(const AbstractSubModel* model) const;
279  std::vector<std::string> GetSubModelsNames() const;
281  AbstractCouplingTerm* GetCouplingTerm(std::string submodel, std::string name);
283  const AbstractCouplingTerm* GetCouplingTerm(std::string submodel, std::string name) const;
285  std::vector< std::pair< std::string, std::vector<std::string> > > GetCouplingTermNames() const;
289  const AbstractScalarFunction* GetScalarFunction(std::string name) const;
291  std::vector<std::string> GetScalarFunctionNames() const;
295  const AbstractMatrixFunction* GetMatrixFunction(std::string name) const;
297  std::vector<std::string> GetMatrixFunctionNames() const;
299  AbstractOperator * GetOperator(std::string name);
301  const AbstractOperator * GetOperator(std::string name) const;
303  std::vector<std::string> GetOperatorsNames() const;
308  bool Initialize();
315  bool FillResidual(Residual & R) const;
319  bool UpdateSolution(const Sparse::Vector & sol, double alpha);
323  bool SetTimeStep(double dt);
325  bool SetTime(double t);
329  bool isInitialized() const {return initialized;}
331  bool areEntriesSet() const { return setentries; }
336  double UpdateMultiplier(const Sparse::Vector & sol) const;
338  double AdjustTimeStep(double dt) const;
339  /*
342  void Adaptation(Mesh & m) const;
343 
344  void PrepareAdaptation(Mesh& m);
345 
346  void NewNode(Cell& c, Node& n, Storage::reference_array cell_hanging_nodes);
347  void NewNode(Face& f, Node& n, Storage::reference_array face_hanging_nodes);
348  void NewNode(Edge& e, Node& n);
349 
350  void NewEdge(Cell& c, Edge& e);
351  void NewEdge(Face& f, Edge& e);
352 
353  void NewFace(Cell& c, Face& f);
354 
355  void CellRefinement(Cell & old_cell, ElementArray<Cell> & new_cells, ElementSet & new_cell_set);
356  void FaceRefinement(Face & old_face, ElementArray<Face> & new_faces);
357  void EdgeRefinement(Edge & old_edge, ElementArray<Edge> & new_edges);
358 
359  void CellCoarsening(ElementArray<Cell> & old_cells, Cell & new_cell, ElementSet & old_cells_set);
360  void FaceCoarsening(ElementArray<Face> & old_faces, Face & new_face);
361  void EdgeCoarsening(ElementArray<Edge> & old_edges, Edge & new_edge);
362  */
363  void ReportErrors(const Residual & R) const;
364  void PrepareOutput();
365  void FinishOutput();
366  };
367 }
368 
369 #endif //USE_AUTODIFF && USE_MESH && USE_SOLVER
370 
371 #endif //INMOST_MODEL_INCLUDED
372 
A class to manage residual fill-in of the coupling between models.
Definition: inmost_model.h:84
virtual bool SetTime(double t)
Provide current time.
Definition: inmost_model.h:105
virtual bool RestoreTimeStep()
Roll back to previous step.
Definition: inmost_model.h:107
virtual bool Initialize(Model &P)=0
Initialize coupling and dependent unknowns.
virtual bool SetTimeStep(double dt)
Provide time step.
Definition: inmost_model.h:103
virtual bool FillResidual(Residual &R) const =0
Fill part of the residual related to my unknowns.
virtual bool PrepareIterations()
This functions might be not necessary:
Definition: inmost_model.h:99
virtual bool SetupCoupling(Model &P)
Setup coupling with unknowns of otheer models.
Definition: inmost_model.h:89
virtual double AdjustTimeStep(double dt) const
Calculate time step for this model. Can simply return dt.
Definition: inmost_model.h:111
virtual double UpdateMultiplier(const Sparse::Vector &sol) const
Calculate multiplier for update for this model. Can simply return 1.
Definition: inmost_model.h:109
virtual bool UpdateTimeStep()
Update time step.
Definition: inmost_model.h:101
This class is used to organize unknowns in abstract way, it should be registered with and managed by ...
This can be used to abstract implementation of particular matrix functions, that depend on mesh data ...
Definition: inmost_model.h:152
virtual bool SetupCoupling(Model &P)
Setup coupling with unknowns of otheer models.
Definition: inmost_model.h:157
virtual bool RestoreTimeStep()
Roll back to previous step.
Definition: inmost_model.h:177
virtual bool PrepareIterations()
This functions might be not necessary:
Definition: inmost_model.h:169
virtual double AdjustTimeStep(double dt) const
Calculate time step for this model. Can simply return dt.
Definition: inmost_model.h:181
virtual bool UpdateTimeStep()
Update time step.
Definition: inmost_model.h:171
virtual bool Initialize(Model &P)=0
Initialize coupling and dependent unknowns.
virtual vMatrix Evaluate(const Storage &e) const =0
The main function provided by operator.
virtual double UpdateMultiplier(const Sparse::Vector &sol) const
Calculate multiplier for update for this function. Can simply return 1.
Definition: inmost_model.h:179
virtual bool SetTime(double t)
Provide current time.
Definition: inmost_model.h:175
virtual bool SetTimeStep(double dt)
Provide time step.
Definition: inmost_model.h:173
This class is responsible to unite access to various point-wise implementations of discrete operators...
This can be used to abstract implementation of particular scalar functions, that depend on mesh data ...
Definition: inmost_model.h:117
virtual double UpdateMultiplier(const Sparse::Vector &sol) const
Calculate multiplier for update for this function. Can simply return 1.
Definition: inmost_model.h:144
virtual bool SetTimeStep(double dt)
Provide time step.
Definition: inmost_model.h:138
virtual bool SetupCoupling(Model &P)
Setup coupling with unknowns of otheer models.
Definition: inmost_model.h:122
virtual bool UpdateTimeStep()
Update time step.
Definition: inmost_model.h:136
virtual double AdjustTimeStep(double dt) const
Calculate time step for this model. Can simply return dt.
Definition: inmost_model.h:146
virtual bool PrepareIterations()
This functions might be not necessary:
Definition: inmost_model.h:134
virtual variable Evaluate(const Storage &e) const =0
The main function provided by operator.
virtual bool Initialize(Model &P)=0
Initialize coupling and dependent unknowns.
virtual bool SetTime(double t)
Provide current time.
Definition: inmost_model.h:140
virtual bool RestoreTimeStep()
Roll back to previous step.
Definition: inmost_model.h:142
A class to manage a submodel within a model.
Definition: inmost_model.h:21
virtual double AdjustTimeStep(double dt) const
Calculate time step for this model. Can simply return dt.
Definition: inmost_model.h:47
virtual bool PrepareIterations()
Initialize data needed for FillResidual.
Definition: inmost_model.h:31
virtual bool FillResidual(Residual &R) const =0
Fill part of the residual related to my unknowns.
virtual bool SetTimeStep(double dt)=0
Provide time step.
virtual bool RestoreTimeStep()=0
Roll back to previous step.
virtual void PrepareOutput()
Prepare submodel data for output.
Definition: inmost_model.h:75
virtual void FinishOutput()
Remove submodel data for output.
Definition: inmost_model.h:77
virtual bool SetTime(double t)=0
Provide current time.
virtual bool Initialize(Model &P)=0
Initialize coupling and dependent unknowns.
virtual bool UpdateSolution(const Sparse::Vector &sol, double alpha)=0
Update solution.
virtual double UpdateMultiplier(const Sparse::Vector &sol) const
Calculate multiplier for update for this model. Can simply return 1.
Definition: inmost_model.h:45
virtual bool UpdateTimeStep()=0
Update time step.
virtual bool PrepareEntries(Model &P)=0
Let the submodel introduce its unknowns.
virtual bool SetupCoupling(Model &P)
Setup coupling with unknowns of other models.
Definition: inmost_model.h:28
The Automatizator class helps in defining primary unknowns of the model and enhances user experience ...
void SynchronizeData()
Update variables contained in all block of automatizator on ghost elements of the grid.
Class for linear algebra operations on dense matrices.
A class to organize a model.
Definition: inmost_model.h:186
AbstractScalarFunction * GetScalarFunction(std::string name)
Retrieve a scalar function of the model by name.
bool PrepareIterations()
Initialize data needed for FillResidual.
const AbstractCouplingTerm * GetCouplingTerm(std::string submodel, std::string name) const
Retrieve a coupling term of the model by name.
double AdjustTimeStep(double dt) const
Calculate optimal time step for submodels.
std::vector< std::string > GetScalarFunctionNames() const
Retrieve all names of scalar functions.
bool SetTime(double t)
Provide current time.
void AddFirstEntry(std::string name, AbstractEntry &entry)
Add an entry of block unknowns to a model as a first entry.
std::vector< std::string > GetOperatorsNames() const
Retrieve all names of operators.
bool UpdateTimeStep()
Move to the next time step.
void AddMatrixFunction(std::string name, AbstractMatrixFunction &func)
Add matrix coupling function to a model.
const Mesh * GetMesh(std::string) const
Retrieve a mesh by name.
void AddMesh(std::string name, Mesh &m)
Add a mesh to a model.
void AddOperator(std::string name, AbstractOperator &op)
Add an operator to a model.
std::string GetSubModelName(const AbstractSubModel *model) const
Retrieve the name by submodel address.
bool SetTimeStep(double dt)
Provide new time step.
AbstractSubModel * GetSubModel(std::string name)
Retrieve a submodel of the model by name.
AbstractEntry * GetEntry(std::string name)
Retrieve an entry that describe unknowns of the model by name.
std::vector< std::string > GetMatrixFunctionNames() const
Retrieve all names of matrix functions.
std::vector< std::string > GetMeshesNames() const
Retrieve all names of meshes.
const AbstractScalarFunction * GetScalarFunction(std::string name) const
Retrieve a scalar function of the model by name.
bool isInitialized() const
Check if the model was initialized.
Definition: inmost_model.h:329
AbstractMatrixFunction * GetMatrixFunction(std::string name)
Retrieve a matrix functon of the model by name.
const AbstractMatrixFunction * GetMatrixFunction(std::string name) const
Retrieve a matrix function of the model by name.
std::vector< std::string > GetEntriesNames() const
Retrieve all names of entries.
bool PrepareEntries()
Each submodel introduces its unknowns into the model so that later it can be accessed.
bool Initialize()
Initialize all entries and submodels.
void AddAfterEntry(std::string name, AbstractEntry &entry, std::string after)
Add an entry of block unknowns to a model after an entry with certain name.
bool RestoreTimeStep()
Roll back to previous time step.
Mesh * GetMesh(std::string)
Retrieve a mesh by name.
void AddScalarFunction(std::string name, AbstractScalarFunction &func)
Add scalar coupling function to a model.
Automatizator & GetAutomatizator()
Retrieve an automatizator.
Definition: inmost_model.h:257
void AddCouplingTerm(std::string submodel, std::string name, AbstractCouplingTerm &term)
Add coupling term to a submodel.
AbstractCouplingTerm * GetCouplingTerm(std::string submodel, std::string name)
Retrieve a coupling term of the model by name.
void SynchronizeData()
Update variables contained in all block of automatizator on ghost elements of the grid.
Definition: inmost_model.h:334
const AbstractOperator * GetOperator(std::string name) const
Retrieve an operator of the model by name.
bool areEntriesSet() const
Check if the entries were set.
Definition: inmost_model.h:331
std::vector< std::pair< std::string, std::vector< std::string > > > GetCouplingTermNames() const
Retrieve all names of coupling terms.
bool UpdateSolution(const Sparse::Vector &sol, double alpha)
Update solution.
const Automatizator & GetAutomatizator() const
Retrieve an automatizator.
Definition: inmost_model.h:259
std::vector< std::string > GetSubModelsNames() const
Retrieve all names of submodules.
void AddSubModel(std::string name, AbstractSubModel &submodel)
Add a submodel to a model.
void AddEntry(std::string name, AbstractEntry &entry)
Add an entry of block unknowns to a model.
bool FillResidual(Residual &R) const
Compute the residual of the model.
bool SetupCoupling()
Link to all couplings after PrepareEntries and Initialize.
const AbstractSubModel * GetSubModel(std::string name) const
Retrieve a submodel of the model by name.
double UpdateMultiplier(const Sparse::Vector &sol) const
Calculate multiplier for update.
AbstractOperator * GetOperator(std::string name)
Retrieve an operator of the model by name.
const AbstractEntry * GetEntry(std::string name) const
Retrieve an entry that describe unknowns of the model by name.
The Residual class provides a representation for array of residuals of nonlinear equations.
Distributed vector class.
Definition: inmost_sparse.h:75
Base class for Mesh, Element, and ElementSet classes.
Definition: inmost_data.h:310
A class that represents a variable with multiple first order variations.