Memosa-FVM  0.2
DiffusionDiscretization.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 _DIFFUSIONDISCRETIZATION_H_
6 #define _DIFFUSIONDISCRETIZATION_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 "DiagonalMatrix.h"
16 #include "Gradient.h"
17 #include "DiagonalTensor.h"
18 
19 template<class T>
20 inline T harmonicAverage(const T& x0, const T& x1)
21 {
22  const T sum = x0+x1;
23  if (x0+x1 != NumTypeTraits<T>::getZero())
24  return 2.0*x0*x1/sum;
25  else
26  return sum;
27 }
28 
29 
30 template<class X, class Diag, class OffDiag>
32 {
33 public:
34 
36 
38  typedef typename CCMatrix::DiagArray DiagArray;
40 
41  typedef Gradient<X> XGrad;
43 
44  typedef Array<X> XArray;
48 
50 
52  const GeomFields& geomFields,
53  Field& varField,
54  const Field& diffusivityField,
55  const Field& varGradientField,
56  const T_Scalar thickness=0.0) :
57  Discretization(meshes),
58  _geomFields(geomFields),
59  _varField(varField),
60  _diffusivityField(diffusivityField),
61  _varGradientField(varGradientField),
62  _thickness(thickness)
63  {}
64 
65  void discretize(const Mesh& mesh, MultiFieldMatrix& mfmatrix,
66  MultiField& xField, MultiField& rField)
67  {
68  const StorageSite& cells = mesh.getCells();
69 
70  const MultiField::ArrayIndex cVarIndex(&_varField,&cells);
71  CCMatrix& matrix = dynamic_cast<CCMatrix&>(mfmatrix.getMatrix(cVarIndex,
72  cVarIndex));
73 
74  const VectorT3Array& cellCentroid =
75  dynamic_cast<const VectorT3Array&>(_geomFields.coordinate[cells]);
76 
77  const TArray& cellVolume =
78  dynamic_cast<const TArray&>(_geomFields.volume[cells]);
79 
80  DiagArray& diag = matrix.getDiag();
81 
82  const XArray& xCell = dynamic_cast<const XArray&>(xField[cVarIndex]);
83  XArray& rCell = dynamic_cast<XArray&>(rField[cVarIndex]);
84 
85  const GradArray& xGradCell =
86  dynamic_cast<const GradArray&>(_varGradientField[cells]);
87 
88  const TArray& diffCell =
89  dynamic_cast<const TArray&>(_diffusivityField[cells]);
90 
91  const IntArray& ibType = dynamic_cast<const IntArray&>(_geomFields.ibType[cells]);
92 
93 
94  foreach(const FaceGroupPtr fgPtr, mesh.getAllFaceGroups())
95  {
96  const FaceGroup& fg = *fgPtr;
97  if (fg.groupType == "dielectric interface")
98  {
99  const StorageSite& faces = fg.site;
100  const int nFaces = faces.getCount();
101  const CRConnectivity& faceCells = mesh.getFaceCells(faces);
102  const VectorT3Array& faceArea =
103  dynamic_cast<const VectorT3Array&>(_geomFields.area[faces]);
104  const TArray& faceAreaMag =
105  dynamic_cast<const TArray&>(_geomFields.areaMag[faces]);
106  //const VectorT3Array& faceCentroid =
107  // dynamic_cast<const VectorT3Array&>(_geomFields.coordinate[faces]);
108  CCAssembler& assembler = matrix.getPairWiseAssembler(faceCells);
109 
110  //cout << "doing dielectric interface " << endl;
111 
112  for(int f=0; f<nFaces; f++)
113  {
114  const int c0 = faceCells(f,0);
115  const int c1 = faceCells(f,1);
116 
117  //T_Scalar vol0 = cellVolume[c0];
118  //T_Scalar vol1 = cellVolume[c1];
119 
120  VectorT3 ds=cellCentroid[c1]-cellCentroid[c0];
121  T_Scalar dsMag = mag(ds);
122 
123  T_Scalar faceDiffusivity = harmonicAverage(diffCell[c0],diffCell[c1]);
124 
126  if (dot(faceArea[f],ds) < 0.0)
127  sign *= -1.0;
128 
129  const T_Scalar diffMetric = sign * faceAreaMag[f] / (dsMag + 0.5* _thickness);
130  const T_Scalar diffCoeff = faceDiffusivity*diffMetric;
131 
132  //const T_Scalar volume = faceAreaMag[f] * _thickness;
133  //const T_Scalar src = 1.0;
134  const X dFlux = diffCoeff*(xCell[c1]-xCell[c0]);
135 
136  rCell[c0] += dFlux;
137  rCell[c1] -= dFlux;
138 
139  assembler.getCoeff01(f) +=diffCoeff;
140  assembler.getCoeff10(f) +=diffCoeff;
141 
142  diag[c0] -= diffCoeff;
143  diag[c1] -= diffCoeff;
144  /*
145  cout << " c0 and c1 " << c0 << " " << c1 << endl;
146  cout << " diffcell " << diffCell[c0] << " " << diffCell[c1] << " " << faceDiffusivity << endl;
147  cout << " diffmetric " << diffMetric << endl;
148  cout << "diffCoeff " << diffCoeff << endl;
149  */
150  }
151  }
152 
153  else
154  {
155  const StorageSite& faces = fg.site;
156  const int nFaces = faces.getCount();
157  const CRConnectivity& faceCells = mesh.getFaceCells(faces);
158  const VectorT3Array& faceArea =
159  dynamic_cast<const VectorT3Array&>(_geomFields.area[faces]);
160  const TArray& faceAreaMag =
161  dynamic_cast<const TArray&>(_geomFields.areaMag[faces]);
162  const VectorT3Array& faceCentroid =
163  dynamic_cast<const VectorT3Array&>(_geomFields.coordinate[faces]);
164  CCAssembler& assembler = matrix.getPairWiseAssembler(faceCells);
165  for(int f=0; f<nFaces; f++)
166  {
167  const int c0 = faceCells(f,0);
168  const int c1 = faceCells(f,1);
169 
170  T_Scalar vol0 = cellVolume[c0];
171  T_Scalar vol1 = cellVolume[c1];
172 
173  VectorT3 ds=cellCentroid[c1]-cellCentroid[c0];
174 
175  // for ib faces ignore the solid cell and use the face centroid for diff metric
176  if (((ibType[c0] == Mesh::IBTYPE_FLUID)
177  && (ibType[c1] == Mesh::IBTYPE_BOUNDARY)) ||
178  ((ibType[c1] == Mesh::IBTYPE_FLUID)
179  && (ibType[c0] == Mesh::IBTYPE_BOUNDARY)))
180  {
181  if (ibType[c0] == Mesh::IBTYPE_FLUID)
182  {
183  vol1 = 0.;
184  ds = faceCentroid[f]-cellCentroid[c0];
185  }
186  else
187  {
188  vol0 = 0.;
189  ds = cellCentroid[c1]-faceCentroid[f];
190  }
191  }
192 
193  T_Scalar faceDiffusivity(1.0);
194  if (vol0 == 0.)
195  faceDiffusivity = diffCell[c1];
196  else if (vol1 == 0.)
197  faceDiffusivity = diffCell[c0];
198  else
199  faceDiffusivity = harmonicAverage(diffCell[c0],diffCell[c1]);
200 
201  const T_Scalar diffMetric = faceAreaMag[f]*faceAreaMag[f]/dot(faceArea[f],ds);
202  const T_Scalar diffCoeff = faceDiffusivity*diffMetric;
203  const VectorT3 secondaryCoeff = faceDiffusivity*(faceArea[f]-ds*diffMetric);
204 
205  const XGrad gradF = (xGradCell[c0]*vol0+xGradCell[c1]*vol1)/(vol0+vol1);
206 
207  const X dFluxSecondary = gradF*secondaryCoeff;
208 
209  const X dFlux = diffCoeff*(xCell[c1]-xCell[c0]) + dFluxSecondary;
210 
211  rCell[c0] += dFlux;
212  rCell[c1] -= dFlux;
213 
214  assembler.getCoeff01(f) +=diffCoeff;
215  assembler.getCoeff10(f) +=diffCoeff;
216 
217  diag[c0] -= diffCoeff;
218  diag[c1] -= diffCoeff;
219 
220  //cout <<"dflux" << dFlux << endl;
221 /*
222  cout << " c0 and c1 " << c0 << " " << c1 << endl;
223  cout << " diffcell " << diffCell[c0] << " " << diffCell[c1] << " " << faceDiffusivity << endl;
224  cout << " diffmetric " << diffMetric << endl;
225  cout << "diffCoeff " << diffCoeff << endl;
226 */
227 
228  }
229 
230  }
231  }
232  }
233 private:
239 };
240 
241 #endif
void discretize(const Mesh &mesh, MultiFieldMatrix &mfmatrix, MultiField &xField, MultiField &rField)
Matrix & getMatrix(const Index &rowIndex, const Index &colIndex)
shared_ptr< FaceGroup > FaceGroupPtr
Definition: Mesh.h:46
Field coordinate
Definition: GeomFields.h:19
T harmonicAverage(const T &x0, const T &x1)
Definition: Mesh.h:28
const FaceGroupList & getAllFaceGroups() const
Definition: Mesh.h:193
Definition: Field.h:14
CCMatrix::PairWiseAssembler CCAssembler
Definition: Mesh.h:49
T mag(const Vector< T, 3 > &a)
Definition: Vector.h:260
OffDiag & getCoeff10(const int np)
Definition: CRMatrix.h:131
Array< Diag > & getDiag()
Definition: CRMatrix.h:856
string groupType
Definition: Mesh.h:42
Field ibType
Definition: GeomFields.h:38
DiffusionDiscretization(const MeshList &meshes, const GeomFields &geomFields, Field &varField, const Field &diffusivityField, const Field &varGradientField, const T_Scalar thickness=0.0)
pair< const Field *, const StorageSite * > ArrayIndex
Definition: MultiField.h:21
NumTypeTraits< X >::T_Scalar T_Scalar
const StorageSite & getCells() const
Definition: Mesh.h:109
Field volume
Definition: GeomFields.h:26
OffDiag & getCoeff01(const int np)
Definition: CRMatrix.h:126
const CRConnectivity & getFaceCells(const StorageSite &site) const
Definition: Mesh.cpp:388
int getCount() const
Definition: StorageSite.h:39
Field area
Definition: GeomFields.h:23
CRMatrix< Diag, OffDiag, X > CCMatrix
PairWiseAssembler & getPairWiseAssembler(const CRConnectivity &pairs)
Definition: CRMatrix.h:867
Field areaMag
Definition: GeomFields.h:25
T dot(const Vector< T, 3 > &a, const Vector< T, 3 > &b)
Definition: Vector.h:253
vector< Mesh * > MeshList
Definition: Mesh.h:439
Vector< T_Scalar, 3 > VectorT3
StorageSite site
Definition: Mesh.h:40