21#ifdef NF_WITH_MPI_SUPPORT
31template<
typename VectorType,
typename IndexType>
65template<
typename ValueType,
typename IndexType = localIdx>
75#ifdef NF_WITH_MPI_SUPPORT
78 if (!ls.commPattern().sendCounts.empty())
80 mpi::Environment mpiEnv;
81 if (mpiEnv.isInitialized() && mpiEnv.rank() != 0)
return;
84 auto lsView = ls.view();
85 const auto ma = ls.faceToMatrixAddress()->view(ls.matrix().sparsity()->rowOffs().view());
86 auto refVal = refValue_;
87 auto refCell = refCell_;
90 {refCell, refCell + 1},
92 auto dIdx = ma.diagIdx(celli);
93 auto diagVal = lsView.matrix.values[dIdx];
94 lsView.rhs[celli] += diagVal * refVal;
95 lsView.matrix.values[dIdx] += diagVal;
110#ifdef NF_WITH_MPI_SUPPORT
113 if (!ls.commPattern().sendCounts.empty())
115 mpi::Environment mpiEnv;
116 if (mpiEnv.isInitialized() && mpiEnv.rank() != 0)
return;
119 auto lsView = ls.view();
120 const auto ma = ls.faceToMatrixAddress()->view(ls.matrix().sparsity()->rowOffs().view());
121 auto refVal = refValue_;
122 auto refCell = refCell_;
125 {refCell, refCell + 1},
127 auto dIdx = ma.diagIdx(celli);
128 auto diagVal = lsView.matrix.values[dIdx];
129 lsView.rhs[celli] += diagVal * refVal;
130 lsView.matrix.values[dIdx] += diagVal;
173template<
typename ValueType,
typename IndexType = localIdx>
177 std::is_same_v<ValueType, scalar>,
178 "FixedValueConstraints only supports scalar fields. "
179 "For non-scalar fields implement applyScalarMatrix()."
185 : mask_(mask), values_(values), nCells_(nCells)
191 auto lsView = ls.view();
192 const auto rowOffs = ls.matrix().sparsity()->rowOffs().view();
193 const auto colIdxs = ls.matrix().sparsity()->colIdxs().view();
194 auto matrixValues = lsView.matrix.values;
195 auto rhs = lsView.rhs;
206 const bool rowPinned = mask[row] !=
scalar(0);
208 for (
auto o = rowOffs[row]; o < rowOffs[row + 1]; ++o)
210 const auto col = colIdxs[o];
213 diagVal = matrixValues[o];
221 else if (mask[col] !=
scalar(0))
225 rhs[row] -= matrixValues[o] * vals[col];
232 rhs[row] = diagVal * vals[row];
235 "FixedValueConstraints"
239 auto& offDiag = ls.offDiagonalMatrix();
240 const localIdx nnz = offDiag.nNonZeros();
243 const auto offRowIdxs = offDiag.sparsity()->rowIdxs().view();
244 auto offValues = offDiag.values().view();
251 "FixedValueConstraints::offDiag"
264template<
typename ValueType,
typename IndexType = localIdx>
274 : exec_(
exp.exec_), temporalOperators_(
exp.temporalOperators_),
275 spatialOperators_(
exp.spatialOperators_)
279 : exec_(oper.
exec()), temporalOperators_(), spatialOperators_()
281 spatialOperators_.push_back(oper);
290 NF_ASSERT(exec_ ==
exp.exec_,
"Executors are not the same");
291 temporalOperators_ =
exp.temporalOperators_;
292 spatialOperators_ =
exp.spatialOperators_;
298 : exec_(oper.
exec()), temporalOperators_(), spatialOperators_()
300 temporalOperators_.push_back(oper);
306 for (
auto& op : temporalOperators_)
310 for (
auto& op : spatialOperators_)
322 accumulateExplicitOperation(source);
329 accumulateExplicitOperation(source);
335 for (
auto& op : temporalOperators_)
339 op.explicitOperation(source, t, dt);
346 template<
typename AssemblyType = ValueType>
349 for (
auto& op : spatialOperators_)
353 op.implicitOperation(ls);
361 template<
typename AssemblyType = ValueType>
366 for (
auto& op : temporalOperators_)
370 op.implicitOperation(ls, t, dt);
376 template<
typename AssemblyType = ValueType>
385 {0, static_cast<localIdx>(rhs.size())},
395 template<
typename AssemblyType = ValueType>
412 template<
typename AssemblyType = ValueType>
429 template<
typename AssemblyType = ValueType>
442 if constexpr (std::is_same_v<AssemblyType, ValueType>)
444 for (
const auto* p : ps)
449 else if constexpr (std::is_same_v<AssemblyType, scalar>)
451 for (
const auto* p : ps)
453 p->applyScalarMatrix(ls);
462 temporalOperators_.push_back(oper);
467 for (
auto& op : equation.temporalOperators_)
469 temporalOperators_.push_back(op);
471 for (
auto& op : equation.spatialOperators_)
473 spatialOperators_.push_back(op);
478 template<
typename OperatorType, Operator::Type Type>
482 auto matchNameAndType = [
name, opType](
const auto& op)
483 {
return op.getName() ==
name && op.getType() == opType; };
484 if constexpr (std::is_same_v<OperatorType, SpatialOperator<ValueType>>)
486 return std::ranges::any_of(spatialOperators_, matchNameAndType);
488 else if constexpr (std::is_same_v<OperatorType, TemporalOperator<ValueType>>)
490 return std::ranges::any_of(temporalOperators_, matchNameAndType);
496 template<Operator::Type Type>
504 template<
typename OperatorType, Operator::Type Type>
509 throw std::runtime_error {
"No operator with given name and type found"};
512 auto matchNameAndType = [
name, opType](
const auto& op)
513 {
return op.getName() ==
name && op.getType() == opType; };
514 if constexpr (std::is_same_v<OperatorType, SpatialOperator<ValueType>>)
516 return *std::ranges::find_if(spatialOperators_, matchNameAndType);
518 else if constexpr (std::is_same_v<OperatorType, TemporalOperator<ValueType>>)
520 return *std::ranges::find_if(temporalOperators_, matchNameAndType);
522 throw std::runtime_error {
"Unknown operator type"};
524 return spatialOperators_[0];
528 template<Operator::Type Type>
533 throw std::runtime_error {
"No operator with given name and type found"};
536 auto matchNameAndType = [
name, opType](
const auto& op)
537 {
return op.getName() ==
name && op.getType() == opType; };
540 std::erase_if(spatialOperators_, matchNameAndType);
544 std::erase_if(temporalOperators_, matchNameAndType);
551 return static_cast<localIdx>(temporalOperators_.size() + spatialOperators_.size());
557 return temporalOperators_;
562 return spatialOperators_;
576 for (
auto& op : spatialOperators_)
580 op.explicitOperation(source);
587 std::vector<TemporalOperator<ValueType>> temporalOperators_;
589 std::vector<SpatialOperator<ValueType>> spatialOperators_;
592template<
typename ValueType>
600template<
typename ValueType>
601[[nodiscard]]
inline Expression<ValueType>
608template<
typename leftOperator,
typename rightOperator>
609[[nodiscard]]
inline Expression<typename leftOperator::VectorValueType>
612 using ValueType =
typename leftOperator::VectorValueType;
619template<
typename ValueType>
635template<
typename ValueType>
636[[nodiscard]]
inline Expression<ValueType>
643template<
typename ValueType>
644[[nodiscard]]
inline Expression<ValueType>
651template<
typename leftOperator,
typename rightOperator>
652[[nodiscard]]
inline Expression<typename leftOperator::VectorValueType>
655 using ValueType =
typename leftOperator::VectorValueType;
A class representing a dictionary that stores key-value pairs.
Represents an unstructured mesh in NeoN.
const scalarVector & cellVolumes() const
Get the field of cell volumes in the mesh.
sizeType nCells() const
Get the number of cells in the mesh.
An executor-aware contiguous container for numerical data with vector-style arithmetic semantics.
A class that represents a coefficient for the NeoN dsl.
Expression & operator=(const Expression &exp)
void assemble(scalar t, scalar dt, la::LinearSystem< AssemblyType, ValueType > &ls, std::vector< const PostAssemblyBase< ValueType, IndexType > * > ps={}) const
la::LinearSystem< AssemblyType, ValueType > assemble(const UnstructuredMesh &mesh, scalar t, scalar dt, std::vector< const PostAssemblyBase< ValueType, IndexType > * > ps={}) const
construct a linear system and force assembly including explicit source terms
std::vector< TemporalOperator< ValueType > > & temporalOperators()
Expression(const SpatialOperator< ValueType > &oper)
void assembleTemporalOperator(la::LinearSystem< AssemblyType, ValueType > &ls, scalar t, scalar dt) const
compute matrix coefficients based on all temporal operators assemble directly into linear system
Vector< ValueType > explicitOperation(Vector< ValueType > &source) const
Expression(const TemporalOperator< ValueType > &oper)
std::vector< SpatialOperator< ValueType > > & spatialOperators()
Expression(const Expression &exp)
bool hasOperatorOfType(const std::string &name) const
returns operator of given type and name exists
const std::vector< TemporalOperator< ValueType > > & temporalOperators() const
void dropOperator(const std::string &name)
removes operator of given name
void assembleSpatialOperator(la::LinearSystem< AssemblyType, ValueType > &ls) const
compute matrix coefficients based on all spatial operators
void read(const Dictionary &input)
void addOperator(const TemporalOperator< ValueType > &oper)
void assemble(scalar t, scalar dt, la::LinearSystem< AssemblyType, ValueType > &ls, const UnstructuredMesh &mesh, std::vector< const PostAssemblyBase< ValueType, IndexType > * > ps={}) const
assemble into a given linear system including explicit source terms
OperatorType & getOperator(const std::string &name)
returns operator of given type and name
bool hasOperator(const std::string &name) const
returns whether the expression contains an operator with a given name
void addExpression(const Expression &equation)
void assembleExplicitSource(la::LinearSystem< AssemblyType, ValueType > &ls, const UnstructuredMesh &mesh) const
Vector< ValueType > explicitOperation(Vector< ValueType > &source, scalar t, scalar dt) const
void addOperator(const SpatialOperator< ValueType > &oper)
const Executor & exec() const
const std::vector< SpatialOperator< ValueType > > & spatialOperators() const
Vector< ValueType > explicitOperation(sizeType nCells) const
ValueType ExpressionValueType
Expression(const Executor &exec)
void operator()(la::LinearSystem< ValueType, ValueType, la::CSRMatrix< ValueType, IndexType > > &ls) const override
FixedValueConstraints(View< const scalar > mask, View< const ValueType > values, localIdx nCells)
SetReference(localIdx refCell, ValueType refValue)
void applyScalarMatrix(la::LinearSystem< scalar, ValueType, la::CSRMatrix< scalar, IndexType >, la::COOMatrix< scalar, IndexType > > &ls) const override
Segregated scalar-matrix / ValueType-rhs form. The scalar diagonal scales the ValueType reference val...
void operator()(la::LinearSystem< ValueType, ValueType, la::CSRMatrix< ValueType, IndexType > > &ls) const override
A class representing a linear system of equations.
Vector< RHSValueType > & rhs()
const Executor & exec() const
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Expression< ValueType > operator+(Expression< ValueType > lhs, const Expression< ValueType > &rhs)
Coeff operator*(const Coeff &lhs, const Coeff &rhs)
Expression< ValueType > operator-(Expression< ValueType > lhs, const Expression< ValueType > &rhs)
Matrix< ValueType, la::CooSparsityPattern< IndexType > > COOMatrix
Matrix< ValueType, la::CsrSparsityPattern< IndexType > > CSRMatrix
LinearSystem< ValueType, RHSValueType, SystemMatrixType, BoundaryMatrixType > createEmptyLinearSystem(const UnstructuredMesh &mesh, std::shared_ptr< MeshIterationStrategy > strategy=std::make_shared< FaceBasedIterator >())
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
KOKKOS_INLINE_FUNCTION T zero()
void parallelFor(const ExecutorType &, std::pair< localIdx, localIdx > range, const Kernel &kernel, std::string name)
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.
auto views(Types &... args)
Unpacks all views of the passed classes.
virtual void operator()(la::LinearSystem< VectorType, VectorType, la::CSRMatrix< VectorType, IndexType > > &) const
virtual ~PostAssemblyBase()=default
virtual void applyScalarMatrix(la::LinearSystem< scalar, VectorType, la::CSRMatrix< scalar, IndexType >, la::COOMatrix< scalar, IndexType > > &) const
Apply to the segregated scalar-matrix / VectorType-rhs form (a scalar coefficient matrix with a Vecto...