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