Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implicit BCs and Laplacian operator #262

Open
wants to merge 20 commits into
base: stack/implicitOperators
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a529edb
Fix operator & return a scalar instead of the vector
HenningScheufler Feb 15, 2025
b6d159f
geometricScheme compute delta and nonOrthoDelta
HenningScheufler Feb 15, 2025
602d5e6
ENH: added facenormalgradient
HenningScheufler Feb 15, 2025
45d7786
WIP added LaplacianOperator
HenningScheufler Feb 15, 2025
e698a60
FIX: 1DMesh face ordering
HenningScheufler Feb 15, 2025
a437e47
add next functionality to be able to consume the tokens
HenningScheufler Feb 15, 2025
7ad1ed2
operators consume tokens similar to a stream
HenningScheufler Feb 15, 2025
1bd7e16
fix: faceNormalGradient
HenningScheufler Feb 15, 2025
7f2259f
implementLaplacian
HenningScheufler Feb 15, 2025
8697b27
laplacian uses fixedValue BCs
HenningScheufler Feb 16, 2025
8b43753
added deltaCoeffs to surfaceNormalGradient
HenningScheufler Feb 16, 2025
0c134bd
FIX: valueFraction return the wrong type
HenningScheufler Feb 16, 2025
dda140d
BC are handled as mixed BC as default to avoid the dispatch per BC
HenningScheufler Feb 16, 2025
fd19c94
Merge branch 'stack/implicitOperators' into feat/implicitBCs
HenningScheufler Feb 16, 2025
8b1a5f5
added implicit Laplacian operator
HenningScheufler Feb 16, 2025
d05ac35
added default constructors and outOfRange check
HenningScheufler Feb 16, 2025
3928a2b
FIX: laplacian operator check of div
HenningScheufler Feb 16, 2025
e7e1154
Fix: scaling of operators sign inside the dsl was not accounted for
HenningScheufler Feb 22, 2025
1abf469
FIX: solverdict always stores doubles
HenningScheufler Feb 22, 2025
2f0eca4
FIX: solutionDict in backwardEuler no longer hardcoded
HenningScheufler Feb 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/NeoFOAM/core/primitives/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ Vector operator*(const scalar& sclr, Vector rhs)
}

KOKKOS_INLINE_FUNCTION
Vector operator&(const Vector& lhs, Vector rhs)
scalar operator&(const Vector& lhs, Vector rhs)
{
return Vector(rhs[0] * lhs[0], rhs[1] * lhs[1], rhs[2] * lhs[2]);
return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
}

KOKKOS_INLINE_FUNCTION
Expand Down
51 changes: 49 additions & 2 deletions include/NeoFOAM/core/tokenList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace NeoFOAM
{


void logOutRange(
const std::out_of_range& e, const std::size_t& key, const std::vector<std::any>& data
);


/**
* @class TokenList
* @brief A class representing a list of tokens.
Expand All @@ -22,13 +27,15 @@ class TokenList
{
public:

TokenList() = default;
TokenList();

TokenList(const TokenList&);

/**
* @brief Construct a TokenList object from a vector of std::any.
* @param data A vector of std::any.
*/
TokenList(const std::vector<std::any>& data);
TokenList(const std::vector<std::any>& data, size_t nextIndex = 0);

/**
* @brief Construct a TokenList object from an initializer list of std::any.
Expand Down Expand Up @@ -94,8 +101,27 @@ class TokenList
logBadAnyCast<ReturnType>(e, idx, data_);
throw e;
}
catch (const std::out_of_range& e)
{
logOutRange(e, idx, data_);
throw e;
}
}

/**
* @brief Retrieves the value associated with the nextIndex, casting it to
* the specified type.
* @tparam T The type to cast the value to.
* @return A reference to the value associated with the index, casted to
* type T.
*/
template<typename ReturnType>
ReturnType& next()
{
ReturnType& retValue = get<ReturnType&>(nextIndex_);
nextIndex_++;
return retValue;
}

/**
* @brief Retrieves the value associated with the given index, casting it to
Expand All @@ -117,6 +143,26 @@ class TokenList
logBadAnyCast<ReturnType>(e, idx, data_);
throw e;
}
catch (const std::out_of_range& e)
{
logOutRange(e, idx, data_);
throw e;
}
}

/**
* @brief Retrieves the value associated with the nextIndex, casting it to
* the specified type.
* @tparam T The type to cast the value to.
* @return A const reference to the value associated with the index, casted to
* type T.
*/
template<typename ReturnType>
const ReturnType& next() const
{
const ReturnType& retValue = get<ReturnType>(nextIndex_);
nextIndex_++;
return retValue;
}

/**
Expand All @@ -132,6 +178,7 @@ class TokenList
private:

std::vector<std::any> data_;
mutable size_t nextIndex_;
};

} // namespace NeoFOAM
9 changes: 9 additions & 0 deletions include/NeoFOAM/dsl/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ template<typename leftOperator, typename rightOperator>
return lhs;
}

template<typename leftOperator, typename rightOperator>
[[nodiscard]] inline Expression operator-(leftOperator lhs, rightOperator rhs)
{
Expression expr(lhs.exec());
expr.addOperator(lhs);
expr.addOperator(Coeff(-1) * rhs);
return expr;
}

[[nodiscard]] inline Expression operator-(const SpatialOperator& lhs, const SpatialOperator& rhs)
{
Expression expr(lhs.exec());
Expand Down
6 changes: 6 additions & 0 deletions include/NeoFOAM/dsl/implicit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ div(fvcc::SurfaceField<NeoFOAM::scalar>& faceFlux, fvcc::VolumeField<NeoFOAM::sc
return SpatialOperator(fvcc::DivOperator(dsl::Operator::Type::Implicit, faceFlux, phi));
}

SpatialOperator
laplacian(fvcc::SurfaceField<NeoFOAM::scalar>& gamma, fvcc::VolumeField<NeoFOAM::scalar>& phi)
{
return SpatialOperator(fvcc::LaplacianOperator(dsl::Operator::Type::Implicit, gamma, phi));
}

} // namespace NeoFOAM
45 changes: 45 additions & 0 deletions include/NeoFOAM/dsl/operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,49 @@ class Operator
};
};


/* @class OperatorMixin
* @brief A mixin class to simplify implementations of concrete operators
* in NeoFOAMs dsl
*
* @ingroup dsl
*/
template<typename FieldType>
class OperatorMixin
{

public:

OperatorMixin(const Executor exec, const Coeff& coeffs, FieldType& field, Operator::Type type)
: exec_(exec), coeffs_(coeffs), field_(field), type_(type) {};


Operator::Type getType() const { return type_; }

virtual ~OperatorMixin() = default;

virtual const Executor& exec() const final { return exec_; }

Coeff& getCoefficient() { return coeffs_; }

const Coeff& getCoefficient() const { return coeffs_; }

FieldType& getField() { return field_; }

const FieldType& getField() const { return field_; }

/* @brief Given an input this function reads required coeffs */
void build([[maybe_unused]] const Input& input) {}

protected:

const Executor exec_; //!< Executor associated with the field. (CPU, GPU, openMP, etc.)

Coeff coeffs_;

FieldType& field_;

Operator::Type type_;
};

} // namespace NeoFOAM::dsl
44 changes: 2 additions & 42 deletions include/NeoFOAM/dsl/spatialOperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class SpatialOperator

SpatialOperator(SpatialOperator&& eqnOperator);

SpatialOperator& operator=(const SpatialOperator& eqnOperator);

void explicitOperation(Field<scalar>& source);

void implicitOperation(la::LinearSystem<scalar, localIdx>& ls);
Expand Down Expand Up @@ -213,46 +215,4 @@ SpatialOperator operator*([[maybe_unused]] CoeffFunction coeffFunc, const Spatia
return result;
}

/* @class OperatorMixin
* @brief A mixin class to simplify implementations of concrete operators
* in NeoFOAMs dsl
*
* @ingroup dsl
*/
template<typename FieldType>
class OperatorMixin
{

public:

OperatorMixin(const Executor exec, FieldType& field, Operator::Type type)
: exec_(exec), coeffs_(), field_(field), type_(type) {};

Operator::Type getType() const { return type_; }

virtual ~OperatorMixin() = default;

virtual const Executor& exec() const final { return exec_; }

Coeff& getCoefficient() { return coeffs_; }

const Coeff& getCoefficient() const { return coeffs_; }

FieldType& getField() { return field_; }

const FieldType& getField() const { return field_; }

/* @brief Given an input this function reads required coeffs */
void build([[maybe_unused]] const Input& input) {}

protected:

const Executor exec_; //!< Executor associated with the field. (CPU, GPU, openMP, etc.)

Coeff coeffs_;

FieldType& field_;

Operator::Type type_;
};
} // namespace NeoFOAM::dsl
2 changes: 1 addition & 1 deletion include/NeoFOAM/fields/boundaryFields.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class BoundaryFields
* @brief Get the view storing the fraction of the boundary value.
* @return The view storing the fraction of the boundary value.
*/
NeoFOAM::Field<scalar>& valueFraction() { return refValue_; }
NeoFOAM::Field<scalar>& valueFraction() { return valueFraction_; }

/** @copydoc BoundaryFields::refGrad()*/
const NeoFOAM::Field<T>& refGrad() const { return refGrad_; }
Expand Down
8 changes: 8 additions & 0 deletions include/NeoFOAM/finiteVolume/cellCentred.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
#include "cellCentred/fields/surfaceField.hpp"
#include "cellCentred/fields/volumeField.hpp"

#include "cellCentred/operators/linearSystem.hpp"
#include "cellCentred/operators/divOperator.hpp"
#include "cellCentred/operators/gaussGreenDiv.hpp"

#include "cellCentred/operators/ddtOperator.hpp"

#include "cellCentred/operators/sourceTerm.hpp"

#include "cellCentred/operators/laplacianOperator.hpp"
#include "cellCentred/operators/gaussGreenLaplacian.hpp"

#include "cellCentred/interpolation/linear.hpp"
#include "cellCentred/interpolation/upwind.hpp"

#include "cellCentred/faceNormalGradient/uncorrected.hpp"
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ void setGradientValue(
)
{
const auto iField = domainField.internalField().span();
auto refGradient = domainField.boundaryField().refGrad().span();
auto value = domainField.boundaryField().value().span();
auto faceCells = mesh.boundaryMesh().faceCells(static_cast<localIdx>(patchID));
auto deltaCoeffs = mesh.boundaryMesh().deltaCoeffs(static_cast<localIdx>(patchID));

auto [refGradient, value, valueFraction, refValue] = spans(
domainField.boundaryField().refGrad(),
domainField.boundaryField().value(),
domainField.boundaryField().valueFraction(),
domainField.boundaryField().refValue()
);

auto faceCells = mesh.boundaryMesh().faceCells();
auto deltaCoeffs = mesh.boundaryMesh().deltaCoeffs();

NeoFOAM::parallelFor(
domainField.exec(),
Expand All @@ -39,6 +45,8 @@ void setGradientValue(
// operator / is not defined for all ValueTypes
value[i] =
iField[static_cast<size_t>(faceCells[i])] + fixedGradient * (1 / deltaCoeffs[i]);
valueFraction[i] = 0.0; // only used refGrad
refValue[i] = fixedGradient; // not used
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ void setFixedValue(
DomainField<ValueType>& domainField, std::pair<size_t, size_t> range, ValueType fixedValue
)
{
auto refValue = domainField.boundaryField().refValue().span();
auto value = domainField.boundaryField().value().span();
auto [refGradient, value, valueFraction, refValue] = spans(
domainField.boundaryField().refGrad(),
domainField.boundaryField().value(),
domainField.boundaryField().valueFraction(),
domainField.boundaryField().refValue()
);

NeoFOAM::parallelFor(
domainField.exec(),
range,
KOKKOS_LAMBDA(const size_t i) {
refValue[i] = fixedValue;
value[i] = fixedValue;
valueFraction[i] = 1.0; // only used refValue
refGradient[i] = fixedValue; // not used
}
);
}
Expand Down
Loading
Loading