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);
207 ValueType diagVal = zero<ValueType>();
208 for (
auto o = rowOffs[row]; o < rowOffs[row + 1]; ++o)
210 const auto col = colIdxs[o];
213 diagVal = matrixValues[o];
219 matrixValues[o] = zero<ValueType>();
221 else if (mask[col] !=
scalar(0))
225 rhs[row] -= matrixValues[o] * vals[col];
226 matrixValues[o] = zero<ValueType>();
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();
249 if (mask[offRowIdxs[i]] !=
scalar(0)) offValues[i] = zero<ValueType>();
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_)
326 for (
auto& op : spatialOperators_)
330 op.explicitOperation(source);
338 for (
auto& op : temporalOperators_)
342 op.explicitOperation(source, t, dt);
349 template<
typename AssemblyType = ValueType>
352 for (
auto& op : spatialOperators_)
356 op.implicitOperation(ls);
364 template<
typename AssemblyType = ValueType>
369 for (
auto& op : temporalOperators_)
373 op.implicitOperation(ls, t, dt);
379 template<
typename AssemblyType = ValueType>
388 {0, static_cast<localIdx>(rhs.size())},
398 template<
typename AssemblyType = ValueType>
406 auto ls = la::createEmptyLinearSystem<AssemblyType, ValueType>(mesh);
407 assemble<AssemblyType>(t, dt, ls, mesh, ps);
415 template<
typename AssemblyType = ValueType>
424 assemble<AssemblyType>(t, dt, ls, ps);
432 template<
typename AssemblyType = ValueType>
445 if constexpr (std::is_same_v<AssemblyType, ValueType>)
447 for (
const auto* p : ps)
452 else if constexpr (std::is_same_v<AssemblyType, scalar>)
454 for (
const auto* p : ps)
456 p->applyScalarMatrix(ls);
465 temporalOperators_.push_back(oper);
470 for (
auto& op : equation.temporalOperators_)
472 temporalOperators_.push_back(op);
474 for (
auto& op : equation.spatialOperators_)
476 spatialOperators_.push_back(op);
481 template<
typename OperatorType, Operator::Type Type>
485 auto matchNameAndType = [
name, opType](
const auto& op)
486 {
return op.getName() ==
name && op.getType() == opType; };
487 if constexpr (std::is_same_v<OperatorType, SpatialOperator<ValueType>>)
489 return std::ranges::any_of(spatialOperators_, matchNameAndType);
491 else if constexpr (std::is_same_v<OperatorType, TemporalOperator<ValueType>>)
493 return std::ranges::any_of(temporalOperators_, matchNameAndType);
499 template<Operator::Type Type>
502 return hasOperatorOfType<SpatialOperator<ValueType>, Type>(
name)
507 template<
typename OperatorType, Operator::Type Type>
510 if (!hasOperatorOfType<OperatorType, Type>(
name))
512 throw std::runtime_error {
"No operator with given name and type found"};
515 auto matchNameAndType = [
name, opType](
const auto& op)
516 {
return op.getName() ==
name && op.getType() == opType; };
517 if constexpr (std::is_same_v<OperatorType, SpatialOperator<ValueType>>)
519 return *std::ranges::find_if(spatialOperators_, matchNameAndType);
521 else if constexpr (std::is_same_v<OperatorType, TemporalOperator<ValueType>>)
523 return *std::ranges::find_if(temporalOperators_, matchNameAndType);
525 throw std::runtime_error {
"Unknown operator type"};
527 return spatialOperators_[0];
531 template<Operator::Type Type>
534 if (!hasOperator<Type>(
name))
536 throw std::runtime_error {
"No operator with given name and type found"};
539 auto matchNameAndType = [
name, opType](
const auto& op)
540 {
return op.getName() ==
name && op.getType() == opType; };
543 std::erase_if(spatialOperators_, matchNameAndType);
547 std::erase_if(temporalOperators_, matchNameAndType);
554 return static_cast<localIdx>(temporalOperators_.size() + spatialOperators_.size());
560 return temporalOperators_;
565 return spatialOperators_;
578 std::vector<TemporalOperator<ValueType>> temporalOperators_;
580 std::vector<SpatialOperator<ValueType>> spatialOperators_;
583template<
typename ValueType>
584[[nodiscard]]
inline Expression<ValueType>
591template<
typename ValueType>
592[[nodiscard]]
inline Expression<ValueType>
599template<
typename leftOperator,
typename rightOperator>
600[[nodiscard]]
inline Expression<typename leftOperator::VectorValueType>
603 using ValueType =
typename leftOperator::VectorValueType;
610template<
typename ValueType>
626template<
typename ValueType>
627[[nodiscard]]
inline Expression<ValueType>
634template<
typename ValueType>
635[[nodiscard]]
inline Expression<ValueType>
642template<
typename leftOperator,
typename rightOperator>
643[[nodiscard]]
inline Expression<typename leftOperator::VectorValueType>
646 using ValueType =
typename leftOperator::VectorValueType;
A class representing a dictionary that stores key-value pairs.
Represents an unstructured mesh in NeoN.
localIdx nCells() const
Get the number of cells in the mesh.
const scalarVector & cellVolumes() const
Get the field of cell volumes in the mesh.
A class to contain the data and executors for a field and define some basic operations.
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
Vector< ValueType > explicitOperation(localIdx nCells) const
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
ValueType ExpressionValueType
Expression(const Executor &exec)
Post-assembly functor that pins a set of cells to prescribed values.
void operator()(la::LinearSystem< ValueType, ValueType, la::CSRMatrix< ValueType, IndexType > > &ls) const override
FixedValueConstraints(View< const scalar > mask, View< const ValueType > values, localIdx nCells)
Post-assembly functor that pins one cell's value to a reference, removing the constant null space tha...
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
Sparse matrix class with compact storage by row (CSR) format.
#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)
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
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...