| 1 |
/* |
| 2 |
* Advanced Simulation Library <http://asl.org.il> |
| 3 |
* |
| 4 |
* Copyright 2015 Avtech Scientific <http://avtechscientific.com> |
| 5 |
* |
| 6 |
* |
| 7 |
* This file is part of Advanced Simulation Library (ASL). |
| 8 |
* |
| 9 |
* ASL is free software: you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU Affero General Public License as |
| 11 |
* published by the Free Software Foundation, version 3 of the License. |
| 12 |
* |
| 13 |
* ASL is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Affero General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Affero General Public License |
| 19 |
* along with ASL. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
#include "aslFDStefanMaxwell.h" |
| 25 |
#include <aslGenerators.h> |
| 26 |
#include <acl/aclGenerators.h> |
| 27 |
#include <acl/acl.h> |
| 28 |
#include <math/aslTemplateVE.h> |
| 29 |
#include <acl/aclMath/aclVectorOfElements.h> |
| 30 |
|
| 31 |
#include <data/aslDataWithGhostNodes.h> |
| 32 |
#include <acl/Kernels/aclKernel.h> |
| 33 |
#include <math/aslTemplates.h> |
| 34 |
|
| 35 |
#include <acl/Kernels/aclKernelConfigurationTemplates.h> |
| 36 |
|
| 37 |
namespace asl |
| 38 |
{ |
| 39 |
FDStefanMaxwell::FDStefanMaxwell(): |
| 40 |
SingleKernelNM(acl::KERNEL_SIMDUA), |
| 41 |
cData(0), |
| 42 |
cInternalData(0), |
| 43 |
vectorTemplate(NULL) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
FDStefanMaxwell::FDStefanMaxwell(Data c1, |
| 49 |
Data c2, |
| 50 |
const acl::VectorOfElements & dC, |
| 51 |
const VectorTemplate* vT): |
| 52 |
SingleKernelNM(acl::KERNEL_SIMDUA), |
| 53 |
cData({c1,c2}), |
| 54 |
cInternalData(0u), |
| 55 |
vectorTemplate(vT), |
| 56 |
diffusionCoefficients({vector<acl::VectorOfElements>({acl::generateVEConstant(0.), dC}), |
| 57 |
vector<acl::VectorOfElements>({dC,acl::generateVEConstant(0.)})}) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
void FDStefanMaxwell::init0() |
| 62 |
{ |
| 63 |
if (cData.size() != diffusionCoefficients.size()) |
| 64 |
errorMessage("FDStefanMaxwell::init - some of compenents are underdefined"); |
| 65 |
|
| 66 |
acl::TypeID type(getElementType(cData[0]->getDContainer())); |
| 67 |
unsigned int nC(cData.size()); |
| 68 |
unsigned int nd(nD(*vectorTemplate)); |
| 69 |
unsigned int nDir(vectorTemplate->vectors.size()); |
| 70 |
|
| 71 |
cInternalData.resize(cData.size()); |
| 72 |
for (unsigned int i(0); i < nC; ++i) |
| 73 |
{ |
| 74 |
cInternalData[i] = clone(cData[i]); |
| 75 |
initData(cInternalData[i]->getEContainer(),cData[i]->getEContainer()); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
acl::VectorOfElements cnew(acl::generateVEPrivateVariable(nC, type)); |
| 80 |
acl::VectorOfElements lambda(acl::generateVEPrivateVariable(nC, type)); |
| 81 |
acl::VectorOfElements cTotal(acl::generateVEPrivateVariable(1, type)); |
| 82 |
acl::MatrixOfElements aM(nC,nC); |
| 83 |
acl::VectorOfElements flux(acl::generateVEPrivateVariable(nC, type)); |
| 84 |
|
| 85 |
vector<TemplateVE> velocityT(nd); |
| 86 |
if(velocity.get() != 0) |
| 87 |
for(unsigned int i(0); i < nd; ++i) |
| 88 |
{ |
| 89 |
velocityT[i].init(*velocity, *vectorTemplate, i,false); |
| 90 |
} |
| 91 |
|
| 92 |
TemplateVE phiT; |
| 93 |
if(efPhi.get() != 0) |
| 94 |
phiT.init(*efPhi, *vectorTemplate, 0, false); |
| 95 |
|
| 96 |
vector<TemplateVE> cT(nC); |
| 97 |
for (unsigned int i(0); i < cData.size(); ++i) |
| 98 |
{ |
| 99 |
cT[i].init(*cData[i], *vectorTemplate, 0, false); |
| 100 |
(*kernel)<<(subVE(cnew,i) =cT[i].getValue(0)); |
| 101 |
} |
| 102 |
|
| 103 |
for(unsigned int kk(1); kk<nDir; ++kk) |
| 104 |
{ |
| 105 |
vector<acl::VectorOfElements> cL; |
| 106 |
acl::VectorOfElements mNablaC(nC); |
| 107 |
|
| 108 |
(*kernel) << (cTotal = acl::generateVEConstant(0)); |
| 109 |
//computation of cTotal and Lambda |
| 110 |
for (unsigned int i(0); i < nC; ++i) |
| 111 |
{ |
| 112 |
cL.push_back((cT[i].getValue(0)+cT[i].getValue(kk))*.5); |
| 113 |
mNablaC[i]=(cT[i].getValue(0)-cT[i].getValue(kk))[0]; |
| 114 |
(*kernel) << (cTotal += cL[i]); |
| 115 |
(*kernel) << (subVE(lambda,i) =acl::generateVEConstant(0.)); |
| 116 |
for (unsigned int j(0); j < nC; ++j) |
| 117 |
if(j!=i) |
| 118 |
(*kernel) << (subVE(lambda,i) += cL[i]/diffusionCoefficients[i][j]); |
| 119 |
} |
| 120 |
(*kernel) << (lambda/=cTotal); |
| 121 |
// eccounting of the electric field |
| 122 |
if(efPhi.get() != 0) |
| 123 |
for (unsigned int i(0); i < nC; ++i) |
| 124 |
mNablaC[i]=(subVE(mNablaC,i)-cL[i]*efCharge[i]* |
| 125 |
(phiT.getValue(kk)-phiT.getValue(0)))[0]; |
| 126 |
|
| 127 |
// compilation of matrix |
| 128 |
for (unsigned int i(0); i < nC; ++i) |
| 129 |
for (unsigned int j(0); j < nC; ++j) |
| 130 |
{ |
| 131 |
if(i==j) |
| 132 |
aM.setElement(i,i, lambda[i]); |
| 133 |
else |
| 134 |
aM.setElement(i,j, (-cL[i]/cTotal/diffusionCoefficients[i][j])[0]); |
| 135 |
} |
| 136 |
if(dustDiffusionCoefficients.size()) |
| 137 |
for (unsigned int i(0); i < nC; ++i) |
| 138 |
aM.setElement(i,i,(1./dustDiffusionCoefficients[i]+aM.getVE(i,i))[0]); |
| 139 |
|
| 140 |
(*kernel) << gcSolveSystem(aM, mNablaC, flux); |
| 141 |
acl::VectorOfElements advFlux(nC); |
| 142 |
if(velocity.get() != 0) |
| 143 |
{ |
| 144 |
acl::VectorOfElements velL((velocityT[0].getValue(kk)+velocityT[0].getValue(0)) * |
| 145 |
.5 * vectorTemplate->vectors[kk]); |
| 146 |
for (unsigned int i(1); i < nd; ++i) |
| 147 |
copy(velL+(velocityT[i].getValue(kk)+velocityT[i].getValue(0)) * |
| 148 |
.5 * vectorTemplate->vectors[kk], velL); |
| 149 |
for (unsigned int i(0); i < nC; ++i) |
| 150 |
advFlux[i]=(cL[i]*velL)[0]; |
| 151 |
} |
| 152 |
else |
| 153 |
copy(acl::generateVEConstantN(nC,0), advFlux); |
| 154 |
|
| 155 |
(*kernel) << (cnew-=(flux + advFlux)*vectorTemplate->laplasCoefs[kk]); |
| 156 |
} |
| 157 |
|
| 158 |
for (unsigned int i(0); i < nC; ++i) |
| 159 |
(*kernel) << (assignmentSafe(cInternalData[i]->getSubContainer(), subVE(cnew,i))); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
void FDStefanMaxwell::postProcessing() |
| 164 |
{ |
| 165 |
for (unsigned int i(0); i < cData.size(); ++i) |
| 166 |
swapBuffers(cData[i]->getDContainer(), cInternalData[i]->getDContainer()); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
void FDStefanMaxwell::setDiffusionCoefficient(acl::VectorOfElements dC, |
| 171 |
unsigned int i,unsigned int j) |
| 172 |
{ |
| 173 |
if(i==j) |
| 174 |
errorMessage("FDStefanMaxwell::setDiffusionCoefficient - i and j should have values"); |
| 175 |
copy(dC,diffusionCoefficients[i][j]); |
| 176 |
copy(dC,diffusionCoefficients[j][i]); |
| 177 |
} |
| 178 |
|
| 179 |
void FDStefanMaxwell::addComponent(Data c, const Param & dC) |
| 180 |
{ |
| 181 |
unsigned int n(diffusionCoefficients.size()); |
| 182 |
diffusionCoefficients.resize(n+1); |
| 183 |
for(unsigned int i(0); i<n+1; ++i) |
| 184 |
diffusionCoefficients[i].resize(n+1); |
| 185 |
copy(acl::generateVEConstant(0),diffusionCoefficients[n][n]); |
| 186 |
for(unsigned int i(0); i<n; ++i) |
| 187 |
setDiffusionCoefficient(subVE(dC,i),n,i); |
| 188 |
cData.push_back(c); |
| 189 |
} |
| 190 |
|
| 191 |
void FDStefanMaxwell::addComponent(Data c, const Param & dC, const Param & q) |
| 192 |
{ |
| 193 |
addComponent(c, dC); |
| 194 |
efCharge.push_back(q); |
| 195 |
} |
| 196 |
|
| 197 |
void FDStefanMaxwell::setVelocity(Field v) |
| 198 |
{ |
| 199 |
velocity=v; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
void FDStefanMaxwell::setElectricField(Field phi) |
| 204 |
{ |
| 205 |
efPhi=phi; |
| 206 |
} |
| 207 |
|
| 208 |
void FDStefanMaxwell::setDustDiffusionCoefficient(unsigned int i, |
| 209 |
const acl::VectorOfElements & dd) |
| 210 |
{ |
| 211 |
if (i>=cData.size()) |
| 212 |
errorMessage("FDStefanMaxwell::setDustDiffusionCoefficient: i is larger than the number of components"); |
| 213 |
|
| 214 |
if (i+1>dustDiffusionCoefficients.size() && i<cData.size()) |
| 215 |
dustDiffusionCoefficients.resize(i+1); |
| 216 |
acl::copy(dd,dustDiffusionCoefficients[i]); |
| 217 |
} |
| 218 |
|
| 219 |
void FDStefanMaxwell::setCharge(unsigned int i, const Param & q) |
| 220 |
{ |
| 221 |
if (i>=cData.size()) |
| 222 |
errorMessage("FDStefanMaxwell::setCharge: i is larger than the number of components"); |
| 223 |
|
| 224 |
if (i+1>efCharge.size() && i<cData.size()) |
| 225 |
efCharge.resize(i+1); |
| 226 |
acl::copy(q,efCharge[i]); |
| 227 |
} |
| 228 |
|
| 229 |
SPFDStefanMaxwell generateFDStefanMaxwell(SPDataWithGhostNodesACLData c1, |
| 230 |
SPDataWithGhostNodesACLData c2, |
| 231 |
double diffusionCoeff, |
| 232 |
SPAbstractDataWithGhostNodes v, |
| 233 |
const VectorTemplate* vt) |
| 234 |
{ |
| 235 |
auto nm(make_shared<FDStefanMaxwell> (c1, c2, acl::generateVEConstant(diffusionCoeff), vt)); |
| 236 |
nm->setVelocity(v); |
| 237 |
return nm; |
| 238 |
} |
| 239 |
|
| 240 |
SPFDStefanMaxwell generateFDStefanMaxwell(SPDataWithGhostNodesACLData c1, |
| 241 |
SPDataWithGhostNodesACLData c2, |
| 242 |
double diffusionCoeff, |
| 243 |
const VectorTemplate* vt) |
| 244 |
{ |
| 245 |
auto nm(make_shared<FDStefanMaxwell> (c1, c2, acl::generateVEConstant(diffusionCoeff), vt)); |
| 246 |
return nm; |
| 247 |
} |
| 248 |
|
| 249 |
FDStefanMaxwellElectricField::FDStefanMaxwellElectricField(SPFDStefanMaxwell sm, Data phi): |
| 250 |
SingleKernelNM(acl::KERNEL_SIMDUA), |
| 251 |
smSolver(sm), |
| 252 |
phi(phi) |
| 253 |
{ |
| 254 |
} |
| 255 |
|
| 256 |
void FDStefanMaxwellElectricField::init0() |
| 257 |
{ |
| 258 |
|
| 259 |
auto & cData(smSolver->getData()); |
| 260 |
auto vt(smSolver->getVectorTemplate()); |
| 261 |
acl::TypeID type(getElementType(cData[0]->getDContainer())); |
| 262 |
unsigned int nC(cData.size()); |
| 263 |
unsigned int nDir(vt->vectors.size()); |
| 264 |
|
| 265 |
phiInternalData = clone(phi); |
| 266 |
initData(phiInternalData->getEContainer(), phi->getEContainer() ); |
| 267 |
|
| 268 |
acl::VectorOfElements phiNew(acl::generateVEPrivateVariable(1, type)); |
| 269 |
acl::VectorOfElements lambda(acl::generateVEPrivateVariable(nC, type)); |
| 270 |
acl::VectorOfElements cTotal(acl::generateVEPrivateVariable(1, type)); |
| 271 |
acl::MatrixOfElements aM(nC,nC); |
| 272 |
acl::VectorOfElements flux(acl::generateVEPrivateVariable(nC, type)); |
| 273 |
|
| 274 |
vector<TemplateVE> cT(nC); |
| 275 |
for (unsigned int i(0); i < cData.size(); ++i) |
| 276 |
cT[i].init(*cData[i], *vt, 0, false); |
| 277 |
|
| 278 |
TemplateVE phiT(*phi, *vt, 0, false); |
| 279 |
(*kernel)<<(phiNew = phiT.getValue(0)+phiS->getSubContainer()*stepFactor); |
| 280 |
|
| 281 |
for(unsigned int kk(1); kk<nDir; ++kk) |
| 282 |
{ |
| 283 |
vector<acl::VectorOfElements> cL; |
| 284 |
acl::VectorOfElements cq(nC); |
| 285 |
acl::VectorOfElements q(nC); |
| 286 |
(*kernel) << (cTotal = acl::generateVEConstant(0)); |
| 287 |
|
| 288 |
//computation of cTotal and Lambda |
| 289 |
for (unsigned int i(0); i < nC; ++i) |
| 290 |
{ |
| 291 |
cL.push_back((cT[i].getValue(0)+cT[i].getValue(kk))*.5); |
| 292 |
q[i]=smSolver->getCharge(i)[0]; |
| 293 |
cq[i]=(smSolver->getCharge(i)*cL[i])[0]; |
| 294 |
(*kernel) << (cTotal += cL[i]); |
| 295 |
(*kernel) << (subVE(lambda,i) =acl:: generateVEConstant(0.)); |
| 296 |
for (unsigned int j(0); j < nC; ++j) |
| 297 |
if(j!=i) |
| 298 |
(*kernel) << (subVE(lambda,i) += cL[i]/smSolver->getDiffusionCoefficient(i,j)); |
| 299 |
} |
| 300 |
(*kernel) << (lambda/=cTotal); |
| 301 |
|
| 302 |
// compilation of matrix |
| 303 |
for (unsigned int i(0); i < nC; ++i) |
| 304 |
for (unsigned int j(0); j < nC; ++j) |
| 305 |
{ |
| 306 |
if(i==j) |
| 307 |
aM.setElement(i,i, lambda[i]); |
| 308 |
else |
| 309 |
aM.setElement(i,j, (-cL[i]/cTotal/smSolver->getDiffusionCoefficient(i,j))[0]); |
| 310 |
} |
| 311 |
// if(dustDiffusionCoefficients.size()) |
| 312 |
for (unsigned int i(0); i < nC; ++i) |
| 313 |
aM.setElement(i,i,(1./smSolver->getDustDiffusionCoefficient(i)+aM.getVE(i,i))[0]); |
| 314 |
|
| 315 |
(*kernel) << gcSolveSystem(aM, cq, flux); |
| 316 |
|
| 317 |
(*kernel) << (phiNew+=stepFactor*(q*flux)* |
| 318 |
(phiT.getValue(kk)-phiT.getValue(0))* |
| 319 |
vt->laplasCoefs[kk]);//!!.... |
| 320 |
} |
| 321 |
|
| 322 |
(*kernel) << (assignmentSafe(phiInternalData->getSubContainer(), phiNew)); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
void FDStefanMaxwellElectricField::postProcessing() |
| 327 |
{ |
| 328 |
swapBuffers(phi->getDContainer(), |
| 329 |
phiInternalData->getDContainer()); |
| 330 |
} |
| 331 |
|
| 332 |
void FDStefanMaxwellElectricField::setPhiS(Field pS) |
| 333 |
{ |
| 334 |
phiS=pS; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
} //asl |