Hi!
I am trying to implement a time dependent force model in damBreak3D case. I tried using setExternalVector as is defined in womersley example. The code is compiling successfully but after the force is modified, the simulation crashes. Here is my code:
[code=“cpp”]
#include “palabos3D.h”
#include “palabos3D.hh”
using namespace plb;
typedef double T;
#define DESCRIPTOR descriptors::ForcedD3Q19Descriptor
#define DYNAMICS GuoExternalForceBGKdynamics<T, DESCRIPTOR>(omega)
const T pi = (T)4.0*std::atan((T)1.0);
T timeDepForce(T t, T A, T omega)
{
return A * std::cos(omega * t);
}
// Smagorinsky constant for LES model.
const T cSmago = 0.14;
// Physical dimensions of the system (in meters).
const T lx = 3.22;
const T ly = 1.0;
const T lz = 1.0;
const T rhoEmpty = T(1);
plint writeImagesIter = 100;
plint getStatisticsIter = 20;
plint maxIter;
plint N;
plint nx, ny, nz;
T delta_t, delta_x;
Array<T,DESCRIPTOR::d> force(1,0,0);
T nuPhys, nuLB, tau, omega, Bo, surfaceTensionLB, contactAngle;
std::string outDir;
plint obstacleCenterXYplane, obstacleLength=0, obstacleWidth=0, obstacleHeight=0, beginWaterReservoir, waterReservoirHeight;
plint waterLevelOne, waterLevelTwo, waterLevelThree, waterLevelFour;
T calcForce(plint iT, T A, T Tp)
{
T omega=2pi/Tp;
return -1Aomegaomegastd::sin(omegaiT)delta_tdelta_t/delta_x;
}
void setupParameters() {
delta_x = lz / N;
nx = util::roundToInt(lx / delta_x);
ny = util::roundToInt(ly / delta_x);
nz = util::roundToInt(lz / delta_x);
tau = (nuPhys*DESCRIPTOR<T>::invCs2*delta_t)/(delta_x*delta_x) + 0.5;
omega = 1./tau;
nuLB = (tau-0.5)*DESCRIPTOR<T>::cs2; // Viscosity in lattice units.
surfaceTensionLB = 0;//rhoEmpty * gLB * N * N / Bo;
obstacleCenterXYplane = util::roundToInt(0.744*N);
beginWaterReservoir = 0;
waterReservoirHeight = util::roundToInt(0.3*N);
waterLevelOne = util::roundToInt(0.496*N);
waterLevelTwo = util::roundToInt(2.*0.496*N);
waterLevelThree = util::roundToInt(3.*0.496*N);
waterLevelFour = util::roundToInt((3.*0.496 + 1.150)*N);
}
void prepareCase(MultiBlockLattice3D<T,DESCRIPTOR>& lattice)
{
force[0] = calcForce(0,5,1.74);
force[1] = 0.;
force[2] = -9.81delta_tdelta_t/delta_x;
// externalForce = Array<T,3>(0,0,0);
setExternalVector(lattice,lattice.getBoundingBox(),DESCRIPTOR::ExternalField::forceBeginsAt,force);
}
// Specifies the initial condition for the fluid (each cell is assigned the
// flag “fluid”, “empty”, or “wall”).
int initialFluidFlags(plint iX, plint iY, plint iZ) {
// Place an obstacle on the left end, which is hit by the fluid.
bool insideObstacle =
iX >= obstacleCenterXYplane-obstacleWidth/2 &&
iX <= obstacleCenterXYplane+obstacleWidth/2 &&
iY >= ny/2-obstacleLength/2 &&
iY <= ny/2+obstacleLength/2 &&
iZ <= obstacleHeight+1;
if (insideObstacle) {
return freeSurfaceFlag::wall;
}
else if (iX >= beginWaterReservoir && iZ <= waterReservoirHeight) {
return freeSurfaceFlag::fluid;
}
else {
return freeSurfaceFlag::empty;
}
}
void writeResults(MultiBlockLattice3D<T,DESCRIPTOR>& lattice, MultiScalarField3D& volumeFraction, plint iT)
{
static const plint nx = lattice.getNx();
static const plint ny = lattice.getNy();
static const plint nz = lattice.getNz();
Box3D slice(0, nx-1, ny/2, ny/2, 0, nz-1);
ImageWriter imageWriter(“leeloo”);
imageWriter.writeScaledPpm(createFileName(“u”, iT, 6),
*computeVelocityNorm(lattice, slice));
imageWriter.writeScaledPpm(createFileName("rho", iT, 6),
*computeDensity(lattice, slice));
imageWriter.writeScaledPpm(createFileName("volumeFraction", iT, 6), *extractSubDomain(volumeFraction, slice));
// Use a marching-cube algorithm to reconstruct the free surface and write an STL file.
std::vector<T> isoLevels;
isoLevels.push_back((T) 0.5);
typedef TriangleSet<T>::Triangle Triangle;
std::vector<Triangle> triangles;
isoSurfaceMarchingCube(triangles, volumeFraction, isoLevels, volumeFraction.getBoundingBox());
TriangleSet<T>(triangles).writeBinarySTL(createFileName(outDir+"/interface", iT, 6)+".stl");
VtkImageOutput3D<T> vtkOut(createFileName("volumeFraction", iT, 6), 1.);
vtkOut.writeData<float>(volumeFraction, "vf", 1.);
}
void writeStatistics(FreeSurfaceFields3D<T,DESCRIPTOR>& fields) {
pcout << " ------------------------------- " << std::endl;
T averageMass = freeSurfaceAverageMass<T,DESCRIPTOR>(fields.freeSurfaceArgs, fields.lattice.getBoundingBox());
pcout << "Average Mass: " << averageMass << std::endl;
T averageDensity = freeSurfaceAverageDensity<T,DESCRIPTOR>(fields.freeSurfaceArgs, fields.lattice.getBoundingBox());
pcout << "Average Density: " << std::setprecision(12) << averageDensity << std::endl;
T averageVolumeFraction = freeSurfaceAverageVolumeFraction<T,DESCRIPTOR>(fields.freeSurfaceArgs, fields.lattice.getBoundingBox());
pcout << "Average Volume-Fraction: " << std::setprecision(12) << averageVolumeFraction << std::endl;
pcout << " -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- " << std::endl;
}
int main(int argc, char **argv)
{
plbInit(&argc, &argv);
global::directories().setInputDir("./");
if (global::argc() != 8) {
pcout << "Error missing some input parameter\n";
}
try {
global::argv(1).read(outDir);
global::directories().setOutputDir(outDir+"/");
global::argv(2).read(nuPhys);
global::argv(3).read(Bo);
global::argv(4).read(contactAngle);
global::argv(5).read(N);
global::argv(6).read(delta_t);
global::argv(7).read(maxIter);
}
catch(PlbIOException& except) {
pcout << except.what() << std::endl;
pcout << "The parameters for this program are :\n";
pcout << "1. Output directory name.\n";
pcout << "2. kinematic viscosity in physical Units (m^2/s) .\n";
pcout << "3. Bond number (Bo = rho * g * L^2 / gamma).\n";
pcout << "4. Contact angle (in degrees).\n";
pcout << "5. number of lattice nodes for lz .\n";
pcout << "6. delta_t .\n";
pcout << "7. maxIter .\n";
pcout << "Reasonable parameters on a desktop computer are: " << (std::string)global::argv(0) << " tmp 1.e-5 100 80.0 40 1.e-3 80000\n";
pcout << "Reasonable parameters on a parallel machine are: " << (std::string)global::argv(0) << " tmp 1.e-6 100 80.0 100 1.e-4 80000\n";
exit (EXIT_FAILURE);
}
setupParameters();
pcout << "delta_t= " << delta_t << std::endl;
pcout << "delta_x= " << delta_x << std::endl;
pcout << "delta_t*delta_t/delta_x= " << delta_t*delta_t/delta_x << std::endl;
pcout << "externalForce= " << force[2] << std::endl;
pcout << "relaxation time= " << tau << std::endl;
pcout << "omega= " << omega << std::endl;
pcout << "kinematic viscosity physical units = " << nuPhys << std::endl;
pcout << "kinematic viscosity lattice units= " << nuLB << std::endl;
global::timer("initialization").start();
SparseBlockStructure3D blockStructure(createRegularDistribution3D(nx, ny, nz));
Dynamics<T,DESCRIPTOR>* dynamics
= new SmagorinskyBGKdynamics<T,DESCRIPTOR>(omega, cSmago);
// If surfaceTensionLB is 0, then the surface tension algorithm is deactivated.
// If contactAngle is less than 0, then the contact angle algorithm is deactivated.
FreeSurfaceFields3D<T,DESCRIPTOR> fields( blockStructure, dynamics->clone(), rhoEmpty,
surfaceTensionLB, contactAngle, force );
//integrateProcessingFunctional(new ShortenBounceBack3D<T,DESCRIPTOR>, fields.lattice.getBoundingBox(), fields.freeSurfaceArgs, 0);
// Set all outer-wall cells to "wall" (here, bulk-cells are also set to "wall", but it
// doesn't matter, because they are overwritten on the next line).
setToConstant(fields.flag, fields.flag.getBoundingBox(), (int)freeSurfaceFlag::wall);
// In the bulk (all except outer wall layer), initialize the flags as specified by
// the function "initialFluidFlags".
setToFunction(fields.flag, fields.flag.getBoundingBox().enlarge(-1), initialFluidFlags);
prepareCase(fields.lattice);
fields.lattice.periodicity().toggle(0,true);
fields.defaultInitialize();
pcout << "Time spent for setting up lattices: "
<< global::timer("initialization").stop() << std::endl;
T lastIterationTime = T();
for (plint iT = 0; iT <= maxIter; ++iT) {
if (iT % writeImagesIter==0)
{
Array<T,DESCRIPTOR<T>::d> force(calcForce(iT,61,1.74),0,0);
setExternalVector(fields.lattice,fields.lattice.getBoundingBox(),
DESCRIPTOR<T>::ExternalField::forceBeginsAt,force);
}
global::timer("iteration").restart();
T sum_of_mass_matrix = T();
T lost_mass = T();
if (iT % getStatisticsIter==0) {
pcout << std::endl;
pcout << "ITERATION = " << iT << std::endl;
pcout << "Time of last iteration is " << lastIterationTime << " seconds" << std::endl;
writeStatistics(fields);
sum_of_mass_matrix = fields.lattice.getInternalStatistics().getSum(0);
pcout << "Sum of mass matrix: " << sum_of_mass_matrix << std::endl;
lost_mass = fields.lattice.getInternalStatistics().getSum(1);
pcout << "Lost mass: " << lost_mass << std::endl;
pcout << "Total mass: " << sum_of_mass_matrix + lost_mass << std::endl;
pcout << "Interface cells: " << fields.lattice.getInternalStatistics().getIntSum(0) << std::endl;
}
if (iT % writeImagesIter == 0) {
global::timer("images").start();
writeResults(fields.lattice, fields.volumeFraction, iT);
pcout << "Total time spent for writing images: "
<< global::timer("images").stop() << std::endl;
}
// This includes the collision-streaming cycle, plus all free-surface operations.
fields.lattice.executeInternalProcessors();
//fields.lattice.collideAndStream();
fields.lattice.evaluateStatistics();
fields.lattice.incrementTime();
//fields.lattice.collideAndStream();
lastIterationTime = global::timer("iteration").stop();
}
}
Any help will be appreciated.
Thanks