Memosa-FVM  0.2
ConvectionDiscretization_Kmodel.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 #ifndef _CONVECTIONDISCRETIZATION_KMODEL_H_
6 #define _CONVECTIONDISCRETIZATION_KMODEL_H_
7 
8 #include "CRMatrix.h"
9 #include "Field.h"
10 #include "MultiField.h"
11 #include "MultiFieldMatrix.h"
12 #include "Mesh.h"
13 #include "Discretization.h"
14 #include "StorageSite.h"
15 #include "Gradient.h"
16 
17 template<class X, class Diag, class OffDiag>
19 {
20 public:
22 
24  typedef typename CCMatrix::DiagArray DiagArray;
26 
27  typedef Gradient<X> XGrad;
28 
30  typedef Array<X> XArray;
34 
36 
38  const GeomFields& geomFields,
39  Field& varField,
40  const double cx,
41  const double cy,
42  const double cz,
43  //const double nondim_length,
44  //const double Lx,
45  //const double Ly,
46  //const double Lz,
47  bool useCentralDifference) :
48  //const bool useCentralDifference=false) :
49 
50  Discretization(meshes),
51  _geomFields(geomFields),
52  _varField(varField),
53  _cx(cx),
54  _cy(cy),
55  _cz(cz),
56  _useCentralDifference(useCentralDifference)
57  {}
58 
59  void discretize(const Mesh& mesh, MultiFieldMatrix& mfmatrix,
60  MultiField& xField, MultiField& rField)
61  {
62 
63  const StorageSite& cells = mesh.getCells();
64  const StorageSite& faces = mesh.getFaces();
65 
66 
67  // should there be some other checks ?
68  //if (!_convectingFluxField.hasArray(faces))
69  // return;
70 
71  const MultiField::ArrayIndex cVarIndex(&_varField,&cells);
72  CCMatrix& matrix =
73  dynamic_cast<CCMatrix&>(mfmatrix.getMatrix(cVarIndex,cVarIndex));
74 
75  const CRConnectivity& faceCells = mesh.getAllFaceCells();
76 
77  CCAssembler& assembler = matrix.getPairWiseAssembler(faceCells);
78  DiagArray& diag = matrix.getDiag();
79 
80  const XArray& xCell = dynamic_cast<const XArray&>(xField[cVarIndex]);
81  XArray& rCell = dynamic_cast<XArray&>(rField[cVarIndex]);
82 
83  //const GradArray& xGradCell = dynamic_cast<GradArray>(_varGradientField[cells]);
84 
85  const IntArray& ibType = dynamic_cast<const IntArray&>(_geomFields.ibType[cells]);
86 
87  const int nFaces = faces.getCount();
88 
89  const VectorT3Array& faceArea = dynamic_cast<const VectorT3Array&>(_geomFields.area[faces]);
90  //const X nondim_length=_options["nonDimLt"];
91  //const X Lx=_options["nonDimLx"];
92  //const X Ly=_options["nonDimLy"];
93  //const X Lz=_options["nonDimLz"];
94 
95  if (_geomFields.gridFlux.hasArray(faces))
96  {
97  shared_ptr<TArray> gridFluxPtr(new TArray(nFaces));
98  TArray& gridFlux = *gridFluxPtr;
99  gridFlux = dynamic_cast<const TArray&>(_geomFields.gridFlux[faces]);
100 
101  for(int f=0; f<nFaces; f++)
102  {
103  const int c0 = faceCells(f,0);
104  const int c1 = faceCells(f,1);
105  //const T_Scalar faceCFlux = convectingFlux[f] - gridFlux[f];
106  const T_Scalar faceCFlux = faceArea[f][0]*_cx+faceArea[f][1]*_cy+faceArea[f][2]*_cz - gridFlux[f];
107  //const T_Scalar faceCFlux = faceArea[f][0]*_cx*nondim_length/Lx+faceArea[f][1]*_cy*nondim_length/Ly+faceArea[f][2]*_cz*nondim_length/Lz - gridFlux[f];
108 
109  X varFlux;
110  if (faceCFlux > T_Scalar(0))
111  {
112  varFlux = faceCFlux*xCell[c0];
113  diag[c0] -= faceCFlux;
114  assembler.getCoeff10(f) += faceCFlux;
115  }
116  else
117  {
118  varFlux = faceCFlux*xCell[c1];
119  diag[c1] += faceCFlux;
120  assembler.getCoeff01(f)-= faceCFlux;
121  }
122 
123  rCell[c0] -= varFlux;
124  rCell[c1] += varFlux;
125  }
126  }
127  else
128  {
130  for(int f=0; f<nFaces; f++)
131  {
132  const int c0 = faceCells(f,0);
133  const int c1 = faceCells(f,1);
134  const T_Scalar faceCFlux = faceArea[f][0]*_cx+faceArea[f][1]*_cy+faceArea[f][2]*_cz;
135  //const T_Scalar faceCFlux = faceArea[f][0]*_cx*nondim_length/Lx+faceArea[f][1]*_cy*nondim_length/Ly+faceArea[f][2]*_cz*nondim_length/Lz;
136  bool isIBFace = (((ibType[c0] == Mesh::IBTYPE_FLUID)
137  && (ibType[c1] == Mesh::IBTYPE_BOUNDARY)) ||
138  ((ibType[c1] == Mesh::IBTYPE_FLUID)
139  && (ibType[c0] == Mesh::IBTYPE_BOUNDARY)));
140 
141 
142  X varFlux =0.5*faceCFlux*(xCell[c0] + xCell[c1]);
143 
144  rCell[c0] -= varFlux;
145  rCell[c1] += varFlux;
146 
147  if (isIBFace)
148  {
149  // linearize the actual flux as calculated
150  // above. this will ensure that the Ib
151  // discretization will be able to fix the value
152  // correctly using the ib face value
153 
154  diag[c0] -= 0.5*faceCFlux;
155  assembler.getCoeff10(f) -= 0.5*faceCFlux;
156  diag[c1] += 0.5*faceCFlux;
157  assembler.getCoeff01(f) += 0.5*faceCFlux;
158  }
159  else
160  {
161  // linearize as upwind flux so that linear system
162  // remains diagonally dominant
163  if (faceCFlux > T_Scalar(0))
164  {
165  diag[c0] -= faceCFlux;
166  assembler.getCoeff10(f) += faceCFlux;
167  }
168  else
169  {
170  diag[c1] += faceCFlux;
171  assembler.getCoeff01(f)-= faceCFlux;
172  }
173  }
174  }
175  }
176  else
177  for(int f=0; f<nFaces; f++)
178  {
179  const int c0 = faceCells(f,0);
180  const int c1 = faceCells(f,1);
181  const T_Scalar faceCFlux = faceArea[f][0]*_cx+faceArea[f][1]*_cy+faceArea[f][2]*_cz;
182  //const T_Scalar faceCFlux = faceArea[f][0]*_cx*nondim_length/Lx+faceArea[f][1]*_cy*nondim_length/Ly+faceArea[f][2]*_cz*nondim_length/Lz;
183 
184 
185  X varFlux;
186 
187  if (faceCFlux > T_Scalar(0))
188  {
189  varFlux = faceCFlux*xCell[c0];
190  diag[c0] -= faceCFlux;
191  assembler.getCoeff10(f) += faceCFlux;
192  }
193  else
194  {
195  varFlux = faceCFlux*xCell[c1];
196  diag[c1] += faceCFlux;
197  assembler.getCoeff01(f)-= faceCFlux;
198  }
199 
200  rCell[c0] -= varFlux;
201  rCell[c1] += varFlux;
202  }
203  }
204 
205  //const int nCells = cells.getSelfCount();
206  //for(int c=0;c<nCells;c++)
207  //{
208  // const T_Scalar cImb = continuityResidual[c];
209  // diag[c] += cImb;
210  //}
211 
212  }
213 private:
215  const Field& _varField;
216  const double _cx;
217  const double _cy;
218  const double _cz;
221 };
222 
223 #endif
Matrix & getMatrix(const Index &rowIndex, const Index &colIndex)
bool hasArray(const StorageSite &s) const
Definition: Field.cpp:37
Definition: Field.h:14
Definition: Mesh.h:49
OffDiag & getCoeff10(const int np)
Definition: CRMatrix.h:131
Field gridFlux
Definition: GeomFields.h:31
Array< Diag > & getDiag()
Definition: CRMatrix.h:856
void discretize(const Mesh &mesh, MultiFieldMatrix &mfmatrix, MultiField &xField, MultiField &rField)
Field ibType
Definition: GeomFields.h:38
const CRConnectivity & getAllFaceCells() const
Definition: Mesh.cpp:378
pair< const Field *, const StorageSite * > ArrayIndex
Definition: MultiField.h:21
const StorageSite & getFaces() const
Definition: Mesh.h:108
const StorageSite & getCells() const
Definition: Mesh.h:109
ConvectionDiscretization_Kmodel(const MeshList &meshes, const GeomFields &geomFields, Field &varField, const double cx, const double cy, const double cz, bool useCentralDifference)
OffDiag & getCoeff01(const int np)
Definition: CRMatrix.h:126
int getCount() const
Definition: StorageSite.h:39
Field area
Definition: GeomFields.h:23
PairWiseAssembler & getPairWiseAssembler(const CRConnectivity &pairs)
Definition: CRMatrix.h:867
vector< Mesh * > MeshList
Definition: Mesh.h:439