Memosa-FVM  0.2
BatteryPCBinaryElectrolyteDiscretization.h
Go to the documentation of this file.
1 #ifndef _BATTERYPCBINARYELECTROLYTEDISCRETIZATION_H_
2 #define _BATTERYPCBINARYELECTROLYTEDISCRETIZATION_H_
3 
4 #include "CRMatrix.h"
5 #include "Field.h"
6 #include "MultiField.h"
7 #include "MultiFieldMatrix.h"
8 #include "Mesh.h"
9 #include "Discretization.h"
10 #include "StorageSite.h"
11 #include "DiagonalMatrix.h"
12 #include "Gradient.h"
13 #include "DiagonalTensor.h"
14 /*
15  template<class T>
16  inline T harmonicAverage(const T& x0, const T& x1)
17  {
18  const T sum = x0+x1;
19  if (x0+x1 != NumTypeTraits<T>::getZero())
20  return 2.0*x0*x1/sum;
21  else
22  return sum; }
23 */
24 
25 template<class X, class Diag, class OffDiag>
27  {
28  public:
29 
31 
33  typedef typename CCMatrix::DiagArray DiagArray;
35 
36  typedef Gradient<X> XGrad;
38 
39  typedef Array<X> XArray;
45 
47  const GeomFields& geomFields,
48  Field& varField,
49  const Field& diffusivityField,
50  const Field& concField,
51  const Field& concGradientField,
52  const bool thermalModel,
53  const int ElectrolyteMeshID) :
54  Discretization(meshes),
55  _geomFields(geomFields),
56  _varField(varField),
57  _diffusivityField(diffusivityField),
58  _concField(concField),
59  _concGradientField(concGradientField),
60  _thermalModel(thermalModel),
61  _ElectrolyteMeshID(ElectrolyteMeshID)
62  {}
63 
64  void discretize(const Mesh& mesh, MultiFieldMatrix& mfmatrix,
65  MultiField& xField, MultiField& rField)
66  {
67  if (mesh.getID() == _ElectrolyteMeshID)
68  {
69  // electric potential variables
70  const StorageSite& cells = mesh.getCells();
71 
72  const MultiField::ArrayIndex cVarIndex(&_varField,&cells);
73 
74  //CCMatrix& matrix = dynamic_cast<CCMatrix&>(mfmatrix.getMatrix(cVarIndex,cVarIndex));
75 
76  const VectorT3Array& cellCentroid =
77  dynamic_cast<const VectorT3Array&>(_geomFields.coordinate[cells]);
78  const TArray& cellVolume =
79  dynamic_cast<const TArray&>(_geomFields.volume[cells]);
80 
81  //DiagArray& diag = matrix.getDiag();
82 
83  const XArray& xCell = dynamic_cast<const XArray&>(xField[cVarIndex]); // need for temp
84  XArray& rCell = dynamic_cast<XArray&>(rField[cVarIndex]);
85 
86  // lnspeciesConcentration variables
87  const TArray& lnSpecConcCell = dynamic_cast<const TArray&>(_concField[cells]);
88 
89  const TGradArray& lnSpecConcGradCell =
90  dynamic_cast<const TGradArray&>(_concGradientField[cells]);
91 
92  const XArray& diffCell =
93  dynamic_cast<const XArray&>(_diffusivityField[cells]);
94 
95  //const IntArray& ibType = dynamic_cast<const IntArray&>(_geomFields.ibType[cells]);
96 
97 
98  foreach(const FaceGroupPtr fgPtr, mesh.getAllFaceGroups())
99  {
100  const FaceGroup& fg = *fgPtr;
101 
102  const StorageSite& faces = fg.site;
103  const int nFaces = faces.getCount();
104  const CRConnectivity& faceCells = mesh.getFaceCells(faces);
105  const VectorT3Array& faceArea =
106  dynamic_cast<const VectorT3Array&>(_geomFields.area[faces]);
107  const TArray& faceAreaMag =
108  dynamic_cast<const TArray&>(_geomFields.areaMag[faces]);
109  //const VectorT3Array& faceCentroid =dynamic_cast<const VectorT3Array&>(_geomFields.coordinate[faces]);
110  //CCAssembler& assembler = matrix.getPairWiseAssembler(faceCells);
111  for(int f=0; f<nFaces; f++)
112  {
113  const int c0 = faceCells(f,0);
114  const int c1 = faceCells(f,1);
115 
116  T_Scalar vol0 = cellVolume[c0];
117  T_Scalar vol1 = cellVolume[c1];
118 
119  VectorT3 ds=cellCentroid[c1]-cellCentroid[c0];
120 
121  // for ib faces ignore the solid cell and use the face centroid for diff metric
122  /*
123  if (((ibType[c0] == Mesh::IBTYPE_FLUID)
124  && (ibType[c1] == Mesh::IBTYPE_BOUNDARY)) ||
125  ((ibType[c1] == Mesh::IBTYPE_FLUID)
126  && (ibType[c0] == Mesh::IBTYPE_BOUNDARY)))
127  {
128  if (ibType[c0] == Mesh::IBTYPE_FLUID)
129  {
130  vol1 = 0.;
131  ds = faceCentroid[f]-cellCentroid[c0];
132  }
133  else
134  {
135  vol0 = 0.;
136  ds = cellCentroid[c1]-faceCentroid[f];
137  }
138  }*/
139 
140  T_Scalar faceDiffusivity(1.0);
141  if (vol0 == 0.)
142  faceDiffusivity = (diffCell[c1])[0];
143  else if (vol1 == 0.)
144  faceDiffusivity = (diffCell[c0])[0];
145  else
146  faceDiffusivity = harmonicAverage((diffCell[c0])[0],(diffCell[c1])[0]);
147 
148  T_Scalar faceTemp(300.0);
149  if (_thermalModel)
150  {
151  if (vol0 == 0.)
152  faceTemp = (xCell[c1])[2];
153  else if (vol1 == 0.)
154  faceTemp = (xCell[c0])[2];
155  else
156  faceTemp = harmonicAverage((xCell[c0])[2],(xCell[c1])[2]);
157  }
158 
159 
160  //convert diffusivity for ln term in battery equation
161  const T_Scalar transportNumber = 0.4;
162  const T_Scalar R = 8.314;
163  const T_Scalar F = 96485.0;
164  faceDiffusivity = faceDiffusivity*(-2.0)*R*faceTemp/F*(1.0-transportNumber);
165 
166  const T_Scalar diffMetric = faceAreaMag[f]*faceAreaMag[f]/dot(faceArea[f],ds);
167  const T_Scalar diffCoeff = faceDiffusivity*diffMetric;
168  const VectorT3 secondaryCoeff = faceDiffusivity*(faceArea[f]-ds*diffMetric);
169 
170  const TGrad gradF = (lnSpecConcGradCell[c0]*vol0+lnSpecConcGradCell[c1]*vol1)/(vol0+vol1);
171 
172  const T_Scalar dFluxSecondary = gradF*secondaryCoeff;
173 
174  const T_Scalar dFlux = diffCoeff*(lnSpecConcCell[c1]-lnSpecConcCell[c0]) + dFluxSecondary;
175 
176  (rCell[c0])[0] += dFlux;
177  (rCell[c1])[0] -= dFlux;
178 
179  //assembler.getCoeff01(f) +=diffCoeff;
180  //assembler.getCoeff10(f) +=diffCoeff;
181 
182  //diag[c0] -= diffCoeff;
183  //diag[c1] -= diffCoeff;
184 
185  //cout <<"dflux" << dFlux << endl;
186  /*
187  cout << " c0 and c1 " << c0 << " " << c1 << endl;
188  cout << " diffcell " << diffCell[c0] << " " << diffCell[c1] << " " << faceDiffusivity << endl;
189  cout << " diffmetric " << diffMetric << endl;
190  cout << "diffCoeff " << diffCoeff << endl;
191  */
192 
193 
194 
195  }
196  }
197  }
198  }
199  private:
205  const bool _thermalModel;
207  };
208 
209 #endif
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
Definition: Mesh.h:49
pair< const Field *, const StorageSite * > ArrayIndex
Definition: MultiField.h:21
BatteryPCBinaryElectrolyteDiscretization(const MeshList &meshes, const GeomFields &geomFields, Field &varField, const Field &diffusivityField, const Field &concField, const Field &concGradientField, const bool thermalModel, const int ElectrolyteMeshID)
void discretize(const Mesh &mesh, MultiFieldMatrix &mfmatrix, MultiField &xField, MultiField &rField)
const StorageSite & getCells() const
Definition: Mesh.h:109
Field volume
Definition: GeomFields.h:26
const CRConnectivity & getFaceCells(const StorageSite &site) const
Definition: Mesh.cpp:388
int getCount() const
Definition: StorageSite.h:39
Field area
Definition: GeomFields.h:23
Field areaMag
Definition: GeomFields.h:25
T dot(const Vector< T, 3 > &a, const Vector< T, 3 > &b)
Definition: Vector.h:253
int getID() const
Definition: Mesh.h:106
vector< Mesh * > MeshList
Definition: Mesh.h:439
StorageSite site
Definition: Mesh.h:40