| 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 "aslLBGK.h" |
| 25 |
#include "aslGenerators.h" |
| 26 |
#include <acl/aclGenerators.h> |
| 27 |
#include "acl/acl.h" |
| 28 |
#include "acl/DataTypes/aclConstant.h" |
| 29 |
#include "math/aslTemplateVE.h" |
| 30 |
|
| 31 |
#include <data/aslDataWithGhostNodes.h> |
| 32 |
#include <acl/Kernels/aclKernel.h> |
| 33 |
#include <acl/aclElementBase.h> |
| 34 |
#include <acl/DataTypes/aclMemBlock.h> |
| 35 |
#include <acl/DataTypes/aclIndex.h> |
| 36 |
#include <math/aslTemplates.h> |
| 37 |
#include "acl/aclMath/aclVectorOfElements.h" |
| 38 |
#include "acl/Kernels/aclKernel.h" |
| 39 |
#include <math/aslVectorsDynamicLengthOperations.h> |
| 40 |
|
| 41 |
#include <acl/Kernels/aclKernelConfigurationTemplates.h> |
| 42 |
|
| 43 |
namespace asl |
| 44 |
{ |
| 45 |
|
| 46 |
acl::VectorOfElements computeRho(acl::VectorOfElements f, const VectorTemplate* vt) |
| 47 |
{ |
| 48 |
return f*acl::generateVEConstant(vt->quasiparticlesCoefs); |
| 49 |
} |
| 50 |
|
| 51 |
acl::VectorOfElements computeMomentum(acl::VectorOfElements f, const VectorTemplate* vt) |
| 52 |
{ |
| 53 |
unsigned int nD(asl::nD(vt->vectors[0])); |
| 54 |
acl::VectorOfElements a(nD); |
| 55 |
unsigned int nv(f.size()); |
| 56 |
vector<double> vComponent(nv); |
| 57 |
for(unsigned int i(0); i<nD; ++i){ |
| 58 |
for(unsigned int j(0); j<nv; ++j) |
| 59 |
vComponent[j]=double(vt->vectors[j][i])*vt->quasiparticlesCoefs[j]; |
| 60 |
a[i]=(f*acl::generateVEConstant(vComponent))[0]; |
| 61 |
} |
| 62 |
return a; |
| 63 |
} |
| 64 |
|
| 65 |
acl::VectorOfElements a_i_dot_v(acl::VectorOfElements v, const VectorTemplate* vt) |
| 66 |
{ |
| 67 |
unsigned int nv(vt->vectors.size()); |
| 68 |
acl::VectorOfElements res(nv); |
| 69 |
for(unsigned int j(0); j<nv; ++j) |
| 70 |
res[j]=(v*acl::generateVEConstant(vt->vectors[j]))[0]; |
| 71 |
return res; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
acl::VectorOfElements generateInverceVector(acl::VectorOfElements f, const VectorTemplate* vt) |
| 76 |
{ |
| 77 |
unsigned int nv(vt->vectors.size()); |
| 78 |
acl::VectorOfElements res(nv); |
| 79 |
|
| 80 |
for(unsigned int i(0); i<nv; ++i) |
| 81 |
res[i]=f[vt->invertVectors[i]]; |
| 82 |
|
| 83 |
return res; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
LBGK::LBGK(): |
| 88 |
SingleKernelNM(acl::KERNEL_SIMDUA), |
| 89 |
vectorTemplate(NULL), |
| 90 |
flagComputeVelocity(true), |
| 91 |
flagComputeRho(true), |
| 92 |
flagCompressible(true) |
| 93 |
{ |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
LBGK::LBGK(DataD v, |
| 98 |
Param nu, |
| 99 |
const VectorTemplate* vT): |
| 100 |
SingleKernelNM(acl::KERNEL_SIMDUA), |
| 101 |
vectorTemplate(vT), |
| 102 |
v(v), |
| 103 |
viscosity(nu), |
| 104 |
flagComputeVelocity(true), |
| 105 |
flagComputeRho(true), |
| 106 |
flagCompressible(true) |
| 107 |
{ |
| 108 |
createData(v->getInternalBlock(), |
| 109 |
v->getEContainer()[0]->getQueue(), |
| 110 |
v->getEContainer()[0]->getTypeID()); |
| 111 |
createCopyKernels(); |
| 112 |
} |
| 113 |
|
| 114 |
LBGK::LBGK(Block b, Param nu, |
| 115 |
const VectorTemplate* vT, |
| 116 |
bool compVel, bool compRho, |
| 117 |
acl::CommandQueue queue): |
| 118 |
SingleKernelNM(acl::KERNEL_SIMDUA), |
| 119 |
vectorTemplate(vT), |
| 120 |
viscosity(nu), |
| 121 |
flagComputeVelocity(compVel), |
| 122 |
flagComputeRho(compRho), |
| 123 |
flagCompressible(true) |
| 124 |
{ |
| 125 |
createData(b,queue,viscosity[0]->getTypeID()); |
| 126 |
createCopyKernels(); |
| 127 |
} |
| 128 |
|
| 129 |
void LBGK::createData(Block b, acl::CommandQueue queue, acl::TypeID type) |
| 130 |
{ |
| 131 |
unsigned int nv(vectorTemplate->vectors.size()); |
| 132 |
fShifts.reset(new AVec<int>(nv)); |
| 133 |
fShiftsIncrement.reset(new AVec<int>(nv)); |
| 134 |
asl::AVec<int> transformVector(offset(b).c2iTransformVector); |
| 135 |
for(unsigned int i(0); i<nv; ++i){ |
| 136 |
(*fShiftsIncrement)[i]=-transformVector*vectorTemplate->vectors[i]; |
| 137 |
} |
| 138 |
|
| 139 |
unsigned int maxIncrement(maxComponent(*fShiftsIncrement)); |
| 140 |
|
| 141 |
unsigned int length(productOfElements(offset(b).getSize())); |
| 142 |
unsigned int poolLength(2*length+4*maxIncrement); |
| 143 |
|
| 144 |
for(unsigned int i(0); i<nv; ++i){ |
| 145 |
(*fShifts)[i]= ((*fShiftsIncrement)[i] >0 ? 0 : poolLength-length); |
| 146 |
} |
| 147 |
|
| 148 |
copy(acl::generateVEData(poolLength, type, nv, queue),fPool); |
| 149 |
|
| 150 |
acl::VectorOfElements container(generateVESubElements(fPool, |
| 151 |
length, |
| 152 |
acl::generateVEVariableSP(fShifts))); |
| 153 |
f=generateDataContainer_SP(b, container, 1u); |
| 154 |
if (!v) |
| 155 |
v=generateDataContainerACL_SP(b, type, nD(b), 1u, queue); |
| 156 |
if (!rho) |
| 157 |
rho=generateDataContainerACL_SP(b, type, 1u, 1u, queue); |
| 158 |
} |
| 159 |
|
| 160 |
void LBGK::createCopyKernels() |
| 161 |
{ |
| 162 |
unsigned int nv(fPool.size()); |
| 163 |
unsigned int length(f->getEContainer()[0]->getSize()); |
| 164 |
unsigned int poolLength(fPool[0]->getSize()); |
| 165 |
|
| 166 |
acl::Element ind(new acl::Index(length)); |
| 167 |
acl::Element backShift(new acl::Constant<cl_int>(poolLength-length)); |
| 168 |
|
| 169 |
copyKernels.resize(nv); |
| 170 |
for(unsigned int i(0); i<nv; ++i){ |
| 171 |
copyKernels[i].reset(new acl::Kernel(acl::KERNEL_SIMDUA)); |
| 172 |
using namespace acl::elementOperators; |
| 173 |
using acl::elementOperators::operator+; |
| 174 |
if((*fShiftsIncrement)[i]>0) |
| 175 |
copyKernels[i]->addExpression( |
| 176 |
operatorAssignmentSafe(excerpt(fPool[i], ind), |
| 177 |
f->getEContainer()[i])); |
| 178 |
else |
| 179 |
copyKernels[i]->addExpression( |
| 180 |
operatorAssignmentSafe(excerpt(fPool[i],backShift+ind), |
| 181 |
f->getEContainer()[i])); |
| 182 |
|
| 183 |
copyKernels[i]->setup(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void LBGK::init0() |
| 188 |
{ |
| 189 |
unsigned int nv(vectorTemplate->vectors.size()); |
| 190 |
acl::TypeID typeID(viscosity[0]->getTypeID()); |
| 191 |
|
| 192 |
unsigned int nd(v->getEContainer().size()); |
| 193 |
|
| 194 |
acl::VectorOfElements fOld(generateVEPrivateVariable(nv,typeID)); |
| 195 |
acl::VectorOfElements fEq(generateVEPrivateVariable(nv,typeID)); |
| 196 |
acl::VectorOfElements pRho(generateVEPrivateVariable(1u, typeID)); |
| 197 |
|
| 198 |
acl::VectorOfElements momentum(generateVEPrivateVariable(nd,typeID)); |
| 199 |
acl::VectorOfElements aDotMomentum(a_i_dot_v(momentum,vectorTemplate)); |
| 200 |
|
| 201 |
acl::VectorOfElements w(generateVEPrivateVariable(1u, typeID)); |
| 202 |
(*kernel)<<(fOld=f->getEContainer()); |
| 203 |
(*kernel)<<(w= 1./(3. * viscosity + .5)); |
| 204 |
(*kernel)<<(momentum = computeMomentum(fOld,vectorTemplate)); |
| 205 |
(*kernel)<<(pRho = computeRho(fOld,vectorTemplate)); |
| 206 |
// valid for both compressible and incompresible |
| 207 |
if(omega.size()>0) |
| 208 |
(*kernel)<< (momentum += (2./w)* crossProduct(omega, momentum)); |
| 209 |
|
| 210 |
if(flagComputeRho) |
| 211 |
(*kernel)<< acl::assignmentSafe(rho->getEContainer(), pRho); |
| 212 |
if(flagComputeVelocity) |
| 213 |
{ |
| 214 |
if (flagCompressible) |
| 215 |
(*kernel)<< acl::assignmentSafe(v->getEContainer(), momentum/pRho); |
| 216 |
else |
| 217 |
(*kernel)<< acl::assignmentSafe(v->getEContainer(), momentum); |
| 218 |
} |
| 219 |
|
| 220 |
if (flagCompressible) |
| 221 |
(*kernel)<<(fEq=pRho * acl::generateVEConstantN(nv, 1.) + |
| 222 |
3. * aDotMomentum + |
| 223 |
4.5 * acl::productOfElements(aDotMomentum,aDotMomentum)/pRho - |
| 224 |
1.5 * momentum*momentum/pRho * acl::generateVEConstantN(nv, 1.) |
| 225 |
); |
| 226 |
else |
| 227 |
(*kernel)<<(fEq=pRho * acl::generateVEConstantN(nv, 1.) + |
| 228 |
3. * aDotMomentum + |
| 229 |
4.5 * acl::productOfElements(aDotMomentum,aDotMomentum) - |
| 230 |
1.5 * momentum*momentum * acl::generateVEConstantN(nv, 1.) |
| 231 |
); |
| 232 |
|
| 233 |
auto fNew(fOld*(1.-w)+ w*fEq); |
| 234 |
(*kernel)<<(assignmentSafe(f->getEContainer(),fNew)); |
| 235 |
} |
| 236 |
|
| 237 |
void LBGK:: preProcessing() |
| 238 |
{ |
| 239 |
unsigned int nv(fShifts->getSize()); |
| 240 |
unsigned int length(f->getEContainer()[0]->getSize()); |
| 241 |
unsigned int poolLength(fPool[0]->getSize()); |
| 242 |
for(unsigned int i(0); i<nv; ++i) |
| 243 |
if((*fShifts)[i]+length+(*fShiftsIncrement)[i]>poolLength || |
| 244 |
(*fShifts)[i]+(*fShiftsIncrement)[i]<0) |
| 245 |
{ |
| 246 |
copyKernels[i]->compute(); |
| 247 |
(*fShifts)[i]=(*fShiftsIncrement)[i] >0 ? 0 : poolLength-length; |
| 248 |
} |
| 249 |
(*fShifts)+=(*fShiftsIncrement); |
| 250 |
} |
| 251 |
|
| 252 |
void LBGK::setOmega(LBGK::Param w) |
| 253 |
{ |
| 254 |
copy(w,omega); |
| 255 |
} |
| 256 |
|
| 257 |
LBGKUtilities::LBGKUtilities(SPLBGK lbgk): |
| 258 |
num(lbgk) |
| 259 |
{ |
| 260 |
} |
| 261 |
|
| 262 |
void LBGKUtilities::initF(Param rho, Param vel) |
| 263 |
{ |
| 264 |
unsigned int nv(num->getVectorTemplate()->vectors.size()); |
| 265 |
acl::VectorOfElements aDotVel(a_i_dot_v(vel,num->getVectorTemplate())); |
| 266 |
|
| 267 |
initData(num->getF()->getEContainer(), |
| 268 |
rho* (acl::generateVEConstantN(nv, 1.) + |
| 269 |
3. * aDotVel + |
| 270 |
4.5 * acl::productOfElements(aDotVel,aDotVel) - |
| 271 |
1.5 * vel * vel * acl::generateVEConstantN(nv, 1.)), |
| 272 |
acl::KERNEL_SIMDUA); |
| 273 |
initData(num->getRho()->getEContainer(),rho); |
| 274 |
initData(num->getVelocity()->getEContainer(),vel); |
| 275 |
} |
| 276 |
|
| 277 |
void LBGKUtilities::initF(Param vel) |
| 278 |
{ |
| 279 |
initF(acl::generateVEConstant(1),vel); |
| 280 |
} |
| 281 |
|
| 282 |
LBGKTurbulence::LBGKTurbulence(Block b, Param nu, |
| 283 |
const VectorTemplate* vT, |
| 284 |
bool compVel, bool compRho, |
| 285 |
acl::CommandQueue queue): |
| 286 |
LBGK(b,nu,vT,compVel, compRho, queue) |
| 287 |
{ |
| 288 |
} |
| 289 |
|
| 290 |
LBGKTurbulence::LBGKTurbulence(DataD v, |
| 291 |
Param nu, |
| 292 |
const VectorTemplate* vT): |
| 293 |
LBGK(v,nu,vT) |
| 294 |
{ |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
void LBGKTurbulence::init0() |
| 299 |
{ |
| 300 |
unsigned int nv(vectorTemplate->vectors.size()); |
| 301 |
acl::TypeID typeID(viscosity[0]->getTypeID()); |
| 302 |
|
| 303 |
unsigned int nd(v->getEContainer().size()); |
| 304 |
|
| 305 |
acl::VectorOfElements fOld(generateVEPrivateVariable(nv,typeID)); |
| 306 |
acl::VectorOfElements fEq(generateVEPrivateVariable(nv,typeID)); |
| 307 |
acl::VectorOfElements pRho(generateVEPrivateVariable(1u, typeID)); |
| 308 |
|
| 309 |
acl::VectorOfElements momentum(generateVEPrivateVariable(nd,typeID)); |
| 310 |
acl::VectorOfElements aDotMomentum(a_i_dot_v(momentum,vectorTemplate)); |
| 311 |
|
| 312 |
acl::VectorOfElements w(generateVEPrivateVariable(1u, typeID)); |
| 313 |
(*kernel)<<(fOld=f->getEContainer()); |
| 314 |
// Turbulence |
| 315 |
auto fL2(fOld*fOld); |
| 316 |
auto fmfEqL2((fOld-fEq)*(fOld-fEq)); |
| 317 |
(*kernel)<<(w=4.*(fmfEqL2/fL2)); |
| 318 |
(*kernel)<<(w = select((1./(3. * viscosity + .5)), |
| 319 |
(1./(3. * min(viscosity*w, |
| 320 |
acl::generateVEConstant(.5)) + .5)), |
| 321 |
w>1, |
| 322 |
typeID)); |
| 323 |
//-------------------------- |
| 324 |
// (*kernel)<<(w= 1./(3. * viscosity + .5)); |
| 325 |
(*kernel)<<(momentum = computeMomentum(fOld,vectorTemplate)); |
| 326 |
(*kernel)<<(pRho = computeRho(fOld,vectorTemplate)); |
| 327 |
// valid for both compressible and incompresible |
| 328 |
if(omega.size()>0) |
| 329 |
(*kernel)<< (momentum += (2./w)* crossProduct(omega, momentum)); |
| 330 |
|
| 331 |
if(flagComputeRho) |
| 332 |
(*kernel)<< acl::assignmentSafe(rho->getEContainer(), pRho); |
| 333 |
if(flagComputeVelocity) |
| 334 |
{ |
| 335 |
if (flagCompressible) |
| 336 |
(*kernel)<< acl::assignmentSafe(v->getEContainer(), momentum/pRho); |
| 337 |
else |
| 338 |
(*kernel)<< acl::assignmentSafe(v->getEContainer(), momentum); |
| 339 |
} |
| 340 |
|
| 341 |
if (flagCompressible) |
| 342 |
(*kernel)<<(fEq=pRho * acl::generateVEConstantN(nv, 1.) + |
| 343 |
3. * aDotMomentum + |
| 344 |
4.5 * acl::productOfElements(aDotMomentum,aDotMomentum)/pRho - |
| 345 |
1.5 * momentum*momentum/pRho * acl::generateVEConstantN(nv, 1.) |
| 346 |
); |
| 347 |
else |
| 348 |
(*kernel)<<(fEq=pRho * acl::generateVEConstantN(nv, 1.) + |
| 349 |
3. * aDotMomentum + |
| 350 |
4.5 * acl::productOfElements(aDotMomentum,aDotMomentum) - |
| 351 |
1.5 * momentum*momentum * acl::generateVEConstantN(nv, 1.) |
| 352 |
); |
| 353 |
auto fNew(fOld*(1.-w)+ w*fEq); |
| 354 |
(*kernel)<<(assignmentSafe(f->getEContainer(),fNew)); |
| 355 |
} |
| 356 |
|
| 357 |
} //asl |
| 358 |
|