Gradient Augmented Levelset Implementation in CPU & GPU
hermite.h (Latest change: Author:Lakshman Anumolu <acrlakshman@yahoo.co.in>, 2019-07-14 14:15:13 -0500, [commit: d5cbae7])
Go to the documentation of this file.
1 // Copyright 2019 Lakshman Anumolu, Raunak Bardia.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 #pragma once
33 
34 #include "gals/cpu/levelset.h"
35 #include "gals/utilities/array.h"
36 #include "gals/utilities/grid.h"
37 #include "gals/utilities/mat3.h"
39 #include "gals/utilities/vec3.h"
40 
41 namespace GALS
42 {
43 namespace INTERPOLATION
44 {
49 template <typename T>
50 struct ControlPoints {
52 };
53 
54 // TODO (lakshman): Cleanup below functions.
56 template <typename T = double>
57 T B0(T eta)
58 {
59  return cube((T)1.0 - eta);
60 }
61 
62 template <typename T = double>
63 T B1(T eta)
64 {
65  return (T)3.0 * eta * sqr((T)1.0 - eta);
66 }
67 
68 template <typename T = double>
69 T B2(T eta)
70 {
71  return (T)3.0 * sqr(eta) * ((T)1.0 - eta);
72 }
73 
74 template <typename T = double>
75 T B3(T eta)
76 {
77  return cube(eta);
78 }
79 
80 template <typename T = double>
81 T B0_Prime(T eta)
82 {
83  return -(T)3.0 * sqr((T)1.0 - eta);
84 }
85 
86 template <typename T = double>
87 T B1_Prime(T eta)
88 {
89  return (T)3 * ((T)1 - eta) * ((T)1 - (T)3 * eta);
90 }
91 
92 template <typename T = double>
93 T B2_Prime(T eta)
94 {
95  return (T)3 * eta * ((T)2 - (T)3 * eta);
96 }
97 
98 template <typename T = double>
99 T B3_Prime(T eta)
100 {
101  return (T)3 * sqr(eta);
102 }
103 
115 template <typename T>
116 static struct ControlPoints<T> get_control_points(const T &phi_0, const T &phi_x_0, const T &phi_1, const T &phi_x_1,
117  const T &dx, bool use_gradient_limiting = false) {
118  struct ControlPoints<T> control_points;
119  control_points.c_30 = phi_0;
120  control_points.c_21 = phi_0 + (phi_x_0 * dx * (T)one_third);
121  control_points.c_12 = phi_1 - (phi_x_1 * dx * (T)one_third);
122  control_points.c_03 = phi_1;
123 
124  if (use_gradient_limiting) {
125  // Implementation of gradient limiting algorithm by Bockmann et al. JCP 258(2014)
126  T D1 = (T)3.0 * (control_points.c_21 - control_points.c_30);
127  T D2 = (T)3.0 * (control_points.c_03 - control_points.c_12);
128  T D3 = (T)1.5 * (control_points.c_12 - control_points.c_30);
129  T D4 = (T)1.5 * (control_points.c_03 - control_points.c_21);
130  T D5 = control_points.c_03 - control_points.c_30;
131 
132  if ((D1 - D3) * (D1 - D5) < (T)0)
133  control_points.c_12 = (T)2 * control_points.c_21 - control_points.c_30;
134  else if ((D2 - D4) * (D2 - D5) < (T)0)
135  control_points.c_21 = (T)2 * control_points.c_12 - control_points.c_03;
136  else if ((D1 - D5) * (D2 - D5) <= (T)0) {
137  control_points.c_21 = (two_thirds * control_points.c_30) + (one_third * control_points.c_03);
138  control_points.c_12 = (two_thirds * control_points.c_03) + (one_third * control_points.c_30);
139  }
140  }
141 
142  return control_points;
143 }
144 
149 template <typename T, typename T_GRID>
150 class Hermite;
151 
156 // Template specialized for 1D
157 template <typename T>
158 class Hermite<T, GALS::CPU::Grid<T, 1>>
159 {
160  public:
161  using value_type = T;
163 
166  Hermite(){};
167 
170  ~Hermite(){};
171 
181  const typename GALS::CPU::Grid<T, 1>::position_type &x_interp,
182  const CPU::Levelset<GALS::CPU::Grid<T, 1>, T> &levelset, const bool use_gradient_limiting = false);
183 
189  void compute(const GALS::CPU::Array<GALS::CPU::Grid<T, 1>, typename GALS::CPU::Grid<T, 1>::position_type> &x_interp,
190  CPU::Levelset<GALS::CPU::Grid<T, 1>, T> &levelset);
191 
199  CPU::Levelset<GALS::CPU::Grid<T, 1>, T> &levelset)
200  {
201  compute(x_interp, levelset);
202  }
203 
214  const GALS::CPU::Array<GALS::CPU::Grid<T, 1>, T> &alpha,
215  GALS::CPU::Array<GALS::CPU::Grid<T, 1>, T> &alpha_interpolated)
216  {
218  }
219 };
220 
225 // Template specialized for 2D
226 template <typename T>
227 class Hermite<T, GALS::CPU::Grid<T, 2>>
228 {
229  public:
230  using value_type = T;
232 
235  Hermite(){};
236 
239  ~Hermite(){};
240 
250  const typename GALS::CPU::Grid<T, 2>::position_type &x_interp,
251  const CPU::Levelset<GALS::CPU::Grid<T, 2>, T> &levelset, const bool use_gradient_limiting = false);
252 
258  void compute(const GALS::CPU::Array<GALS::CPU::Grid<T, 2>, typename GALS::CPU::Grid<T, 2>::position_type> &x_interp,
259  CPU::Levelset<GALS::CPU::Grid<T, 2>, T> &levelset);
260 
268  CPU::Levelset<GALS::CPU::Grid<T, 2>, T> &levelset)
269  {
270  compute(x_interp, levelset);
271  }
272 
283  const GALS::CPU::Array<GALS::CPU::Grid<T, 2>, T> &alpha,
284  GALS::CPU::Array<GALS::CPU::Grid<T, 2>, T> &alpha_interpolated)
285  {
287  }
288 };
289 
294 // Template specialized for 3D
295 template <typename T>
296 class Hermite<T, GALS::CPU::Grid<T, 3>>
297 {
298  public:
299  using value_type = T;
301 
304  Hermite(){};
305 
308  ~Hermite(){};
309 
319  const typename GALS::CPU::Grid<T, 3>::position_type &x_interp,
320  const CPU::Levelset<GALS::CPU::Grid<T, 3>, T> &levelset, const bool use_gradient_limiting = false);
321 
327  void compute(const GALS::CPU::Array<GALS::CPU::Grid<T, 3>, typename GALS::CPU::Grid<T, 3>::position_type> &x_interp,
328  CPU::Levelset<GALS::CPU::Grid<T, 3>, T> &levelset);
329 
337  CPU::Levelset<GALS::CPU::Grid<T, 3>, T> &levelset)
338  {
339  compute(x_interp, levelset);
340  }
341 
352  const GALS::CPU::Array<GALS::CPU::Grid<T, 3>, T> &alpha,
353  GALS::CPU::Array<GALS::CPU::Grid<T, 3>, T> &alpha_interpolated)
354  {
356  }
357 };
358 
359 } // namespace INTERPOLATION
360 } // namespace GALS
void operator()(const GALS::CPU::Array< GALS::CPU::Grid< T, 3 >, typename GALS::CPU::Grid< T, 3 >::position_type > &x_interp, const GALS::CPU::Array< GALS::CPU::Grid< T, 3 >, T > &alpha, GALS::CPU::Array< GALS::CPU::Grid< T, 3 >, T > &alpha_interpolated)
Definition: hermite.h:350
void operator()(const GALS::CPU::Array< GALS::CPU::Grid< T, 2 >, typename GALS::CPU::Grid< T, 2 >::position_type > &x_interp, CPU::Levelset< GALS::CPU::Grid< T, 2 >, T > &levelset)
Definition: hermite.h:266
void operator()(const GALS::CPU::Array< GALS::CPU::Grid< T, 2 >, typename GALS::CPU::Grid< T, 2 >::position_type > &x_interp, const GALS::CPU::Array< GALS::CPU::Grid< T, 2 >, T > &alpha, GALS::CPU::Array< GALS::CPU::Grid< T, 2 >, T > &alpha_interpolated)
Definition: hermite.h:281
void operator()(const GALS::CPU::Array< GALS::CPU::Grid< T, 3 >, typename GALS::CPU::Grid< T, 3 >::position_type > &x_interp, CPU::Levelset< GALS::CPU::Grid< T, 3 >, T > &levelset)
Definition: hermite.h:335
T B0(T eta)
cubic Bernstein polynomials
Definition: hermite.h:57
static const double two_thirds
Definition: utilities.h:59
#define GALS_FUNCTION_NOT_IMPLEMENTED(var)
Definition: utilities.h:40
T B0_Prime(T eta)
Definition: hermite.h:81
void operator()(const GALS::CPU::Array< GALS::CPU::Grid< T, 1 >, typename GALS::CPU::Grid< T, 1 >::position_type > &x_interp, const GALS::CPU::Array< GALS::CPU::Grid< T, 1 >, T > &alpha, GALS::CPU::Array< GALS::CPU::Grid< T, 1 >, T > &alpha_interpolated)
Definition: hermite.h:212
T value_type
Definition: grid.h:71
T B2_Prime(T eta)
Definition: hermite.h:93
static const double one_third
Definition: utilities.h:58
static struct ControlPoints< T > get_control_points(const T &phi_0, const T &phi_x_0, const T &phi_1, const T &phi_x_1, const T &dx, bool use_gradient_limiting=false)
Definition: hermite.h:116
T B3_Prime(T eta)
Definition: hermite.h:99
static T sqr(T a)
Compute square.
Definition: utilities.h:77
static T cube(T a)
Compute cube.
Definition: utilities.h:84
T B1_Prime(T eta)
Definition: hermite.h:87
void operator()(const GALS::CPU::Array< GALS::CPU::Grid< T, 1 >, typename GALS::CPU::Grid< T, 1 >::position_type > &x_interp, CPU::Levelset< GALS::CPU::Grid< T, 1 >, T > &levelset)
Definition: hermite.h:197