Particle base class and sphereParticle class have been updated

This commit is contained in:
Hamidreza Norouzi
2024-01-25 03:07:59 -08:00
parent 9c86fe8f31
commit 20be76aed0
11 changed files with 1051 additions and 523 deletions

View File

@ -19,10 +19,12 @@ Licence:
-----------------------------------------------------------------------------*/
#include "sphereParticles.hpp"
#include "setFieldList.hpp"
#include "sphereParticlesKernels.hpp"
#include "systemControl.hpp"
#include "vocabs.hpp"
//#include "setFieldList.hpp"
//#include "sphereParticlesKernels.hpp"
pFlow::uniquePtr<pFlow::List<pFlow::eventObserver*>>
/*pFlow::uniquePtr<pFlow::List<pFlow::eventObserver*>>
pFlow::sphereParticles::getFieldObjectList()const
{
auto objListPtr = particles::getFieldObjectList();
@ -78,10 +80,10 @@ bool pFlow::sphereParticles::initializeParticles()
static_cast<int32>(shapeName_.size()));
return insertSphereParticles(shapeName_, indices, false);
}
}*/
bool pFlow::sphereParticles::beforeIteration()
/*bool pFlow::sphereParticles::beforeIteration()
{
particles::beforeIteration();
@ -98,45 +100,19 @@ bool pFlow::sphereParticles::beforeIteration()
intPredictTimer_.end();
return true;
}
}*/
bool pFlow::sphereParticles::iterate()
{
accelerationTimer_.start();
//INFO<<"before accelerationTimer_ "<<endINFO;
pFlow::sphereParticlesKernels::acceleration(
control().g(),
mass().deviceVectorAll(),
contactForce().deviceVectorAll(),
I().deviceVectorAll(),
contactTorque().deviceVectorAll(),
pStruct().activePointsMaskD(),
accelertion().deviceVectorAll(),
rAcceleration().deviceVectorAll()
);
accelerationTimer_.end();
intCorrectTimer_.start();
dynPointStruct_.correct(this->dt(), accelertion_);
rVelIntegration_().correct(this->dt(), rVelocity_, rAcceleration_);
intCorrectTimer_.end();
return true;
}
bool pFlow::sphereParticles::afterIteration()
/*bool pFlow::sphereParticles::afterIteration()
{
return true;
}
}*/
bool pFlow::sphereParticles::insertSphereParticles(
/*bool pFlow::sphereParticles::insertSphereParticles(
const wordVector& names,
const int32IndexContainer& indices,
bool setId
@ -219,6 +195,39 @@ bool pFlow::sphereParticles::insertSphereParticles(
return true;
}*/
bool pFlow::sphereParticles::initInertia()
{
using exeSpace = typename realPointField_D::execution_space;
using policy = Kokkos::RangePolicy<
exeSpace,
Kokkos::IndexType<uint32>>;
auto aPointsMask = dynPointStruct().activePointsMaskDevice();
auto aRange = aPointsMask.activeRange();
auto field_I = I_.fieldDevice();
auto field_shapeIndex = shapeIndex().fieldDevice();
const auto& shp = getShapes();
realVector_D I ("I", shp.Inertia());
auto d_I = I.deviceVector();
Kokkos::parallel_for(
"particles::initInertia",
policy(aRange.start(), aRange.end()),
LAMBDA_HD(uint32 i)
{
if(aPointsMask(i))
{
uint32 index = field_shapeIndex[i];
field_I[i] = d_I[index];
}
});
Kokkos::fence();
return true;
}
@ -227,78 +236,67 @@ pFlow::sphereParticles::sphereParticles(
const property& prop
)
:
particles(
control,
control.settingsDict().getValOrSet(
"integrationMethod",
word("AdamsBashforth3")
)
particles(control),
spheres_
(
shapeFile__,
&control.caseSetup(),
prop
),
I_
(
objectFile
(
"I",
"",
objectFile::READ_NEVER,
objectFile::WRITE_NEVER
),
property_(prop),
shapes_(
control.caseSetup().emplaceObjectOrGet<sphereShape>(
objectFile(
sphereShapeFile__,
"",
objectFile::READ_ALWAYS,
objectFile::WRITE_NEVER
)
)
),
I_(
this->time().emplaceObject<realPointField_D>(
objectFile(
"I",
"",
objectFile::READ_NEVER,
objectFile::WRITE_ALWAYS
),
pStruct(),
static_cast<real>(0.0000000001)
)
),
rVelocity_(
this->time().emplaceObject<realx3PointField_D>(
objectFile(
"rVelocity",
"",
objectFile::READ_IF_PRESENT,
objectFile::WRITE_ALWAYS
),
pStruct(),
zero3
)
),
rAcceleration_(
this->time().emplaceObject<realx3PointField_D>(
objectFile(
"rAcceleration",
"",
objectFile::READ_IF_PRESENT,
objectFile::WRITE_ALWAYS
),
pStruct(),
zero3
)
dynPointStruct(),
static_cast<real>(0.0000000001)
),
rVelocity_
(
objectFile
(
"rVelocity",
"",
objectFile::READ_IF_PRESENT,
objectFile::WRITE_ALWAYS
),
dynPointStruct(),
zero3
),
rAcceleration_
(
objectFile(
"rAcceleration",
"",
objectFile::READ_IF_PRESENT,
objectFile::WRITE_ALWAYS
),
dynPointStruct(),
zero3
),
accelerationTimer_(
"Acceleration", &this->timers() ),
intPredictTimer_(
"Integration-predict", &this->timers() ),
intCorrectTimer_(
"Integration-correct", &this->timers() )
{
REPORT(1)<<"Creating integration method "<<greenText(this->integrationMethod())
<< " for rotational velocity."<<endREPORT;
auto intMethod = control.settingsDict().getVal<word>("integrationMethod");
REPORT(1)<<"Creating integration method "<<Green_Text(intMethod)
<< " for rotational velocity."<<END_REPORT;
rVelIntegration_ =
integration::create(
rVelIntegration_ = integration::create
(
"rVelocity",
this->time().integration(),
this->pStruct(),
this->integrationMethod());
dynPointStruct(),
intMethod,
rVelocity_.field()
);
if( !rVelIntegration_ )
{
@ -307,7 +305,8 @@ pFlow::sphereParticles::sphereParticles(
fatalExit;
}
if(rVelIntegration_->needSetInitialVals())
WARNING<<"setFields for rVelIntegration_"<<END_WARNING;
/*if(rVelIntegration_->needSetInitialVals())
{
auto [minInd, maxInd] = pStruct().activeRange();
@ -327,17 +326,17 @@ pFlow::sphereParticles::sphereParticles(
REPORT(2)<< "Initializing the required vectors for rotational velocity integratoin\n "<<endREPORT;
rVelIntegration_->setInitialVals(indexHD, rvel);
}
}*/
if(!initializeParticles())
if(!initInertia())
{
fatalExit;
}
}
bool pFlow::sphereParticles::update(const eventMessage& msg)
/*bool pFlow::sphereParticles::update(const eventMessage& msg)
{
if(rVelIntegration_->needSetInitialVals())
@ -362,9 +361,9 @@ bool pFlow::sphereParticles::update(const eventMessage& msg)
}
return true;
}
}*/
bool pFlow::sphereParticles::insertParticles
/*bool pFlow::sphereParticles::insertParticles
(
const realx3Vector& position,
const wordVector& shapes,
@ -419,4 +418,70 @@ bool pFlow::sphereParticles::insertParticles
return true;
}
}*/
bool pFlow::sphereParticles::beforeIteration()
{
particles::beforeIteration();
intPredictTimer_.start();
dynPointStruct().predict(dt(), accelertion());
rVelIntegration_().predict(dt(),rVelocity_, rAcceleration_);
intPredictTimer_.end();
WARNING<<"pFlow::sphereParticles::beforeIteration()"<<END_WARNING;
return true;
}
bool pFlow::sphereParticles::iterate()
{
particles::iterate();
accelerationTimer_.start();
WARNING<<"pFlow::sphereParticlesKernels::acceleration"<<END_WARNING;
//INFO<<"before accelerationTimer_ "<<endINFO;
/*pFlow::sphereParticlesKernels::acceleration(
control().g(),
mass().deviceVectorAll(),
contactForce().deviceVectorAll(),
I().deviceVectorAll(),
contactTorque().deviceVectorAll(),
pStruct().activePointsMaskD(),
accelertion().deviceVectorAll(),
rAcceleration().deviceVectorAll()
);*/
accelerationTimer_.end();
intCorrectTimer_.start();
if(!dynPointStruct().correct(dt(), accelertion()))
{
return false;
}
if(!rVelIntegration_().correct(
dt(),
rVelocity_,
rAcceleration_))
{
return false;
}
intCorrectTimer_.end();
return true;
}
bool pFlow::sphereParticles::afterIteration()
{
particles::afterIteration();
return true;
}
pFlow::word pFlow::sphereParticles::shapeTypeName()const
{
return "sphere";
}
const pFlow::shape &pFlow::sphereParticles::getShapes() const
{
return spheres_;
}

View File

@ -44,22 +44,21 @@ class sphereParticles
:
public particles
{
protected:
public:
using ShapeType = sphereShape;
private:
/// reference to material properties
const property& property_;
/// reference to shapes
sphereShape& shapes_;
/// reference to shapes
ShapeType spheres_;
/// pointField of inertial of particles
realPointField_D& I_;
realPointField_D I_;
/// pointField of rotational Velocity of particles on device
realx3PointField_D& rVelocity_;
realx3PointField_D rVelocity_;
/// pointField of rotational acceleration of particles on device
realx3PointField_D& rAcceleration_;
realx3PointField_D rAcceleration_;
/// rotational velocity integrator
uniquePtr<integration> rVelIntegration_ = nullptr;
@ -73,9 +72,9 @@ protected:
/// timer for integration computations (correction step)
Timer intCorrectTimer_;
bool diameterMassInertiaPropId(const word& shName, real& diam, real& mass, real& I, int8& propIdx);
bool initInertia();
bool initializeParticles();
/*bool initializeParticles();
bool insertSphereParticles(
const wordVector& names,
@ -83,25 +82,17 @@ protected:
bool setId = true);
virtual uniquePtr<List<eventObserver*>> getFieldObjectList()const override;
*/
public:
/// construct from systemControl and property
sphereParticles(systemControl &control, const property& prop);
sphereParticles(
systemControl &control,
const property& prop);
/// no copy constructor
sphereParticles(const sphereParticles&) = delete;
/// move constructor
sphereParticles(sphereParticles&&) = default;
/// no copy-assignement
sphereParticles& operator=(const sphereParticles&) = delete;
/// move-assignement
sphereParticles& operator=(sphereParticles&&) = default;
virtual ~sphereParticles()=default;
~sphereParticles()override=default;
/**
* Insert new particles in position with specified shapes
@ -112,17 +103,17 @@ public:
* \param shape shape of new particles
* \param setField initial value of the selected fields for new particles
*/
bool insertParticles
/*bool insertParticles
(
const realx3Vector& position,
const wordVector& shapes,
const setFieldList& setField
) override ;
) override ;*/
/// const reference to shapes object
const auto& shapes()const
const auto& spheres()const
{
return shapes_;
return spheres_;
}
/// const reference to inertia pointField
@ -137,41 +128,29 @@ public:
return I_;
}
const realx3Vector_D rVelocity()const
const auto& rVelocity()const
{
return rVelocity_;
}
const realVector_D& boundingSphere()const override
{
return this->diameter();
auto& rVelocity()
{
return rVelocity_;
}
word shapeTypeName() const override
bool hearChanges
(
real t,
real dt,
uint32 iter,
const message& msg,
const anyList& varList
) override
{
return "sphere";
}
void boundingSphereMinMax(
real& minDiam,
real& maxDiam )const override
{
shapes_.diameterMinMax(minDiam, maxDiam);
}
bool update(const eventMessage& msg) override;
realx3PointField_D& rAcceleration() override
{
return rAcceleration_;
notImplementedFunction;
return false;
}
const realx3PointField_D& rAcceleration() const override
{
return rAcceleration_;
}
/// before iteration step
bool beforeIteration() override;
@ -180,8 +159,25 @@ public:
/// after iteration step
bool afterIteration() override;
realx3PointField_D& rAcceleration() override
{
return rAcceleration_;
}
const realx3PointField_D& rAcceleration() const override
{
return rAcceleration_;
}
const realPointField_D& boundingSphere()const override
{
return diameter();
}
word shapeTypeName()const override;
const shape& getShapes()const override;
}; //sphereParticles

View File

@ -19,186 +19,177 @@ Licence:
-----------------------------------------------------------------------------*/
#include "sphereShape.hpp"
#include "dictionary.hpp"
#include "vocabs.hpp"
#include "streams.hpp"
bool pFlow::sphereShape::insertNames
(
const wordVector& names
)
bool pFlow::sphereShape::readDictionary()
{
names_.clear();
uint32 i=0;
for(const auto& nm:names)
{
if(!names_.insertIf(nm, i))
{
fatalErrorInFunction<<
" repeated name in the list of sphere names: " << names;
return false;
}
i++;
}
names_.rehash(0);
numShapes_ = names_.size();
return true;
}
diameters_ = getVal<realVector>("diameters");
bool pFlow::sphereShape::readDictionary
(
const dictionary& dict
)
{
diameters_ = dict.getVal<realVector>("diameters");
materials_ = dict.getVal<wordVector>("materials");
auto names = dict.getVal<wordVector>("names");
if(diameters_.size() != materials_.size() )
if(diameters_.size() != numShapes() )
{
fatalErrorInFunction<<
" number of elements in diameters and properties are not the same in "<< dict.globalName()<<endl;
return false;
}
else if(diameters_.size() != names.size() )
{
fatalErrorInFunction<<
" number of elements in diameters and names are not the same in "<< dict.globalName()<<endl;
return false;
}
if( !insertNames(names) )
{
fatalErrorInFunction<<
" error in reading dictionary "<< dict.globalName();
fatalErrorInFunction<<
" number of elements in diameters in "<< globalName()<<" is not consistent"<<endl;
return false;
}
return true;
}
bool pFlow::sphereShape::writeDictionary
(
dictionary& dict
)const
bool pFlow::sphereShape::writeToDict(dictionary& dict)const
{
if(!shape::writeToDict(dict))return false;
if( !dict.add("diamters", diameters_) )
if( !dict.add("diameters", diameters_) )
{
fatalErrorInFunction<<
fatalErrorInFunction<<
" Error in writing diameters to dictionary "<< dict.globalName()<<endl;
return false;
}
if( !dict.add("properties", materials_) )
{
fatalErrorInFunction<<
" Error in writing properties to dictionary "<< dict.globalName()<<endl;
return false;
}
size_t n = names_.size();
wordVector names(n);
names.clear();
word nm;
for(label i=0; i<n; i++)
{
indexToName(i, nm);
names.push_back(nm);
}
if( !dict.add("names", names) )
{
fatalErrorInFunction<<
" Error in writing names to dictionary "<< dict.globalName()<<endl;
return false;
}
return true;
}
pFlow::sphereShape::sphereShape
(
const realVector& diameter,
const wordVector& property,
const wordVector& name
const word& fileName,
repository* owner,
const property& prop
)
:
diameters_(diameter),
materials_(property)
{
if( !insertNames( name) )
{
fatalExit;
}
}
bool pFlow::sphereShape::shapeToDiameter
(
wordVector& names,
realVector& diams
)const
{
diams.clear();
uint32 idx;
for(const auto& nm:names)
{
if(!nameToIndex(nm, idx))
{
fatalErrorInFunction<<
" invalid shape name requested "<< nm <<endl;
return false;
}
diams.push_back(diameters_[idx]);
}
return true;
}
bool pFlow::sphereShape::read(iIstream& is)
shape(fileName, owner, prop)
{
dictionary sphereDict(sphereShapeFile__, true);
if( !sphereDict.read(is) )
{
ioErrorInFile(is.name(), is.lineNumber()) <<
" error in reading dictionray " << sphereShapeFile__ <<" from file. \n";
return false;
}
if( !readDictionary(sphereDict) )
{
return false;
}
return true;
}
bool pFlow::sphereShape::write(iOstream& os)const
pFlow::real pFlow::sphereShape::maxBoundingSphere() const
{
dictionary sphereDict(sphereShapeFile__, true);
if( !writeDictionary(sphereDict))
{
return false;
}
if( !sphereDict.write(os) )
{
ioErrorInFile( os.name(), os.lineNumber() )<<
" error in writing dictionray to file. \n";
return false;
}
return true;
return max(diameters_);
}
pFlow::real pFlow::sphereShape::minBoundingSphere() const
{
return min(diameters_);
}
bool pFlow::sphereShape::boundingDiameter(uint32 index, real &bDiam) const
{
if( indexValid(index))
{
bDiam = diameters_[index];
return true;
}
return false;
}
pFlow::real pFlow::sphereShape::boundingDiameter(uint32 index) const
{
if(indexValid(index))
{
return diameters_[index];
}
fatalErrorInFunction<<"Invalid index for diameter "<<
index<<endl;
fatalExit;
return 0.0;
}
pFlow::realVector pFlow::sphereShape::boundingDiameter() const
{
return diameters_;
}
bool pFlow::sphereShape::mass(uint32 index, real &m) const
{
if( indexValid(index) )
{
real d = diameters_[index];
real rho = indexToDensity(index);
m = Pi/6.0*pow(d,3)*rho;
return true;
}
return false;
}
pFlow::real pFlow::sphereShape::mass(uint32 index) const
{
if(real m; mass(index, m))
{
return m;
}
fatalErrorInFunction<<"bad index for mass "<< index<<endl;
fatalExit;
return 0;
}
pFlow::realVector pFlow::sphereShape::mass() const
{
return realVector ("mass", Pi/6*pow(diameters_,(real)3.0)*density());
}
pFlow::realVector pFlow::sphereShape::density()const
{
auto pids = shapePropertyIds();
realVector rho("rho", numShapes());
ForAll(i, pids)
{
rho[i] = properties().density(pids[i]);
}
return rho;
}
bool pFlow::sphereShape::Inertia(uint32 index, real &I) const
{
if( indexValid(index) )
{
I = 0.4 * mass(index) * pow(diameters_[index]/2.0,2.0);
return true;
}
return false;
}
pFlow::real pFlow::sphereShape::Inertia(uint32 index) const
{
if(real I; Inertia(index, I))
{
return I;
}
fatalExit;
return 0;
}
pFlow::realVector pFlow::sphereShape::Inertia() const
{
return realVector("I", 0.4*mass()*pow(0.5*diameters_,(real)2.0));
}
bool pFlow::sphereShape::Inertia_xx(uint32 index, real &Ixx) const
{
return Inertia(index,Ixx);
}
pFlow::real pFlow::sphereShape::Inertial_xx(uint32 index) const
{
return Inertia(index);
}
bool pFlow::sphereShape::Inertia_yy(uint32 index, real &Iyy) const
{
return Inertia(index,Iyy);
}
pFlow::real pFlow::sphereShape::Inertial_yy(uint32 index) const
{
return Inertia(index);
}
bool pFlow::sphereShape::Inertia_zz(uint32 index, real &Izz) const
{
return Inertia(index,Izz);
}
pFlow::real pFlow::sphereShape::Inertial_zz(uint32 index) const
{
return Inertia(index);
}

View File

@ -21,154 +21,78 @@ Licence:
#ifndef __sphereShape_hpp__
#define __sphereShape_hpp__
#include "Vectors.hpp"
#include "hashMap.hpp"
#include "shape.hpp"
namespace pFlow
{
class dictionary;
class sphereShape
:
public shape
{
protected:
private:
// - diameter of spheres
realVector diameters_;
// - property name of spheres
wordVector materials_;
bool readDictionary();
// - hashed list of spheres names
wordHashMap<uint32> names_;
protected:
size_t numShapes_;
bool insertNames(const wordVector& names);
bool readDictionary(const dictionary& dict);
bool writeDictionary(dictionary& dict)const;
bool writeToDict(dictionary& dict)const override;
public:
// - type info
TypeInfoNV("sphereShape");
TypeInfo("shape<sphere>");
sphereShape(
const word& fileName,
repository* owner,
const property& prop);
sphereShape(){}
sphereShape(
const realVector& diameter,
const wordVector& property,
const wordVector& name
);
sphereShape(const sphereShape&) = default;
sphereShape(sphereShape&&) = default;
sphereShape& operator=(const sphereShape&) = default;
sphereShape& operator=(sphereShape&&) = default;
auto clone()const
{
return makeUnique<sphereShape>(*this);
}
sphereShape* clonePtr()const
{
return new sphereShape(*this);
}
~sphereShape() = default;
~sphereShape() override = default;
//// - Methods
const auto& names()const{
return names_;
}
const auto& diameters()const{
return diameters_;
}
real maxBoundingSphere()const override;
const auto& materials()const{
return materials_;
}
real minBoundingSphere()const override;
const auto diameter(label i)const{
return diameters_[i];
}
bool boundingDiameter(uint32 index, real& bDiam)const override;
const auto material(label i)const{
return materials_[i];
}
real boundingDiameter(uint32 index)const override;
realVector boundingDiameter()const override;
// name to index
bool nameToIndex(const word& name, uint32& index)const
{
if(auto[iter, found] = names_.findIf(name); found )
{
index = iter->second;
return true;
}
else
{
index = 0;
return false;
}
}
bool mass(uint32 index, real& m)const override;
uint32 nameToIndex(const word& name)const
{
return names_.at(name);
}
real mass(uint32 index) const override;
bool indexToName(uint32 i, word& name)const
{
for(auto& nm: names_)
{
if(nm.second == i)
{
name = nm.first;
return true;
}
}
name = "";
return false;
}
realVector mass()const override;
bool shapeToDiameter(wordVector& names, realVector& diams)const;
realVector density() const override;
void diameterMinMax(real& minD, real& maxD)const
{
minD = min(diameters_);
maxD = max(diameters_);
}
bool Inertia(uint32 index, real& I)const override;
//// - IO operatoin
real Inertia(uint32 index)const override;
// - read from stream/file
bool read(iIstream& is);
realVector Inertia()const override;
bool Inertia_xx(uint32 index, real& Ixx)const override;
// - write to stream/file
bool write(iOstream& os)const;
real Inertial_xx(uint32 index)const override;
bool Inertia_yy(uint32 index, real& Iyy)const override;
// - read from dictionary
bool read(const dictionary& dict)
{
return readDictionary(dict);
}
real Inertial_yy(uint32 index)const override;
// - write to dictionary
bool write(dictionary& dict)const
{
return writeDictionary(dict);
}
bool Inertia_zz(uint32 index, real& Izz)const override;
real Inertial_zz(uint32 index)const override;
};