diff --git a/Makefile b/Makefile index 3a792393..4c6713fb 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,7 @@ RDG := no RICHARDS := no STOCHASTIC_TOOLS := no TENSOR_MECHANICS := no +THERMAL_HYDRAULICS := no XFEM := no include $(MOOSE_DIR)/modules/modules.mk diff --git a/include/actions/AddFoamBCAction.h b/include/actions/AddFoamBCAction.h index cc90072b..84853b3d 100644 --- a/include/actions/AddFoamBCAction.h +++ b/include/actions/AddFoamBCAction.h @@ -1,7 +1,9 @@ #pragma once #include "InputParameters.h" -#include "MooseObjectAction.h" +#include "FoamProblem.h" + +#include class AddFoamBCAction : public MooseObjectAction { @@ -15,4 +17,7 @@ class AddFoamBCAction : public MooseObjectAction protected: // Create AuxVariable associated with new-style BCs void createAuxVariable(); + + // Create Receiver for Postprocessor-based BCs + void createReceiver(FoamProblem & problem); }; diff --git a/include/bcs/FoamBCBase.h b/include/bcs/FoamBCBase.h index 11b865ee..bce3c3d5 100644 --- a/include/bcs/FoamBCBase.h +++ b/include/bcs/FoamBCBase.h @@ -7,6 +7,24 @@ #include #include #include +#include "VariadicTable.h" + +typedef VariadicTable BCInfoTable; + +template +inline std::string +listFromVector(std::vector vec, StrType sep = ", ") +{ + if (vec.size() == 0) + return std::string(); + else if (vec.size() == 1) + return vec.at(0); + + std::string str; + auto binary_op = [&](const std::string & acc, const std::string & it) { return acc + sep + it; }; + std::accumulate(vec.begin(), vec.end(), str, binary_op); + return str; +} class FoamBCBase : public MooseObject, public Coupleable { @@ -26,7 +44,10 @@ class FoamBCBase : public MooseObject, public Coupleable // returns the name of the foam boundaries the BC applies to std::vector boundary() const { return _boundary; }; - virtual void initialSetup(); + virtual void initialSetup() = 0; + + // Add information about BC to table + virtual void addInfoRow(BCInfoTable & table) = 0; protected: // OpenFOAM variable which this BC is to be imposed on diff --git a/include/bcs/FoamFixedGradientBC.h b/include/bcs/FoamFixedGradientBC.h index 97041d43..0672b563 100644 --- a/include/bcs/FoamFixedGradientBC.h +++ b/include/bcs/FoamFixedGradientBC.h @@ -1,9 +1,9 @@ #pragma once -#include "FoamBCBase.h" +#include "FoamVariableBCBase.h" #include "InputParameters.h" -class FoamFixedGradientBC : public FoamBCBase +class FoamFixedGradientBC : public FoamVariableBCBase { public: // Validate input file parameters diff --git a/include/bcs/FoamFixedGradientPostprocessorBC.h b/include/bcs/FoamFixedGradientPostprocessorBC.h new file mode 100644 index 00000000..5f35fd37 --- /dev/null +++ b/include/bcs/FoamFixedGradientPostprocessorBC.h @@ -0,0 +1,19 @@ +#pragma once + +#include "FoamPostprocessorBCBase.h" +#include "InputParameters.h" + +class FoamFixedGradientPostprocessorBC : public FoamPostprocessorBCBase +{ +public: + static InputParameters validParams(); + + FoamFixedGradientPostprocessorBC(const InputParameters & params); + + // impose boundary condition + virtual void imposeBoundaryCondition() override; + +protected: + // name of diffusivity coefficient used to divide flux + std::string _diffusivity_coefficient; +}; diff --git a/include/bcs/FoamFixedValueBC.h b/include/bcs/FoamFixedValueBC.h index 70a9f865..00932728 100644 --- a/include/bcs/FoamFixedValueBC.h +++ b/include/bcs/FoamFixedValueBC.h @@ -1,9 +1,9 @@ #pragma once -#include "FoamBCBase.h" +#include "FoamVariableBCBase.h" #include "InputParameters.h" -class FoamFixedValueBC : public FoamBCBase +class FoamFixedValueBC : public FoamVariableBCBase { public: // Validate input file parameters diff --git a/include/bcs/FoamFixedValuePostprocessorBC.h b/include/bcs/FoamFixedValuePostprocessorBC.h new file mode 100644 index 00000000..57010a45 --- /dev/null +++ b/include/bcs/FoamFixedValuePostprocessorBC.h @@ -0,0 +1,14 @@ +#pragma once + +#include "FoamPostprocessorBCBase.h" + +class FoamFixedValuePostprocessorBC : public FoamPostprocessorBCBase +{ +public: + static InputParameters validParams(); + + FoamFixedValuePostprocessorBC(const InputParameters & params); + + // Impose boundary conditions (to be called from FoamProblem class) + virtual void imposeBoundaryCondition() override; +}; diff --git a/include/bcs/FoamMappedInletBCBase.h b/include/bcs/FoamMappedInletBCBase.h new file mode 100644 index 00000000..29e31355 --- /dev/null +++ b/include/bcs/FoamMappedInletBCBase.h @@ -0,0 +1,70 @@ +#pragma once + +#include "FoamPostprocessorBCBase.h" +#include + +class FoamMappedInletBCBase : public FoamPostprocessorBCBase +{ +public: + static InputParameters validParams(); + + FoamMappedInletBCBase(const InputParameters & params); + + virtual ~FoamMappedInletBCBase() { destroyCommunicator(_foam_comm); } + +protected: + Foam::vector _offset; + + std::map> _send_map; + + std::map> _recv_map; + + Foam::label _foam_comm; + + MPI_Comm _mpi_comm; + + // create send and receive information for mapping + void createPatchProcMap(); + + // get array from mapped plane on the inlet processes + template + Foam::Field getMappedArray(const Foam::word & name); + + // check if bounding box intersects with rank + bool intersectMapPlane(const Foam::fvMesh & mesh, Real cart_bbox[6]); + + // create/assign communicators for the transfers between map and inlet planes + void createMapComm(const Foam::fvMesh & mesh, + Foam::vectorField face_centres, + std::vector & send_process, + std::vector & recv_process); + + // find index of cell containing point or raise error if not found + int findIndex(const Foam::point & location, const MPI_Comm & comm); + + // handle creation of new communicators in parallel or serial + Foam::label + createCommunicator(const Foam::label parent_comm, std::vector procs, MPI_Comm & new_comm) + { + Foam::label foam_comm; + if (Foam::UPstream::parRun()) + { + Foam::labelList foam_procs(procs.begin(), procs.end()); + foam_comm = Foam::UPstream::allocateCommunicator(parent_comm, foam_procs, true); + new_comm = Foam::PstreamGlobals::MPICommunicators_[foam_comm]; + } + else + { + foam_comm = Foam::UPstream::worldComm; + new_comm = MPI_COMM_WORLD; + } + return foam_comm; + } + + // free communicators if parallel run + void destroyCommunicator(Foam::label comm) + { + if (Foam::UPstream::parRun()) + Foam::UPstream::freeCommunicator(comm); + } +}; diff --git a/include/bcs/FoamMassFlowRateInletBC.h b/include/bcs/FoamMassFlowRateInletBC.h new file mode 100644 index 00000000..741b2b6e --- /dev/null +++ b/include/bcs/FoamMassFlowRateInletBC.h @@ -0,0 +1,17 @@ +#include "FoamPostprocessorBCBase.h" +#include "InputParameters.h" +#include "MooseTypes.h" +#include "PostprocessorInterface.h" + +class FoamMassFlowRateInletBC : public FoamPostprocessorBCBase +{ +public: + static InputParameters validParams(); + + FoamMassFlowRateInletBC(const InputParameters & params); + + virtual void imposeBoundaryCondition() override; + +protected: + const Real _scale_factor; +}; diff --git a/include/bcs/FoamMassFlowRateMappedInletBC.h b/include/bcs/FoamMassFlowRateMappedInletBC.h new file mode 100644 index 00000000..39b8520f --- /dev/null +++ b/include/bcs/FoamMassFlowRateMappedInletBC.h @@ -0,0 +1,18 @@ +#pragma once +#include "FoamMappedInletBCBase.h" +#include "MooseEnum.h" + +class FoamMassFlowRateMappedInletBC : public FoamMappedInletBCBase +{ +public: + static InputParameters validParams(); + + FoamMassFlowRateMappedInletBC(const InputParameters & params); + + virtual void imposeBoundaryCondition() override; + +protected: + MooseEnum _scale_method; + + Real _scale_factor; +}; diff --git a/include/bcs/FoamPostprocessorBCBase.h b/include/bcs/FoamPostprocessorBCBase.h new file mode 100644 index 00000000..be37f63d --- /dev/null +++ b/include/bcs/FoamPostprocessorBCBase.h @@ -0,0 +1,27 @@ +#pragma once + +#include "FoamBCBase.h" +#include "InputParameters.h" +#include "MooseTypes.h" +#include "PostprocessorInterface.h" + +class FoamPostprocessorBCBase : public FoamBCBase, public PostprocessorInterface +{ +public: + static InputParameters validParams(); + + explicit FoamPostprocessorBCBase(const InputParameters & params); + + // returns the moose AuxVariable imposed on OpenFOAM + VariableName moosePostprocessor() const { return _pp_name; } + + virtual void initialSetup() {}; + + virtual void addInfoRow(BCInfoTable & table); + +protected: + const PostprocessorName _pp_name; + + // Pointer to Moose variable used to impose BC + const PostprocessorValue & _pp_value; +}; diff --git a/include/bcs/FoamScalarBulkMappedInletBC.h b/include/bcs/FoamScalarBulkMappedInletBC.h new file mode 100644 index 00000000..2a68fd0c --- /dev/null +++ b/include/bcs/FoamScalarBulkMappedInletBC.h @@ -0,0 +1,19 @@ +#pragma once +#include "FoamMappedInletBCBase.h" +#include "MooseEnum.h" + +class FoamScalarBulkMappedInletBC : public FoamMappedInletBCBase +{ +public: + static InputParameters validParams(); + + FoamScalarBulkMappedInletBC(const InputParameters & params); + + virtual void imposeBoundaryCondition() override; + +protected: + MooseEnum _scale_method; + + template + T applyScaleMethod(T & var, const Real bulk_ref, const Real bulk); +}; diff --git a/include/bcs/FoamVariableBCBase.h b/include/bcs/FoamVariableBCBase.h new file mode 100644 index 00000000..00300294 --- /dev/null +++ b/include/bcs/FoamVariableBCBase.h @@ -0,0 +1,29 @@ +#pragma once + +#include "FoamBCBase.h" +#include "InputParameters.h" + +class FoamVariableBCBase : public FoamBCBase +{ +public: + static InputParameters validParams(); + + explicit FoamVariableBCBase(const InputParameters & params); + + // returns the moose AuxVariable imposed on OpenFOAM + VariableName mooseVariable() const { return _moose_var->name(); } + + virtual void initialSetup(); + + virtual void addInfoRow(BCInfoTable & table); + +protected: + // Get the value of the MOOSE variable at an element + Real variableValueAtElement(const libMesh::Elem * elem); + + // Get the data vector of the MOOSE field on a subdomain + std::vector getMooseVariableArray(int subdomainId); + + // Pointer to Moose variable used to impose BC + MooseVariableFieldBase * _moose_var; +}; diff --git a/src/actions/AddFoamBCAction.C b/src/actions/AddFoamBCAction.C index f2ffce49..84767518 100644 --- a/src/actions/AddFoamBCAction.C +++ b/src/actions/AddFoamBCAction.C @@ -7,6 +7,17 @@ registerMooseAction("hippoApp", AddFoamBCAction, "add_foam_bc"); +namespace +{ +inline bool +findParamKey(const InputParameters & params, const std::string & key) +{ + return std::find_if(params.begin(), + params.end(), + [&](const auto & param) { return param.first == key; }) != params.end(); +} +} + InputParameters AddFoamBCAction::validParams() { @@ -27,9 +38,13 @@ AddFoamBCAction::act() mooseError("FoamBCs system can only be used with FoamProblem."); // Do not create aux variable if variable provided. - if (!_moose_object_pars.isParamSetByUser("v")) + if (findParamKey(_moose_object_pars, "v") && !_moose_object_pars.isParamSetByUser("v")) createAuxVariable(); + // Create receiver if pp not provided and pp is an allowed parameter + if (findParamKey(_moose_object_pars, "pp") && !_moose_object_pars.isParamSetByUser("pp")) + createReceiver(*foam_problem); + foam_problem->addObject(_type, _name, _moose_object_pars, false); } } @@ -56,3 +71,12 @@ AddFoamBCAction::createAuxVariable() std::static_pointer_cast(_action_factory.create(class_name, name(), action_params)); _awh.addActionBlock(action); } + +void +AddFoamBCAction::createReceiver(FoamProblem & problem) +{ + auto params = _factory.getValidParams("Receiver"); + + Hippo::internal::copyParamFromParam(params, _moose_object_pars, "default"); + problem.addPostprocessor("Receiver", name(), params); +} diff --git a/src/bcs/FoamBCBase.C b/src/bcs/FoamBCBase.C index b2879f11..bf637bf7 100644 --- a/src/bcs/FoamBCBase.C +++ b/src/bcs/FoamBCBase.C @@ -14,16 +14,6 @@ #include #include -namespace -{ -// Private function to check if variables are constant monomials -inline bool -is_constant_monomial(const MooseVariableFieldBase & var) -{ - return var.order() == libMesh::Order::CONSTANT && var.feType().family == FEFamily::MONOMIAL; -} -} - InputParameters FoamBCBase::validParams() { @@ -32,14 +22,10 @@ FoamBCBase::validParams() "Name of a Foam field. e.g. T (temperature) U (velocity)."); params.addParam>("boundary", "Boundaries that the boundary condition applies to."); + params.addRequiredParam("foam_variable", + "Name of a Foam field. e.g. T (temperature) U (velocity)."); - params.addParam( - "v", - "Optional variable to use in BC. This allows existing AuxVariables to be" - " used rather than creating a new one under the hood."); - // Get desired parameters from Variable objects - params.transferParam>(MooseVariable::validParams(), "initial_condition"); - + params.addPrivateParam("_foam_var_settable", true); params.registerSystemAttributeName("FoamBC"); params.registerBase("FoamBC"); @@ -50,7 +36,6 @@ FoamBCBase::FoamBCBase(const InputParameters & params) : MooseObject(params), Coupleable(this, false), _foam_variable(params.get("foam_variable")), - _moose_var(nullptr), _boundary(params.get>("boundary")) { auto * problem = dynamic_cast(&_c_fe_problem); @@ -60,7 +45,8 @@ FoamBCBase::FoamBCBase(const InputParameters & params) _mesh = &problem->mesh(); // check that the foam variable exists - if (!_mesh->foamHasObject(_foam_variable)) + if (!params.isPrivate("foam_variable") && + !_mesh->foamHasObject(_foam_variable)) mooseError("There is no OpenFOAM field named '", _foam_variable, "'"); // check that the boundary is in the FoamMesh @@ -75,45 +61,3 @@ FoamBCBase::FoamBCBase(const InputParameters & params) if (_boundary.empty()) _boundary = all_subdomain_names; } - -void -FoamBCBase::initialSetup() -{ - // Check variable exists - auto var_name = parameters().isParamValid("v") ? parameters().get("v") : _name; - if (!_c_fe_problem.hasVariable(var_name)) - mooseError("Variable '", var_name, "' doesn't exist"); - - THREAD_ID tid = parameters().get("_tid"); - _moose_var = &_c_fe_problem.getVariable(tid, var_name); - - // Check variable is constant monomial in case it is provided. - if (!is_constant_monomial(*_moose_var)) - mooseError("Variable '", var_name, "' must be a constant monomial."); -} - -Real -FoamBCBase::variableValueAtElement(const libMesh::Elem * elem) -{ - auto & sys = _moose_var->sys(); - auto dof = elem->dof_number(sys.number(), _moose_var->number(), 0); - return sys.solution()(dof); -} - -std::vector -FoamBCBase::getMooseVariableArray(int subdomain_id) -{ - size_t patch_count = _mesh->getPatchCount(subdomain_id); - size_t patch_offset = _mesh->getPatchOffset(subdomain_id); - - std::vector var_array(patch_count); - for (size_t j = 0; j < patch_count; ++j) - { - auto elem = patch_offset + j; - auto elem_ptr = _mesh->getElemPtr(elem + _mesh->rank_element_offset); - assert(elem_ptr); - var_array[j] = variableValueAtElement(elem_ptr); - } - - return var_array; -} diff --git a/src/bcs/FoamFixedGradientBC.C b/src/bcs/FoamFixedGradientBC.C index 7434d077..120bb6b7 100644 --- a/src/bcs/FoamFixedGradientBC.C +++ b/src/bcs/FoamFixedGradientBC.C @@ -10,7 +10,7 @@ registerMooseObject("hippoApp", FoamFixedGradientBC); InputParameters FoamFixedGradientBC::validParams() { - auto params = FoamBCBase::validParams(); + auto params = FoamVariableBCBase::validParams(); params.addClassDescription("A FoamBC that imposes a fixed gradient dirichlet boundary condition " "on the OpenFOAM simulation"); params.addParam("diffusivity_coefficient", @@ -20,7 +20,7 @@ FoamFixedGradientBC::validParams() } FoamFixedGradientBC::FoamFixedGradientBC(const InputParameters & parameters) - : FoamBCBase(parameters), + : FoamVariableBCBase(parameters), _diffusivity_coefficient(parameters.get("diffusivity_coefficient")) { // check that the diffusivity coefficient is a OpenFOAM scalar field diff --git a/src/bcs/FoamFixedGradientPostprocessorBC.C b/src/bcs/FoamFixedGradientPostprocessorBC.C new file mode 100644 index 00000000..392311aa --- /dev/null +++ b/src/bcs/FoamFixedGradientPostprocessorBC.C @@ -0,0 +1,73 @@ +#include "FoamFixedGradientPostprocessorBC.h" +#include "PstreamReduceOps.H" +#include "Registry.h" +#include "fixedGradientFvPatchFields.H" +#include + +registerMooseObject("hippoApp", FoamFixedGradientPostprocessorBC); + +InputParameters +FoamFixedGradientPostprocessorBC::validParams() +{ + auto params = FoamPostprocessorBCBase::validParams(); + params.addParam("diffusivity_coefficient", + "", + "OpenFOAM scalar field name to be specified if 'v' is " + "a flux rather than a gradient"); + return params; +} + +FoamFixedGradientPostprocessorBC::FoamFixedGradientPostprocessorBC(const InputParameters & params) + : FoamPostprocessorBCBase(params), + _diffusivity_coefficient(params.get("diffusivity_coefficient")) +{ + // check that the diffusivity coefficient is a OpenFOAM scalar field + if (!_diffusivity_coefficient.empty() && + !_mesh->fvMesh().foundObject(_diffusivity_coefficient)) + mooseError( + "Diffusivity coefficient '", _diffusivity_coefficient, "' not a Foam volScalarField"); +} + +void +FoamFixedGradientPostprocessorBC::imposeBoundaryCondition() +{ + auto & foam_mesh = _mesh->fvMesh(); + + // Get subdomains this FoamBC acts on + // TODO: replace with BoundaryRestriction member functions once FoamMesh is updated + auto subdomains = _mesh->getSubdomainIDs(_boundary); + for (auto subdomain : subdomains) + { + auto & boundary = foam_mesh.boundary()[subdomain]; + // Get underlying field from OpenFOAM boundary patch. + // TODO: Change to function on rebase + auto & var = const_cast &>( + boundary.lookupPatchField(_foam_variable)); + + // Get the gradient associated with the field + Foam::scalarField & foam_gradient( + Foam::refCast(var).gradient()); + + // If diffusivity_coefficient is specified grad array is a flux, so result + // must be divided by it + if (!_diffusivity_coefficient.empty()) + { + // Get the underlying diffusivity field + auto & coeff = foam_mesh.boundary()[subdomain].lookupPatchField( + _diffusivity_coefficient); + + // Calculate the bulk value of the diffusivity coefficient + auto area = boundary.magSf(); + auto total_area = Foam::returnReduce(Foam::sum(area), Foam::sumOp()); + auto coeff_bulk = + Foam::returnReduce(Foam::sum(coeff * area), Foam::sumOp()) / total_area; + + // set gradient + std::fill(foam_gradient.begin(), foam_gradient.end(), _pp_value / coeff_bulk); + } + else // if no diffusivity coefficient grad_array is just the gradient so fill + { + std::fill(foam_gradient.begin(), foam_gradient.end(), _pp_value); + } + } +} diff --git a/src/bcs/FoamFixedValueBC.C b/src/bcs/FoamFixedValueBC.C index 11c74268..b072695b 100644 --- a/src/bcs/FoamFixedValueBC.C +++ b/src/bcs/FoamFixedValueBC.C @@ -7,13 +7,16 @@ registerMooseObject("hippoApp", FoamFixedValueBC); InputParameters FoamFixedValueBC::validParams() { - auto params = FoamBCBase::validParams(); + auto params = FoamVariableBCBase::validParams(); params.addClassDescription("A FoamBC that imposes a fixed value dirichlet boundary condition " "on the OpenFOAM simulation"); return params; } -FoamFixedValueBC::FoamFixedValueBC(const InputParameters & parameters) : FoamBCBase(parameters) {} +FoamFixedValueBC::FoamFixedValueBC(const InputParameters & parameters) + : FoamVariableBCBase(parameters) +{ +} void FoamFixedValueBC::imposeBoundaryCondition() diff --git a/src/bcs/FoamFixedValuePosprocessorBC.C b/src/bcs/FoamFixedValuePosprocessorBC.C new file mode 100644 index 00000000..5614196e --- /dev/null +++ b/src/bcs/FoamFixedValuePosprocessorBC.C @@ -0,0 +1,34 @@ +#include "FoamFixedValuePostprocessorBC.h" +#include "Registry.h" + +registerMooseObject("hippoApp", FoamFixedValuePostprocessorBC); + +InputParameters +FoamFixedValuePostprocessorBC::validParams() +{ + return FoamPostprocessorBCBase::validParams(); +} + +FoamFixedValuePostprocessorBC::FoamFixedValuePostprocessorBC(const InputParameters & params) + : FoamPostprocessorBCBase(params) +{ +} + +void +FoamFixedValuePostprocessorBC::imposeBoundaryCondition() +{ + auto & foam_mesh = _mesh->fvMesh(); + + // Get subdomains this FoamBC acts on + // TODO: replace with BoundaryRestriction member functions once FoamMesh is updated + auto subdomains = _mesh->getSubdomainIDs(_boundary); + for (auto subdomain : subdomains) + { + // Get underlying field from OpenFOAM boundary patch + auto & foam_var = const_cast &>( + foam_mesh.boundary()[subdomain].lookupPatchField( + _foam_variable)); + + std::fill(foam_var.begin(), foam_var.end(), _pp_value); + } +} diff --git a/src/bcs/FoamMappedInletBCBase.C b/src/bcs/FoamMappedInletBCBase.C new file mode 100644 index 00000000..9ca25de2 --- /dev/null +++ b/src/bcs/FoamMappedInletBCBase.C @@ -0,0 +1,389 @@ +#include "FoamMappedInletBCBase.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace +{ +// Get the cartesian bounding box of the mapped inlet plane +void +getBBox(const Foam::vectorField points, Real bbox[6]) +{ + bbox[0] = DBL_MAX; + bbox[1] = DBL_MIN; + bbox[2] = DBL_MAX; + bbox[3] = DBL_MIN; + bbox[4] = DBL_MAX; + bbox[5] = DBL_MIN; + for (auto p : points) + { + bbox[0] = std::min(bbox[0], p.x()); + bbox[1] = std::max(bbox[1], p.x()); + bbox[2] = std::min(bbox[2], p.y()); + bbox[3] = std::max(bbox[3], p.y()); + bbox[4] = std::min(bbox[4], p.z()); + bbox[5] = std::max(bbox[5], p.z()); + } + + MPI_Allreduce(MPI_IN_PLACE, &bbox[0], 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); + MPI_Allreduce(MPI_IN_PLACE, &bbox[1], 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); + MPI_Allreduce(MPI_IN_PLACE, &bbox[2], 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); + MPI_Allreduce(MPI_IN_PLACE, &bbox[3], 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); + MPI_Allreduce(MPI_IN_PLACE, &bbox[4], 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); + MPI_Allreduce(MPI_IN_PLACE, &bbox[5], 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); +} +} + +bool +FoamMappedInletBCBase::intersectMapPlane(const Foam::fvMesh & mesh, Real cart_bbox[6]) +{ + auto & vertices = mesh.points(); + for (int i = 0; i < mesh.nCells(); ++i) + { + auto points = mesh.cellPoints(i); + for (auto pointI : points) + { + // check whether cell intersects bbox + const Foam::point & p = vertices[pointI]; + if (p.x() >= cart_bbox[0] && p.x() <= cart_bbox[1] && p.y() >= cart_bbox[2] && + p.y() <= cart_bbox[3] && p.z() >= cart_bbox[4] && p.z() <= cart_bbox[5]) + { + return true; + } + } + + // The bbox could by narrower than the cell, check in each direction whether bbox is within the + // cell bbox + Real cell_bbox[6] = {DBL_MAX, DBL_MIN, DBL_MAX, DBL_MIN, DBL_MAX, DBL_MIN}; + for (auto point : points) + { + const Foam::point & p = vertices[point]; + cell_bbox[0] = std::min(cell_bbox[0], p.x()); + cell_bbox[1] = std::max(cell_bbox[1], p.x()); + cell_bbox[2] = std::min(cell_bbox[2], p.y()); + cell_bbox[3] = std::max(cell_bbox[3], p.y()); + cell_bbox[4] = std::min(cell_bbox[4], p.z()); + cell_bbox[5] = std::max(cell_bbox[5], p.z()); + } + // check is cart bbox is narrower than cell bbox in x direction + if ((cart_bbox[0] >= cell_bbox[0] && cart_bbox[1] <= cell_bbox[1]) && + (cart_bbox[3] >= cell_bbox[2] || cart_bbox[2] <= cell_bbox[3]) && + (cart_bbox[5] >= cell_bbox[4] || cart_bbox[4] <= cell_bbox[5])) + { + return true; + } + + // check is cart bbox is narrower than cell bbox in y direction + if ((cart_bbox[2] >= cell_bbox[2] && cart_bbox[3] <= cell_bbox[3]) && + (cart_bbox[1] >= cell_bbox[0] || cart_bbox[0] <= cell_bbox[1]) && + (cart_bbox[5] >= cell_bbox[4] || cart_bbox[4] <= cell_bbox[5])) + { + return true; + } + // check is cart bbox is narrower than cell bbox in z direction + if ((cart_bbox[4] >= cell_bbox[4] && cart_bbox[5] <= cell_bbox[5]) && + (cart_bbox[1] >= cell_bbox[0] || cart_bbox[0] <= cell_bbox[1]) && + (cart_bbox[3] >= cell_bbox[2] || cart_bbox[2] <= cell_bbox[3])) + { + return true; + } + } + + return false; +} + +void +FoamMappedInletBCBase::createMapComm(const Foam::fvMesh & mesh, + const Foam::vectorField face_centres, + std::vector & map_process, + std::vector & inlet_process) +{ + Real cart_bbox[6]; + auto mapped_plane = face_centres + _offset; + getBBox(mapped_plane(), cart_bbox); + + int mappedPlaneProcess = intersectMapPlane(mesh, cart_bbox); + int inletPlaneProcess = face_centres.size() > 0; + + std::vector inlet_procs(Foam::UPstream::nProcs()); + std::vector map_procs(Foam::UPstream::nProcs()); + + MPI_Allgather(&mappedPlaneProcess, 1, MPI_INT, map_procs.data(), 1, MPI_INT, MPI_COMM_WORLD); + MPI_Allgather(&inletPlaneProcess, 1, MPI_INT, inlet_procs.data(), 1, MPI_INT, MPI_COMM_WORLD); + + map_process.clear(); + inlet_process.clear(); + + // create list of processes in new communicator and whether they are in the inlet or mapped plane + std::vector processes; + int j = 0; + for (int i = 0; i < Foam::UPstream::nProcs(); ++i) + { + if (inlet_procs[i] || map_procs[i]) + { + processes.push_back(i); + if (inlet_procs[i]) + inlet_process.push_back(j); + if (map_procs[i]) + map_process.push_back(j); + ++j; + } + } + + _foam_comm = createCommunicator(Foam::UPstream::worldComm, processes, _mpi_comm); +} + +void +FoamMappedInletBCBase::createPatchProcMap() +{ + auto & foam_mesh = _mesh->fvMesh(); + auto & boundary = foam_mesh.boundary()[_boundary[0]]; + auto face_centres = boundary.Cf(); + + std::vector map_procs, inlet_procs; + createMapComm(foam_mesh, face_centres, map_procs, inlet_procs); + + if (_mpi_comm == MPI_COMM_NULL) // process not in mapped or inlet planes + return; + + Foam::PstreamBuffers send_points( + Foam::UPstream::commsTypes::nonBlocking, Foam::UPstream::msgType(), _foam_comm); + int rank = Foam::UPstream::myProcNo(_foam_comm); + bool isMapProc = std::find(map_procs.begin(), map_procs.end(), rank) != map_procs.end(); + bool isInletProc = std::find(inlet_procs.begin(), inlet_procs.end(), rank) != inlet_procs.end(); + + if (isInletProc) // send points from inlet process to all map processes + { + for (int proc : map_procs) + { + Foam::UOPstream send(proc, send_points); + send << face_centres; + } + } + + send_points.finishedSends(true); + + std::vector size_requests(inlet_procs.size()); + std::vector data_requests(inlet_procs.size()); + std::vector> recv_indices_procs(inlet_procs.size()); + MPI_Comm map_comm; + auto foam_map_comm = createCommunicator(_foam_comm, map_procs, map_comm); + + if (isMapProc) // check points from each process to see if they are local + { + for (auto i = 0lu; i < inlet_procs.size(); ++i) + { + Foam::vectorField field; + Foam::UIPstream recieve(inlet_procs[i], send_points); + recieve >> field; + auto & vec = _send_map[inlet_procs[i]]; + auto & recv_indices = recv_indices_procs[i]; + for (int j = 0; j < field.size(); ++j) + { + auto index = findIndex(field[j] + _offset, map_comm); + if (index >= 0) + { + vec.push_back(index); // assign to send map required indices + recv_indices.push_back(j); + } + } + if (vec.size() == 0) + _send_map.erase(inlet_procs[i]); + + // Let original processes know which points will come from each rank + int size = recv_indices.size(); + MPI_Isend(&size, 1, MPI_INT, inlet_procs[i], 0, _mpi_comm, &size_requests.at(i)); + MPI_Isend(recv_indices.data(), + recv_indices.size(), + MPI_INT, + inlet_procs[i], + 1, + _mpi_comm, + &data_requests.at(i)); + } + } + + destroyCommunicator(foam_map_comm); + + if (isInletProc) // create map to determine where data from map processes should go + { + for (auto & proc : map_procs) + { + int size; + MPI_Recv(&size, 1, MPI_INT, proc, 0, _mpi_comm, MPI_STATUS_IGNORE); + + std::vector recv_indices(size); + MPI_Recv(recv_indices.data(), size, MPI_INT, proc, 1, _mpi_comm, MPI_STATUS_IGNORE); + for (auto & index : recv_indices) + { + assert(index < face_centres.size()); + _recv_map[proc].push_back(index); + } + } + } + MPI_Barrier(_mpi_comm); +} + +int +FoamMappedInletBCBase::findIndex(const Foam::point & location, const MPI_Comm & comm) +{ + /* + This function uses several ways of finding the mapped plane cell + 1. use findCell function + - Sometimes on cell boundaries it may not find the cell or two processes with both find it + 2. If none found, find the closest point and do an expanded bounding box search, raise error + if none still found. + 3. If multiple found, use the cell with cell centre closer to desired location + 4. If still multiple found, use the cell closer to the inlet. + - In the unlikely chance there are still multiple cells detected, raise a warning + Note that it is possible there is some non-deterministic behaviour in the function but this + shouldn't be a problem in practice. + */ + int index = _mesh->fvMesh().findCell(location, Foam::polyMesh::FACE_PLANES); + int gl_index; + MPI_Allreduce(&index, &gl_index, 1, MPI_INT, MPI_MAX, comm); + + // expand cell bounding box and repeat search + if (gl_index < 0) + { + Foam::label celli = _mesh->fvMesh().findNearestCell(location); + + bool in_cell = _mesh->fvMesh().pointInCellBB(location, celli, 0.1); + index = (in_cell) ? celli : -1; + MPI_Allreduce(&index, &gl_index, 1, MPI_INT, MPI_MAX, comm); + + int rank; + MPI_Comm_rank(comm, &rank); + if (gl_index < 0 && rank == 0) + { + mooseError("Face centre at location (", + location[0], + ",", + location[1], + ",", + location[2], + ") does not have a mapped plane location"); + } + } + + // use cell with cell centre closest to the location + Foam::scalar dist{DBL_MAX}, gl_dist; + if (index != -1) + dist = Foam::mag(_mesh->fvMesh().cellCentres()[index] - location); + + MPI_Allreduce(&dist, &gl_dist, 1, MPI_DOUBLE, MPI_MIN, comm); + if (dist != gl_dist) + index = -1; + + // 2. use cell centre closest to inlet point + int in_cell = index != -1; + MPI_Allreduce(MPI_IN_PLACE, &in_cell, 1, MPI_INT, MPI_SUM, comm); + if (in_cell > 1) + { + if (index != -1) + dist = Foam::mag(_mesh->fvMesh().cellCentres()[index] - (location - _offset)); + MPI_Allreduce(&dist, &gl_dist, 1, MPI_DOUBLE, MPI_MIN, comm); + if (dist != gl_dist) + index = -1; + } + in_cell = index != -1; + MPI_Allreduce(MPI_IN_PLACE, &in_cell, 1, MPI_INT, MPI_SUM, comm); + + if (in_cell > 1) + mooseWarning("More than 1 process found location (", + location[0], + ",", + location[1], + ",", + location[2], + ")"); + return index; +} + +InputParameters +FoamMappedInletBCBase::validParams() +{ + auto params = FoamPostprocessorBCBase::validParams(); + params.addRequiredParam>("translation_vector", + "A vector indicating the location of recycling plane"); + + return params; +} + +FoamMappedInletBCBase::FoamMappedInletBCBase(const InputParameters & params) + : FoamPostprocessorBCBase(params), + _offset(), + _send_map(), + _recv_map(), + _foam_comm(0), + _mpi_comm(MPI_COMM_NULL) +{ + if (_boundary.size() > 1) + mooseError("There can only be one boundary using this method"); + + auto param_offset = params.get>("translation_vector"); + assert(param_offset.size() == 3); + + _offset = {param_offset[0], param_offset[1], param_offset[2]}; + createPatchProcMap(); +} + +template +Foam::Field +FoamMappedInletBCBase::getMappedArray(const Foam::word & name) +{ + if (_mpi_comm == MPI_COMM_NULL) + return Foam::Field(); + + auto & foam_mesh = _mesh->fvMesh(); + auto & boundary_patch = foam_mesh.boundary()[_boundary[0]]; + + Foam::PstreamBuffers sendBuf( + Foam::UPstream::commsTypes::nonBlocking, Foam::UPstream::msgType(), _foam_comm); + if (_send_map.size() > 0) + { + auto & var = foam_mesh.lookupObject>(name); + for (auto & pair : _send_map) + { + auto & proc = pair.first; + auto & send_indices = pair.second; + + Foam::Field points(send_indices.size()); + for (auto j = 0lu; j < send_indices.size(); ++j) + points[j] = var[send_indices[j]]; + + Foam::UOPstream send(proc, sendBuf); + send << points; + } + } + sendBuf.finishedSends(true); + + Foam::Field boundaryData(boundary_patch.size()); + if (_recv_map.size() > 0) + { + for (auto & pair : _recv_map) + { + auto & proc = pair.first; + auto & recv_indices = pair.second; + + Foam::UIPstream recv(proc, sendBuf); + Foam::Field recvData; + recv >> recvData; + for (auto j = 0lu; j < recv_indices.size(); ++j) + { + boundaryData[recv_indices[j]] = recvData[j]; + } + } + } + + return boundaryData; +} diff --git a/src/bcs/FoamMassFlowRateInletBC.C b/src/bcs/FoamMassFlowRateInletBC.C new file mode 100644 index 00000000..785061cf --- /dev/null +++ b/src/bcs/FoamMassFlowRateInletBC.C @@ -0,0 +1,44 @@ +#include "FoamMassFlowRateInletBC.h" +#include "InputParameters.h" +#include "MooseTypes.h" +#include "PstreamReduceOps.H" +#include "Registry.h" + +registerMooseObject("hippoApp", FoamMassFlowRateInletBC); + +InputParameters +FoamMassFlowRateInletBC::validParams() +{ + auto params = FoamPostprocessorBCBase::validParams(); + + params.addParam("scale_factor", 1., "Scale factor multiply mass flow rate pp by."); + params.remove("foam_variable"); + params.addPrivateParam("foam_variable", "U"); + + return params; +} + +FoamMassFlowRateInletBC::FoamMassFlowRateInletBC(const InputParameters & params) + : FoamPostprocessorBCBase(params), _scale_factor(params.get("scale_factor")) +{ +} + +void +FoamMassFlowRateInletBC::imposeBoundaryCondition() +{ + auto & foam_mesh = _mesh->fvMesh(); + + // Get subdomains this FoamBC acts on + // TODO: replace with BoundaryRestriction member functions once FoamMesh is updated + auto subdomains = _mesh->getSubdomainIDs(_boundary); + for (auto subdomain : subdomains) + { + auto & boundary_patch = foam_mesh.boundary()[subdomain]; + + auto & U_var = const_cast &>( + boundary_patch.lookupPatchField("U")); + auto & rho = boundary_patch.lookupPatchField("rho"); + Real area = Foam::returnReduce(Foam::sum(boundary_patch.magSf()), Foam::sumOp()); + U_var == -_scale_factor * _pp_value * boundary_patch.nf() / (rho * area); + } +} diff --git a/src/bcs/FoamMassFlowRateMappedInletBC.C b/src/bcs/FoamMassFlowRateMappedInletBC.C new file mode 100644 index 00000000..88ba1f96 --- /dev/null +++ b/src/bcs/FoamMassFlowRateMappedInletBC.C @@ -0,0 +1,73 @@ +#include "FoamMassFlowRateMappedInletBC.h" +#include "InputParameters.h" +#include "MooseTypes.h" +#include "PstreamReduceOps.H" +#include "Registry.h" + +#include "ops.H" +#include "vectorField.H" +#include "volFieldsFwd.H" + +registerMooseObject("hippoApp", FoamMassFlowRateMappedInletBC); + +InputParameters +FoamMassFlowRateMappedInletBC::validParams() +{ + auto params = FoamMappedInletBCBase::validParams(); + + params.addParam("scale_factor", 1., "Scale factor multiply mass flow rate pp by."); + MooseEnum scaleEnum("SCALE NONE", "SCALE"); + params.addParam("scale_method", + scaleEnum, + "Method used to maintain inlet bulk variable. " + "SCALE means the variable is multiplied by a factor, " + "NONE means the variable is not scaled."); + + params.remove("foam_variable"); + params.addPrivateParam("foam_variable", "U"); + + return params; +} + +FoamMassFlowRateMappedInletBC::FoamMassFlowRateMappedInletBC(const InputParameters & params) + : FoamMappedInletBCBase(params), + _scale_method(params.get("scale_method")), + _scale_factor(params.get("scale_factor")) +{ +} + +void +FoamMassFlowRateMappedInletBC::imposeBoundaryCondition() +{ + auto & foam_mesh = _mesh->fvMesh(); + auto & boundary_patch = foam_mesh.boundary()[_boundary[0]]; + + // currently we map mass flux rather than velocity, maybe useful to have option + auto && U_map = getMappedArray("U"); + auto && rho_map = getMappedArray("rho"); + auto g_map = rho_map * U_map; + + auto & rho = boundary_patch.lookupPatchField("rho"); + auto & U_var = const_cast &>( + boundary_patch.lookupPatchField("U")); + + Foam::vectorField g_var(U_var.size()); + g_var = rho_map * U_map; + if (_scale_method == "SCALE") + { + auto & Sf = boundary_patch.Sf(); + + auto m_dot = Foam::returnReduce(Foam::sum(g_map & -Sf), Foam::sumOp()); + if (fabs(m_dot) > 1e-8) + { + g_var *= _scale_factor * _pp_value / m_dot; + } + else + { + auto area = Foam::returnReduce(Foam::sum(boundary_patch.magSf()), Foam::sumOp()); + g_var -= _scale_factor * boundary_patch.nf() * _pp_value / area; + } + } + + U_var == g_var / rho; +} diff --git a/src/bcs/FoamPostprocessorBCBase.C b/src/bcs/FoamPostprocessorBCBase.C new file mode 100644 index 00000000..b8323a3d --- /dev/null +++ b/src/bcs/FoamPostprocessorBCBase.C @@ -0,0 +1,33 @@ +#include "FoamBCBase.h" +#include "FoamPostprocessorBCBase.h" +#include "InputParameters.h" +#include "MooseTypes.h" +#include "PostprocessorInterface.h" +#include "Receiver.h" + +InputParameters +FoamPostprocessorBCBase::validParams() +{ + auto params = FoamBCBase::validParams(); + + params.addParam("pp", "optional postprocessor to be used in BC"); + params.transferParam(Receiver::validParams(), "default"); + + return params; +} + +FoamPostprocessorBCBase::FoamPostprocessorBCBase(const InputParameters & params) + : FoamBCBase(params), + PostprocessorInterface(this), + _pp_name((params.isParamSetByUser("pp")) ? params.get("pp") : _name), + _pp_value(getPostprocessorValueByName(_pp_name)) +{ + if (params.isParamSetByUser("pp") && params.isParamSetByUser("default")) + mooseWarning("'pp' and 'default' should not both be set. 'default' ignored."); +} + +void +FoamPostprocessorBCBase::addInfoRow(BCInfoTable & table) +{ + table.addRow(name(), type(), foamVariable(), moosePostprocessor(), listFromVector(boundary())); +} diff --git a/src/bcs/FoamScalarBulkMappedInletBC.C b/src/bcs/FoamScalarBulkMappedInletBC.C new file mode 100644 index 00000000..f1cb83d9 --- /dev/null +++ b/src/bcs/FoamScalarBulkMappedInletBC.C @@ -0,0 +1,74 @@ +#include "FoamScalarBulkMappedInletBC.h" +#include "InputParameters.h" +#include "MooseTypes.h" +#include "PstreamReduceOps.H" +#include "Registry.h" + +#include "ops.H" +#include "volFieldsFwd.H" + +registerMooseObject("hippoApp", FoamScalarBulkMappedInletBC); + +InputParameters +FoamScalarBulkMappedInletBC::validParams() +{ + auto params = FoamMappedInletBCBase::validParams(); + MooseEnum scaleEnum("SCALE SUBTRACT NONE", "SCALE"); + params.addParam("scale_method", + scaleEnum, + "Method used to maintain inlet bulk variable. " + "SCALE means the variable is multiplied by a factor, " + "SUBTRACT means the variable is reduced by constant," + "NONE means the variable is not scaled."); + + return params; +} + +FoamScalarBulkMappedInletBC::FoamScalarBulkMappedInletBC(const InputParameters & params) + : FoamMappedInletBCBase(params), _scale_method(params.get("scale_method")) +{ +} + +void +FoamScalarBulkMappedInletBC::imposeBoundaryCondition() +{ + auto & foam_mesh = _mesh->fvMesh(); + auto & boundary_patch = foam_mesh.boundary()[_boundary[0]]; + + auto && var_map = getMappedArray(_foam_variable); + auto & Sf = boundary_patch.magSf(); + + auto totalArea = Foam::sum(Sf); + Foam::reduce(totalArea, Foam::sumOp()); + + auto var_bulk = Foam::sum(var_map * Sf) / totalArea; + + Foam::reduce(var_bulk, Foam::sumOp()); + + auto & var = const_cast &>( + boundary_patch.lookupPatchField(_foam_variable)); + + var == applyScaleMethod(var_map, _pp_value, var_bulk); +} + +template +T +FoamScalarBulkMappedInletBC::applyScaleMethod(T & var, const Real bulk_ref, const Real bulk) +{ + if (_scale_method == "SCALE") + { + return (var * bulk_ref / bulk)(); + } + else if (_scale_method == "SUBTRACT") + { + return (var + bulk_ref - bulk)(); + } + else if (_scale_method == "NONE") + { + return var; + } + else + { + mooseError("Invalid scale method '", _scale_method, "'."); + } +} diff --git a/src/bcs/FoamVariableBCBase.C b/src/bcs/FoamVariableBCBase.C new file mode 100644 index 00000000..4caa83b6 --- /dev/null +++ b/src/bcs/FoamVariableBCBase.C @@ -0,0 +1,81 @@ +#include "FEProblemBase.h" +#include "FoamVariableBCBase.h" + +namespace +{ +// Private function to check if variables are constant monomials +inline bool +is_constant_monomial(const MooseVariableFieldBase & var) +{ + return var.order() == libMesh::Order::CONSTANT && var.feType().family == FEFamily::MONOMIAL; +} +} + +InputParameters +FoamVariableBCBase::validParams() +{ + InputParameters params = FoamBCBase::validParams(); + + params.addParam( + "v", + "Optional variable to use in BC. This allows existing AuxVariables to be" + " used rather than creating a new one under the hood."); + // Get desired parameters from Variable objects + params.transferParam>(MooseVariable::validParams(), "initial_condition"); + + return params; +} + +FoamVariableBCBase::FoamVariableBCBase(const InputParameters & params) + : FoamBCBase(params), _moose_var(nullptr) +{ +} + +void +FoamVariableBCBase::initialSetup() +{ + // Check variable exists + auto var_name = parameters().isParamValid("v") ? parameters().get("v") : _name; + if (!_c_fe_problem.hasVariable(var_name)) + mooseError("Variable '", var_name, "' doesn't exist"); + + THREAD_ID tid = parameters().get("_tid"); + _moose_var = &_c_fe_problem.getVariable(tid, var_name); + + // Check variable is constant monomial in case it is provided. + if (!is_constant_monomial(*_moose_var)) + mooseError("Variable '", var_name, "' must be a constant monomial."); +} + +void +FoamVariableBCBase::addInfoRow(BCInfoTable & table) +{ + // List info about BC + table.addRow(name(), type(), foamVariable(), mooseVariable(), listFromVector(boundary())); +} + +Real +FoamVariableBCBase::variableValueAtElement(const libMesh::Elem * elem) +{ + auto & sys = _moose_var->sys(); + auto dof = elem->dof_number(sys.number(), _moose_var->number(), 0); + return sys.solution()(dof); +} + +std::vector +FoamVariableBCBase::getMooseVariableArray(int subdomainId) +{ + size_t patch_count = _mesh->getPatchCount(subdomainId); + size_t patch_offset = _mesh->getPatchOffset(subdomainId); + + std::vector var_array(patch_count); + for (size_t j = 0; j < patch_count; ++j) + { + auto elem = patch_offset + j; + auto elem_ptr = _mesh->getElemPtr(elem + _mesh->rank_element_offset); + assert(elem_ptr); + var_array[j] = variableValueAtElement(elem_ptr); + } + + return var_array; +} diff --git a/src/problems/FoamProblem.C b/src/problems/FoamProblem.C index d1c470f9..7146148d 100644 --- a/src/problems/FoamProblem.C +++ b/src/problems/FoamProblem.C @@ -24,25 +24,6 @@ registerMooseObject("hippoApp", FoamProblem); -namespace -{ -// Create comma separated list from vector -template -inline std::string -listFromVector(std::vector vec, StrType sep = ", ") -{ - if (vec.size() == 0) - return std::string(); - else if (vec.size() == 1) - return vec.at(0); - - std::string str; - auto binary_op = [&](const std::string & acc, const std::string & it) { return acc + sep + it; }; - std::accumulate(vec.begin(), vec.end(), str, binary_op); - return str; -} -} - InputParameters FoamProblem::validParams() { @@ -154,7 +135,7 @@ FoamProblem::verifyFoamBCs() "FoamBC name", "Type", "Foam variable", - "Moose variable", + "Moose variable/postprocessor", "Boundaries", }); @@ -172,11 +153,7 @@ FoamProblem::verifyFoamBCs() auto && boundary = bc->boundary(); used_bcs.insert(used_bcs.end(), boundary.begin(), boundary.end()); // List info about BC - vt.addRow(bc->name(), - bc->type(), - bc->foamVariable(), - bc->mooseVariable(), - listFromVector(boundary)); + bc->addInfoRow(vt); } } diff --git a/test/OpenFOAM/foam_modules.mk b/test/OpenFOAM/foam_modules.mk index 80bf70a8..61815659 100644 --- a/test/OpenFOAM/foam_modules.mk +++ b/test/OpenFOAM/foam_modules.mk @@ -12,3 +12,4 @@ build_foam_tests: @$(MAKE) -s -j $(MOOSE_JOBS) test/OpenFOAM/modules/laplacianTestSolver/ @$(MAKE) -s -j $(MOOSE_JOBS) test/OpenFOAM/modules/odeTestSolver/ @$(MAKE) -s -j $(MOOSE_JOBS) test/OpenFOAM/modules/postprocessorTestSolver/ + @$(MAKE) -s -j $(MOOSE_JOBS) test/OpenFOAM/modules/mappedInletTestSolver/ diff --git a/test/OpenFOAM/modules/mappedInletTestSolver/Make/files b/test/OpenFOAM/modules/mappedInletTestSolver/Make/files new file mode 100644 index 00000000..0f5991d4 --- /dev/null +++ b/test/OpenFOAM/modules/mappedInletTestSolver/Make/files @@ -0,0 +1,3 @@ +SOURCE += mappedInletTestSolver.C + +LIB = $(FOAM_USER_LIBBIN)/libmappedInletTestSolver diff --git a/test/OpenFOAM/modules/mappedInletTestSolver/Make/options b/test/OpenFOAM/modules/mappedInletTestSolver/Make/options new file mode 100644 index 00000000..cc845ca8 --- /dev/null +++ b/test/OpenFOAM/modules/mappedInletTestSolver/Make/options @@ -0,0 +1,20 @@ +EXE_INC = \ + -I$(LIB_SRC)/physicalProperties/lnInclude \ + -I$(LIB_SRC)/thermophysicalModels/basic/lnInclude \ + -I$(LIB_SRC)/thermophysicalModels/solidThermo/lnInclude \ + -I$(LIB_SRC)/ThermophysicalTransportModels/thermophysicalTransportModel/lnInclude \ + -I$(LIB_SRC)/ThermophysicalTransportModels/solid/lnInclude \ + -I$(LIB_SRC)/finiteVolume/lnInclude \ + -I$(LIB_SRC)/meshTools/lnInclude \ + -I$(LIB_SRC)/sampling/lnInclude + +LIB_LIBS = \ + -lsolidThermo \ + -lsolidThermophysicalTransportModels \ + -lcoupledThermophysicalTransportModels \ + -lspecie \ + -lfiniteVolume \ + -lmeshTools \ + -lsampling \ + -lfvModels \ + -lfvConstraints diff --git a/test/OpenFOAM/modules/mappedInletTestSolver/mappedInletTestSolver.C b/test/OpenFOAM/modules/mappedInletTestSolver/mappedInletTestSolver.C new file mode 100644 index 00000000..17f910c8 --- /dev/null +++ b/test/OpenFOAM/modules/mappedInletTestSolver/mappedInletTestSolver.C @@ -0,0 +1,183 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2022-2024 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "dimensionSets.H" +#include "dimensionedScalar.H" +#include "dimensionedVector.H" +#include "fvMesh.H" +#include "mappedInletTestSolver.H" +#include "fvMeshMover.H" +#include "addToRunTimeSelectionTable.H" +#include "fvConstraints.H" +#include "fvmLaplacian.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace solvers +{ +defineTypeNameAndDebug(mappedInletTestSolver, 0); +addToRunTimeSelectionTable(solver, mappedInletTestSolver, fvMesh); +} +} + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * // + +bool +Foam::solvers::mappedInletTestSolver::dependenciesModified() const +{ + return runTime.controlDict().modified(); +} + +bool +Foam::solvers::mappedInletTestSolver::read() +{ + solver::read(); + + maxDeltaT_ = runTime.controlDict().found("maxDeltaT") + ? runTime.controlDict().lookup("maxDeltaT", runTime.userUnits()) + : vGreat; + + return true; +} + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // +// Solver based on solid.C module +Foam::solvers::mappedInletTestSolver::mappedInletTestSolver(fvMesh & mesh, + autoPtr thermoPtr) + : solver(mesh), + + thermoPtr_(thermoPtr), + thermo_(thermoPtr_()), + + T_(IOobject("T", mesh.time().name(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE), mesh), + + U_(IOobject("U", mesh.time().name(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE), mesh), + + thermophysicalTransport(solidThermophysicalTransportModel::New(thermo_)), + thermo(thermo_), + T(T_), + U(U_) +{ + thermo.validate("solid", "h", "e"); +} + +Foam::solvers::mappedInletTestSolver::mappedInletTestSolver(fvMesh & mesh) + : mappedInletTestSolver(mesh, solidThermo::New(mesh)) +{ + // Read the controls + read(); +} + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::solvers::mappedInletTestSolver::~mappedInletTestSolver() {} + +// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // + +Foam::scalar +Foam::solvers::mappedInletTestSolver::maxDeltaT() const +{ + return min(fvModels().maxDeltaT(), maxDeltaT_); +} + +void +Foam::solvers::mappedInletTestSolver::preSolve() +{ + fvModels().preUpdateMesh(); + + // Update the mesh for topology change, mesh to mesh mapping + mesh_.update(); +} + +void +Foam::solvers::mappedInletTestSolver::moveMesh() +{ + if (pimple.firstIter() || pimple.moveMeshOuterCorrectors()) + { + if (!mesh_.mover().solidBody()) + { + FatalErrorInFunction << "Region " << name() << " of type " << type() + << " does not support non-solid body mesh motion" << exit(FatalError); + } + + mesh_.move(); + } +} + +void +Foam::solvers::mappedInletTestSolver::motionCorrector() +{ +} + +void +Foam::solvers::mappedInletTestSolver::prePredictor() +{ +} + +void +Foam::solvers::mappedInletTestSolver::momentumPredictor() +{ + auto & coords = mesh.C().primitiveField(); + auto & U_field = U_.primitiveFieldRef(); + auto time = mesh.time().userTimeValue(); + + auto x = coords.component(0)(); + auto y = coords.component(1)(); + auto z = coords.component(2)(); + + U_field.replace(0, x + y + z + time); + U_field.replace(1, x - y + z + time); + U_field.replace(2, x + y - z + time); +} + +void +Foam::solvers::mappedInletTestSolver::thermophysicalPredictor() +{ + volScalarField & e = thermo.he(); + auto & coords = mesh.C().primitiveField(); + e.primitiveFieldRef() = mag(coords) + mesh.time().userTimeValue(); + thermo_.correct(); +} + +void +Foam::solvers::mappedInletTestSolver::pressureCorrector() +{ +} + +void +Foam::solvers::mappedInletTestSolver::postCorrector() +{ +} + +void +Foam::solvers::mappedInletTestSolver::postSolve() +{ +} + +// ************************************************************************* // diff --git a/test/OpenFOAM/modules/mappedInletTestSolver/mappedInletTestSolver.H b/test/OpenFOAM/modules/mappedInletTestSolver/mappedInletTestSolver.H new file mode 100644 index 00000000..24705868 --- /dev/null +++ b/test/OpenFOAM/modules/mappedInletTestSolver/mappedInletTestSolver.H @@ -0,0 +1,152 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2022-2023 OpenFOAM Foundation + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::solvers::mappedInletTestSolver + +Description + Solver module for to test mapped inlet implementation method + +SourceFiles + mappedInletTestSolver.C + +\*---------------------------------------------------------------------------*/ + +#pragma once + +#include "solver.H" +#include "solidThermophysicalTransportModel.H" +#include "volFieldsFwd.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace solvers +{ + +/*---------------------------------------------------------------------------*\ + Class mappedInletTestSolver Declaration +\*---------------------------------------------------------------------------*/ + +class mappedInletTestSolver : public solver +{ + +protected: + // Control parameters + scalar maxDeltaT_; + + // Thermophysical properties + autoPtr thermoPtr_; + + solidThermo & thermo_; + + volScalarField T_; + + volVectorField U_; + + autoPtr thermophysicalTransport; + + // Protected Member Functions + + //- Return true if the solver's dependencies have been modified + virtual bool dependenciesModified() const; + + //- Read controls + virtual bool read(); + +public: + // Public Data + // reference to thermophysical properties + solidThermo & thermo; + + //- Reference to the temperature field + const volScalarField & T; + + //- Reference velocity field + const volVectorField & U; + + //- Runtime type information + TypeName("mappedInletTestSolver"); + + // Constructors + + //- Construct from region mesh + mappedInletTestSolver(fvMesh & mesh, autoPtr thermoPtr); + + mappedInletTestSolver(fvMesh & mesh); + + //- Disallow default bitwise copy construction + mappedInletTestSolver(const mappedInletTestSolver &) = delete; + + //- Destructor + virtual ~mappedInletTestSolver(); + + // Member Functions + + //- Return the current maximum time-step for stable solution + virtual scalar maxDeltaT() const; + + //- Called at the start of the time-step, before the PIMPLE loop + virtual void preSolve(); + + //- Called at the start of the PIMPLE loop to move the mesh + virtual void moveMesh(); + + //- Corrections that follow mesh motion + virtual void motionCorrector(); + + //- Called at the beginning of the PIMPLE loop + virtual void prePredictor(); + + //- Construct and optionally solve the momentum equation + virtual void momentumPredictor(); + + //- Construct and solve the energy equation, + // convert to temperature + // and update thermophysical and transport properties + virtual void thermophysicalPredictor(); + + //- Construct and solve the pressure equation in the PISO loop + virtual void pressureCorrector(); + + //- Correct the thermophysical transport modelling + virtual void postCorrector(); + + //- Called after the PIMPLE loop at the end of the time-step + virtual void postSolve(); + + // Member Operators + + //- Disallow default bitwise assignment + void operator=(const mappedInletTestSolver &) = delete; +}; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace solvers +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// ************************************************************************* // diff --git a/test/include/bcs/FoamTestBC.h b/test/include/bcs/FoamTestBC.h index 28cd2a91..0ae3cf71 100644 --- a/test/include/bcs/FoamTestBC.h +++ b/test/include/bcs/FoamTestBC.h @@ -1,10 +1,10 @@ -#include "FoamBCBase.h" +#include "FoamVariableBCBase.h" #include "InputParameters.h" -class FoamTestBC : public FoamBCBase +class FoamTestBC : public FoamVariableBCBase { public: - explicit FoamTestBC(const InputParameters & params) : FoamBCBase(params) {}; + explicit FoamTestBC(const InputParameters & params) : FoamVariableBCBase(params) {}; void imposeBoundaryCondition() {}; }; diff --git a/test/tests/bcs/fixed_gradient_pp/foam/0/T b/test/tests/bcs/fixed_gradient_pp/foam/0/T new file mode 100644 index 00000000..6da637de --- /dev/null +++ b/test/tests/bcs/fixed_gradient_pp/foam/0/T @@ -0,0 +1,52 @@ +/*--------------------------------*- 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 0.; + +boundaryField +{ + left + { + type fixedValue; + value uniform 0.; + } + right + { + type fixedGradient; + gradient uniform 1.; + } + top + { + type zeroGradient; + } + front + { + type zeroGradient; + } + bottom + { + type zeroGradient; + } + back + { + type zeroGradient; + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_gradient_pp/foam/constant/physicalProperties b/test/tests/bcs/fixed_gradient_pp/foam/constant/physicalProperties new file mode 100644 index 00000000..6dab7b1c --- /dev/null +++ b/test/tests/bcs/fixed_gradient_pp/foam/constant/physicalProperties @@ -0,0 +1,51 @@ +/*--------------------------------*- 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 dictionary; + location "constant"; + object physicalProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +thermoType +{ + type heSolidThermo; + mixture pureMixture; + transport constIsoSolid; + thermo eConst; + equationOfState rhoConst; + specie specie; + energy sensibleInternalEnergy; +} + +mixture +{ + specie + { + molWeight 1; + } + thermodynamics + { + Cv 1; // Specific heat capacity [J/(kg·K)] + Hf 1; // Heat of formation [J/kg] + Tref 0; + } + transport + { + kappa 2.; // Thermal conductivity [W/(m·K)] + } + equationOfState + { + rho 1; // Density [kg/m^3] + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_gradient_pp/foam/system/blockMeshDict b/test/tests/bcs/fixed_gradient_pp/foam/system/blockMeshDict new file mode 100644 index 00000000..face9ea6 --- /dev/null +++ b/test/tests/bcs/fixed_gradient_pp/foam/system/blockMeshDict @@ -0,0 +1,85 @@ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object blockMeshDict; +} + +vertices +( + ( 0.0 0.0 0.0 ) + ( 10.0 0.0 0.0 ) + ( 10.0 1.0 0.0 ) + ( 0.0 1.0 0.0 ) + + ( 0.0 0.0 1.0) + ( 10.0 0.0 1.0) + ( 10.0 1.0 1.0) + ( 0.0 1.0 1.0) +); + +blocks +( + hex (0 1 2 3 4 5 6 7) (50 1 1) simpleGrading (25 1 1) +); + +boundary +( + + // interface + left + { + type wall; + faces + ( + (4 7 3 0) + ); + } + + right + { + type wall; + faces + ( + (6 5 1 2) + ); + } + + top + { + type wall; + faces + ( + (2 3 7 6) + ); + } + + front + { + type wall; + faces + ( + (3 2 1 0) + ); + } + + bottom + { + type wall; + faces + ( + (0 1 5 4) + ); + } + + back + { + type wall; + faces + ( + (4 5 6 7) + ); + } + +); diff --git a/test/tests/bcs/fixed_gradient_pp/foam/system/controlDict b/test/tests/bcs/fixed_gradient_pp/foam/system/controlDict new file mode 100644 index 00000000..89301ab0 --- /dev/null +++ b/test/tests/bcs/fixed_gradient_pp/foam/system/controlDict @@ -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 dictionary; + object controlDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solver bcTestSolver; + +startFrom startTime; + +startTime 0; + +stopAt endTime; + +endTime 32.; + +deltaT 1.; + +writeControl timeStep; + +writeInterval 1; + +writeFormat ascii; + +writePrecision 20; + +writeCompression off; + +timeFormat general; + +timePrecision 20; + +runTimeModifiable true; + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_gradient_pp/foam/system/fvSchemes b/test/tests/bcs/fixed_gradient_pp/foam/system/fvSchemes new file mode 100644 index 00000000..a24aaf80 --- /dev/null +++ b/test/tests/bcs/fixed_gradient_pp/foam/system/fvSchemes @@ -0,0 +1,46 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSchemes; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +ddtSchemes +{ + default Euler; +} + +gradSchemes +{ + default Gauss linear; +} + +divSchemes +{ + default Gauss linear; +} + +laplacianSchemes +{ + default Gauss linear corrected; +} + +interpolationSchemes +{ + default linear; +} + +snGradSchemes +{ + default corrected; +} + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_gradient_pp/foam/system/fvSolution b/test/tests/bcs/fixed_gradient_pp/foam/system/fvSolution new file mode 100644 index 00000000..639b272d --- /dev/null +++ b/test/tests/bcs/fixed_gradient_pp/foam/system/fvSolution @@ -0,0 +1,49 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSolution; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solvers +{ + rho + { + solver diagonal; + } + + rhoFinal + { + $rho; + } + + + e + { + solver PBiCGStab; + preconditioner DIC; + tolerance 1e-15; + relTol 1e-15; + } + + eFinal + { + $e; + tolerance 1e-15; + relTol 1e-15; + } +} + +PIMPLE +{ +} + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_gradient_pp/main.i b/test/tests/bcs/fixed_gradient_pp/main.i new file mode 100644 index 00000000..cce2ed4a --- /dev/null +++ b/test/tests/bcs/fixed_gradient_pp/main.i @@ -0,0 +1,56 @@ +[Mesh] + type = FoamMesh + case = 'foam' + foam_patch = 'left right' +[] + +[Variables] + [dummy] + family = MONOMIAL + order = CONSTANT + initial_condition = 999 + [] +[] + +[FoamBCs] + [T_value] + type=FoamFixedValuePostprocessorBC + foam_variable = T + default = 0. + boundary = 'left' + [] + [T_flux] + type=FoamFixedGradientPostprocessorBC + foam_variable = T + boundary = 'right' + diffusivity_coefficient = kappa + pp = T_flux + [] +[] + +[Postprocessors] + [T_flux] + type = ParsedPostprocessor + expression = '2*t' + use_t = true + execute_on = 'INITIAL TIMESTEP_BEGIN' + [] +[] + +[Problem] + type = FoamProblem +[] + +[Executioner] + type = Transient + end_time = 32 + [TimeSteppers] + [foam] + type = FoamControlledTimeStepper + [] + [] +[] + +[Outputs] + exodus = true +[] diff --git a/test/tests/bcs/fixed_gradient_pp/test.py b/test/tests/bcs/fixed_gradient_pp/test.py new file mode 100644 index 00000000..98b48ce7 --- /dev/null +++ b/test/tests/bcs/fixed_gradient_pp/test.py @@ -0,0 +1,29 @@ +"""Tests for imposing BCs in OpenFOAM using MOOSE input file syntax""" + +import unittest +import fluidfoam as ff +import numpy as np + +from read_hippo_data import get_foam_times # pylint: disable=E0401 + + +class TestFoamBCFixedGradient(unittest.TestCase): + """Test class for imposing fixed value BCs in Hippo.""" + + def test_fixed_gradient_x(self): + """Test case for imposing fixed value.""" + case_dir = "foam/" + times = get_foam_times(case_dir, string=True)[1:] + + for time in times: + coords = dict(zip(("x", "y", "z"), ff.readof.readmesh(case_dir))) + + temp = ff.readof.readscalar(case_dir, time, "T") + + temp_ref = coords["x"] * np.float64(time) + + temp_diff_max = np.argmax(abs(temp - temp_ref)) + assert np.allclose(temp_ref, temp, rtol=1e-7, atol=1e-12), ( + f"Max diff ({time}): {abs(temp - temp_ref).max()} " + f"{temp[temp_diff_max]} {temp_ref[temp_diff_max]}" + ) diff --git a/test/tests/bcs/fixed_gradient_pp/tests b/test/tests/bcs/fixed_gradient_pp/tests new file mode 100644 index 00000000..ba30399b --- /dev/null +++ b/test/tests/bcs/fixed_gradient_pp/tests @@ -0,0 +1,39 @@ +[Tests] + [foam_bc_fixed_gradient_pp] + [setup] + type = RunCommand + command = 'bash -c "foamCleanCase -case foam && blockMesh -case foam"' + [] + [diffusivity_coeff_err] + type = RunException + input = main.i + prereq = foam_bc_fixed_gradient_pp/setup + expect_err = "Diffusivity coefficient 'kappa1' not a Foam volScalarField" + cli_args = 'FoamBCs/T_flux/diffusivity_coefficient=kappa1' + allow_warnings = true + [] + [run] + type = RunApp + input = main.i + prereq = foam_bc_fixed_gradient_pp/setup + allow_warnings = true + [] + [verify] + type = PythonUnitTest + input = test.py + prereq = foam_bc_fixed_gradient_pp/run + [] + [run_no_kappa] + type = RunApp + input = main.i + cli_args = "FoamBCs/T_flux/diffusivity_coefficient='' Postprocessors/T_flux/expression='t'" + prereq = foam_bc_fixed_gradient_pp/verify + allow_warnings = true + [] + [verify_no_kappa] + type = PythonUnitTest + input = test.py + prereq = foam_bc_fixed_gradient_pp/run_no_kappa + [] + [] +[] diff --git a/test/tests/bcs/fixed_value_pp/foam/0/T b/test/tests/bcs/fixed_value_pp/foam/0/T new file mode 100644 index 00000000..65a14be8 --- /dev/null +++ b/test/tests/bcs/fixed_value_pp/foam/0/T @@ -0,0 +1,56 @@ +/*--------------------------------*- 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 0.1; + +boundaryField +{ + left + { + type fixedValue; + value uniform 0.; + } + right + { + type fixedValue; + value uniform 0.; + } + top + { + type fixedValue; + value uniform 0.; + } + bottom + { + type fixedValue; + value uniform 0.; + } + back + { + type fixedValue; + value uniform 0.; + } + front + { + type fixedValue; + value uniform 0.; + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_value_pp/foam/constant/physicalProperties b/test/tests/bcs/fixed_value_pp/foam/constant/physicalProperties new file mode 100644 index 00000000..96a14c15 --- /dev/null +++ b/test/tests/bcs/fixed_value_pp/foam/constant/physicalProperties @@ -0,0 +1,51 @@ +/*--------------------------------*- 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 dictionary; + location "constant"; + object physicalProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +thermoType +{ + type heSolidThermo; + mixture pureMixture; + transport constIsoSolid; + thermo eConst; + equationOfState rhoConst; + specie specie; + energy sensibleInternalEnergy; +} + +mixture +{ + specie + { + molWeight 1; + } + thermodynamics + { + Cv 1; // Specific heat capacity [J/(kg·K)] + Hf 1; // Heat of formation [J/kg] + Tref 0; + } + transport + { + kappa 1; // Thermal conductivity [W/(m·K)] + } + equationOfState + { + rho 1; // Density [kg/m^3] + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_value_pp/foam/system/blockMeshDict b/test/tests/bcs/fixed_value_pp/foam/system/blockMeshDict new file mode 100644 index 00000000..face9ea6 --- /dev/null +++ b/test/tests/bcs/fixed_value_pp/foam/system/blockMeshDict @@ -0,0 +1,85 @@ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object blockMeshDict; +} + +vertices +( + ( 0.0 0.0 0.0 ) + ( 10.0 0.0 0.0 ) + ( 10.0 1.0 0.0 ) + ( 0.0 1.0 0.0 ) + + ( 0.0 0.0 1.0) + ( 10.0 0.0 1.0) + ( 10.0 1.0 1.0) + ( 0.0 1.0 1.0) +); + +blocks +( + hex (0 1 2 3 4 5 6 7) (50 1 1) simpleGrading (25 1 1) +); + +boundary +( + + // interface + left + { + type wall; + faces + ( + (4 7 3 0) + ); + } + + right + { + type wall; + faces + ( + (6 5 1 2) + ); + } + + top + { + type wall; + faces + ( + (2 3 7 6) + ); + } + + front + { + type wall; + faces + ( + (3 2 1 0) + ); + } + + bottom + { + type wall; + faces + ( + (0 1 5 4) + ); + } + + back + { + type wall; + faces + ( + (4 5 6 7) + ); + } + +); diff --git a/test/tests/bcs/fixed_value_pp/foam/system/controlDict b/test/tests/bcs/fixed_value_pp/foam/system/controlDict new file mode 100644 index 00000000..cb155c26 --- /dev/null +++ b/test/tests/bcs/fixed_value_pp/foam/system/controlDict @@ -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 dictionary; + object controlDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solver bcTestSolver; + +startFrom startTime; + +startTime 0; + +stopAt endTime; + +endTime 0.32; + +deltaT 0.01; + +writeControl timeStep; + +writeInterval 1; + +writeFormat ascii; + +writePrecision 20; + +writeCompression off; + +timeFormat general; + +timePrecision 20; + +runTimeModifiable true; + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_value_pp/foam/system/fvSchemes b/test/tests/bcs/fixed_value_pp/foam/system/fvSchemes new file mode 100644 index 00000000..0e534821 --- /dev/null +++ b/test/tests/bcs/fixed_value_pp/foam/system/fvSchemes @@ -0,0 +1,46 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSchemes; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +ddtSchemes +{ + default Euler; +} + +gradSchemes +{ + default Gauss linear; +} + +divSchemes +{ + default Gauss linear; +} + +laplacianSchemes +{ + default Gauss linear corrected; +} + +interpolationSchemes +{ + default linear; +} + +snGradSchemes +{ + default corrected; +} + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_value_pp/foam/system/fvSolution b/test/tests/bcs/fixed_value_pp/foam/system/fvSolution new file mode 100644 index 00000000..59db6a2a --- /dev/null +++ b/test/tests/bcs/fixed_value_pp/foam/system/fvSolution @@ -0,0 +1,61 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSolution; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solvers +{ + rho + { + solver diagonal; + } + + rhoFinal + { + $rho; + } + + + e + { + solver PBiCGStab; + preconditioner DIC; + tolerance 1e-8; + relTol 1e-8; + } + + eFinal + { + $e; + tolerance 1e-8; + relTol 1e-8; + } +} + +PIMPLE +{ + momentumPredictor yes; + pRefCell 0; + pRefValue 0; +} + +relaxationFactors +{ + equations + { + h 1; + U 1; + } +} + +// ************************************************************************* // diff --git a/test/tests/bcs/fixed_value_pp/main.i b/test/tests/bcs/fixed_value_pp/main.i new file mode 100644 index 00000000..c49df10a --- /dev/null +++ b/test/tests/bcs/fixed_value_pp/main.i @@ -0,0 +1,77 @@ +[Mesh] + type = FoamMesh + case = 'foam' + foam_patch = 'left right bottom top back front' +[] + +[Variables] + [dummy] + family = MONOMIAL + order = CONSTANT + initial_condition = 999 + [] +[] + +[FoamBCs] + [temp1] + type=FoamFixedValuePostprocessorBC + foam_variable = T + boundary = 'left right top' # test boundary restrictions + pp = T_bc1 + [] + [temp2] + type=FoamFixedValuePostprocessorBC + foam_variable = T + boundary = 'bottom front back' # test boundary restrictions + pp = T_bc2 + [] +[] + +[FoamVariables] + [T_shadow] + type = FoamVariableField + foam_variable = 'T' + [] + [e_shadow] + type = FoamVariableField + foam_variable = 'e' + [] + [whf_shadow] + type = FoamFunctionObject + foam_variable = 'wallHeatFlux' + [] +[] + +[Postprocessors] + [T_bc1] + type=ParsedPostprocessor + expression = '0.05 + t' + use_t = true + execute_on='TIMESTEP_BEGIN INITIAL' + [] + [T_bc2] + type=ParsedPostprocessor + expression = '0.05 + 2*t' + use_t = true + execute_on='TIMESTEP_BEGIN INITIAL' + [] +[] + +[Problem] + type = FoamProblem + # Take the boundary temperature from OpenFOAM and set it on the MOOSE mesh. +[] + +[Executioner] + type = Transient + end_time = 0.32 + [TimeSteppers] + [foam] + type = FoamControlledTimeStepper + [] + [] +[] + +[Outputs] + exodus = true +[] diff --git a/test/tests/bcs/fixed_value_pp/test.py b/test/tests/bcs/fixed_value_pp/test.py new file mode 100644 index 00000000..a42a0eed --- /dev/null +++ b/test/tests/bcs/fixed_value_pp/test.py @@ -0,0 +1,30 @@ +"""Tests for imposing BCs in OpenFOAM using MOOSE input file syntax""" + +import unittest +import fluidfoam as ff +import numpy as np + +from read_hippo_data import get_foam_times # pylint: disable=E0401 + + +class TestFoamBCFixedValuePostprocessor(unittest.TestCase): + """Test class for imposing fixed value BCs in Hippo using postprocessors.""" + + def test_fixed_value(self): + """Test case for imposing fixed value.""" + case_dir = "foam/" + boundaries = ["top", "bottom", "front", "back", "left", "right"] + times = get_foam_times(case_dir, string=True)[1:] + + for time in times: + for boundary in boundaries: + temp = ff.readof.readscalar(case_dir, time, "T", boundary=boundary) + + scale = 1.0 if boundary in ("left", "right", "top") else 2.0 + temp_ref = 0.05 + scale * np.float64(time) + + temp_diff_max = np.argmax(abs(temp - temp_ref)) + assert np.allclose(temp_ref, temp, rtol=1e-7, atol=1e-12), ( + f"Max diff {boundary} ({time}): {abs(temp - temp_ref).max()} " + f"{temp[temp_diff_max]} {temp_ref[temp_diff_max]}" + ) diff --git a/test/tests/bcs/fixed_value_pp/tests b/test/tests/bcs/fixed_value_pp/tests new file mode 100644 index 00000000..002cd453 --- /dev/null +++ b/test/tests/bcs/fixed_value_pp/tests @@ -0,0 +1,19 @@ +[Tests] + [foam_bc_action_test] + [setup] + type = RunCommand + command = 'bash -c "foamCleanCase -case foam && blockMesh -case foam"' + [] + [run] + type = RunApp + input = main.i + prereq = foam_bc_action_test/setup + allow_warnings = true + [] + [verify] + type = PythonUnitTest + input = test.py + prereq = foam_bc_action_test/run + [] + [] +[] diff --git a/test/tests/bcs/laplace_fixed_gradient/tests b/test/tests/bcs/laplace_fixed_gradient/tests index ab7bf175..5166c3b1 100644 --- a/test/tests/bcs/laplace_fixed_gradient/tests +++ b/test/tests/bcs/laplace_fixed_gradient/tests @@ -23,5 +23,17 @@ input = test.py prereq = foam_bc_fixed_gradient/run [] + [run_no_kappa] + type = RunApp + input = main.i + cli_args = "FoamBCs/T_flux/diffusivity_coefficient='' AuxKernels/T_flux/expression='t'" + prereq = foam_bc_fixed_gradient/verify + allow_warnings = true + [] + [verify_no_kappa] + type = PythonUnitTest + input = test.py + prereq = foam_bc_fixed_gradient/run_no_kappa + [] [] [] diff --git a/test/tests/bcs/mapped_inlet/foam/0/T b/test/tests/bcs/mapped_inlet/foam/0/T new file mode 100644 index 00000000..69c3f725 --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/0/T @@ -0,0 +1,56 @@ +/*--------------------------------*- 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 2.; + +boundaryField +{ + left + { + type fixedValue; + value uniform 0.; + } + right + { + type calculated; + value uniform 0.; + } + top + { + type calculated; + value uniform 0.; + } + bottom + { + type fixedValue; + value uniform 0.; + } + back + { + type calculated; + value uniform 0.; + } + front + { + type fixedValue; + value uniform 0.; + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/0/U b/test/tests/bcs/mapped_inlet/foam/0/U new file mode 100644 index 00000000..15b296fa --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/0/U @@ -0,0 +1,31 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class volVectorField; + location "0"; + object U; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [ 0 1 -1 0 0 0 0 ]; + +internalField uniform (1 -0.5 0.25); + +boundaryField +{ + + ".*" + { + type fixedValue; + value $internalField; + } +} + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/0/rho b/test/tests/bcs/mapped_inlet/foam/0/rho new file mode 100644 index 00000000..d30a04dc --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/0/rho @@ -0,0 +1,56 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class volScalarField; + location "0"; + object rho; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [1 -3 0 0 0 0 0]; + +internalField uniform 1; + +boundaryField +{ + left + { + type calculated; + value uniform 1; + } + right + { + type calculated; + value uniform 1; + } + top + { + type calculated; + value uniform 1; + } + front + { + type calculated; + value uniform 1; + } + bottom + { + type calculated; + value uniform 1; + } + back + { + type calculated; + value uniform 1; + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/constant/g b/test/tests/bcs/mapped_inlet/foam/constant/g new file mode 100644 index 00000000..8af96f3a --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/constant/g @@ -0,0 +1,21 @@ +/*--------------------------------*- 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 uniformDimensionedVectorField; + location "constant"; + object g; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [0 1 -2 0 0 0 0]; +value (0 0 0); + + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/constant/momentumTransport b/test/tests/bcs/mapped_inlet/foam/constant/momentumTransport new file mode 100644 index 00000000..0416f1a9 --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/constant/momentumTransport @@ -0,0 +1,18 @@ +/*--------------------------------*- 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 dictionary; + object RASProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +simulationType laminar; + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/constant/physicalProperties b/test/tests/bcs/mapped_inlet/foam/constant/physicalProperties new file mode 100644 index 00000000..4c5dac6a --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/constant/physicalProperties @@ -0,0 +1,52 @@ +/*--------------------------------*- 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 dictionary; + location "constant"; + object physicalProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +thermoType +{ + type heSolidThermo; + mixture pureMixture; + transport constIsoSolid; + thermo eConst; + equationOfState rhoConst; + specie specie; + energy sensibleInternalEnergy; +} + +mixture +{ + specie + { + molWeight 1; + } + thermodynamics + { + Cv 1; // Specific heat capacity [J/(kg·K)] + Hf 1; // Heat of formation [J/kg] + Tref 0; + } + transport + { + kappa 1; // Thermal conductivity [W/(m·K)] + mu 1.; + } + equationOfState + { + rho 0.5; // Density [kg/m^3] + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/system/blockMeshDict b/test/tests/bcs/mapped_inlet/foam/system/blockMeshDict new file mode 100644 index 00000000..8221a4b1 --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/system/blockMeshDict @@ -0,0 +1,85 @@ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object blockMeshDict; +} + +vertices +( + ( 0.0 0.0 0.0 ) + ( 2.0 0.0 0.0 ) + ( 2.0 2.0 0.0 ) + ( 0.0 2.0 0.0 ) + + ( 0.0 0.0 2.0) + ( 2.0 0.0 2.0) + ( 2.0 2.0 2.0) + ( 0.0 2.0 2.0) +); + +blocks +( + hex (0 1 2 3 4 5 6 7) (9 9 9) simpleGrading (1 1 1) +); + +boundary +( + + // interface + left + { + type wall; + faces + ( + (4 7 3 0) + ); + } + + right + { + type wall; + faces + ( + (6 5 1 2) + ); + } + + top + { + type wall; + faces + ( + (2 3 7 6) + ); + } + + front + { + type wall; + faces + ( + (3 2 1 0) + ); + } + + bottom + { + type wall; + faces + ( + (0 1 5 4) + ); + } + + back + { + type wall; + faces + ( + (4 5 6 7) + ); + } + +); diff --git a/test/tests/bcs/mapped_inlet/foam/system/blockMeshDict.face_edge b/test/tests/bcs/mapped_inlet/foam/system/blockMeshDict.face_edge new file mode 100644 index 00000000..4a877e58 --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/system/blockMeshDict.face_edge @@ -0,0 +1,85 @@ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object blockMeshDict; +} + +vertices +( + ( 0.0 0.0 0.0 ) + ( 2.0 0.0 0.0 ) + ( 2.0 2.0 0.0 ) + ( 0.0 2.0 0.0 ) + + ( 0.0 0.0 2.0) + ( 2.0 0.0 2.0) + ( 2.0 2.0 2.0) + ( 0.0 2.0 2.0) +); + +blocks +( + hex (0 1 2 3 4 5 6 7) (8 8 8) simpleGrading (1 1 1) +); + +boundary +( + + // interface + left + { + type wall; + faces + ( + (4 7 3 0) + ); + } + + right + { + type wall; + faces + ( + (6 5 1 2) + ); + } + + top + { + type wall; + faces + ( + (2 3 7 6) + ); + } + + front + { + type wall; + faces + ( + (3 2 1 0) + ); + } + + bottom + { + type wall; + faces + ( + (0 1 5 4) + ); + } + + back + { + type wall; + faces + ( + (4 5 6 7) + ); + } + +); diff --git a/test/tests/bcs/mapped_inlet/foam/system/blockMeshDict.orig b/test/tests/bcs/mapped_inlet/foam/system/blockMeshDict.orig new file mode 100644 index 00000000..8221a4b1 --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/system/blockMeshDict.orig @@ -0,0 +1,85 @@ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object blockMeshDict; +} + +vertices +( + ( 0.0 0.0 0.0 ) + ( 2.0 0.0 0.0 ) + ( 2.0 2.0 0.0 ) + ( 0.0 2.0 0.0 ) + + ( 0.0 0.0 2.0) + ( 2.0 0.0 2.0) + ( 2.0 2.0 2.0) + ( 0.0 2.0 2.0) +); + +blocks +( + hex (0 1 2 3 4 5 6 7) (9 9 9) simpleGrading (1 1 1) +); + +boundary +( + + // interface + left + { + type wall; + faces + ( + (4 7 3 0) + ); + } + + right + { + type wall; + faces + ( + (6 5 1 2) + ); + } + + top + { + type wall; + faces + ( + (2 3 7 6) + ); + } + + front + { + type wall; + faces + ( + (3 2 1 0) + ); + } + + bottom + { + type wall; + faces + ( + (0 1 5 4) + ); + } + + back + { + type wall; + faces + ( + (4 5 6 7) + ); + } + +); diff --git a/test/tests/bcs/mapped_inlet/foam/system/controlDict b/test/tests/bcs/mapped_inlet/foam/system/controlDict new file mode 100644 index 00000000..2bbe0e5f --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/system/controlDict @@ -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 dictionary; + object controlDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solver mappedInletTestSolver; + +startFrom startTime; + +startTime 0; + +stopAt endTime; + +endTime 5.; + +deltaT 1.; + +writeControl timeStep; + +writeInterval 1; + +writeFormat ascii; + +writePrecision 20; + +writeCompression off; + +timeFormat general; + +timePrecision 20; + +runTimeModifiable true; + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/system/decomposeParDict b/test/tests/bcs/mapped_inlet/foam/system/decomposeParDict new file mode 100644 index 00000000..9df2378c --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/system/decomposeParDict @@ -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 dictionary; + location "system"; + object decomposeParDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +numberOfSubdomains 5; + +method scotch; + +simpleCoeffs +{ + n (2 1 1); +} + +hierarchicalCoeffs +{ + n (1 1 1); + order xyz; +} + +manualCoeffs +{ + dataFile ""; +} + +distributed no; + +roots ( ); + + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/system/decomposeParDict.face_edge b/test/tests/bcs/mapped_inlet/foam/system/decomposeParDict.face_edge new file mode 100644 index 00000000..0204a6b9 --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/system/decomposeParDict.face_edge @@ -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 dictionary; + location "system"; + object decomposeParDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +numberOfSubdomains 2; + +method simple; + +simpleCoeffs +{ + n (2 1 1); +} + +hierarchicalCoeffs +{ + n (1 1 1); + order xyz; +} + +manualCoeffs +{ + dataFile ""; +} + +distributed no; + +roots ( ); + + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/system/decomposeParDict.orig b/test/tests/bcs/mapped_inlet/foam/system/decomposeParDict.orig new file mode 100644 index 00000000..9df2378c --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/system/decomposeParDict.orig @@ -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 dictionary; + location "system"; + object decomposeParDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +numberOfSubdomains 5; + +method scotch; + +simpleCoeffs +{ + n (2 1 1); +} + +hierarchicalCoeffs +{ + n (1 1 1); + order xyz; +} + +manualCoeffs +{ + dataFile ""; +} + +distributed no; + +roots ( ); + + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/system/fvSchemes b/test/tests/bcs/mapped_inlet/foam/system/fvSchemes new file mode 100644 index 00000000..787f3b83 --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/system/fvSchemes @@ -0,0 +1,51 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSchemes; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +ddtSchemes +{ + default Euler; +} + +gradSchemes +{ + default Gauss linear; +} + +divSchemes +{ + default none; + + div(phi,U) Gauss linear; + div(phi,K) Gauss linear; + div(phi,h) Gauss linear; + div(((rho*nuEff)*dev2(T(grad(U))))) Gauss linear; +} + +laplacianSchemes +{ + default Gauss linear corrected; +} + +interpolationSchemes +{ + default linear; +} + +snGradSchemes +{ + default corrected; +} + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/foam/system/fvSolution b/test/tests/bcs/mapped_inlet/foam/system/fvSolution new file mode 100644 index 00000000..61143b21 --- /dev/null +++ b/test/tests/bcs/mapped_inlet/foam/system/fvSolution @@ -0,0 +1,61 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSolution; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solvers +{ + rho + { + solver diagonal; + } + + rhoFinal + { + $rho; + } + + + "(U|h|p_rgh)" + { + solver PBiCGStab; + preconditioner DILU; + tolerance 1e-8; + relTol 1e-8; + } + + "(U|h|p_rgh)Final" + { + $U; + tolerance 1e-8; + relTol 1e-8; + } +} + +PIMPLE +{ + momentumPredictor yes; + pRefCell 0; + pRefValue 0; +} + +relaxationFactors +{ + equations + { + h 1; + U 1; + } +} + +// ************************************************************************* // diff --git a/test/tests/bcs/mapped_inlet/main.i b/test/tests/bcs/mapped_inlet/main.i new file mode 100644 index 00000000..50a34648 --- /dev/null +++ b/test/tests/bcs/mapped_inlet/main.i @@ -0,0 +1,76 @@ +[Mesh] + type = FoamMesh + case = 'foam' + foam_patch = 'left right bottom top back front' +[] + +[FoamBCs] + [mass_flowx] + type=FoamMassFlowRateMappedInletBC + boundary = 'left' + default = 1 + translation_vector = '1. 0 0' + [] + [tempx] + type=FoamScalarBulkMappedInletBC + boundary = 'left' + default = 1 + translation_vector = '1. 0 0' + foam_variable = 'T' + [] + [mass_flowy] + type=FoamMassFlowRateMappedInletBC + boundary = 'bottom' + default = 1 + translation_vector = '0 1. 0' + [] + [tempy] + type=FoamScalarBulkMappedInletBC + boundary = 'bottom' + default = 1 + translation_vector = '0 1. 0' + foam_variable = 'T' + [] + [mass_flowz] + type=FoamMassFlowRateMappedInletBC + boundary = 'front' + default = 1 + translation_vector = '0 0 1.' + [] + [tempz] + type=FoamScalarBulkMappedInletBC + boundary = 'front' + default = 1 + translation_vector = '0 0 1.' + foam_variable = 'T' + [] +[] + +[Postprocessors] + [pp] + type = ParsedPostprocessor + expression = '2' + execute_on = TIMESTEP_BEGIN + [] +[] + + +[Problem] + type = FoamProblem + # Take the boundary temperature from OpenFOAM and set it on the MOOSE mesh. +[] + +[Executioner] + type = Transient + end_time = 5 + [TimeSteppers] + [foam] + type = FoamControlledTimeStepper + [] + [] +[] + +[Outputs] + exodus = true + csv=true +[] diff --git a/test/tests/bcs/mapped_inlet/main_rotated.i b/test/tests/bcs/mapped_inlet/main_rotated.i new file mode 100644 index 00000000..553b6d3d --- /dev/null +++ b/test/tests/bcs/mapped_inlet/main_rotated.i @@ -0,0 +1,50 @@ +[Mesh] + type = FoamMesh + case = 'foam' + foam_patch = 'left right bottom top back front' +[] + +[FoamBCs] + [mass_flowx] + type=FoamMassFlowRateMappedInletBC + boundary = 'left' + default = 1 + translation_vector = '${fparse sqrt(0.5)} ${fparse sqrt(0.5)} 0' + [] + [tempx] + type=FoamScalarBulkMappedInletBC + boundary = 'left' + default = 1 + translation_vector = '${fparse sqrt(0.5)} ${fparse sqrt(0.5)} 0' + foam_variable = 'T' + [] +[] + +[Postprocessors] + [pp] + type = ParsedPostprocessor + expression = '2' + execute_on = TIMESTEP_BEGIN + [] +[] + + +[Problem] + type = FoamProblem + # Take the boundary temperature from OpenFOAM and set it on the MOOSE mesh. +[] + +[Executioner] + type = Transient + end_time = 5 + [TimeSteppers] + [foam] + type = FoamControlledTimeStepper + [] + [] +[] + +[Outputs] + exodus = true + csv=true +[] diff --git a/test/tests/bcs/mapped_inlet/test.py b/test/tests/bcs/mapped_inlet/test.py new file mode 100644 index 00000000..015437dc --- /dev/null +++ b/test/tests/bcs/mapped_inlet/test.py @@ -0,0 +1,91 @@ +"""Tests for imposing BCs in OpenFOAM using MOOSE input file syntax""" + +import unittest +import fluidfoam as ff +import numpy as np + +from read_hippo_data import get_foam_times + +CASE_DIR = "foam/" +TIMES = get_foam_times(CASE_DIR, string=True)[1:] + + +class TestFoamBCMappedInlet(unittest.TestCase): + """Test class for mapped inlet BCs in Hippo.""" + + def test_mapped_inlet(self): + """Test case for mapped inlet.""" + + for i in range(len(TIMES)): + self._check_u_temp_refs(i, "left", [1.0, 0, 0]) + self._check_u_temp_refs(i, "bottom", [0, 1.0, 0]) + self._check_u_temp_refs(i, "front", [0, 0, 1.0]) + + def test_mapped_inlet_subtract(self): + """Test case for mapped inlet when temperature is scaled by subtracting the difference in bulk.""" + + for i in range(len(TIMES)): + self._check_u_temp_refs(i, "left", [1.0, 0, 0], False) + self._check_u_temp_refs(i, "bottom", [0, 1.0, 0], False) + self._check_u_temp_refs(i, "front", [0, 0, 1.0], False) + + def test_mapped_inlet_rotated(self): + """Test case for when inlet's are not aligned with the axis.""" + for i in range(len(TIMES)): + self._check_u_temp_refs(i, "left", [np.sqrt(0.5), np.sqrt(0.5), 0]) + + def test_mapped_inlet_face_point(self): + """Test case for mapped inlet where the point is on the interface between boundaries.""" + + for i in range(len(TIMES)): + self._check_u_temp_refs(i, "left", [7.0 / 8, 0, 0]) + self._check_u_temp_refs(i, "bottom", [0, 7.0 / 8, 0]) + self._check_u_temp_refs(i, "front", [0, 0, 7.0 / 8]) + + def _check_u_temp_refs(self, idx, boundary, offset, use_scale=True): + rho = 0.5 + mdot_pp = 1 + t_pp = 1 + time = TIMES[idx] + + x, y, z = ff.readof.readmesh(CASE_DIR, boundary=boundary) + u = ff.readof.readvector(CASE_DIR, time, "U", boundary=boundary).T + temp = ff.readof.readscalar(CASE_DIR, time, "T", boundary=boundary) + + x += offset[0] + y += offset[1] + z += offset[2] + + if idx != 0: + t = np.float64(TIMES[idx - 1]) + u_ref = np.array( + [ + x + y + z + t, + x - y + z + t, + x + y - z + t, + ] + ).T + temp_ref = np.sqrt(x * x + y * y + z * z) + t + else: + # first time step uses initialised value + u_ref = np.array([[1, -0.5, 0.25]]) + temp_ref = 2 + + area = 4.0 + normal = np.array(offset) / np.linalg.norm(offset) + mdot = rho * np.mean(np.vecdot(u_ref, normal)) * area + u_ref *= mdot_pp / mdot + + t_bulk = np.mean(temp_ref) + if use_scale: + temp_ref *= t_pp / t_bulk + else: + temp_ref += t_pp - t_bulk + + assert np.allclose(u_ref, u, rtol=1e-7, atol=1e-12), ( + f"Max diff ({boundary}) (velocity) ({TIMES[idx]}): {abs(u - u_ref).max()} " + ) + + assert np.allclose(temp_ref, temp, rtol=1e-7, atol=1e-12), ( + f"Max diff ({boundary}) (temperature) ({time}): {abs(temp - temp_ref).max()} {temp} {temp_ref}" + ) diff --git a/test/tests/bcs/mapped_inlet/tests b/test/tests/bcs/mapped_inlet/tests new file mode 100644 index 00000000..cc19eb5a --- /dev/null +++ b/test/tests/bcs/mapped_inlet/tests @@ -0,0 +1,153 @@ +[Tests] + [mapped_mass_flow_receiver] + [setup] + type = RunCommand + command = 'bash -c "foamCleanCase -case foam && blockMesh -case foam && decomposePar -case foam"' + [] + [run_err] + type = RunException + input = main.i + prereq = mapped_mass_flow_receiver/setup + allow_warnings = true + min_parallel = 5 + max_parallel = 5 + cli_args = "FoamBCs/mass_flowx/translation_vector='-0.5 0 0'" + expect_err = "does not have a mapped plane location" + [] + [run] + type = RunApp + input = main.i + prereq = mapped_mass_flow_receiver/setup + allow_warnings = true + min_parallel = 5 + max_parallel = 5 + [] + [reconstruct] + type = RunCommand + command = 'reconstructPar -case foam' + prereq = mapped_mass_flow_receiver/run + [] + [verify] + type = PythonUnitTest + input = test.py + prereq = mapped_mass_flow_receiver/reconstruct + test_case = 'TestFoamBCMappedInlet.test_mapped_inlet' + [] + [] + [mapped_mass_flow_subtract] + [setup] + type = RunCommand + command = 'bash -c "foamCleanCase -case foam && blockMesh -case foam && decomposePar -case foam"' + [] + [run] + type = RunApp + input = main.i + prereq = mapped_mass_flow_receiver/setup + allow_warnings = true + min_parallel = 5 + max_parallel = 5 + cli_args = "FoamBCs/tempx/scale_method=subtract FoamBCs/tempy/scale_method=subtract FoamBCs/tempz/scale_method=subtract" + [] + [reconstruct] + type = RunCommand + command = 'reconstructPar -case foam' + prereq = mapped_mass_flow_receiver/run + [] + [verify] + type = PythonUnitTest + input = test.py + prereq = mapped_mass_flow_receiver/reconstruct + test_case = 'TestFoamBCMappedInlet.test_mapped_inlet_subtract' + [] + [] + [mapped_mass_flow] + [setup] + type = RunCommand + command = 'bash -c "foamCleanCase -case foam && blockMesh -case foam && decomposePar -case foam"' + prereq = mapped_mass_flow_receiver/reconstruct + [] + [run] + type = RunApp + input = main.i + prereq = mapped_mass_flow/setup + allow_warnings = true + min_parallel = 5 + max_parallel = 5 + cli_args = 'FoamBCs/mass_flowx/pp=pp FoamBCs/tempx/pp=pp FoamBCs/mass_flowy/pp=pp FoamBCs/tempy/pp=pp FoamBCs/mass_flowz/pp=pp FoamBCs/tempz/pp=pp Postprocessors/pp/expression=1' + [] + [reconstruct] + type = RunCommand + command = 'reconstructPar -case foam' + prereq = mapped_mass_flow/run + [] + [verify] + type = PythonUnitTest + input = test.py + prereq = mapped_mass_flow/reconstruct + test_case = 'TestFoamBCMappedInlet.test_mapped_inlet' + [] + [] + [mapped_rotated] + [setup] + type = RunCommand + command = 'bash -c "foamCleanCase -case foam && blockMesh -case foam && transformPoints -case foam "Rz=45" && decomposePar -case foam"' + prereq = mapped_mass_flow/reconstruct + [] + [run] + type = RunApp + input = main_rotated.i + prereq = mapped_rotated/setup + allow_warnings = true + min_parallel = 5 + max_parallel = 5 + [] + [reconstruct] + type = RunCommand + command = 'reconstructPar -case foam' + prereq = mapped_rotated/run + [] + [verify] + type = PythonUnitTest + input = test.py + prereq = mapped_rotated/reconstruct + test_case = 'TestFoamBCMappedInlet.test_mapped_inlet_rotated' + [] + [] + [face_edge_case] + [update_mesh] + type = RunCommand + command = 'bash -c "foamCleanCase -case foam && cp foam/system/blockMeshDict.face_edge foam/system/blockMeshDict && cp foam/system/decomposeParDict.face_edge foam/system/decomposeParDict"' + prereq = mapped_rotated/verify + [] + [setup] + type = RunCommand + command = 'bash -c "blockMesh -case foam && decomposePar -case foam"' + prereq = face_edge_case/update_mesh + [] + [run] + type = RunApp + input = main.i + prereq = face_edge_case/setup + allow_warnings = true + min_parallel = 2 + max_parallel = 2 + [] + [reconstruct] + type = RunCommand + command = 'reconstructPar -case foam' + prereq = face_edge_case/run + [] + [verify] + type = PythonUnitTest + input = test.py + prereq = face_edge_case/reconstruct + test_case = 'TestFoamBCMappedInlet.test_mapped_inlet_face_point' + [] + [reset_mesh] + type = RunCommand + command = 'bash -c "cp foam/system/blockMeshDict.orig foam/system/blockMeshDict && cp foam/system/decomposeParDict.orig foam/system/decomposeParDict"' + prereq = 'face_edge_case/update_mesh' + [] + [] + +[] diff --git a/test/tests/bcs/mass_flow_rate/foam/0/T b/test/tests/bcs/mass_flow_rate/foam/0/T new file mode 100644 index 00000000..f8312bb2 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/0/T @@ -0,0 +1,56 @@ +/*--------------------------------*- 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 0.1; + +boundaryField +{ + left + { + type calculated; + value uniform 0.; + } + right + { + type calculated; + value uniform 0.; + } + top + { + type calculated; + value uniform 0.; + } + bottom + { + type calculated; + value uniform 0.; + } + back + { + type calculated; + value uniform 0.; + } + front + { + type calculated; + value uniform 0.; + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/0/U b/test/tests/bcs/mass_flow_rate/foam/0/U new file mode 100644 index 00000000..d23654bb --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/0/U @@ -0,0 +1,31 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class volVectorField; + location "0"; + object U; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [ 0 1 -1 0 0 0 0 ]; + +internalField uniform (2 -1 0); + +boundaryField +{ + + ".*" + { + type fixedValue; + value $internalField; + } +} + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/0/p b/test/tests/bcs/mass_flow_rate/foam/0/p new file mode 100644 index 00000000..0d00fc2f --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/0/p @@ -0,0 +1,30 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class volScalarField; + location "0"; + object p; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [ 1 -1 -2 0 0 0 0 ]; + +internalField uniform 0; + +boundaryField +{ + ".*" + { + type calculated; + value $internalField; + } +} + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/0/p_rgh b/test/tests/bcs/mass_flow_rate/foam/0/p_rgh new file mode 100644 index 00000000..cfc89a8c --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/0/p_rgh @@ -0,0 +1,30 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class volScalarField; + location "0"; + object p_rgh; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [ 1 -1 -2 0 0 0 0 ]; + +internalField uniform 0; + +boundaryField +{ + ".*" + { + type fixedValue; + value $internalField; + } +} + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/0/rho b/test/tests/bcs/mass_flow_rate/foam/0/rho new file mode 100644 index 00000000..d30a04dc --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/0/rho @@ -0,0 +1,56 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class volScalarField; + location "0"; + object rho; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [1 -3 0 0 0 0 0]; + +internalField uniform 1; + +boundaryField +{ + left + { + type calculated; + value uniform 1; + } + right + { + type calculated; + value uniform 1; + } + top + { + type calculated; + value uniform 1; + } + front + { + type calculated; + value uniform 1; + } + bottom + { + type calculated; + value uniform 1; + } + back + { + type calculated; + value uniform 1; + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/constant/g b/test/tests/bcs/mass_flow_rate/foam/constant/g new file mode 100644 index 00000000..8af96f3a --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/constant/g @@ -0,0 +1,21 @@ +/*--------------------------------*- 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 uniformDimensionedVectorField; + location "constant"; + object g; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dimensions [0 1 -2 0 0 0 0]; +value (0 0 0); + + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/constant/momentumTransport b/test/tests/bcs/mass_flow_rate/foam/constant/momentumTransport new file mode 100644 index 00000000..0416f1a9 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/constant/momentumTransport @@ -0,0 +1,18 @@ +/*--------------------------------*- 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 dictionary; + object RASProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +simulationType laminar; + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/constant/physicalProperties b/test/tests/bcs/mass_flow_rate/foam/constant/physicalProperties new file mode 100644 index 00000000..8a4c6a29 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/constant/physicalProperties @@ -0,0 +1,52 @@ +/*--------------------------------*- 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 dictionary; + location "constant"; + object physicalProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +thermoType +{ + type heRhoThermo; + mixture pureMixture; + transport const; + thermo hConst; + equationOfState rhoConst; + specie specie; + energy sensibleEnthalpy; +} + +mixture +{ + specie + { + molWeight 1; + } + thermodynamics + { + Cp 1; // Specific heat capacity [J/(kg·K)] + Hf 1; // Heat of formation [J/kg] + Tref 0; + } + transport + { + kappa 1; // Thermal conductivity [W/(m·K)] + mu 1.; + } + equationOfState + { + rho 0.5; // Density [kg/m^3] + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/system/blockMeshDict b/test/tests/bcs/mass_flow_rate/foam/system/blockMeshDict new file mode 100644 index 00000000..1adab343 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/system/blockMeshDict @@ -0,0 +1,85 @@ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object blockMeshDict; +} + +vertices +( + ( 0.0 0.0 0.0 ) + ( 10.0 0.0 0.0 ) + ( 10.0 1.0 0.0 ) + ( 0.0 1.0 0.0 ) + + ( 0.0 0.0 1.0) + ( 10.0 0.0 1.0) + ( 10.0 1.0 1.0) + ( 0.0 1.0 1.0) +); + +blocks +( + hex (0 1 2 3 4 5 6 7) (50 2 2) simpleGrading (25 1 1) +); + +boundary +( + + // interface + left + { + type wall; + faces + ( + (4 7 3 0) + ); + } + + right + { + type wall; + faces + ( + (6 5 1 2) + ); + } + + top + { + type wall; + faces + ( + (2 3 7 6) + ); + } + + front + { + type wall; + faces + ( + (3 2 1 0) + ); + } + + bottom + { + type wall; + faces + ( + (0 1 5 4) + ); + } + + back + { + type wall; + faces + ( + (4 5 6 7) + ); + } + +); diff --git a/test/tests/bcs/mass_flow_rate/foam/system/controlDict b/test/tests/bcs/mass_flow_rate/foam/system/controlDict new file mode 100644 index 00000000..4e2baf59 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/system/controlDict @@ -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 dictionary; + object controlDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solver postprocessorTestSolver; + +startFrom startTime; + +startTime 0; + +stopAt endTime; + +endTime 0.32; + +deltaT 0.01; + +writeControl timeStep; + +writeInterval 1; + +writeFormat ascii; + +writePrecision 20; + +writeCompression off; + +timeFormat general; + +timePrecision 20; + +runTimeModifiable true; + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/system/decomposeParDict b/test/tests/bcs/mass_flow_rate/foam/system/decomposeParDict new file mode 100644 index 00000000..0204a6b9 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/system/decomposeParDict @@ -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 dictionary; + location "system"; + object decomposeParDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +numberOfSubdomains 2; + +method simple; + +simpleCoeffs +{ + n (2 1 1); +} + +hierarchicalCoeffs +{ + n (1 1 1); + order xyz; +} + +manualCoeffs +{ + dataFile ""; +} + +distributed no; + +roots ( ); + + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/system/fvSchemes b/test/tests/bcs/mass_flow_rate/foam/system/fvSchemes new file mode 100644 index 00000000..787f3b83 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/system/fvSchemes @@ -0,0 +1,51 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSchemes; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +ddtSchemes +{ + default Euler; +} + +gradSchemes +{ + default Gauss linear; +} + +divSchemes +{ + default none; + + div(phi,U) Gauss linear; + div(phi,K) Gauss linear; + div(phi,h) Gauss linear; + div(((rho*nuEff)*dev2(T(grad(U))))) Gauss linear; +} + +laplacianSchemes +{ + default Gauss linear corrected; +} + +interpolationSchemes +{ + default linear; +} + +snGradSchemes +{ + default corrected; +} + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/foam/system/fvSolution b/test/tests/bcs/mass_flow_rate/foam/system/fvSolution new file mode 100644 index 00000000..61143b21 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/foam/system/fvSolution @@ -0,0 +1,61 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSolution; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solvers +{ + rho + { + solver diagonal; + } + + rhoFinal + { + $rho; + } + + + "(U|h|p_rgh)" + { + solver PBiCGStab; + preconditioner DILU; + tolerance 1e-8; + relTol 1e-8; + } + + "(U|h|p_rgh)Final" + { + $U; + tolerance 1e-8; + relTol 1e-8; + } +} + +PIMPLE +{ + momentumPredictor yes; + pRefCell 0; + pRefValue 0; +} + +relaxationFactors +{ + equations + { + h 1; + U 1; + } +} + +// ************************************************************************* // diff --git a/test/tests/bcs/mass_flow_rate/gold/main_out.csv b/test/tests/bcs/mass_flow_rate/gold/main_out.csv new file mode 100644 index 00000000..bb1c32b8 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/gold/main_out.csv @@ -0,0 +1,34 @@ +time,m_dot,pp +0,0,0 +0.01,-0.01,0.01 +0.02,-0.02,0.02 +0.03,-0.03,0.03 +0.04,-0.04,0.04 +0.05,-0.05,0.05 +0.06,-0.06,0.06 +0.07,-0.07,0.07 +0.08,-0.08,0.08 +0.09,-0.09,0.09 +0.1,-0.1,0.1 +0.11,-0.11,0.11 +0.12,-0.12,0.12 +0.13,-0.13,0.13 +0.14,-0.14,0.14 +0.15,-0.15,0.15 +0.16,-0.16,0.16 +0.17,-0.17,0.17 +0.18,-0.18,0.18 +0.19,-0.19,0.19 +0.2,-0.2,0.2 +0.21,-0.21,0.21 +0.22,-0.22,0.22 +0.23,-0.23,0.23 +0.24,-0.24,0.24 +0.25,-0.25,0.25 +0.26,-0.26,0.26 +0.27,-0.27,0.27 +0.28,-0.28,0.28 +0.29,-0.29,0.29 +0.3,-0.3,0.3 +0.31,-0.31,0.31 +0.32,-0.32,0.32 diff --git a/test/tests/bcs/mass_flow_rate/main.i b/test/tests/bcs/mass_flow_rate/main.i new file mode 100644 index 00000000..43fc5346 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/main.i @@ -0,0 +1,48 @@ +[Mesh] + type = FoamMesh + case = 'foam' + foam_patch = 'left right bottom top back front' +[] + +[FoamBCs] + [temp1] + type=FoamMassFlowRateInletBC + boundary = 'left' # test boundary restrictions + pp = pp + [] +[] + +[Postprocessors] + [pp] + type = ParsedPostprocessor + expression = 't' + use_t = true + execute_on = TIMESTEP_BEGIN + [] + [m_dot] + type = FoamSideAdvectiveFluxIntegral + boundary = left + foam_scalar = rho + [] +[] + + +[Problem] + type = FoamProblem + # Take the boundary temperature from OpenFOAM and set it on the MOOSE mesh. +[] + +[Executioner] + type = Transient + end_time = 0.32 + [TimeSteppers] + [foam] + type = FoamControlledTimeStepper + [] + [] +[] + +[Outputs] + exodus = true + csv=true +[] diff --git a/test/tests/bcs/mass_flow_rate/tests b/test/tests/bcs/mass_flow_rate/tests new file mode 100644 index 00000000..68cf4430 --- /dev/null +++ b/test/tests/bcs/mass_flow_rate/tests @@ -0,0 +1,15 @@ +[Tests] + [mass_flow_rate] + [setup] + type = RunCommand + command = 'bash -c "foamCleanCase -case foam && blockMesh -case foam"' + [] + [run] + type = CSVDiff + input = main.i + prereq = mass_flow_rate/setup + csvdiff = main_out.csv + allow_warnings = true + [] + [] +[] diff --git a/test/tests/bcs/receiver_pp/foam/0/T b/test/tests/bcs/receiver_pp/foam/0/T new file mode 100644 index 00000000..6da637de --- /dev/null +++ b/test/tests/bcs/receiver_pp/foam/0/T @@ -0,0 +1,52 @@ +/*--------------------------------*- 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 0.; + +boundaryField +{ + left + { + type fixedValue; + value uniform 0.; + } + right + { + type fixedGradient; + gradient uniform 1.; + } + top + { + type zeroGradient; + } + front + { + type zeroGradient; + } + bottom + { + type zeroGradient; + } + back + { + type zeroGradient; + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/receiver_pp/foam/constant/physicalProperties b/test/tests/bcs/receiver_pp/foam/constant/physicalProperties new file mode 100644 index 00000000..6dab7b1c --- /dev/null +++ b/test/tests/bcs/receiver_pp/foam/constant/physicalProperties @@ -0,0 +1,51 @@ +/*--------------------------------*- 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 dictionary; + location "constant"; + object physicalProperties; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +thermoType +{ + type heSolidThermo; + mixture pureMixture; + transport constIsoSolid; + thermo eConst; + equationOfState rhoConst; + specie specie; + energy sensibleInternalEnergy; +} + +mixture +{ + specie + { + molWeight 1; + } + thermodynamics + { + Cv 1; // Specific heat capacity [J/(kg·K)] + Hf 1; // Heat of formation [J/kg] + Tref 0; + } + transport + { + kappa 2.; // Thermal conductivity [W/(m·K)] + } + equationOfState + { + rho 1; // Density [kg/m^3] + } +} + + +// ************************************************************************* // diff --git a/test/tests/bcs/receiver_pp/foam/system/blockMeshDict b/test/tests/bcs/receiver_pp/foam/system/blockMeshDict new file mode 100644 index 00000000..face9ea6 --- /dev/null +++ b/test/tests/bcs/receiver_pp/foam/system/blockMeshDict @@ -0,0 +1,85 @@ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object blockMeshDict; +} + +vertices +( + ( 0.0 0.0 0.0 ) + ( 10.0 0.0 0.0 ) + ( 10.0 1.0 0.0 ) + ( 0.0 1.0 0.0 ) + + ( 0.0 0.0 1.0) + ( 10.0 0.0 1.0) + ( 10.0 1.0 1.0) + ( 0.0 1.0 1.0) +); + +blocks +( + hex (0 1 2 3 4 5 6 7) (50 1 1) simpleGrading (25 1 1) +); + +boundary +( + + // interface + left + { + type wall; + faces + ( + (4 7 3 0) + ); + } + + right + { + type wall; + faces + ( + (6 5 1 2) + ); + } + + top + { + type wall; + faces + ( + (2 3 7 6) + ); + } + + front + { + type wall; + faces + ( + (3 2 1 0) + ); + } + + bottom + { + type wall; + faces + ( + (0 1 5 4) + ); + } + + back + { + type wall; + faces + ( + (4 5 6 7) + ); + } + +); diff --git a/test/tests/bcs/receiver_pp/foam/system/controlDict b/test/tests/bcs/receiver_pp/foam/system/controlDict new file mode 100644 index 00000000..89301ab0 --- /dev/null +++ b/test/tests/bcs/receiver_pp/foam/system/controlDict @@ -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 dictionary; + object controlDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solver bcTestSolver; + +startFrom startTime; + +startTime 0; + +stopAt endTime; + +endTime 32.; + +deltaT 1.; + +writeControl timeStep; + +writeInterval 1; + +writeFormat ascii; + +writePrecision 20; + +writeCompression off; + +timeFormat general; + +timePrecision 20; + +runTimeModifiable true; + +// ************************************************************************* // diff --git a/test/tests/bcs/receiver_pp/foam/system/fvSchemes b/test/tests/bcs/receiver_pp/foam/system/fvSchemes new file mode 100644 index 00000000..a24aaf80 --- /dev/null +++ b/test/tests/bcs/receiver_pp/foam/system/fvSchemes @@ -0,0 +1,46 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSchemes; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +ddtSchemes +{ + default Euler; +} + +gradSchemes +{ + default Gauss linear; +} + +divSchemes +{ + default Gauss linear; +} + +laplacianSchemes +{ + default Gauss linear corrected; +} + +interpolationSchemes +{ + default linear; +} + +snGradSchemes +{ + default corrected; +} + +// ************************************************************************* // diff --git a/test/tests/bcs/receiver_pp/foam/system/fvSolution b/test/tests/bcs/receiver_pp/foam/system/fvSolution new file mode 100644 index 00000000..639b272d --- /dev/null +++ b/test/tests/bcs/receiver_pp/foam/system/fvSolution @@ -0,0 +1,49 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Version: 12 + \\/ M anipulation | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + format ascii; + class dictionary; + object fvSolution; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +solvers +{ + rho + { + solver diagonal; + } + + rhoFinal + { + $rho; + } + + + e + { + solver PBiCGStab; + preconditioner DIC; + tolerance 1e-15; + relTol 1e-15; + } + + eFinal + { + $e; + tolerance 1e-15; + relTol 1e-15; + } +} + +PIMPLE +{ +} + +// ************************************************************************* // diff --git a/test/tests/bcs/receiver_pp/main.i b/test/tests/bcs/receiver_pp/main.i new file mode 100644 index 00000000..cd1da1a7 --- /dev/null +++ b/test/tests/bcs/receiver_pp/main.i @@ -0,0 +1,48 @@ +[Mesh] + type = FoamMesh + case = 'foam' + foam_patch = 'left right' +[] + +[Variables] + [dummy] + family = MONOMIAL + order = CONSTANT + initial_condition = 999 + [] +[] + +[FoamBCs] + [T_value] + type=FoamFixedValuePostprocessorBC + foam_variable = T + default = 0. + boundary = 'left' + [] + [T_flux] + type=FoamFixedGradientPostprocessorBC + foam_variable = T + boundary = 'right' + diffusivity_coefficient = kappa + default=2 + [] +[] + + +[Problem] + type = FoamProblem +[] + +[Executioner] + type = Transient + end_time = 1 + [TimeSteppers] + [foam] + type = FoamControlledTimeStepper + [] + [] +[] + +[Outputs] + exodus = true +[] diff --git a/test/tests/bcs/receiver_pp/test.py b/test/tests/bcs/receiver_pp/test.py new file mode 100644 index 00000000..98b48ce7 --- /dev/null +++ b/test/tests/bcs/receiver_pp/test.py @@ -0,0 +1,29 @@ +"""Tests for imposing BCs in OpenFOAM using MOOSE input file syntax""" + +import unittest +import fluidfoam as ff +import numpy as np + +from read_hippo_data import get_foam_times # pylint: disable=E0401 + + +class TestFoamBCFixedGradient(unittest.TestCase): + """Test class for imposing fixed value BCs in Hippo.""" + + def test_fixed_gradient_x(self): + """Test case for imposing fixed value.""" + case_dir = "foam/" + times = get_foam_times(case_dir, string=True)[1:] + + for time in times: + coords = dict(zip(("x", "y", "z"), ff.readof.readmesh(case_dir))) + + temp = ff.readof.readscalar(case_dir, time, "T") + + temp_ref = coords["x"] * np.float64(time) + + temp_diff_max = np.argmax(abs(temp - temp_ref)) + assert np.allclose(temp_ref, temp, rtol=1e-7, atol=1e-12), ( + f"Max diff ({time}): {abs(temp - temp_ref).max()} " + f"{temp[temp_diff_max]} {temp_ref[temp_diff_max]}" + ) diff --git a/test/tests/bcs/receiver_pp/tests b/test/tests/bcs/receiver_pp/tests new file mode 100644 index 00000000..01645088 --- /dev/null +++ b/test/tests/bcs/receiver_pp/tests @@ -0,0 +1,39 @@ +[Tests] + [receiver_pp] + [setup] + type = RunCommand + command = 'bash -c "foamCleanCase -case foam && blockMesh -case foam"' + [] + [diffusivity_coeff_err] + type = RunException + input = main.i + prereq = receiver_pp/setup + expect_err = "Diffusivity coefficient 'kappa1' not a Foam volScalarField" + cli_args = 'FoamBCs/T_flux/diffusivity_coefficient=kappa1' + allow_warnings = true + [] + [run] + type = RunApp + input = main.i + prereq = receiver_pp/setup + allow_warnings = true + [] + [verify] + type = PythonUnitTest + input = test.py + prereq = receiver_pp/run + [] + [run_no_kappa] + type = RunApp + input = main.i + cli_args = "FoamBCs/T_flux/diffusivity_coefficient='' FoamBCs/T_flux/default=1" + prereq = receiver_pp/verify + allow_warnings = true + [] + [verify_no_kappa] + type = PythonUnitTest + input = test.py + prereq = receiver_pp/run_no_kappa + [] + [] +[]