Skip to content

Commit

Permalink
Rework 0D enclosure to leverage Physics system: lets a SpeciesTrappin…
Browse files Browse the repository at this point in the history
…gPhysics live there

Rework 1D structure to use a DiffusionPhysics
refs idaholab#8
  • Loading branch information
GiudGiud committed Jan 13, 2025
1 parent 7a0a642 commit 86e065e
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 146 deletions.
50 changes: 32 additions & 18 deletions include/components/Enclosure0D.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,51 @@

#pragma once

#include "Component.h"
#include "ComponentAction.h"
#include "PhysicsComponentHelper.h"

class Enclosure0D : public Component
/**
* Enclosure component which can trap a species, e.g. Tritium
*/
class Enclosure0D : public virtual ComponentAction, public PhysicsComponentHelper
{
public:
Enclosure0D(const InputParameters & params);

static InputParameters validParams();

/// Return the species living on this component
const std::vector<NonlinearVariableName> & species() const { return _species; }
/// Return the scaling factors to use for these species
const std::vector<Real> & scalingFactors() const { return _scaling_factors; }
/// Return the initial conditions to use for these species
const std::vector<Real> & ics() const { return _ics; }
/// Return the temperature of this enclosure
Real temperature() const { return _temperature; }
/// Returns the scaled volume of the enclosure
virtual Real volume() const override { return _volume; }
/// Returns the scaled outer boundary surface area
virtual Real outerSurfaceArea() const override { return _surface_area; }
/// Returns the boundary of the enclosure, connecting with the structure
virtual const std::vector<BoundaryName> & outerSurfaceBoundaries() const override { _console << Moose::stringify(_outer_boundaries); return _outer_boundaries; }
/// Get the connected structure name
ComponentName connectedStructure() const { return getParam<ComponentName>("connected_structure");}

protected:
Real scaledVolume() const;
Real scaledSurfaceArea() const;
virtual void initComponentPhysics() override;

/// Vector of the names of the species to track
std::vector<NonlinearVariableName> _species;
/// Scaling factors for the nonlinear species equations
std::vector<Real> _scaling_factors;
/// Initial conditions for each species
std::vector<Real> _ics;
/// Temperature of the enclosure
const Real _temperature;
const Real _length_unit;
const Real _pressure_unit;
/// Outer surface area of the enclosure
const Real _surface_area;
/// Volume of the enclosure
const Real _volume;
/// Surface connecting the enclosure with the structure
const std::vector<BoundaryName> _outer_boundaries;
};

inline Real
Enclosure0D::scaledVolume() const
{
return _volume * Utility::pow<3>(_length_unit);
}

inline Real
Enclosure0D::scaledSurfaceArea() const
{
return _surface_area * Utility::pow<2>(_length_unit);
}
20 changes: 13 additions & 7 deletions include/components/Structure1D.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,31 @@

#pragma once

#include "Component.h"
#include "FunctionInterface.h"
#include "ComponentAction.h"
#include "PhysicsComponentHelper.h"

class Structure1D : public Component, public FunctionInterface
/**
* A 1D structure on which a species can diffuse
*/
class Structure1D : public virtual ComponentAction, public PhysicsComponentHelper
{
public:
Structure1D(const InputParameters & params);

static InputParameters validParams();

void addVariables() override;
void addMooseObjects() override;
virtual void addMeshGenerators() override;
virtual void initComponentPhysics() override;

protected:
void setupMesh() override;

/// Names of the variables for the species
const std::vector<NonlinearVariableName> _species;
/// Scaling factors for each nonlinear variable
const std::vector<Real> _scaling_factors;
/// Initial values for the variables
const std::vector<Real> _ics;
/// Diffusion coefficients
const std::vector<FunctionName> _input_Ds;
/// Unit for the mesh
const Real _length_unit;
};
45 changes: 33 additions & 12 deletions src/components/Enclosure0D.C
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,61 @@

#include "Enclosure0D.h"
#include "TMAPUtils.h"
#include "SpeciesTrappingPhysics.h"

registerMooseObject("TMAPApp", Enclosure0D);
registerMooseAction("TMAP8App", Enclosure0D, "init_component_physics");

InputParameters
Enclosure0D::validParams()
{
auto params = Component::validParams();
auto params = ComponentAction::validParams();
params += PhysicsComponentHelper::validParams();
params += TMAP::enclosureCommonParams();
params.makeParamRequired<std::vector<PhysicsName>>("physics");
return params;
}

Enclosure0D::Enclosure0D(const InputParameters & params)
: Component(params),
: ComponentAction(params),
PhysicsComponentHelper(params),
_species(getParam<std::vector<NonlinearVariableName>>("species")),
_scaling_factors(isParamValid("species_scaling_factors")
? getParam<std::vector<Real>>("species_scaling_factors")
: std::vector<Real>(_species.size(), 1)),
_ics(isParamValid("species_initial_pressures")
? getParam<std::vector<Real>>("species_initial_pressures")
: std::vector<Real>()),
_ics(getParam<std::vector<Real>>("species_initial_pressures")),
_temperature(getParam<Real>("temperature")),
_length_unit(getParam<Real>("length_unit")),
_pressure_unit(getParam<Real>("pressure_unit")),
_surface_area(getParam<Real>("surface_area")),
_volume(getParam<Real>("volume"))
_volume(getParam<Real>("volume")),
_outer_boundaries({getParam<BoundaryName>("boundary")})
{
for (auto & specie : _species)
specie += ("_" + name());

if (_species.size() != _scaling_factors.size())
paramError("species_scaling_factors",
"The number of species scaling factors must match the number of species.");

if (_ics.size() && (_ics.size() != _species.size()))
paramError("species_initial_pressures",
"The number of species partial pressures must match the number of species.");
if (_physics.empty())
paramError("physics", "A physics must be specified in the enclosure");
if (_physics.size() > 1)
paramError("physics",
"Enclosure0D has only been implemented for a single 'SpeciesTrappingPhysics'");
}

void
Enclosure0D::initComponentPhysics()
{
// Check the type of the Physics. This component is not implemented for all types
if (!physicsExists<SpeciesTrappingPhysics>(_physics_names[0]))
paramError("physics",
"Physics '" + _physics_names[0] +
"' not a 'SpeciesTrappingPhysics'. This component has only been implemented for "
"'SpeciesTrappingPhysics'.");

if (_verbose)
mooseInfoRepeated("Adding Physics '" + _physics[0]->name() + "'.");

// Transfer the data specified in the Component to the Physics
const auto stp = dynamic_cast<SpeciesTrappingPhysics *>(_physics[0]);
stp->addComponent(*this);
}
148 changes: 39 additions & 109 deletions src/components/Structure1D.C
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,34 @@

#include "Structure1D.h"
#include "TMAPUtils.h"
#include "MooseParsedFunction.h"
#include "THMMesh.h"
#include "DiffusionPhysicsBase.h"

#include "libmesh/unstructured_mesh.h"
#include "libmesh/boundary_info.h"
#include "libmesh/mesh_generation.h"

registerMooseObject("TMAPApp", Structure1D);
registerMooseAction("TMAP8App", Structure1D, "add_mesh_generator");

InputParameters
Structure1D::validParams()
{
auto params = Component::validParams();
auto params = ComponentAction::validParams();
params += PhysicsComponentHelper::validParams();
params += TMAP::structureCommonParams();
params.addRequiredParam<unsigned int>("nx", "The number of elements in the structure.");
params.addRequiredParam<Real>("xmax", "The maximum x-value.");
params.addRequiredParam<Real>("length_unit_scaling", "Scaling to apply on the mesh");
return params;
}

Structure1D::Structure1D(const InputParameters & params)
: Component(params),
FunctionInterface(this),
: ComponentAction(params),
PhysicsComponentHelper(params),
_species(getParam<std::vector<NonlinearVariableName>>("species")),
_scaling_factors(isParamValid("species_scaling_factors")
? getParam<std::vector<Real>>("species_scaling_factors")
: std::vector<Real>(_species.size(), 1)),
_ics(isParamValid("species_initial_concentrations")
? getParam<std::vector<Real>>("species_initial_concentrations")
: std::vector<Real>()),
_ics(getParam<std::vector<Real>>("species_initial_concentrations")),
_input_Ds(getParam<std::vector<FunctionName>>("diffusivities")),
_length_unit(getParam<Real>("length_unit"))
_length_unit(getParam<Real>("length_unit_scaling"))
{
_dimension = 1;
if (_species.size() != _scaling_factors.size())
paramError("species_scaling_factors",
"The number of species scaling factors must match the number of species.");
Expand All @@ -53,104 +49,38 @@ Structure1D::Structure1D(const InputParameters & params)
}

void
Structure1D::setupMesh()
Structure1D::initComponentPhysics()
{
auto & lm_mesh = static_cast<UnstructuredMesh &>(_mesh.getMesh());
MeshTools::Generation::build_line(
lm_mesh, getParam<unsigned int>("nx"), 0, getParam<Real>("xmax") * _length_unit, EDGE2);
auto & bi = lm_mesh.get_boundary_info();
bi.sideset_name(0) = name() + "_left";
bi.nodeset_name(0) = name() + "_left";
bi.sideset_name(1) = name() + "_right";
bi.nodeset_name(1) = name() + "_right";
// Check the type of the Physics. This component is not implemented for all types
if (!physicsExists<DiffusionPhysicsBase>(_physics_names[0]))
paramError("physics",
"Physics '" + _physics_names[0] +
"' not a 'SpeciesTrappingPhysics'. This component has only been implemented for "
"'SpeciesTrappingPhysics'.");

if (_verbose)
mooseInfoRepeated("Adding Physics '" + _physics[0]->name() + "'.");

// Transfer the data specified in the Component to the Physics
const auto stp = dynamic_cast<DiffusionPhysicsBase *>(_physics[0]);
stp->addComponent(*this);
}

void
Structure1D::addVariables()
Structure1D::addMeshGenerators()
{
for (const auto i : index_range(_species))
// Has another structure component already added this variable?
if (!_sim.hasVariable(_species[i]))
_sim.addSimVariable(true, _species[i], FEType(FIRST, LAGRANGE), _scaling_factors[i]);
}

void
Structure1D::addMooseObjects()
{
for (const auto i : index_range(_ics))
{
const std::string class_name = "ConstantIC";
auto params = _factory.getValidParams(class_name);
params.set<Real>("value") = _ics[i];
params.set<VariableName>("variable") = _species[i];
params.applyParameter(_pars, "block");
_sim.addInitialCondition(
class_name,
MooseUtils::join(std::vector<std::string>({_species[i], name(), "ic"}), "_"),
params);
}

std::vector<FunctionName> scaled_D_functions(_species.size());

// First create the functions that will be consumed by the material
for (const auto i : index_range(_species))
{
const auto & input_D = getFunctionByName(_input_Ds[i]);
if (!dynamic_cast<const MooseParsedFunction *>(&input_D))
paramError("equilibrium_constants", "All TMAP functions should be parsed functions");

const auto & input_D_params = input_D.parameters();
const auto & input_D_value = input_D_params.get<std::string>("value");
const std::string scaled_D_value =
std::to_string(_length_unit) + "^2 * (" + input_D_value + ")";

const std::string class_name = "ParsedFunction";
auto params = _factory.getValidParams(class_name);
params.set<std::string>("value") = scaled_D_value;
params.applyParameter(input_D_params, "vars");
params.applyParameter(input_D_params, "vals");
_sim.addFunction(class_name, _input_Ds[i] + "_scaled", params);
scaled_D_functions[i] = _input_Ds[i] + "_scaled";
}

// Next create the material that will be consumed by the diffusion kernels
{
const std::string class_name = "GenericFunctionMaterial";
auto params = _factory.getValidParams(class_name);
params.applyParameter(_pars, "block");
std::vector<std::string> prop_names(_species.size());
for (const auto i : index_range(_species))
prop_names[i] = "D_" + _species[i];
params.set<std::vector<std::string>>("prop_names") = prop_names;
params.set<std::vector<FunctionName>>("prop_values") = scaled_D_functions;
_sim.addMaterial(class_name, "diff_material_" + name(), params);
}

// Now create the kernels
for (const auto i : index_range(_species))
{
const auto & specie = _species[i];

{
const std::string class_name = "TimeDerivative";
auto params = _factory.getValidParams(class_name);
params.applyParameter(_pars, "block");
params.set<NonlinearVariableName>("variable") = specie;
_sim.addKernel(
class_name,
MooseUtils::join(std::vector<std::string>({specie, name(), "time_deriv"}), "_"),
params);
}

{
const std::string class_name = "MatDiffusion";
auto params = _factory.getValidParams(class_name);
params.applyParameter(_pars, "block");
params.set<NonlinearVariableName>("variable") = specie;
params.set<MaterialPropertyName>("diffusivity") = "D_" + specie;
_sim.addKernel(class_name,
MooseUtils::join(std::vector<std::string>({specie, name(), "diffusion"}), "_"),
params);
}
}
InputParameters params = _factory.getValidParams("GeneratedMeshGenerator");
params.set<MooseEnum>("dim") = _dimension;
params.set<Real>("xmax") = {getParam<Real>("xmax") * _length_unit};
params.set<unsigned int>("nx") = {getParam<unsigned int>("nx")};
params.set<std::string>("boundary_name_prefix") = name();
params.set<SubdomainName>("subdomain_name") = name();
_app.getMeshGeneratorSystem().addMeshGenerator(
"GeneratedMeshGenerator", name() + "_base", params);

// Keep track of the component mesh
_mg_name = name() + "_base";
_outer_boundaries.push_back(name() + "_left");
_outer_boundaries.push_back(name() + "_right");
_blocks.push_back(name());
}
File renamed without changes.

0 comments on commit 86e065e

Please sign in to comment.