MFEM v4.9.0
Finite element discretization library
Loading...
Searching...
No Matches
spacing.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_SPACING
13#define MFEM_SPACING
14
15#include "../linalg/vector.hpp"
16
17#include <memory>
18#include <vector>
19
20namespace mfem
21{
22
26
27/// Class for spacing functions that define meshes in a dimension, using a
28/// formula or method implemented in a derived class.
30{
31public:
32 /** @brief Base class constructor.
33 @param[in] n Size or number of intervals, which defines elements.
34 @param[in] r Whether to reverse the spacings, false by default.
35 @param[in] s Whether to scale parameters by the refinement or coarsening
36 factor, in the function @a SpacingFunction::ScaleParameters.
37 */
38 SpacingFunction(int n, bool r=false, bool s=false) : n(n), reverse(r), scale(s)
39 { }
40
41 /// Returns the size, or number of intervals (elements).
42 inline int Size() const { return n; }
43
44 /// Sets the size, or number of intervals (elements).
45 virtual void SetSize(int size) = 0;
46
47 /// Sets the property that determines whether the spacing is reversed.
48 void SetReverse(bool r) { reverse = r; }
49
50 bool GetReverse() const { return reverse; }
51
52 void Flip() { reverse = !reverse; }
53
54 /// Returns the width of interval @a p (between 0 and @a Size() - 1).
55 virtual real_t Eval(int p) const = 0;
56
57 /// Returns the width of all intervals, resizing @a s to @a Size().
58 void EvalAll(Vector & s) const
59 {
60 s.SetSize(n);
61 for (int i=0; i<n; ++i)
62 {
63 s[i] = Eval(i);
64 }
65 }
66
67 /** @brief Scales parameters by the factor @a a associated with @a Size().
68
69 Note that parameters may be scaled inversely during coarsening and
70 refining, so the scaling should be linear in the sense that scaling by a
71 number followed by scaling by its inverse has no effect on parameters. */
72 virtual void ScaleParameters(real_t a) { }
73
74 /// Returns the spacing type, indicating the derived class.
75 virtual SpacingType GetSpacingType() const = 0;
76
77 /** @brief Prints all the data necessary to define the spacing function and
78 its current state (size and other parameters).
79
80 The format is generally
81 SpacingType numIntParam numDoubleParam {int params} {double params} */
82 virtual void Print(std::ostream &os) const = 0;
83
84 /// Returns the number of integer parameters defining the spacing function.
85 virtual int NumIntParameters() const = 0;
86
87 /// Returns the number of double parameters defining the spacing function.
88 virtual int NumDoubleParameters() const = 0;
89
90 /// Returns the array of integer parameters defining the spacing function.
91 /// @param[out] p Array of integer parameters, resized appropriately.
92 virtual void GetIntParameters(Array<int> & p) const = 0;
93
94 /// Returns the array of double parameters defining the spacing function.
95 /// @param[out] p Array of double parameters, resized appropriately.
96 virtual void GetDoubleParameters(Vector & p) const = 0;
97
98 /// Returns true if the spacing function is nested during refinement.
99 virtual bool Nested() const = 0;
100
101 /// Returns a clone (deep-copy) of this spacing function.
102 virtual std::unique_ptr<SpacingFunction> Clone() const;
103
105 {
106 const int nfine = n;
107 SetSize(1);
108 ScaleParameters(nfine);
109 }
110
111 virtual ~SpacingFunction() = default;
112
113protected:
114 int n; ///< Size, or number of intervals (elements)
115 bool reverse; ///< Whether to reverse the spacing
116 bool scale; ///< Whether to scale parameters in ScaleParameters.
117};
118
119/** @brief Uniform spacing function, dividing the unit interval into @a Size()
120 equally spaced intervals (elements).
121
122 This function is nested and has no scaled parameters. */
124{
125public:
128 {
129 CalculateSpacing();
130 }
131
132 void SetSize(int size) override
133 {
134 n = size;
135 CalculateSpacing();
136 }
137
138 real_t Eval(int p) const override
139 {
140 return s;
141 }
142
143 void Print(std::ostream &os) const override
144 {
145 // SpacingType numIntParam numDoubleParam {int params} {double params}
146 os << int(SpacingType::UNIFORM_SPACING) << " 1 0 " << n << "\n";
147 }
148
151 int NumIntParameters() const override { return 1; }
152 int NumDoubleParameters() const override { return 0; }
153
154 void GetIntParameters(Array<int> & p) const override
155 {
156 p.SetSize(1);
157 p[0] = n;
158 }
159
160 void GetDoubleParameters(Vector & p) const override
161 {
162 p.SetSize(0);
163 }
164
165 bool Nested() const override { return true; }
166
167 std::unique_ptr<SpacingFunction> Clone() const override
168 {
169 return std::unique_ptr<SpacingFunction>(
170 new UniformSpacingFunction(*this));
171 }
172
173private:
174 real_t s; ///< Width of each interval (element)
175
176 /// Calculate interval width @a s
177 void CalculateSpacing()
178 {
179 // Spacing is 1 / n
180 s = 1.0 / ((real_t) n);
181 }
182};
183
184/** @brief Linear spacing function, defining the width of interval i as
185 s + i * d.
186
187 The initial interval width s is prescribed as a parameter, which can be
188 scaled, and d is computed as a function of Size(), to ensure that the widths
189 sum to 1. This function is not nested. */
191{
192public:
193 LinearSpacingFunction(int n, bool r, real_t s, bool scale)
194 : SpacingFunction(n, r, scale), s(s)
195 {
196 MFEM_VERIFY(0.0 < s && s < 1.0, "Initial spacing must be in (0,1)");
197 CalculateDifference();
198 }
199
200 void SetSize(int size) override
201 {
202 n = size;
203 CalculateDifference();
204 }
205
206 void ScaleParameters(real_t a) override
207 {
208 if (scale)
209 {
210 s *= a;
211 CalculateDifference();
212 }
213 }
214
215 real_t Eval(int p) const override
216 {
217 MFEM_ASSERT(0 <= p && p < n, "Access element " << p
218 << " of spacing function, size = " << n);
219 const int i = reverse ? n - 1 - p : p;
220 return s + (i * d);
221 }
222
223 void Print(std::ostream &os) const override
224 {
225 // SpacingType numIntParam numDoubleParam {int params} {double params}
226 os << int(SpacingType::LINEAR) << " 3 1 " << n << " " << (int) reverse
227 << " " << (int) scale << " " << s << "\n";
228 }
229
230 SpacingType GetSpacingType() const override { return SpacingType::LINEAR; }
231 int NumIntParameters() const override { return 3; }
232 int NumDoubleParameters() const override { return 1; }
233
234 void GetIntParameters(Array<int> & p) const override
235 {
236 p.SetSize(3);
237 p[0] = n;
238 p[1] = (int) reverse;
239 p[2] = (int) scale;
240 }
241
242 void GetDoubleParameters(Vector & p) const override
243 {
244 p.SetSize(1);
245 p[0] = s;
246 }
247
248 bool Nested() const override { return false; }
249
250 std::unique_ptr<SpacingFunction> Clone() const override
251 {
252 return std::unique_ptr<SpacingFunction>(
253 new LinearSpacingFunction(*this));
254 }
255
256private:
257 real_t s, d; ///< Spacing parameters, set by @a CalculateDifference
258
259 void CalculateDifference()
260 {
261 if (n < 2)
262 {
263 d = 0.0;
264 return;
265 }
266
267 // Spacings are s, s + d, ..., s + (n-1)d, which must sum to 1:
268 // 1 = ns + dn(n-1)/2
269 d = 2.0 * (1.0 - (n * s)) / ((real_t) (n*(n-1)));
270
271 if (s + ((n-1) * d) <= 0.0)
272 {
273 MFEM_ABORT("Invalid linear spacing parameters");
274 }
275 }
276};
277
278/** @brief Geometric spacing function.
279
280 The spacing of interval i is s*r^i for 0 <= i < n, with
281 s + s*r + s*r^2 + ... + s*r^(n-1) = 1
282 s * (r^n - 1) / (r - 1) = 1
283 The initial spacing s and number of intervals n are inputs, and r is solved
284 for by Newton's method. The parameter s can be scaled. This function is not
285 nested. */
287{
288public:
290 : SpacingFunction(n, r, scale), s(s)
291 {
292 CalculateSpacing();
293 }
294
295 void SetSize(int size) override
296 {
297 n = size;
298 CalculateSpacing();
299 }
300
301 void ScaleParameters(real_t a) override
302 {
303 if (scale)
304 {
305 s *= a;
306 CalculateSpacing();
307 }
308 }
309
310 real_t Eval(int p) const override
311 {
312 const int i = reverse ? n - 1 - p : p;
313 return n == 1 ? 1.0 : s * std::pow(r, i);
314 }
315
316 void Print(std::ostream &os) const override
317 {
318 // SpacingType numIntParam numDoubleParam {int params} {double params}
319 os << int(SpacingType::GEOMETRIC) << " 3 1 " << n << " "
320 << (int) reverse << " " << (int) scale << " " << s << "\n";
321 }
322
324 { return SpacingType::GEOMETRIC; }
325 int NumIntParameters() const override { return 3; }
326 int NumDoubleParameters() const override { return 1; }
327
328 void GetIntParameters(Array<int> & p) const override
329 {
330 p.SetSize(3);
331 p[0] = n;
332 p[1] = (int) reverse;
333 p[2] = (int) scale;
334 }
335
336 void GetDoubleParameters(Vector & p) const override
337 {
338 p.SetSize(1);
339 p[0] = s;
340 }
341
342 bool Nested() const override { return false; }
343
344 std::unique_ptr<SpacingFunction> Clone() const override
345 {
346 return std::unique_ptr<SpacingFunction>(
347 new GeometricSpacingFunction(*this));
348 }
349
350private:
351 real_t s; ///< Initial spacing
352 real_t r; ///< Ratio
353
354 /// Calculate parameters used by @a Eval and @a EvalAll
355 void CalculateSpacing();
356};
357
358/** @brief Bell spacing function, which produces spacing resembling a Bell
359 curve.
360
361 The widths of the first and last intervals (elements) are prescribed, and
362 the remaining interior spacings are computed by an algorithm that minimizes
363 the ratios of adjacent spacings. If the first and last intervals are wide
364 enough, the spacing may decrease in the middle of the domain. The first and
365 last interval widths can be scaled. This function is not nested.
366 */
368{
369public:
370 /** @brief Constructor for BellSpacingFunction.
371 @param[in] n Size or number of intervals, which defines elements.
372 @param[in] r Whether to reverse the spacings.
373 @param[in] s0 Width of the first interval (element).
374 @param[in] s1 Width of the last interval (element).
375 @param[in] s Whether to scale parameters by the refinement or coarsening
376 factor, in the function @a SpacingFunction::ScaleParameters.
377 */
378 BellSpacingFunction(int n, bool r, real_t s0, real_t s1, bool s)
379 : SpacingFunction(n, r, s), s0(s0), s1(s1)
380 {
381 CalculateSpacing();
382 }
383
384 void SetSize(int size) override
385 {
386 n = size;
387 CalculateSpacing();
388 }
389
390 void ScaleParameters(real_t a) override
391 {
392 if (scale)
393 {
394 s0 *= a;
395 s1 *= a;
396 CalculateSpacing();
397 }
398 }
399
400 real_t Eval(int p) const override
401 {
402 const int i = reverse ? n - 1 - p : p;
403 return s[i];
404 }
405
406 void Print(std::ostream &os) const override
407 {
408 // SpacingType numIntParam numDoubleParam {int params} {double params}
409 os << int(SpacingType::BELL) << " 3 2 " << n << " " << (int) reverse
410 << " " << (int) scale << " " << s0 << " " << s1 << "\n";
411 }
412
413 SpacingType GetSpacingType() const override { return SpacingType::BELL; }
414 int NumIntParameters() const override { return 3; }
415 int NumDoubleParameters() const override { return 2; }
416
417 void GetIntParameters(Array<int> & p) const override
418 {
419 p.SetSize(3);
420 p[0] = n;
421 p[1] = (int) reverse;
422 p[2] = (int) scale;
423 }
424
425 void GetDoubleParameters(Vector & p) const override
426 {
427 p.SetSize(2);
428 p[0] = s0;
429 p[1] = s1;
430 }
431
432 bool Nested() const override { return false; }
433
434 std::unique_ptr<SpacingFunction> Clone() const override
435 {
436 return std::unique_ptr<SpacingFunction>(
437 new BellSpacingFunction(*this));
438 }
439
440private:
441 real_t s0, s1; ///< First and last interval widths
442 Vector s; ///< Stores the spacings calculated by @a CalculateSpacing
443
444 /// Calculate parameters used by @a Eval and @a EvalAll
445 void CalculateSpacing();
446};
447
448/** @brief Gaussian spacing function of the general form
449 g(x) = a exp(-(x-m)^2 / c^2) for some scalar parameters a, m, c.
450
451 The widths of the first and last intervals (elements) are prescribed, and
452 the remaining interior spacings are computed by using Newton's method to
453 compute parameters that fit the endpoint widths. The results of this spacing
454 function are very similar to those of @a BellSpacingFunction, but they may
455 differ by about 1%. If the first and last intervals are wide enough, the
456 spacing may decrease in the middle of the domain. The first and last
457 interval widths can be scaled. This function is not nested. */
459{
460public:
461 /** @brief Constructor for GaussianSpacingFunction.
462 @param[in] n Size or number of intervals, which defines elements.
463 @param[in] r Whether to reverse the spacings.
464 @param[in] s0 Width of the first interval (element).
465 @param[in] s1 Width of the last interval (element).
466 @param[in] s Whether to scale parameters by the refinement or coarsening
467 factor, in the function @a SpacingFunction::ScaleParameters.
468 */
469 GaussianSpacingFunction(int n, bool r, real_t s0, real_t s1, bool s)
470 : SpacingFunction(n, r, s), s0(s0), s1(s1)
471 {
472 CalculateSpacing();
473 }
474
475 void SetSize(int size) override
476 {
477 n = size;
478 CalculateSpacing();
479 }
480
481 void ScaleParameters(real_t a) override
482 {
483 if (scale)
484 {
485 s0 *= a;
486 s1 *= a;
487 CalculateSpacing();
488 }
489 }
490
491 real_t Eval(int p) const override
492 {
493 const int i = reverse ? n - 1 - p : p;
494 return s[i];
495 }
496
497 void Print(std::ostream &os) const override
498 {
499 // SpacingType numIntParam numDoubleParam {int params} {double params}
500 os << int(SpacingType::GAUSSIAN) << " 3 2 " << n << " " << (int) reverse
501 << " " << (int) scale << " " << s0 << " " << s1 << "\n";
502 }
503
505 int NumIntParameters() const override { return 3; }
506 int NumDoubleParameters() const override { return 2; }
507
508 void GetIntParameters(Array<int> & p) const override
509 {
510 p.SetSize(3);
511 p[0] = n;
512 p[1] = (int) reverse;
513 p[2] = (int) scale;
514 }
515
516 void GetDoubleParameters(Vector & p) const override
517 {
518 p.SetSize(2);
519 p[0] = s0;
520 p[1] = s1;
521 }
522
523 bool Nested() const override { return false; }
524
525 std::unique_ptr<SpacingFunction> Clone() const override
526 {
527 return std::unique_ptr<SpacingFunction>(
528 new GaussianSpacingFunction(*this));
529 }
530
531private:
532 real_t s0, s1; ///< First and last interval widths
533 Vector s; ///< Stores the spacings calculated by @a CalculateSpacing
534
535 /// Calculate parameters used by @a Eval and @a EvalAll
536 void CalculateSpacing();
537};
538
539/** @brief Logarithmic spacing function, uniform in log base 10 by default.
540
541 The log base can be changed as an input parameter. Decreasing it makes the
542 distribution more uniform, whereas increasing it makes the spacing vary
543 more. Another input option is a flag to make the distribution symmetric
544 (default is non-symmetric). There are no scaled parameters. This function is
545 nested. */
547{
548public:
549 LogarithmicSpacingFunction(int n, bool r, bool sym=false, real_t b=10.0)
550 : SpacingFunction(n, r), sym(sym), logBase(b)
551 {
552 CalculateSpacing();
553 }
554
555 void SetSize(int size) override
556 {
557 n = size;
558 CalculateSpacing();
559 }
560
561 real_t Eval(int p) const override
562 {
563 const int i = reverse ? n - 1 - p : p;
564 return s[i];
565 }
566
567 void Print(std::ostream &os) const override
568 {
569 // SpacingType numIntParam numDoubleParam {int params} {double params}
570 os << int(SpacingType::LOGARITHMIC) << " 3 1 " << n << " " <<
571 (int) reverse << " " << (int) sym << " " << logBase << "\n";
572 }
573
575 { return SpacingType::LOGARITHMIC; }
576 int NumIntParameters() const override { return 3; }
577 int NumDoubleParameters() const override { return 1; }
578
579 void GetIntParameters(Array<int> & p) const override
580 {
581 p.SetSize(3);
582 p[0] = n;
583 p[1] = (int) reverse;
584 p[2] = (int) sym;
585 }
586
587 void GetDoubleParameters(Vector & p) const override
588 {
589 p.SetSize(1);
590 p[0] = logBase;
591 }
592
593 bool Nested() const override { return true; }
594
595 std::unique_ptr<SpacingFunction> Clone() const override
596 {
597 return std::unique_ptr<SpacingFunction>(
598 new LogarithmicSpacingFunction(*this));
599 }
600
601private:
602 bool sym; ///< Whether to make the spacing symmetric
603 real_t logBase; ///< Base of the logarithmic function
604 Vector s; ///< Stores the spacings calculated by @a CalculateSpacing
605
606 /// Calculate parameters used by @a Eval and @a EvalAll
607 void CalculateSpacing();
608 /// Symmetric case for @a CalculateSpacing
609 void CalculateSymmetric();
610 /// Nonsymmetric case for @a CalculateSpacing
611 void CalculateNonsymmetric();
612};
613
614/** @brief Piecewise spacing function, with spacing functions defining spacing
615 within arbitarily many fixed subintervals of the unit interval.
616
617 The number of elements in each piece (or subinterval) is determined by the
618 constructor input @a relN, which is the relative number of intervals. For
619 equal numbers, relN would be all 1's. The total number of elements for this
620 spacing function must be an integer multiple of the sum of entries in relN
621 (stored in n0).
622
623 The scaling of parameters is done for the spacing function on each
624 subinterval separately. This function is nested if and only if the functions
625 on all subintervals are nested.
626 */
628{
629public:
630 /** @brief Constructor for PiecewiseSpacingFunction.
631 @param[in] n Size or number of intervals, which defines elements.
632 @param[in] np Number of pieces (subintervals of unit interval).
633 @param[in] r Whether to reverse the spacings.
634 @param[in] relN Relative number of elements per piece.
635 @param[in] ipar Integer parameters for all np spacing functions. For each
636 piece, these parameters are type, number of integer
637 parameters, number of double parameters, integer parameters.
638 @param[in] dpar Double parameters for all np spacing functions. The first
639 np - 1 entries define the partition of the unit interval,
640 and the remaining are for the pieces.
641 */
642 PiecewiseSpacingFunction(int n, int np, bool r, Array<int> const& relN,
643 Array<int> const& ipar, Vector const& dpar)
644 : SpacingFunction(n, r), np(np), partition(np - 1)
645 {
646 npartition = relN;
647 SetupPieces(ipar, dpar);
648 CalculateSpacing();
649 }
650
651 /// Copy constructor (deep-copy all data, including SpacingFunction pieces)
653 : SpacingFunction(sf.n, sf.reverse), np(sf.np), partition(sf.partition),
654 npartition(sf.npartition), pieces(), n0(sf.n0), s(sf.s)
655 {
656 // To copy, the pointers must be cloned.
657 for (const auto &f : sf.pieces) { pieces.emplace_back(f->Clone()); }
658 }
659
661 {
663 std::swap(tmp, *this);
664 return *this;
665 }
666
669
670 void SetSize(int size) override
671 {
672 n = size;
673 CalculateSpacing();
674 }
675
676 real_t Eval(int p) const override
677 {
678 const int i = reverse ? n - 1 - p : p;
679 return s[i];
680 }
681
682 void ScaleParameters(real_t a) override;
683
684 void Print(std::ostream &os) const override;
685
686 std::unique_ptr<SpacingFunction> Clone() const override
687 {
688 return std::unique_ptr<SpacingFunction>(
689 new PiecewiseSpacingFunction(*this));
690 }
691
692 void SetupPieces(Array<int> const& ipar, Vector const& dpar);
693
695 int NumIntParameters() const override
696 {
697 int count = 3 + np;
698 for (const auto &p : pieces)
699 {
700 // Add three for the type and the integer and double parameter counts.
701 count += p->NumIntParameters() + 3;
702 }
703 return count;
704 }
705
706 int NumDoubleParameters() const override
707 {
708 int count = np - 1;
709 for (const auto &p : pieces)
710 {
711 count += p->NumDoubleParameters();
712 }
713 return count;
714 }
715
716 Array<int> RelativePieceSizes() const { return npartition; }
717
718 void ScalePartition(Array<int> const& f, bool reorient)
719 {
720 MFEM_VERIFY(npartition.Size() == f.Size(), "");
721 n0 = 0;
722 for (int i=0; i<f.Size(); ++i)
723 {
724 const int ir = reorient && reverse ? f.Size() - 1 - i : i;
725 npartition[i] *= f[ir];
726 n0 += npartition[i];
727 }
728 }
729
730 void GetIntParameters(Array<int> & p) const override
731 {
732 p.SetSize(NumIntParameters());
733 p[0] = n;
734 p[1] = np;
735 p[2] = (int) reverse;
736 for (int i=0; i<np; ++i) { p[3 + i] = npartition[i]; }
737
738 Array<int> ipar;
739 int os = 3 + np;
740 for (int i=0; i<np; ++i)
741 {
742 p[os++] = (int) pieces[i]->GetSpacingType();
743 p[os++] = pieces[i]->NumIntParameters();
744 p[os++] = pieces[i]->NumDoubleParameters();
745
746 pieces[i]->GetIntParameters(ipar);
747 for (auto ip : ipar)
748 {
749 p[os++] = ip;
750 }
751 }
752 }
753
754 void GetDoubleParameters(Vector & p) const override
755 {
756 p.SetSize(NumDoubleParameters());
757 for (int i=0; i<partition.Size(); ++i) { p[i] = partition[i]; }
758 int os = partition.Size();
759 Vector dpar;
760 for (int i=0; i<np; ++i)
761 {
762 pieces[i]->GetDoubleParameters(dpar);
763 for (auto dp : dpar)
764 {
765 p[os++] = dp;
766 }
767 }
768 }
769
770 // PiecewiseSpacingFunction is nested if and only if all pieces are nested.
771 bool Nested() const override;
772
773private:
774 int np; ///< Number of pieces
775 Vector partition; ///< Partition of the unit interval
776 Array<int> npartition; ///< Relative number of intervals in each partition
777 std::vector<std::unique_ptr<SpacingFunction>> pieces;
778
779 int n0 = 0; ///< Sum of npartition
780
781 Vector s; ///< Stores the spacings calculated by @a CalculateSpacing
782
783 /// Calculate parameters used by @a Eval and @a EvalAll
784 void CalculateSpacing();
785};
786
787/** @brief Partial spacing function, defined as part of an existing spacing
788 function.
789 */
791{
792public:
793 PartialSpacingFunction(int n, bool r, int rel_first_elem, int rel_num_elems,
794 int rel_num_elems_full,
795 Array<int> const& ipar, Vector const& dpar,
796 SpacingType typeFull)
797 : SpacingFunction(n, r), num_elems_full(rel_num_elems_full),
798 num_elems(rel_num_elems), first_elem(rel_first_elem)
799 {
800 SetupFull(typeFull, ipar, dpar);
801 CalculateSpacing();
802 }
803
804 /// Copy constructor (deep-copy all data, including SpacingFunction pieces)
806 : SpacingFunction(sf.n, sf.reverse), fullSpacing(sf.fullSpacing->Clone()),
807 num_elems_full(sf.num_elems_full), num_elems(sf.num_elems),
808 first_elem(sf.first_elem), s(sf.s)
809 { }
810
812 {
814 std::swap(tmp, *this);
815 return *this;
816 }
817
820
821 void SetSize(int size) override
822 {
823 n = size;
824 CalculateSpacing();
825 }
826
827 real_t Eval(int p) const override
828 {
829 const int i = reverse ? n - 1 - p : p;
830 return s[i];
831 }
832
833 void ScaleParameters(real_t a) override;
834
835 void Print(std::ostream &os) const override;
836
837 std::unique_ptr<SpacingFunction> Clone() const override
838 {
839 return std::unique_ptr<SpacingFunction>(
840 new PartialSpacingFunction(*this));
841 }
842
843 void SetupFull(SpacingType typeFull,
844 Array<int> const& ipar, Vector const& dpar);
845
847 int NumIntParameters() const override { return 8 + fullSpacing->NumIntParameters(); }
848 int NumDoubleParameters() const override { return fullSpacing->NumDoubleParameters(); }
849
850 void GetIntParameters(Array<int> & p) const override
851 {
852 Array<int> ipar;
853 fullSpacing->GetIntParameters(ipar);
854 p.SetSize(8 + ipar.Size());
855 p[0] = n;
856 p[1] = (int) reverse;
857 p[2] = first_elem;
858 p[3] = num_elems;
859 p[4] = num_elems_full;
860 p[5] = (int) fullSpacing->GetSpacingType();
861 p[6] = ipar.Size();
862 p[7] = NumDoubleParameters();
863 for (int i=0; i<ipar.Size(); ++i) { p[8 + i] = ipar[i]; }
864 }
865
866 void GetDoubleParameters(Vector & p) const override
867 {
868 fullSpacing->GetDoubleParameters(p);
869 }
870
871 // PartialSpacingFunction is nested if and only if the full spacing is nested.
872 bool Nested() const override { return fullSpacing->Nested(); }
873
874private:
875 std::unique_ptr<SpacingFunction> fullSpacing;
876
877 // The following numbers and indices may be relative, not absolute.
878 int num_elems_full; ///< Reference number of elements in fullSpacing
879 int num_elems; ///< Number of elements, relative to num_elems_full
880 int first_elem; ///< Index of the first element, relative to num_elems_full
881
882 Vector s; ///< Stores the spacings calculated by @a CalculateSpacing
883
884 /// Calculate parameters used by @a Eval and @a EvalAll
885 void CalculateSpacing();
886};
887
888/// Returns a new SpacingFunction instance defined by the type and parameters
889std::unique_ptr<SpacingFunction> GetSpacingFunction(const SpacingType type,
890 Array<int> const& ipar,
891 Vector const& dpar);
892}
893#endif
int Size() const
Return the logical size of the array.
Definition array.hpp:166
Bell spacing function, which produces spacing resembling a Bell curve.
Definition spacing.hpp:368
std::unique_ptr< SpacingFunction > Clone() const override
Returns a clone (deep-copy) of this spacing function.
Definition spacing.hpp:434
void ScaleParameters(real_t a) override
Scales parameters by the factor a associated with Size().
Definition spacing.hpp:390
void SetSize(int size) override
Sets the size, or number of intervals (elements).
Definition spacing.hpp:384
void GetDoubleParameters(Vector &p) const override
Definition spacing.hpp:425
void Print(std::ostream &os) const override
Prints all the data necessary to define the spacing function and its current state (size and other pa...
Definition spacing.hpp:406
BellSpacingFunction(int n, bool r, real_t s0, real_t s1, bool s)
Constructor for BellSpacingFunction.
Definition spacing.hpp:378
void GetIntParameters(Array< int > &p) const override
Definition spacing.hpp:417
int NumDoubleParameters() const override
Returns the number of double parameters defining the spacing function.
Definition spacing.hpp:415
real_t Eval(int p) const override
Returns the width of interval p (between 0 and Size() - 1).
Definition spacing.hpp:400
bool Nested() const override
Returns true if the spacing function is nested during refinement.
Definition spacing.hpp:432
SpacingType GetSpacingType() const override
Returns the spacing type, indicating the derived class.
Definition spacing.hpp:413
int NumIntParameters() const override
Returns the number of integer parameters defining the spacing function.
Definition spacing.hpp:414
Gaussian spacing function of the general form g(x) = a exp(-(x-m)^2 / c^2) for some scalar parameters...
Definition spacing.hpp:459
real_t Eval(int p) const override
Returns the width of interval p (between 0 and Size() - 1).
Definition spacing.hpp:491
void GetIntParameters(Array< int > &p) const override
Definition spacing.hpp:508
void ScaleParameters(real_t a) override
Scales parameters by the factor a associated with Size().
Definition spacing.hpp:481
void Print(std::ostream &os) const override
Prints all the data necessary to define the spacing function and its current state (size and other pa...
Definition spacing.hpp:497
int NumDoubleParameters() const override
Returns the number of double parameters defining the spacing function.
Definition spacing.hpp:506
int NumIntParameters() const override
Returns the number of integer parameters defining the spacing function.
Definition spacing.hpp:505
void GetDoubleParameters(Vector &p) const override
Definition spacing.hpp:516
bool Nested() const override
Returns true if the spacing function is nested during refinement.
Definition spacing.hpp:523
std::unique_ptr< SpacingFunction > Clone() const override
Returns a clone (deep-copy) of this spacing function.
Definition spacing.hpp:525
GaussianSpacingFunction(int n, bool r, real_t s0, real_t s1, bool s)
Constructor for GaussianSpacingFunction.
Definition spacing.hpp:469
void SetSize(int size) override
Sets the size, or number of intervals (elements).
Definition spacing.hpp:475
SpacingType GetSpacingType() const override
Returns the spacing type, indicating the derived class.
Definition spacing.hpp:504
Geometric spacing function.
Definition spacing.hpp:287
int NumDoubleParameters() const override
Returns the number of double parameters defining the spacing function.
Definition spacing.hpp:326
std::unique_ptr< SpacingFunction > Clone() const override
Returns a clone (deep-copy) of this spacing function.
Definition spacing.hpp:344
GeometricSpacingFunction(int n, bool r, real_t s, bool scale)
Definition spacing.hpp:289
int NumIntParameters() const override
Returns the number of integer parameters defining the spacing function.
Definition spacing.hpp:325
SpacingType GetSpacingType() const override
Returns the spacing type, indicating the derived class.
Definition spacing.hpp:323
void ScaleParameters(real_t a) override
Scales parameters by the factor a associated with Size().
Definition spacing.hpp:301
void GetDoubleParameters(Vector &p) const override
Definition spacing.hpp:336
void Print(std::ostream &os) const override
Prints all the data necessary to define the spacing function and its current state (size and other pa...
Definition spacing.hpp:316
bool Nested() const override
Returns true if the spacing function is nested during refinement.
Definition spacing.hpp:342
real_t Eval(int p) const override
Returns the width of interval p (between 0 and Size() - 1).
Definition spacing.hpp:310
void GetIntParameters(Array< int > &p) const override
Definition spacing.hpp:328
void SetSize(int size) override
Sets the size, or number of intervals (elements).
Definition spacing.hpp:295
Linear spacing function, defining the width of interval i as s + i * d.
Definition spacing.hpp:191
void Print(std::ostream &os) const override
Prints all the data necessary to define the spacing function and its current state (size and other pa...
Definition spacing.hpp:223
void SetSize(int size) override
Sets the size, or number of intervals (elements).
Definition spacing.hpp:200
int NumIntParameters() const override
Returns the number of integer parameters defining the spacing function.
Definition spacing.hpp:231
void GetDoubleParameters(Vector &p) const override
Definition spacing.hpp:242
bool Nested() const override
Returns true if the spacing function is nested during refinement.
Definition spacing.hpp:248
SpacingType GetSpacingType() const override
Returns the spacing type, indicating the derived class.
Definition spacing.hpp:230
void GetIntParameters(Array< int > &p) const override
Definition spacing.hpp:234
void ScaleParameters(real_t a) override
Scales parameters by the factor a associated with Size().
Definition spacing.hpp:206
real_t Eval(int p) const override
Returns the width of interval p (between 0 and Size() - 1).
Definition spacing.hpp:215
LinearSpacingFunction(int n, bool r, real_t s, bool scale)
Definition spacing.hpp:193
std::unique_ptr< SpacingFunction > Clone() const override
Returns a clone (deep-copy) of this spacing function.
Definition spacing.hpp:250
int NumDoubleParameters() const override
Returns the number of double parameters defining the spacing function.
Definition spacing.hpp:232
Logarithmic spacing function, uniform in log base 10 by default.
Definition spacing.hpp:547
int NumIntParameters() const override
Returns the number of integer parameters defining the spacing function.
Definition spacing.hpp:576
int NumDoubleParameters() const override
Returns the number of double parameters defining the spacing function.
Definition spacing.hpp:577
real_t Eval(int p) const override
Returns the width of interval p (between 0 and Size() - 1).
Definition spacing.hpp:561
bool Nested() const override
Returns true if the spacing function is nested during refinement.
Definition spacing.hpp:593
void Print(std::ostream &os) const override
Prints all the data necessary to define the spacing function and its current state (size and other pa...
Definition spacing.hpp:567
SpacingType GetSpacingType() const override
Returns the spacing type, indicating the derived class.
Definition spacing.hpp:574
void GetIntParameters(Array< int > &p) const override
Definition spacing.hpp:579
void GetDoubleParameters(Vector &p) const override
Definition spacing.hpp:587
LogarithmicSpacingFunction(int n, bool r, bool sym=false, real_t b=10.0)
Definition spacing.hpp:549
std::unique_ptr< SpacingFunction > Clone() const override
Returns a clone (deep-copy) of this spacing function.
Definition spacing.hpp:595
void SetSize(int size) override
Sets the size, or number of intervals (elements).
Definition spacing.hpp:555
Partial spacing function, defined as part of an existing spacing function.
Definition spacing.hpp:791
PartialSpacingFunction(int n, bool r, int rel_first_elem, int rel_num_elems, int rel_num_elems_full, Array< int > const &ipar, Vector const &dpar, SpacingType typeFull)
Definition spacing.hpp:793
bool Nested() const override
Returns true if the spacing function is nested during refinement.
Definition spacing.hpp:872
void Print(std::ostream &os) const override
Prints all the data necessary to define the spacing function and its current state (size and other pa...
Definition spacing.cpp:662
real_t Eval(int p) const override
Returns the width of interval p (between 0 and Size() - 1).
Definition spacing.hpp:827
SpacingType GetSpacingType() const override
Returns the spacing type, indicating the derived class.
Definition spacing.hpp:846
void SetSize(int size) override
Sets the size, or number of intervals (elements).
Definition spacing.hpp:821
void GetIntParameters(Array< int > &p) const override
Definition spacing.hpp:850
void SetupFull(SpacingType typeFull, Array< int > const &ipar, Vector const &dpar)
Definition spacing.cpp:620
int NumDoubleParameters() const override
Returns the number of double parameters defining the spacing function.
Definition spacing.hpp:848
PartialSpacingFunction & operator=(const PartialSpacingFunction &sf)
Definition spacing.hpp:811
PartialSpacingFunction(const PartialSpacingFunction &sf)
Copy constructor (deep-copy all data, including SpacingFunction pieces)
Definition spacing.hpp:805
int NumIntParameters() const override
Returns the number of integer parameters defining the spacing function.
Definition spacing.hpp:847
void ScaleParameters(real_t a) override
Scales parameters by the factor a associated with Size().
Definition spacing.cpp:657
PartialSpacingFunction & operator=(PartialSpacingFunction &&sf)=default
std::unique_ptr< SpacingFunction > Clone() const override
Returns a clone (deep-copy) of this spacing function.
Definition spacing.hpp:837
PartialSpacingFunction(PartialSpacingFunction &&sf)=default
void GetDoubleParameters(Vector &p) const override
Definition spacing.hpp:866
Piecewise spacing function, with spacing functions defining spacing within arbitarily many fixed subi...
Definition spacing.hpp:628
void ScaleParameters(real_t a) override
Scales parameters by the factor a associated with Size().
Definition spacing.cpp:479
PiecewiseSpacingFunction & operator=(PiecewiseSpacingFunction &&sf)=default
PiecewiseSpacingFunction(const PiecewiseSpacingFunction &sf)
Copy constructor (deep-copy all data, including SpacingFunction pieces)
Definition spacing.hpp:652
PiecewiseSpacingFunction(PiecewiseSpacingFunction &&sf)=default
void Print(std::ostream &os) const override
Prints all the data necessary to define the spacing function and its current state (size and other pa...
Definition spacing.cpp:484
Array< int > RelativePieceSizes() const
Definition spacing.hpp:716
bool Nested() const override
Returns true if the spacing function is nested during refinement.
Definition spacing.cpp:607
PiecewiseSpacingFunction(int n, int np, bool r, Array< int > const &relN, Array< int > const &ipar, Vector const &dpar)
Constructor for PiecewiseSpacingFunction.
Definition spacing.hpp:642
real_t Eval(int p) const override
Returns the width of interval p (between 0 and Size() - 1).
Definition spacing.hpp:676
void ScalePartition(Array< int > const &f, bool reorient)
Definition spacing.hpp:718
void GetDoubleParameters(Vector &p) const override
Definition spacing.hpp:754
void SetSize(int size) override
Sets the size, or number of intervals (elements).
Definition spacing.hpp:670
int NumDoubleParameters() const override
Returns the number of double parameters defining the spacing function.
Definition spacing.hpp:706
void SetupPieces(Array< int > const &ipar, Vector const &dpar)
Definition spacing.cpp:414
SpacingType GetSpacingType() const override
Returns the spacing type, indicating the derived class.
Definition spacing.hpp:694
std::unique_ptr< SpacingFunction > Clone() const override
Returns a clone (deep-copy) of this spacing function.
Definition spacing.hpp:686
void GetIntParameters(Array< int > &p) const override
Definition spacing.hpp:730
PiecewiseSpacingFunction & operator=(const PiecewiseSpacingFunction &sf)
Definition spacing.hpp:660
int NumIntParameters() const override
Returns the number of integer parameters defining the spacing function.
Definition spacing.hpp:695
virtual void GetDoubleParameters(Vector &p) const =0
virtual void SetSize(int size)=0
Sets the size, or number of intervals (elements).
bool scale
Whether to scale parameters in ScaleParameters.
Definition spacing.hpp:116
void EvalAll(Vector &s) const
Returns the width of all intervals, resizing s to Size().
Definition spacing.hpp:58
SpacingFunction(int n, bool r=false, bool s=false)
Base class constructor.
Definition spacing.hpp:38
virtual SpacingType GetSpacingType() const =0
Returns the spacing type, indicating the derived class.
virtual int NumDoubleParameters() const =0
Returns the number of double parameters defining the spacing function.
virtual void ScaleParameters(real_t a)
Scales parameters by the factor a associated with Size().
Definition spacing.hpp:72
virtual void Print(std::ostream &os) const =0
Prints all the data necessary to define the spacing function and its current state (size and other pa...
int Size() const
Returns the size, or number of intervals (elements).
Definition spacing.hpp:42
virtual int NumIntParameters() const =0
Returns the number of integer parameters defining the spacing function.
virtual void GetIntParameters(Array< int > &p) const =0
virtual std::unique_ptr< SpacingFunction > Clone() const
Returns a clone (deep-copy) of this spacing function.
Definition spacing.cpp:85
void SetReverse(bool r)
Sets the property that determines whether the spacing is reversed.
Definition spacing.hpp:48
bool GetReverse() const
Definition spacing.hpp:50
virtual ~SpacingFunction()=default
virtual real_t Eval(int p) const =0
Returns the width of interval p (between 0 and Size() - 1).
bool reverse
Whether to reverse the spacing.
Definition spacing.hpp:115
int n
Size, or number of intervals (elements)
Definition spacing.hpp:114
virtual bool Nested() const =0
Returns true if the spacing function is nested during refinement.
Uniform spacing function, dividing the unit interval into Size() equally spaced intervals (elements).
Definition spacing.hpp:124
real_t Eval(int p) const override
Returns the width of interval p (between 0 and Size() - 1).
Definition spacing.hpp:138
void SetSize(int size) override
Sets the size, or number of intervals (elements).
Definition spacing.hpp:132
SpacingType GetSpacingType() const override
Returns the spacing type, indicating the derived class.
Definition spacing.hpp:149
void GetIntParameters(Array< int > &p) const override
Definition spacing.hpp:154
bool Nested() const override
Returns true if the spacing function is nested during refinement.
Definition spacing.hpp:165
int NumDoubleParameters() const override
Returns the number of double parameters defining the spacing function.
Definition spacing.hpp:152
std::unique_ptr< SpacingFunction > Clone() const override
Returns a clone (deep-copy) of this spacing function.
Definition spacing.hpp:167
int NumIntParameters() const override
Returns the number of integer parameters defining the spacing function.
Definition spacing.hpp:151
void Print(std::ostream &os) const override
Prints all the data necessary to define the spacing function and its current state (size and other pa...
Definition spacing.hpp:143
void GetDoubleParameters(Vector &p) const override
Definition spacing.hpp:160
Vector data type.
Definition vector.hpp:82
int Size() const
Returns the size of the vector.
Definition vector.hpp:234
void SetSize(int s)
Resize the vector to size s.
Definition vector.hpp:584
real_t b
Definition lissajous.cpp:42
real_t a
Definition lissajous.cpp:41
std::unique_ptr< SpacingFunction > GetSpacingFunction(const SpacingType spacingType, Array< int > const &ipar, Vector const &dpar)
Returns a new SpacingFunction instance defined by the type and parameters.
Definition spacing.cpp:17
SpacingType
Definition spacing.hpp:23
float real_t
Definition config.hpp:46
std::function< real_t(const Vector &)> f(real_t mass_coeff)
Definition lor_mms.hpp:30
real_t p(const Vector &x, real_t t)