Cantera  3.1.0
Loading...
Searching...
No Matches
refine.h
1// This file is part of Cantera. See License.txt in the top-level directory or
2// at https://cantera.org/license.txt for license and copyright information.
3
4#ifndef CT_REFINE_H
5#define CT_REFINE_H
6
8
9namespace Cantera
10{
11
12class Domain1D;
13
14//! Refine Domain1D grids so that profiles satisfy adaptation tolerances
15//! @ingroup onedUtilsGroup
16 class Refiner
17{
18public:
19 Refiner(Domain1D& domain);
20 virtual ~Refiner() {}
21 Refiner(const Refiner&) = delete;
22 Refiner& operator=(const Refiner&) = delete;
23
24 /**
25 * Set grid refinement criteria.
26 *
27 * The ratio parameter is the maximum allowed ratio between grid spacing at
28 * adjacent intervals. The ratio parameter considers the situation where the
29 * left interval is much larger than the right interval, or if the right
30 * interval is much larger than the left interval. See
31 * [ratio](../reference/onedim/grid-refinement.html#ratio).
32 *
33 * The slope parameter is the maximum fractional change in the value of each
34 * solution component between adjacent grid points. See
35 * [slope](../reference/onedim/grid-refinement.html#slope).
36 *
37 * The curve parameter is the maximum fractional change in the derivative of
38 * each solution component between adjacent grid points. See
39 * [curve](../reference/onedim/grid-refinement.html#curve).
40 *
41 * The prune parameter is a threshold for removing unnecessary grid points.
42 * See [prune](../reference/onedim/grid-refinement.html#prune).
43 *
44 * @param ratio Maximum ratio between grid spacing at adjacent intervals.
45 * That is, `(x[j+1] - x[j]) / (x[j] - x[j-1]) < ratio`
46 * @param slope Maximum fractional change in the value of each solution
47 * component between adjacent grid points.
48 * @param curve Maximum fractional change in the derivative of each
49 * solution component between adjacent grid points.
50 * @param prune Threshold for removing unnecessary grid points. `prune`
51 * should be smaller than both `slope` and `curve`. Set `prune <= 0`
52 * to disable pruning.
53 */
54 void setCriteria(double ratio = 10.0,
55 double slope = 0.8,
56 double curve = 0.8,
57 double prune = -0.1);
58
59 //! Get the grid refinement criteria. @see Refiner::setCriteria
60 vector<double> getCriteria()
61 {
62 return {m_ratio, m_slope, m_curve, m_prune};
63 }
64
65 /**
66 * Set the active state for a component.
67 *
68 * @param comp Component index
69 * @param state True if the component should be considered for grid refinement
70 */
71 void setActive(int comp, bool state = true) {
72 m_active[comp] = state;
73 }
74
75 //! Set the maximum number of points allowed in the domain
76 void setMaxPoints(int npmax) {
77 m_npmax = npmax;
78 }
79
80 //! Returns the maximum number of points allowed in the domain
81 size_t maxPoints() const {
82 return m_npmax;
83 }
84
85 //! Set the minimum allowable spacing between adjacent grid points [m].
86 void setGridMin(double gridmin) {
87 m_gridmin = gridmin;
88 }
89
90 //! Returns the the minimum allowable spacing between adjacent
91 //! grid points [m].
92 double gridMin() const {
93 return m_gridmin;
94 }
95
96 /**
97 * Determine locations in the grid that need additional grid points and
98 * update the internal state of the Refiner with this information.
99 *
100 * @param[in] n Number of grid points
101 * @param[in] z Point to array of grid points
102 * @param[in] x Pointer to solution vector
103 *
104 * @returns The number of new grid points needed (size of #m_insertPts)
105 */
106 int analyze(size_t n, const double* z, const double* x);
107
108 /**
109 * Constructs a new grid based on refinement locations determined by the analyze()
110 * method.
111 *
112 * This function generates a new grid by inserting additional points into the
113 * current grid at locations where the analyze() method has identified a need for
114 * refinement. The new grid points are placed midway between existing points deemed
115 * necessary for increased resolution. If no refinement is needed, the original
116 * grid is copied directly.
117 *
118 * @param[in] n The number of points in the original grid array `z`.
119 * @param[in] z Pointer to the array of original grid points.
120 * @param[in] nn The maximum number of points that the new grid array `znew` can hold.
121 * @param[out] znew Pointer to the array where the new grid points will be stored.
122 *
123 * @return The function returns 0 upon successful creation of the new grid. Throws
124 * an exception if the provided output array size is insufficient to hold
125 * the new grid.
126 *
127 * @deprecated Unused. To be removed after %Cantera 3.1.
128 */
129 int getNewGrid(int n, const double* z, int nn, double* znew);
130
131 //! Returns the number of new grid points that were needed.
133 return static_cast<int>(m_insertPts.size());
134 }
135
136 /**
137 * Displays the results of the grid refinement analysis.
138 *
139 * This method logs information about where new grid points have been inserted
140 * and the components that required these insertions, if any. If no new points
141 * were needed, it logs that the current grid is sufficient.
142 *
143 * @note This method should be called after analyze() to report the outcomes of
144 * the refinement analysis.
145 */
146 void show();
147
148 /**
149 * Returns true if a new grid point is needed to the right of grid index j.
150 *
151 * @param j Grid point index
152 */
153 bool newPointNeeded(size_t j) {
154 return m_insertPts.find(j) != m_insertPts.end();
155 }
156
157 /**
158 * Returns true if the grid point at index j should be kept.
159 *
160 * @param j Grid point index
161 */
162 bool keepPoint(size_t j) {
163 return (m_keep[j] != REMOVE);
164 }
165
166 /**
167 * Returns the value of the solution component, n, at grid point j.
168 *
169 * @param x %Solution vector
170 * @param n %Solution component index
171 * @param j Grid point index
172 */
173 double value(const double* x, size_t n, size_t j);
174
175 //! Returns the maximum allowable ratio of grid spacing between adjacent intervals
176 double maxRatio() {
177 return m_ratio;
178 }
179
180 //! Returns the maximum allowable difference in value between adjacent points
181 double maxDelta() {
182 return m_slope;
183 }
184
185 //! Returns the maximum allowable difference in the derivative between adjacent points
186 double maxSlope() {
187 return m_curve;
188 }
189
190 //! Returns the threshold for removing unnecessary grid points
191 double prune() {
192 return m_prune;
193 }
194
195protected:
196 //! Indices of grid points that need new grid points added after them
197 set<size_t> m_insertPts;
198
199 // Grid point refinement status
200 enum GridPointStatus {
201 UNSET = 0,
202 KEEP = 1,
203 REMOVE = -1
204 };
205
206 //! Status of whether each grid point should be kept or removed.
207 map<size_t, GridPointStatus> m_keep;
208
209 //! Names of components that require the addition of new grid points
210 set<string> m_componentNames;
211
212 //! Flags for whether each component should be considered for grid refinement
213 vector<bool> m_active;
214
215 //! @name Refinement criteria
216 //! @{
217 double m_ratio = 10.0; //!< grid spacing refinement criteria
218 double m_slope = 0.8; //!< function change refinement criteria
219 double m_curve = 0.8; //!< function slope refinement criteria
220 double m_prune = -0.001; //!< pruning refinement criteria
221 //! @}
222
223 //! Threshold for ignoring small changes around a constant during refinement.
224 double m_min_range = 0.01;
225
226 Domain1D* m_domain; //!< Pointer to the domain to be refined.
227 size_t m_nv; //!< Number of components in the domain
228 size_t m_npmax = 1000; //!< Maximum number of grid points
229
230 //! Absolute tolerance threshold for solution components in the domain
231 double m_thresh = std::sqrt(std::numeric_limits<double>::epsilon());
232 double m_gridmin = 1e-10; //!< minimum grid spacing [m]
233};
234
235}
236
237#endif
Base class for one-dimensional domains.
Definition Domain1D.h:29
Refine Domain1D grids so that profiles satisfy adaptation tolerances.
Definition refine.h:17
double maxSlope()
Returns the maximum allowable difference in the derivative between adjacent points.
Definition refine.h:186
double value(const double *x, size_t n, size_t j)
Returns the value of the solution component, n, at grid point j.
Definition refine.cpp:211
set< string > m_componentNames
Names of components that require the addition of new grid points.
Definition refine.h:210
bool newPointNeeded(size_t j)
Returns true if a new grid point is needed to the right of grid index j.
Definition refine.h:153
size_t maxPoints() const
Returns the maximum number of points allowed in the domain.
Definition refine.h:81
void setActive(int comp, bool state=true)
Set the active state for a component.
Definition refine.h:71
double m_thresh
Absolute tolerance threshold for solution components in the domain.
Definition refine.h:231
double m_ratio
grid spacing refinement criteria
Definition refine.h:217
int nNewPoints()
Returns the number of new grid points that were needed.
Definition refine.h:132
void show()
Displays the results of the grid refinement analysis.
Definition refine.cpp:216
size_t m_nv
Number of components in the domain.
Definition refine.h:227
double maxRatio()
Returns the maximum allowable ratio of grid spacing between adjacent intervals.
Definition refine.h:176
vector< double > getCriteria()
Get the grid refinement criteria.
Definition refine.h:60
double m_prune
pruning refinement criteria
Definition refine.h:220
double m_min_range
Threshold for ignoring small changes around a constant during refinement.
Definition refine.h:224
double maxDelta()
Returns the maximum allowable difference in value between adjacent points.
Definition refine.h:181
void setMaxPoints(int npmax)
Set the maximum number of points allowed in the domain.
Definition refine.h:76
set< size_t > m_insertPts
Indices of grid points that need new grid points added after them.
Definition refine.h:197
void setCriteria(double ratio=10.0, double slope=0.8, double curve=0.8, double prune=-0.1)
Set grid refinement criteria.
Definition refine.cpp:21
double m_curve
function slope refinement criteria
Definition refine.h:219
vector< bool > m_active
Flags for whether each component should be considered for grid refinement.
Definition refine.h:213
size_t m_npmax
Maximum number of grid points.
Definition refine.h:228
int analyze(size_t n, const double *z, const double *x)
Determine locations in the grid that need additional grid points and update the internal state of the...
Definition refine.cpp:43
bool keepPoint(size_t j)
Returns true if the grid point at index j should be kept.
Definition refine.h:162
double gridMin() const
Returns the the minimum allowable spacing between adjacent grid points [m].
Definition refine.h:92
double m_slope
function change refinement criteria
Definition refine.h:218
double prune()
Returns the threshold for removing unnecessary grid points.
Definition refine.h:191
double m_gridmin
minimum grid spacing [m]
Definition refine.h:232
int getNewGrid(int n, const double *z, int nn, double *znew)
Constructs a new grid based on refinement locations determined by the analyze() method.
Definition refine.cpp:238
map< size_t, GridPointStatus > m_keep
Status of whether each grid point should be kept or removed.
Definition refine.h:207
void setGridMin(double gridmin)
Set the minimum allowable spacing between adjacent grid points [m].
Definition refine.h:86
Domain1D * m_domain
Pointer to the domain to be refined.
Definition refine.h:226
This file contains definitions of constants, types and terms that are used in internal routines and a...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595