Skip to content

Commit

Permalink
Merge pull request #150 from exasim-project/impl/dsl_interface
Browse files Browse the repository at this point in the history
Implementation of  DSL Equations, see #150
  • Loading branch information
greole authored Oct 8, 2024
2 parents 37b8763 + 9c8aa79 commit b182f06
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 401 deletions.
100 changes: 44 additions & 56 deletions include/NeoFOAM/DSL/eqnSystem.hpp → include/NeoFOAM/DSL/equation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@
namespace NeoFOAM::DSL
{

class EqnSystem
class Equation
{
public:

EqnSystem(const NeoFOAM::Executor& exec, std::size_t nCells)
Equation(const Executor& exec, std::size_t nCells)
: exec_(exec), nCells_(nCells), temporalOperators_(), implicitOperators_(),
explicitOperators_(), volumeField_(nullptr)
explicitOperators_()
{}

NeoFOAM::Field<NeoFOAM::scalar> explicitOperation()
/* @brief perform all explicit operation and accumulate the result */
Field<scalar> explicitOperation() const
{
Field<scalar> source(exec_, nCells_, 0.0);
return explicitOperation(source);
}

/* @brief perform all explicit operation and accumulate the result */
Field<scalar> explicitOperation(Field<scalar>& source) const
{
NeoFOAM::Field<NeoFOAM::scalar> source(exec_, nCells_);
NeoFOAM::fill(source, 0.0);
for (auto& Operator : explicitOperators_)
{
Operator.explicitOperation(source);
Expand All @@ -51,17 +57,17 @@ class EqnSystem
}
}

void addSystem(const EqnSystem& eqnSys)
void addEquation(const Equation& equation)
{
for (auto& Operator : eqnSys.temporalOperators_)
for (auto& Operator : equation.temporalOperators_)
{
temporalOperators_.push_back(Operator);
}
for (auto& Operator : eqnSys.implicitOperators_)
for (auto& Operator : equation.implicitOperators_)
{
implicitOperators_.push_back(Operator);
}
for (auto& Operator : eqnSys.explicitOperators_)
for (auto& Operator : equation.explicitOperators_)
{
explicitOperators_.push_back(Operator);
}
Expand All @@ -75,14 +81,17 @@ class EqnSystem
}
if (temporalOperators_.size() > 0)
{
NF_ERROR_EXIT("Not implemented.");
// integrate equations in time
}
else
{
NF_ERROR_EXIT("Not implemented.");
// solve sparse matrix system
}
}

/* @brief getter for the total number of terms in the equation */
size_t size() const
{
return temporalOperators_.size() + implicitOperators_.size() + explicitOperators_.size();
Expand All @@ -101,69 +110,50 @@ class EqnSystem

std::vector<Operator>& explicitOperators() { return explicitOperators_; }

const NeoFOAM::Executor& exec() const { return exec_; }
const Executor& exec() const { return exec_; }

const std::size_t nCells() const { return nCells_; }

scalar getDt() const { return dt_; }

fvcc::VolumeField<NeoFOAM::scalar>* volumeField()
{
if (temporalOperators_.size() == 0 && implicitOperators_.size() == 0)
{
NF_ERROR_EXIT("No temporal or implicit terms to solve.");
}
if (temporalOperators_.size() > 0)
{
// FIXME
NF_ERROR_EXIT("Not implemented.");
// volumeField_ = temporalOperators_[0].volumeField();
}
else
{
// FIXME
NF_ERROR_EXIT("Not implemented.");
// volumeField_ = implicitOperators_[0].volumeField();
}
return volumeField_;
}

NeoFOAM::scalar dt_ = 0;
scalar dt_ = 0;

private:

const NeoFOAM::Executor exec_;
const Executor exec_;

const std::size_t nCells_;

std::vector<Operator> temporalOperators_;

std::vector<Operator> implicitOperators_;

std::vector<Operator> explicitOperators_;
fvcc::VolumeField<NeoFOAM::scalar>* volumeField_;
};

EqnSystem operator+(EqnSystem lhs, const EqnSystem& rhs)
Equation operator+(Equation lhs, const Equation& rhs)
{
lhs.addSystem(rhs);
lhs.addEquation(rhs);
return lhs;
}

EqnSystem operator+(EqnSystem lhs, const Operator& rhs)
Equation operator+(Equation lhs, const Operator& rhs)
{
lhs.addOperator(rhs);
return lhs;
}

EqnSystem operator+(const Operator& lhs, const Operator& rhs)
Equation operator+(const Operator& lhs, const Operator& rhs)
{
NF_ERROR_EXIT("Not implemented.");
// EqnSystem eqnSys(lhs.exec(), lhs.nCells());
// eqnSys.addOperator(lhs);
// eqnSys.addOperator(rhs);
// return eqnSys;
Equation equation(lhs.exec(), lhs.getSize());
equation.addOperator(lhs);
equation.addOperator(rhs);
return equation;
}

EqnSystem operator*(NeoFOAM::scalar scale, const EqnSystem& es)
Equation operator*(scalar scale, const Equation& es)
{
EqnSystem results(es.exec(), es.nCells());
Equation results(es.exec(), es.nCells());
for (const auto& Operator : es.temporalOperators())
{
results.addOperator(scale * Operator);
Expand All @@ -179,26 +169,24 @@ EqnSystem operator*(NeoFOAM::scalar scale, const EqnSystem& es)
return results;
}

EqnSystem operator-(EqnSystem lhs, const EqnSystem& rhs)
Equation operator-(Equation lhs, const Equation& rhs)
{
lhs.addSystem(-1.0 * rhs);
lhs.addEquation(-1.0 * rhs);
return lhs;
}

EqnSystem operator-(EqnSystem lhs, const Operator& rhs)
Equation operator-(Equation lhs, const Operator& rhs)
{
lhs.addOperator(-1.0 * rhs);
return lhs;
}

EqnSystem operator-(const Operator& lhs, const Operator& rhs)
Equation operator-(const Operator& lhs, const Operator& rhs)
{
NF_ERROR_EXIT("Not implemented.");
// EqnSystem results(lhs.exec(), lhs.nCells());
// results.addOperator(lhs);
// results.addOperator(-1.0 * rhs);
// return results;
Equation equation(lhs.exec(), lhs.getSize());
equation.addOperator(lhs);
equation.addOperator(Coeff(-1) * rhs);
return equation;
}


} // namespace NeoFOAM::DSL
14 changes: 11 additions & 3 deletions include/NeoFOAM/DSL/operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Operator

Operator(Operator&& eqnOperator) : model_ {std::move(eqnOperator.model_)} {}

void explicitOperation(Field<scalar>& source) { model_->explicitOperation(source); }
void explicitOperation(Field<scalar>& source) const { model_->explicitOperation(source); }

void temporalOperation(Field<scalar>& field) { model_->temporalOperation(field); }

Expand All @@ -111,6 +111,9 @@ class Operator

Coeff getCoefficient() const { return model_->getCoefficient(); }

/* get the corresponding field size over which the operator operates */
size_t getSize() const { return model_->getSize(); }

/* @brief Given an input this function reads required coeffs */
void build(const Input& input) { model_->build(input); }

Expand All @@ -127,7 +130,7 @@ class Operator
{
virtual ~OperatorConcept() = default;

virtual void explicitOperation(Field<scalar>& source) = 0;
virtual void explicitOperation(Field<scalar>& source) const = 0;

virtual void temporalOperation(Field<scalar>& field) = 0;

Expand All @@ -140,6 +143,8 @@ class Operator
/* returns the fundamental type of an operator, ie explicit, implicit, temporal */
virtual Operator::Type getType() const = 0;

virtual size_t getSize() const = 0;

/* @brief get the associated coefficient for this term */
virtual Coeff& getCoefficient() = 0;

Expand All @@ -163,7 +168,7 @@ class Operator
/* returns the name of the operator */
std::string getName() const override { return concreteOp_.getName(); }

virtual void explicitOperation(Field<scalar>& source) override
virtual void explicitOperation(Field<scalar>& source) const override
{
if constexpr (HasExplicitOperator<ConcreteOperatorType>)
{
Expand Down Expand Up @@ -194,6 +199,9 @@ class Operator
/* @brief get the associated coefficient for this term */
virtual Coeff getCoefficient() const override { return concreteOp_.getCoefficient(); }

/* @brief get the associated coefficient for this term */
virtual size_t getSize() const override { return concreteOp_.getSize(); }

// The Prototype Design Pattern
std::unique_ptr<OperatorConcept> clone() const override
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ForwardEuler : public TimeIntegrationFactory::Register<ForwardEuler>

public:

ForwardEuler(const dsl::EqnSystem& eqnSystem, const Dictionary& dict);
ForwardEuler(const DSL::Equation& eqnSystem, const Dictionary& dict);

static std::string name() { return "forwardEuler"; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "NeoFOAM/finiteVolume/cellCentred.hpp"

#include "NeoFOAM/DSL/operator.hpp"
#include "NeoFOAM/DSL/eqnSystem.hpp"
#include "NeoFOAM/DSL/equation.hpp"

#include <functional>

Expand All @@ -21,14 +21,14 @@ namespace NeoFOAM::finiteVolume::cellCentred
class TimeIntegrationFactory :
public NeoFOAM::RuntimeSelectionFactory<
TimeIntegrationFactory,
Parameters<const dsl::EqnSystem&, const Dictionary&>>
Parameters<const dsl::Equation&, const Dictionary&>>
{

public:

static std::string name() { return "timeIntegrationFactory"; }

TimeIntegrationFactory(const dsl::EqnSystem& eqnSystem, const Dictionary& dict)
TimeIntegrationFactory(const dsl::Equation& eqnSystem, const Dictionary& dict)
: eqnSystem_(eqnSystem), dict_(dict)
{}

Expand All @@ -41,7 +41,8 @@ class TimeIntegrationFactory :

protected:

dsl::EqnSystem eqnSystem_;
DSL::Equation eqnSystem_;

const Dictionary& dict_;
};

Expand All @@ -56,7 +57,7 @@ class TimeIntegration
TimeIntegration(TimeIntegration&& timeIntegrate)
: timeIntegrateStrategy_(std::move(timeIntegrate.timeIntegrateStrategy_)) {};

TimeIntegration(const dsl::EqnSystem& eqnSystem, const Dictionary& dict)
TimeIntegration(const DSL::Equation& eqnSystem, const Dictionary& dict)
: timeIntegrateStrategy_(
TimeIntegrationFactory::create(dict.get<std::string>("type"), eqnSystem, dict)
) {};
Expand Down
8 changes: 4 additions & 4 deletions src/finiteVolume/cellCentred/timeIntegration/forwardEuler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace NeoFOAM::finiteVolume::cellCentred
{


ForwardEuler::ForwardEuler(const dsl::EqnSystem& eqnSystem, const Dictionary& dict)
ForwardEuler::ForwardEuler(const dsl::Equation& eqnSystem, const Dictionary& dict)
: TimeIntegrationFactory::Register<ForwardEuler>(eqnSystem, dict)
{
// Constructor
Expand All @@ -21,7 +21,7 @@ void ForwardEuler::solve()
{
std::cout << "Solving using Forward Euler" << std::endl;
scalar dt = eqnSystem_.getDt();
fvcc::VolumeField<scalar>* refField = eqnSystem_.volumeField();
// fvcc::VolumeField<scalar>* refField = eqnSystem_.volumeField();
// Field<scalar> Phi(eqnSystem_.exec(), eqnSystem_.nCells());
// NeoFOAM::fill(Phi, 0.0);
Field<scalar> source = eqnSystem_.explicitOperation();
Expand All @@ -31,8 +31,8 @@ void ForwardEuler::solve()
// eqnTerm.temporalOperation(Phi);
// }
// Phi += source*dt;
refField->internalField() -= source * dt;
refField->correctBoundaryConditions();
// refField->internalField() -= source * dt;
// refField->correctBoundaryConditions();

// check if execturo is GPU
if (std::holds_alternative<NeoFOAM::GPUExecutor>(eqnSystem_.exec()))
Expand Down
3 changes: 1 addition & 2 deletions test/dsl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@

neofoam_unit_test(coeff)
neofoam_unit_test(operator)

# FIXME neofoam_unit_test(dsl)
neofoam_unit_test(equation)
Loading

0 comments on commit b182f06

Please sign in to comment.