Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/base/FoamInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

#include <mpi.h>

#include <vector>
#include <string>
#include <filesystem>
#include <memory>
#include <string>
#include <vector>

namespace fs = std::filesystem;

/*
* In theory the class through which all calls to the OpenFOAM "environment" are made
Expand Down Expand Up @@ -78,6 +81,8 @@ class FoamInterface
Foam::Time & getRuntime();
Foam::argList & getArglist();
Foam::fvMesh & getMesh();
fs::path currentTimePath() const;
void readTime(const std::string & path);
void write();
};
}
10 changes: 10 additions & 0 deletions include/base/FoamProblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class FoamProblem : public ExternalProblem
virtual FoamMesh const & mesh() const override { return *_foam_mesh; }
virtual FoamMesh & mesh() override { return *_foam_mesh; }

/**
* Save the current state of the OpenFOAM solve.
*/
void saveState();

/**
* Load the saved state of the OpenFOAM solve.
*/
void loadState();

protected:
FoamMesh * _foam_mesh = nullptr;
Hippo::FoamInterface * _interface = nullptr;
Expand Down
12 changes: 3 additions & 9 deletions include/base/hippoApp.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include <MooseApp.h>
Expand All @@ -21,4 +12,7 @@ class hippoApp : public MooseApp

static void registerApps();
static void registerAll(Factory & f, ActionFactory & af, Syntax & s);

void preBackup() override;
void postRestore(const bool /* for_restart */) override;
};
12 changes: 12 additions & 0 deletions src/base/FoamInterface.C
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ FoamInterface::getArglist()
return _impl->_args.args;
}

fs::path
FoamInterface::currentTimePath() const
{
return fs::path(_impl->_runtime.timePath());
}

void
FoamInterface::readTime(const std::string & path)
{
getRuntime().read();
}

void
FoamInterface::write()
{
Expand Down
24 changes: 24 additions & 0 deletions src/base/FoamProblem.C
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <MooseError.h>
#include <MooseTypes.h>
#include <MooseVariableFieldBase.h>
#include <filesystem>
#include <libmesh/enum_order.h>
#include <libmesh/fe_type.h>

Expand Down Expand Up @@ -101,6 +102,29 @@ FoamProblem::externalSolve()
{
}

void
FoamProblem::saveState()
{
_interface->write();
_backup_foam_timestep_dir = _interface->currentTimePath().string();
printf("saveState() -> %s", _backup_foam_timestep_dir.c_str());
}

void
FoamProblem::loadState()
{
if (_backup_foam_timestep_dir.empty())
{
mooseError("Cannot load OpenFOAM state: the backup state path is not set.");
}
if (!fs::is_directory(_backup_foam_timestep_dir))
{
mooseError("Cannot load OpenFOAM state: the backup state path does not exist.");
}
printf("loadState() -> %s", _backup_foam_timestep_dir.c_str());
_interface->readTime(_backup_foam_timestep_dir);
}

InputParameters
BuoyantFoamProblem::validParams()
{
Expand Down
102 changes: 98 additions & 4 deletions src/base/hippoApp.C
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include "hippoApp.h"
#include "Moose.h"
#include "AppFactory.h"
#include "ModulesApp.h"
#include "MooseSyntax.h"
#include "FoamProblem.h"

#include <Moose.h>
#include <AppFactory.h>
#include <ModulesApp.h>
#include <MooseSyntax.h>

#include <cstdlib>

InputParameters
hippoApp::validParams()
Expand Down Expand Up @@ -38,6 +42,96 @@ hippoApp::registerApps()
registerApp(hippoApp);
}

void
hippoApp::preBackup()
{
auto problem = reinterpret_cast<FoamProblem *>(&feProblem());
if (!problem)
{
mooseError("FoamProblem must be used with a Hippo App");
}
printf("preBackup()\n");
problem->saveState();

// auto problem = reinterpret_cast<FoamProblem *>(&feProblem());
// if (!problem)
// {
// mooseError("FoamProblem must be used with a Hippo App");
// }
// auto * foam_interface = problem->mesh().getFoamInterface();
// foam_interface->write();

// // std::string temp_dir = std::getenv("TMP");
// // if (temp_dir.empty())
// // {
// // temp_dir = "/tmp";
// // }

// auto backup_dir = fs::path("./backup");
// if (!fs::is_directory(backup_dir))
// {
// fs::create_directory(backup_dir);
// }
// auto time_path = foam_interface->currentTimePath();
// auto backup_path = backup_dir / time_path.filename();
// printf("backup %s -> %s\n", time_path.c_str(), backup_path.c_str());
// fs::copy(
// time_path, backup_path, fs::copy_options::overwrite_existing |
// fs::copy_options::recursive);

// // auto tid = parameters().get<THREAD_ID>("_tid");
// // THREAD_ID tid = processor_id();
// // auto * app =
// // reinterpret_cast<hippoApp
// // *>(AppFactory::instance().registeredObjects().at("hippoApp").get());
// // std::vector<std::string> vol_scalar_fields;
// // for (const auto & obj_name : app->feProblem())

// // _restartable_data[tid].addData(std::move(foam_data));
// // printf("tid=%u\n", tid);
// // printf("_restartable_data.size()=%lu\n", _restartable_data.size());

// // // TODO: look at 'readCheckpoint' and 'writeCheckpoint' in precice adapter.
// // // OR:
// // // _interface->getMesh().writeObject(
// // // IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, )
// // }
}

void
hippoApp::postRestore(const bool for_restart)
{
auto problem = reinterpret_cast<FoamProblem *>(&feProblem());
if (!problem)
{
mooseError("FoamProblem must be used with a Hippo App");
}
printf("postRestore()\n");
problem->loadState();

// auto problem = reinterpret_cast<FoamProblem *>(&feProblem());
// if (!problem)
// {
// mooseError("FoamProblem must be used with a Hippo App");
// }
// auto * foam_interface = problem->mesh().getFoamInterface();

// auto time_path = foam_interface->currentTimePath();
// auto backup_dir = fs::path("./backup");
// auto backup_path = backup_dir / time_path.filename();
// if (!fs::is_directory(backup_path))
// {
// return;
// }
// printf("restoring %s -> %s\n", backup_path.c_str(), time_path.c_str());
// fs::copy(
// backup_path, time_path, fs::copy_options::overwrite_existing |
// fs::copy_options::recursive);
// foam_interface->readTime("");
// // std::vector<double> _;
// // foam_interface->getWallHeatFlux(_, 0);
}

/***************************************************************************************************
*********************** Dynamic Library Entry Points - DO NOT MODIFY ******************************
**************************************************************************************************/
Expand Down
13 changes: 13 additions & 0 deletions test/tests/restart/restart_from_transient_serial/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Files generated by running the buoyantFoam tests
**/processor*/*
**/postProcessing/*
**/constant/polyMesh/*
**/buoyantCavity/[1-9]/*
**/buoyantCavity/[1-9][0-9]/*
*.blockMesh
*.buoyantFoam
*.createGraphs
*.postProcess.*

# Files for displaying OpenFOAM meshes in paraview
**/buoyantCavity/*.foam
44 changes: 44 additions & 0 deletions test/tests/restart/restart_from_transient_serial/buoyantCavity/0/T
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*--------------------------------*- C++ -*----------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Version: 10
\\/ M anipulation |
\*---------------------------------------------------------------------------*/
FoamFile
{
format ascii;
class volScalarField;
location "0";
object T;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions [0 0 0 1 0 0 0];

internalField uniform 293;

boundaryField
{
topAndBottom
{
type zeroGradient;
}
frontAndBack
{
type zeroGradient;
}
hot
{
type fixedValue;
value uniform 307.75;
}
cold
{
type fixedValue;
value uniform 288.15;
}
}


// ************************************************************************* //
42 changes: 42 additions & 0 deletions test/tests/restart/restart_from_transient_serial/buoyantCavity/0/U
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*--------------------------------*- C++ -*----------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Version: 10
\\/ M anipulation |
\*---------------------------------------------------------------------------*/
FoamFile
{
format ascii;
class volVectorField;
location "0";
object U;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions [0 1 -1 0 0 0 0];

internalField uniform (0 0 0);

boundaryField
{
topAndBottom
{
type noSlip;
}
frontAndBack
{
type noSlip;
}
hot
{
type noSlip;
}
cold
{
type noSlip;
}
}


// ************************************************************************* //
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*--------------------------------*- C++ -*----------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Version: 10
\\/ M anipulation |
\*---------------------------------------------------------------------------*/
FoamFile
{
format ascii;
class volScalarField;
location "0";
object alphat;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions [1 -1 -1 0 0 0 0];

internalField uniform 0;

boundaryField
{
topAndBottom
{
type compressible::alphatWallFunction;
Prt 0.85;
value uniform 0;
}
frontAndBack
{
type compressible::alphatWallFunction;
Prt 0.85;
value uniform 0;
}
hot
{
type compressible::alphatWallFunction;
Prt 0.85;
value uniform 0;
}
cold
{
type compressible::alphatWallFunction;
Prt 0.85;
value uniform 0;
}
}


// ************************************************************************* //
Loading