31 #include <pointFieldsFwd.H> 32 #include <interpolatePointToCell.H> 41 # include <fdeep/fdeep.hpp> 46 std::unique_ptr<Foam::AMRFoam> initializeRefine(
52 mesh->takeGeoMesh(vgm);
57 Foam::polyMesh mesh1(Foam::IOobject(Foam::polyMesh::defaultRegion,
58 runTime.timeName(), runTime,
59 Foam::IOobject::MUST_READ));
62 auto amr = std::unique_ptr<Foam::AMRFoam>{
new Foam::AMRFoam(mesh1)};
68 if (!opts.
writeMesh) amr->enableRefHistoryData();
70 if (opts.
writeMesh) amr->enableMeshWriting();
75 void finishRefine(
Foam::AMRFoam *amr,
const std::string &outputMeshFile,
76 const Foam::Time &runTime) {
80 auto fm =
new Foam::fvMesh(Foam::IOobject(
"", runTime.timeName(), runTime,
81 Foam::IOobject::MUST_READ));
84 mesh->takeGeoMesh(fgm_);
85 mesh->write(outputMeshFile);
96 inputFieldFile(
std::move(inputFieldFile)),
97 refCriteria(refCriteria) {}
112 std::unique_ptr<getDicts> initFoam;
113 initFoam = std::unique_ptr<getDicts>(
new getDicts());
115 this->opts_.timeStep, this->opts_.endTime);
116 initFoam->createDynamicMeshDict(
true);
117 auto controlDict_ = initFoam->createControlDict(
true);
118 Foam::Time runTime(controlDict_.get(),
".",
".");
120 auto amr = initializeRefine(this->
files_, this->
opts_, runTime);
125 Foam::volScalarField meshFieldXY =
129 for (
int i = 0; i < nSteps; i++) {
131 meshFieldXY, this->opts_.lowerRefineLevel,
132 this->opts_.upperRefineLevel, this->opts_.unrefineAbove,
133 this->opts_.unrefineBelow, this->opts_.nBufferLayers,
134 this->opts_.maxCells);
140 Foam::volScalarField magGrad = amr->getGradient(meshFieldXY);
141 for (
int i = 0; i < nSteps; i++) {
143 magGrad, this->opts_.lowerRefineLevel,
144 this->opts_.upperRefineLevel, this->opts_.unrefineAbove,
145 this->opts_.unrefineBelow, this->opts_.nBufferLayers,
146 this->opts_.maxCells);
155 std::cout <<
"End!" << std::endl;
159 FoamMLRefineDriver::FoamMLRefineDriver(
Files files,
Opts opts)
162 FoamMLRefineDriver::FoamMLRefineDriver() : FoamMLRefineDriver({{}, {}}, {}) {}
164 const FoamMLRefineDriver::Opts &FoamMLRefineDriver::getOpts()
const {
168 void FoamMLRefineDriver::setOpts(
Opts opts) { this->
opts_ = std::move(opts); }
170 void FoamMLRefineDriver::execute()
const {
172 std::unique_ptr<getDicts> initFoam;
173 initFoam = std::unique_ptr<getDicts>(
new getDicts());
175 this->opts_.timeStep, this->opts_.endTime);
176 initFoam->createDynamicMeshDict(
true);
177 auto controlDict_ = initFoam->createControlDict(
true);
178 Foam::Time runTime(controlDict_.get(),
".",
".");
180 std::shared_ptr<meshBase>
mesh =
182 auto amr = initializeRefine(this->
files_, this->
opts_, runTime);
186 std::vector<double> nonDimUGrad;
187 std::vector<double> X;
188 std::vector<double> Y;
189 std::vector<double> Z;
190 mesh->getCellDataArray(
"nonDimUGrad", nonDimUGrad);
191 mesh->getCellDataArray(
"X", X);
192 mesh->getCellDataArray(
"Y", Y);
193 mesh->getCellDataArray(
"Z", Z);
196 const auto model = fdeep::load_model(this->
opts_.mlModel);
199 std::vector<int> refinementVec;
200 for (
int i = 0; i < nonDimUGrad.size(); i++) { refinementVec.push_back(0); }
201 for (
int i = 0; i < nonDimUGrad.size(); i++) {
202 const auto result = model.predict(
203 {fdeep::tensor(fdeep::tensor_shape(static_cast<double>(4)),
204 {X[i], Y[i], Z[i], nonDimUGrad[i]})});
206 if (result[0].
get(0, 0, 0, 0, 0) >= 0.5) refinementVec[i] = 1;
210 for (
int i = 0; i < nSteps; i++) {
212 this->opts_.nBufferLayers, this->opts_.maxCells,
219 std::cout <<
"End!" << std::endl;
std::string inputFieldFile
std::string outputMeshFile
void enableUpdatedField()
Access to updated field at every time step.
geoMeshBase * Read(const std::string &fileName)
Read a mesh from file.
geoMeshBase * New(MeshType meshType)
Create a new mesh object.
RefineDriver(Files files)
AMRFoam class is a standalone adaptive mesh refinement application that uses polyMesh as its base and...
volScalarField assignToVolScalarField(const std::vector< int > &vec)
Converts vector to volScalarField for refinement/unrefinement.
const Opts & getOpts() const
std::string inputMeshFile
void execute() const override
Run the workflow represented by the driver.
std::shared_ptr< meshBase > mesh
static std::shared_ptr< meshBase > CreateShared(const std::string &fname)
Create shared ptr from fname.
bool updateAMRML(const int &refineInterval, const int &maxRefinement, const int &nBufferLayers, const int &maxCells, volScalarField &vFld)
New method for machine learning ML.
void writeMesh()
Ability to write current mesh into current time directory.
A concrete implementation of geoMeshBase representing a mesh in a fvMesh.