Skip to content

Commit

Permalink
Merge pull request #16 from aurora-multiphysics/hsaunders1904/flow_ov…
Browse files Browse the repository at this point in the history
…er_heated_plate

Implement flow over heated plate example/test
  • Loading branch information
hsaunders1904 authored Oct 11, 2024
2 parents 15d69aa + 997d904 commit b774e58
Show file tree
Hide file tree
Showing 153 changed files with 38,262 additions and 27,003 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ FLUID_PROPERTIES := no
FSI := no
FUNCTIONAL_EXPANSION_TOOLS := no
GEOCHEMISTRY := no
HEAT_CONDUCTION := no
HEAT_CONDUCTION := yes
LEVEL_SET := no
MISC := no
NAVIER_STOKES := no
Expand Down
33 changes: 33 additions & 0 deletions include/auxkernels/HeatFluxAux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <AuxKernel.h>
#include <MooseTypes.h>

/**
* Auxiliary kernel to calculate the heat flux at the given boundaries.
*/
class HeatFluxAux : public AuxKernel
{
public:
HeatFluxAux(const InputParameters & params);

static InputParameters validParams();

/**
* Compute the heat flux at the given quadrature point.
*
* Computes q = -s * k * ∇T, where 's' is a scale factor, k is the thermal conductivity, and T
* is temperature.
*
* Note that we are assuming the thermal conductivity is equal at every quadrature point on the
* given boundary.
*/
Real computeValue();

private:
VariableGradient const & _grad;
Real const _thermal_conductivity{0.0};
MooseArray<Point> const & _normals;
unsigned const _dim{0};
Real const _scale{1.0};
};
26 changes: 22 additions & 4 deletions include/base/FoamProblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#include "FoamInterface.h"
#include "buoyantFoamApp.h"

#include <libmesh/elem.h>
#include <ExternalProblem.h>
#include <MooseTypes.h>
#include <MooseVariableFieldBase.h>
#include <SystemBase.h>

/* Base class for FoamProblems */

Expand All @@ -30,6 +33,7 @@ class FoamProblem : public ExternalProblem
protected:
FoamMesh * _foam_mesh = nullptr;
Hippo::FoamInterface * _interface = nullptr;
std::string _backup_foam_timestep_dir;
};

/* Specific class to run buoyantFoam problems */
Expand All @@ -45,9 +49,23 @@ class BuoyantFoamProblem : public FoamProblem
virtual void addExternalVariables();

protected:
// TODO(hsaunders1904): can we generalise `_app` so we don't need to implement the transfer
// for each 'FoamProblem' we implement?
Hippo::buoyantFoamApp _app;
};

// Local Variables:
// mode: c++
// End:
private:
enum class SyncVariables
{
WallTemperature,
WallHeatFlux,
Both
};

template <SyncVariables sync_vars>
void syncFromOpenFoam();
template <SyncVariables sync_vars>
void syncToOpenFoam();
MooseVariableFieldBase *
getConstantMonomialVariableFromParameters(const std::string & variable_name);
Real variableValueAtElement(const libMesh::Elem * element, MooseVariableFieldBase * variable);
};
28 changes: 26 additions & 2 deletions include/base/buoyantFoamApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,35 @@ class buoyantFoamApp
~buoyantFoamApp();
buoyantFoamApp(FoamInterface * _interface);
bool run();
// Append the patch's face temperatures to the vector T.
// Return the number of items added.
/**
* Append the patch's face temperatures to the vector T.
*
* Return the number of items added.
*/
size_t append_patch_face_T(int patch_id, std::vector<double> & T);

size_t patch_size(int patch_id);

/**
* Apply the temperatures as a fixedValue boundary condition on the patch.
*
* @param patch_id
* @param moose_T
*/
void set_patch_face_t(int patch_id, const std::vector<double> & moose_T);

/**
* Apply the negative heat flux as a fixedGradient boundary condition to the given patch.
*
* Note that typically this heat flux will be coming from a boundary that interfaces the given
* OpenFOAM boundary; this heat flux will hence be in the opposite direction in OpenFOAM's
* coordinate system, meaning we should take the negative. By Fourier's law (q = -k.∇T), to
* convert heat flux to temperature gradient we need to take the negative of the heat flux again.
* Therefore it is convenient to provide an interface to set the *negative* heat flux, to avoid
* applying a minus sign twice.
*/
void set_patch_face_negative_heat_flux(int patch_id, const std::vector<double> & negative_hf);

std::unique_ptr<buoyantFoamImpl> _impl;
};
}
51 changes: 51 additions & 0 deletions src/auxkernels/HeatFluxAux.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "HeatFluxAux.h"

#include <Assembly.h>
#include <MooseError.h>

registerMooseObject("hippoApp", HeatFluxAux);

namespace
{
inline Real
dot_product(const int n_dimensions, const Point & X, const Point & Y)
{
Real tot = 0.0;
for (int i = 0; i < n_dimensions; ++i)
{
tot += X(i) * Y(i);
}
return tot;
}
}

InputParameters
HeatFluxAux::validParams()
{
InputParameters params = AuxKernel::validParams();
params.addRequiredCoupledVar("T", "Temperature field");
params.addRequiredParam<Real>("thermal_conductivity", "Thermal Conductivity of material");
params.addParam<Real>("scale", 1.0, "Scaling factor for heat flux");
// Heat flux can only be calculated on a boundary.
params.makeParamRequired<std::vector<BoundaryName>>("boundary");
return params;
}

HeatFluxAux::HeatFluxAux(const InputParameters & params)
: AuxKernel(params),
_grad{coupledGradient("T")},
_thermal_conductivity{getParam<Real>("thermal_conductivity")},
_normals{_assembly.normals()},
_dim{_mesh.dimension()},
_scale{getParam<Real>("scale")}
{
mooseAssert(0 < _dim && _dim <= 3, "Dimension should be 1, 2 or 3");
}

Real
HeatFluxAux::computeValue()
{
mooseAssert(_qp < _grad.size(), "Cannot index _grad with _qp");
mooseAssert(_qp < _normals.size(), "Cannot index _normals with _qp");
return -dot_product(_dim, _grad[_qp], _normals[_qp]) * _thermal_conductivity * _scale;
}
26 changes: 8 additions & 18 deletions src/base/FoamInterface.C
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,17 @@ FoamInterface::getWallHeatFlux(std::vector<double> & fill_vector, const Foam::la
{
static const Foam::word WALL_HEAT_FLUX = "wallHeatFlux";

// Calculate the wall heat flux for the given patch.
auto & impl = getImpl();

// TODO:
// Need to work out if it's necessary to recalculate this every time.
// We need to recalculate for every time step, so it doesn't work to check the mesh for
// registered objects and only recalculate in those cases - we just end up with the values
// from the first time step.
// One option is to return the boundary field and let the caller index the patch. This API would
// allow us to only calculate the flux on a specific boundary, though. If I can work out how to
// do that.

// We may be able to constrain the patches that the flux is calculated for, by defining
// a Foam::dictionary that defines a `wordlist` of patch names.
// https://www.openfoam.com/documentation/guides/latest/doc/guide-fos-field-wallHeatFlux.html#sec-fos-field-wallHeatFlux-usage
// E.g.:
// auto dict = impl->_runtime.controlDict();
// dict["functions"]["wallHeatFlux"].push_back("fluid_bottom");
Foam::functionObjects::wallHeatFlux wall_flux(
WALL_HEAT_FLUX, impl->getRuntime(), impl->_runtime.controlDict());
auto dict = impl->_runtime.controlDict();
auto whf_dict = impl->_runtime.controlDict().lookupOrDefault(WALL_HEAT_FLUX, Foam::dictionary());
auto patch_name = getMesh().boundaryMesh()[patch_id].name();
whf_dict.set("patches", Foam::wordList({patch_name}));
whf_dict.set("writeToFile", false);
Foam::functionObjects::wallHeatFlux wall_flux(WALL_HEAT_FLUX, impl->getRuntime(), whf_dict);
wall_flux.execute();

// Fetch the heat flux and copy it into the given vector.
auto patch = impl->getPatch(patch_id);
auto wall_heat_flux = impl->_mesh.lookupObject<Foam::volScalarField>(WALL_HEAT_FLUX);
auto & hf_bf = wall_heat_flux.boundaryField();
Expand Down
Loading

0 comments on commit b774e58

Please sign in to comment.