MorphoGraphX  2.0-1-227
Solver.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2016 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 
12 #ifndef SOLVER_HPP
13 #define SOLVER_HPP
14 
15 #include <Mesh.hpp>
16 #include <DistMatrix.hpp>
17 
18 using namespace std;
19 
20 namespace mgx
21 {
22 
23  // Type of distributed neighborhood object
25 
28 
29  typedef thrust::host_vector<Matrix2d> DVM2f;
30  typedef std::vector<Matrix2d> HVM2f;
31 
33  {
34  // Forward euler method
36  // Backward euler method
38  };
39 
40 
41  class Solver
42  {
43  public:
46  int variables;
47  double eulerDt;
48 
49  // CG parms
50  double cgTolerance, cgMaxIter;
53 
55 
56  // Distributed object
58 
59  struct VtxData
60  {
63 
64  bool operator==(const VtxData &other) const
65  {
66  if(x == other.x and b == other.b and j == other.j)
67  return true;
68  return false;
69  }
70  };
71 
72  struct EdgData
73  {
75 
76  bool operator==(const EdgData &other) const
77  {
78  if(j == other.j)
79  return true;
80  return false;
81  }
82  };
83 
84  // Distributed matrix vectors
89 
93 
94  // Attr Maps
97 
98  typedef std::pair<cell, cell> CellCellPair;
99 
102 
103 
104  Solver(Mesh* m, cellGraph& pC, SolvingMethod method, int variables, double eulerDt, double _Dx=0.001, bool debug = true)
105  : C(pC), method(method), variables(variables), eulerDt(eulerDt), debugOutput(debug)
106  {
107  nhbd = &m->nhbdC();
108 
109  cellVars = &m->attributes().attrMap<cell, VtxData>("VtxData");
110  cellEdges = &m->attributes().attrMap<CellCellPair, EdgData>("EdgData");
111  }
112 
113  // Parameters
114  enum ParmNames { pSolvingMethod, pDt, pCGTolerance, pCGMaxIter, pMaxNewtonSteps, pErrTolerance, pDebug, pNumParms };
115 
116  void processParms(const QStringList &parms);
117 
118  QStringList parmNames() const
119  {
120  QVector <QString> vec(pNumParms);
121 
122  vec[pSolvingMethod] = "SolvingMethod";
123  vec[pDt] = "Dt";
124  vec[pDebug] = "Debug";
125 
126  return vec.toList();
127  }
128 
129  QStringList parmDescs() const
130  {
131  QVector <QString> vec(pNumParms);
132 
133  vec[pSolvingMethod] = "SolvingMethod";
134  vec[pDt] = "Dt";
135  vec[pDebug] = "Debug";
136 
137  return vec.toList();
138  }
139 
140  QStringList parmDefaults() const
141  {
142  QVector <QString> vec(pNumParms);
143 
144  vec[pSolvingMethod] = "Euler";
145  vec[pDt] = "0.005";
146  vec[pDebug] = "Yes";
147 
148  return vec.toList();
149  }
150 
151 
152  // set values and derivatives
153  void setInput(std::vector<double>& values, std::vector<double>& derivatives);
154 
155  // solve according to parms and values
156  void solve();
157 
158  virtual std::vector<double> getValues(const cell &c) {std::vector<double> d; return d;}
159  virtual void setValues(const cell& c, std::vector<double> values) {}
160  virtual void updateDerivatives(const cell& c) {}
161  virtual std::vector<double> getDerivatives(const cell& c) {std::vector<double> d; return d;}
162 
163  private:
164 
165  // init for backward euler
166  void initialize();
167 
168  // create jacobian for backward euler
169  void buildJacobian(const cell& c);
170 
171  // solver forward euler
172  void solveEuler();
173 
174  // solver backward euler
175  void solveBackwardEuler();
176 
177  };
178 // Read/write Edge data
179  bool inline readAttr(Solver::VtxData &m, const QByteArray &ba, size_t &pos)
180  {
181  return readChar((char *)&m, sizeof(Solver::VtxData), ba, pos);
182  }
183  bool inline writeAttr(const Solver::VtxData &m, QByteArray &ba)
184  {
185  return writeChar((char *)&m, sizeof(Solver::VtxData), ba);
186  }
187 
188  // Read/write Edge data
189  bool inline readAttr(Solver::EdgData &m, const QByteArray &ba, size_t &pos)
190  {
191  return readChar((char *)&m, sizeof(Solver::EdgData), ba, pos);
192  }
193  bool inline writeAttr(const Solver::EdgData &m, QByteArray &ba)
194  {
195  return writeChar((char *)&m, sizeof(Solver::EdgData), ba);
196  }
197 }
198 
199 #endif
200 
mgx::Solver::VtxData::b
Point2d b
Definition: Solver.hpp:61
mgx::SolvingMethod
SolvingMethod
Definition: Solver.hpp:32
mgx::Solver::EdgData::j
Matrix2d j
Definition: Solver.hpp:74
mgx::Mesh::attributes
const Attributes & attributes() const
Get the mesh attributes.
Definition: Mesh.hpp:187
mgx::Solver::parmDefaults
QStringList parmDefaults() const
Definition: Solver.hpp:140
mgx::Solver::CellAttr2
AttrMap< cell, VtxData > CellAttr2
Definition: Solver.hpp:95
mgx::Solver::parmNames
QStringList parmNames() const
Definition: Solver.hpp:118
mgx::DMatrix2d
DMatrix< DistNhbdC, Matrix2d, Point2d, double > DMatrix2d
Definition: Solver.hpp:27
mgx::Solver
Definition: Solver.hpp:41
mgx::DVector2d
DVector< DistNhbdC, Matrix2d, Point2d, double > DVector2d
Definition: Solver.hpp:26
mgx::DistVertexAttr
Definition: DistObject.hpp:240
mgx::DVM2f
thrust::host_vector< Matrix2d > DVM2f
Definition: Solver.hpp:29
mgx::Solver::setValues
virtual void setValues(const cell &c, std::vector< double > values)
Definition: Solver.hpp:159
mgx::Solver::CellCellPair
std::pair< cell, cell > CellCellPair
Definition: Solver.hpp:98
mgx::Solver::ParmNames
ParmNames
Definition: Solver.hpp:114
mgx::Solver::newtonErrTolerance
double newtonErrTolerance
Definition: Solver.hpp:52
mgx::Solver::getDerivatives
virtual std::vector< double > getDerivatives(const cell &c)
Definition: Solver.hpp:161
mgx::Solver::cellEdges
MyEdgeAttr2 * cellEdges
Definition: Solver.hpp:101
mgx::Solver::cgTolerance
double cgTolerance
Definition: Solver.hpp:50
mgx::Solver::cellVars
CellAttr2 * cellVars
Definition: Solver.hpp:96
mgx::DistEdgeAttr
Definition: DistObject.hpp:398
mgx::Solver::eJ
DistEdgeAttr< DistNhbdC, EdgData, Matrix2d > * eJ
Definition: Solver.hpp:86
mgx::Solver::getValues
virtual std::vector< double > getValues(const cell &c)
Definition: Solver.hpp:158
Mesh.hpp
mgx::Mesh::nhbdC
CellTissue::DistNhbdC & nhbdC()
Get the neighborhood object for the cell graph.
Definition: Mesh.hpp:405
mgx::HVM2f
std::vector< Matrix2d > HVM2f
Definition: Solver.hpp:30
mgx::Solver::VtxData::j
Matrix2d j
Definition: Solver.hpp:62
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::DVector
Definition: DistMatrix.hpp:20
mgx::Solver::parmDescs
QStringList parmDescs() const
Definition: Solver.hpp:129
mgx::CellCellPair
std::pair< cell, cell > CellCellPair
Definition: Types.hpp:174
mgx::BackwardEuler
@ BackwardEuler
Definition: Solver.hpp:37
mgx::Solver::C
cellGraph & C
Definition: Solver.hpp:44
mgx::Solver::variables
int variables
Definition: Solver.hpp:46
mgx::DMatrix
Definition: DistMatrix.hpp:21
mgx::Attributes::attrMap
AttrMap< KeyT, ValueT > & attrMap(const QString &name, bool saveRequired=true)
Get the attribute, if it does not exist create it and add to the set If it exists,...
Definition: Attributes.hpp:1116
mgx::Solver::VtxData
Definition: Solver.hpp:59
DistMatrix.hpp
mgx::Solver::vJ
DistVertexAttr< DistNhbdC, VtxData, Matrix2d > * vJ
Definition: Solver.hpp:85
mgx::readAttr
bool readAttr(Solver::EdgData &m, const QByteArray &ba, size_t &pos)
Definition: Solver.hpp:189
mgx::Solver::VtxData::x
Point2d x
Definition: Solver.hpp:61
mgx::Solver::B
DVector2d * B
Definition: Solver.hpp:92
mgx::Solver::EdgData::operator==
bool operator==(const EdgData &other) const
Definition: Solver.hpp:76
mgx::Solver::J
DMatrix2d * J
Definition: Solver.hpp:90
mgx::cell
cellGraph::vertex_t cell
Type of a vertex.
Definition: Types.hpp:122
mgx::Mesh
Definition: Mesh.hpp:54
mgx::Solver::vB
DistVertexAttr< DistNhbdC, VtxData, Point2d > * vB
Definition: Solver.hpp:88
mgx::DistNhbd< cellGraph >
mgx::Vector< 2, double >
mgx::Solver::EdgData
Definition: Solver.hpp:72
mgx::Solver::X
DVector2d * X
Definition: Solver.hpp:91
mgx::Solver::eulerDt
double eulerDt
Definition: Solver.hpp:47
mgx::DistNhbdC
DistNhbd< cellGraph > DistNhbdC
Definition: Solver.hpp:24
mgx::writeAttr
bool writeAttr(const Solver::EdgData &m, QByteArray &ba)
Definition: Solver.hpp:193
mgx::Solver::updateDerivatives
virtual void updateDerivatives(const cell &c)
Definition: Solver.hpp:160
mgx::Solver::Solver
Solver(Mesh *m, cellGraph &pC, SolvingMethod method, int variables, double eulerDt, double _Dx=0.001, bool debug=true)
Definition: Solver.hpp:104
mgx::VVGraph< mgx::CellData, mgx::WallData >
mgx::Euler
@ Euler
Definition: Solver.hpp:35
mgx::Matrix< 2, 2, double >
mgx::Solver::method
SolvingMethod method
Definition: Solver.hpp:45
mgx::Solver::debugOutput
bool debugOutput
Definition: Solver.hpp:54
mgx::Solver::VtxData::operator==
bool operator==(const VtxData &other) const
Definition: Solver.hpp:64
mgx::Solver::vX
DistVertexAttr< DistNhbdC, VtxData, Point2d > * vX
Definition: Solver.hpp:87
mgx::Solver::pSolvingMethod
@ pSolvingMethod
Definition: Solver.hpp:114
mgx::AttrMap
Attribute map wraps std::map.
Definition: Attributes.hpp:686
mgx::Solver::maxNewtonSteps
int maxNewtonSteps
Definition: Solver.hpp:51
mgx::Vertex
Definition: Vertex.hpp:58
mgx::Solver::MyEdgeAttr2
AttrMap< CellCellPair, EdgData > MyEdgeAttr2
Definition: Solver.hpp:100
mgx::Solver::nhbd
DistNhbdC * nhbd
Definition: Solver.hpp:57