NeoN
A framework for CFD software
Loading...
Searching...
No Matches
fixedValue.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <vector>
8
9#include <Kokkos_Core.hpp>
10
11#include "NeoN/core/error.hpp"
12#include "NeoN/fields/field.hpp"
17
19{
20
21namespace detail
22{
23// TODO move to source
24// Without this function the compiler warns that calling a __host__ function
25// from a __device__ function is not allowed
26template<typename ValueType>
28 Field<ValueType>& domainVector, std::pair<sizeType, sizeType> range, ValueType fixedValue
29)
30{
31 auto [refGradient, value, valueFraction, refValue] = views(
32 domainVector.boundaryData().refGrad(),
33 domainVector.boundaryData().value(),
34 domainVector.boundaryData().valueFraction(),
35 domainVector.boundaryData().refValue()
36 );
37
39 domainVector.exec(),
40 range,
41 NEON_LAMBDA(const localIdx i) {
42 refValue[i] = fixedValue;
43 value[i] = fixedValue;
44 valueFraction[i] = 1.0; // only used refValue
45 refGradient[i] = fixedValue; // not used
46 },
47 "setFixedValueVolume"
48 );
49}
50
51// Per-face variant: the patch carries one value per boundary face, e.g. an inflow
52// profile read from a nonuniform OpenFOAM patch field.
53template<typename ValueType>
55 Field<ValueType>& domainVector,
56 std::pair<sizeType, sizeType> range,
57 const Vector<ValueType>& fixedValues
58)
59{
60 auto [refGradient, value, valueFraction, refValue] = views(
61 domainVector.boundaryData().refGrad(),
62 domainVector.boundaryData().value(),
63 domainVector.boundaryData().valueFraction(),
64 domainVector.boundaryData().refValue()
65 );
66 auto fixedValuesView = fixedValues.view();
67 const auto start = static_cast<localIdx>(range.first);
68
70 domainVector.exec(),
71 range,
72 NEON_LAMBDA(const localIdx i) {
73 auto patchValue = fixedValuesView[i - start];
74 refValue[i] = patchValue;
75 value[i] = patchValue;
76 valueFraction[i] = 1.0; // only used refValue
77 refGradient[i] = patchValue; // not used
78 },
79 "setFixedValuesVolume"
80 );
81}
82
83}
84
85template<typename ValueType>
86class FixedValue : public VolumeBoundaryFactory<ValueType>::template Register<FixedValue<ValueType>>
87{
89
90public:
91
92 using Base::correctBoundaryCondition;
93
95 : Base(mesh, dict, patchID, {.assignable = false, .fixesValue = true}),
96 fixedValue_(
97 dict.contains("fixedValue") ? dict.get<ValueType>("fixedValue") : ValueType {}
98 ),
99 fixedValues_(
100 mesh.exec(),
101 dict.contains("fixedValues") ? dict.get<std::vector<ValueType>>("fixedValues")
102 : std::vector<ValueType> {}
103 )
104 {
105 const bool hasUniform = dict.contains("fixedValue");
106 const bool hasPerFace = dict.contains("fixedValues");
107 if (!hasUniform && !hasPerFace)
108 {
109 NF_THROW(
110 "fixedValue boundary condition on patch " + std::to_string(patchID)
111 + " requires either a uniform 'fixedValue' or a per-face 'fixedValues' entry"
112 );
113 }
114 // Validate on the key being present, not on the list being non-empty: an empty
115 // 'fixedValues' on a non-empty patch is malformed per-face data, and testing the
116 // size alone would let it through to the uniform fallback and silently apply zero.
117 if (hasPerFace && toLocalIdx(fixedValues_.size()) != this->patchSize())
118 {
119 NF_THROW(
120 "'fixedValues' holds " + std::to_string(fixedValues_.size()) + " values but patch "
121 + std::to_string(patchID) + " has " + std::to_string(this->patchSize()) + " faces"
122 );
123 }
124 }
125
126 virtual void correctBoundaryCondition(Field<ValueType>& domainVector) final
127 {
128 if (fixedValues_.size() > 0)
129 {
130 detail::setFixedValues(domainVector, this->range(), fixedValues_);
131 }
132 else
133 {
134 detail::setFixedValue(domainVector, this->range(), fixedValue_);
135 }
136 }
137
138 static std::string name() { return "fixedValue"; }
139
140 std::string getName() const override { return name(); }
141
142 static std::string doc() { return "Set a fixed value on the boundary"; }
143
144 static std::string schema() { return "none"; }
145
146 virtual std::unique_ptr<VolumeBoundaryFactory<ValueType>> clone() const final
147 {
148 return std::make_unique<FixedValue>(*this);
149 }
150
151private:
152
153 ValueType fixedValue_;
154 Vector<ValueType> fixedValues_;
155};
156
157}
A class representing a dictionary that stores key-value pairs.
Represents the domain fields for a computational domain.
Definition field.hpp:34
const BoundaryData< ValueType > & boundaryData() const
Definition field.hpp:96
const Executor & exec() const
Definition field.hpp:101
Represents an unstructured mesh in NeoN.
An executor-aware contiguous container for numerical data with vector-style arithmetic semantics.
Definition vector.hpp:40
View< ValueType > view() &&=delete
Prevents creating a View from a temporary Vector.
std::pair< localIdx, localIdx > range() const
VolumeBoundaryFactory(const UnstructuredMesh &mesh, const Dictionary &dict, localIdx patchID, BoundaryAttributes attributes)
virtual std::unique_ptr< VolumeBoundaryFactory< ValueType > > clone() const final
virtual void correctBoundaryCondition(Field< ValueType > &domainVector) final
FixedValue(const UnstructuredMesh &mesh, const Dictionary &dict, localIdx patchID)
A template class for registering derived classes with a base class.
#define NF_THROW(message)
Macro for throwing a NeoNException with the specified error message.
Definition error.hpp:103
void setFixedValues(Field< ValueType > &domainVector, std::pair< sizeType, sizeType > range, const Vector< ValueType > &fixedValues)
void setFixedValue(Field< ValueType > &domainVector, std::pair< sizeType, sizeType > range, ValueType fixedValue)
localIdx toLocalIdx(sizeType value)
void parallelFor(const ExecutorType &, std::pair< localIdx, localIdx > range, const Kernel &kernel, std::string name)
int32_t localIdx
Definition label.hpp:50
auto views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:108
#define NEON_LAMBDA