Memosa-FVM  0.2
TimeDerivativeStructureDiscretization.h
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 
6 #ifndef _TIMEDERIVATIVESTRUCTUREDISCRETIZATION_H_
7 #define _TIMEDERIVATIVESTRUCTUREDISCRETIZATION_H_
8 
9 #include "CRMatrix.h"
10 #include "Field.h"
11 #include "MultiField.h"
12 #include "MultiFieldMatrix.h"
13 #include "Mesh.h"
14 #include "Discretization.h"
15 #include "StorageSite.h"
16 
17 template<class X, class Diag, class OffDiag>
19 {
20 public:
22 
24  typedef typename CCMatrix::DiagArray DiagArray;
26 
27  typedef Array<X> XArray;
29 
30 
32  const GeomFields& geomFields,
33  Field& varField,
34  Field& varN1Field,
35  Field& varN2Field,
36  Field& varN3Field,
37  const Field& densityField,
38  const Field& volume0Field,
39  const bool& variableTimeStep,
40  const T_Scalar dT,
41  const T_Scalar dTN1,
42  const T_Scalar dTN2) :
43  Discretization(meshes),
44  _geomFields(geomFields),
45  _varField(varField),
46  _varN1Field(varN1Field),
47  _varN2Field(varN2Field),
48  _varN3Field(varN3Field),
49  _densityField(densityField),
50  _volume0Field(volume0Field),
51  _variableTimeStep(variableTimeStep),
52  _dT(dT),
53  _dTN1(dTN1),
54  _dTN2(dTN2)
55  {}
56 
57  void discretize(const Mesh& mesh, MultiFieldMatrix& mfmatrix,
58  MultiField& xField, MultiField& rField)
59  {
60 
61  const StorageSite& cells = mesh.getCells();
62 
63  const TArray& density =
64  dynamic_cast<const TArray&>(_densityField[cells]);
65 
66  //const TArray& cellVolume =
67  // dynamic_cast<const TArray&>(_geomFields.volume[cells]);
68 
69  const TArray& cellVolume0 =
70  dynamic_cast<const TArray&>(_volume0Field[cells]);
71 
72  const MultiField::ArrayIndex cVarIndex(&_varField,&cells);
73  CCMatrix& matrix =
74  dynamic_cast<CCMatrix&>(mfmatrix.getMatrix(cVarIndex,cVarIndex));
75 
76  DiagArray& diag = matrix.getDiag();
77 
78  const XArray& x = dynamic_cast<const XArray&>(_varField[cells]);
79  const XArray& xN1 = dynamic_cast<const XArray&>(_varN1Field[cells]);
80 
81  XArray& rCell = dynamic_cast<XArray&>(rField[cVarIndex]);
82 
83 
84  const int nCells = cells.getSelfCount();
85 
86 
87  // second order
88  const XArray& xN2 = dynamic_cast<const XArray&>(_varN2Field[cells]);
89 
90  T_Scalar two(2.0);
91  const T_Scalar _dT2 = _dT*_dT;
92 
94  {
95  if (_varN3Field.hasArray(cells))
96  {
97  const XArray& xN3 = dynamic_cast<const XArray&>(_varN3Field[cells]);
98  T_Scalar five(5.0);
99  T_Scalar four(4.0);
100  for(int c=0; c<nCells; c++)
101  {
102  const T_Scalar rhoVbydT2 = density[c]*cellVolume0[c]/_dT2;
103  rCell[c] -= rhoVbydT2*(two*x[c] - five*xN1[c] + four*xN2[c]
104  - xN3[c]);
105  diag[c] -= two*rhoVbydT2;
106  }
107  }
108  else
109  {
110  for(int c=0; c<nCells; c++)
111  {
112  const T_Scalar rhoVbydT2 = density[c]*cellVolume0[c]/_dT2;
113  rCell[c] -= rhoVbydT2*(x[c]- two*xN1[c]
114  + xN2[c]);
115  diag[c] -= rhoVbydT2;
116  }
117  }
118  }
119  else
120  {
121  T_Scalar a = (_dT + _dTN1)/_dT;
122  T_Scalar b = (_dT + _dTN1 + _dTN2)/_dT;
123  T_Scalar one(1.0);
124  if (_varN3Field.hasArray(cells))
125  {
126  T_Scalar c1 = (two*a*b*(pow(a,two)-pow(b,two))+two*b*(pow(b,two)-one)-two*a*(pow(a,two)-one))/
127  (a*b*(a-one)*(b-one)*(a-b));
128  T_Scalar c2 = -two*(a+b)/((a-1)*(b-1));
129  T_Scalar c3 = -two*(b+one)/(a*(a-b)*(a-one));
130  T_Scalar c4 = two*(a+one)/(b*(a-b)*(b-one));
131  const XArray& xN3 = dynamic_cast<const XArray&>(_varN3Field[cells]);
132  for(int c=0; c<nCells; c++)
133  {
134  const T_Scalar rhoVbydT2 = density[c]*cellVolume0[c]/_dT2;
135  rCell[c] -= rhoVbydT2*(c1*x[c] + c2*xN1[c] + c3*xN2[c]
136  + c4*xN3[c]);
137  diag[c] -= c1*rhoVbydT2;
138  }
139  }
140  else
141  {
142  T_Scalar c1 = two/a;
143  T_Scalar c2 = -two/(a-one);
144  T_Scalar c3 = two/(a*(a-one));
145  for(int c=0; c<nCells; c++)
146  {
147  const T_Scalar rhoVbydT2 = density[c]*cellVolume0[c]/_dT2;
148  rCell[c] -= rhoVbydT2*(c1*x[c] + c2*xN1[c]
149  + c3*xN2[c]);
150  diag[c] -= c1*rhoVbydT2;
151  }
152  }
153  }
154 
155 
156  }
157 
158  void explicitAdvance(const Mesh& mesh,
159  MultiField& xField,
160  MultiField& rField,
161  MultiField& deltaField)
162  {
163 
164  const StorageSite& cells = mesh.getCells();
165 
166  const TArray& density =
167  dynamic_cast<const TArray&>(_densityField[cells]);
168 
169  //const TArray& cellVolume =
170  // dynamic_cast<const TArray&>(_geomFields.volume[cells]);
171 
172  const TArray& cellVolume0 =
173  dynamic_cast<const TArray&>(_volume0Field[cells]);
174 
175  const MultiField::ArrayIndex cVarIndex(&_varField,&cells);
176 
177 
178  const XArray& x = dynamic_cast<const XArray&>(_varField[cells]);
179  const XArray& xN1 = dynamic_cast<const XArray&>(_varN1Field[cells]);
180 
181  XArray& rCell = dynamic_cast<XArray&>(rField[cVarIndex]);
182  XArray& deltaCell = dynamic_cast<XArray&>(deltaField[cVarIndex]);
183 
184 
185  const int nCells = cells.getSelfCount();
186 
187 
188  // second order
189  const XArray& xN2 = dynamic_cast<const XArray&>(_varN2Field[cells]);
190 
191  T_Scalar two(2.0);
192  const T_Scalar _dT2 = _dT*_dT;
193 
194  if (_varN3Field.hasArray(cells))
195  {
196  const XArray& xN3 = dynamic_cast<const XArray&>(_varN3Field[cells]);
197  T_Scalar five(5.0);
198  T_Scalar four(4.0);
199  for(int c=0; c<nCells; c++)
200  {
201  const T_Scalar rhoVbydT2 = density[c]*cellVolume0[c]/_dT2;
202  rCell[c] -= rhoVbydT2*(two*x[c] - five*xN1[c] + four*xN2[c]
203  - xN3[c]);
204 
205  T_Scalar diag = -two*rhoVbydT2;
206  deltaCell[c] = -rCell[c]/diag;
207  }
208  }
209  else
210  {
211  for(int c=0; c<nCells; c++)
212  {
213  const T_Scalar rhoVbydT2 = density[c]*cellVolume0[c]/_dT2;
214  rCell[c] -= rhoVbydT2*(x[c]- two*xN1[c]
215  + xN2[c]);
216  T_Scalar diag = -rhoVbydT2;
217  deltaCell[c] = -rCell[c]/diag;
218  }
219  }
220 
221  }
222 
223 private:
225  const Field& _varField;
231  const bool _variableTimeStep;
232  const T_Scalar _dT;
235 };
236 
237 #endif
Matrix & getMatrix(const Index &rowIndex, const Index &colIndex)
int getSelfCount() const
Definition: StorageSite.h:40
TimeDerivativeStructureDiscretization(const MeshList &meshes, const GeomFields &geomFields, Field &varField, Field &varN1Field, Field &varN2Field, Field &varN3Field, const Field &densityField, const Field &volume0Field, const bool &variableTimeStep, const T_Scalar dT, const T_Scalar dTN1, const T_Scalar dTN2)
bool hasArray(const StorageSite &s) const
Definition: Field.cpp:37
Definition: Field.h:14
Definition: Mesh.h:49
Array< Diag > & getDiag()
Definition: CRMatrix.h:856
void discretize(const Mesh &mesh, MultiFieldMatrix &mfmatrix, MultiField &xField, MultiField &rField)
pair< const Field *, const StorageSite * > ArrayIndex
Definition: MultiField.h:21
void explicitAdvance(const Mesh &mesh, MultiField &xField, MultiField &rField, MultiField &deltaField)
const StorageSite & getCells() const
Definition: Mesh.h:109
vector< Mesh * > MeshList
Definition: Mesh.h:439