Gradient Augmented Levelset Implementation in CPU & GPU
hermite-2d.cc (Latest change: Author:Lakshman Anumolu <acrlakshman@yahoo.co.in>, 2019-07-21 16:12:22 -0500, [commit: d791c50])
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 
33 
34 #include <math.h>
35 
36 template <typename T>
39  const typename GALS::CPU::Grid<T, 2>::position_type &x_interp,
40  const GALS::CPU::Levelset<GALS::CPU::Grid<T, 2>, T> &levelset, const bool use_gradient_limiting)
41 {
43 
45  const int dim = T_GRID::dim;
46  const int axis_x = 0, axis_y = 1;
48  const GALS::CPU::Vec3<int> base_i_j = grid.baseNodeId(x_interp);
49  const GALS::CPU::Vec3<int> base_ip1_j = GALS::CPU::Vec3<int>(base_i_j[0] + axis_vectors(0, 0), base_i_j[1], 0);
50  const GALS::CPU::Vec3<int> base_i_jp1 = GALS::CPU::Vec3<int>(base_i_j[0], base_i_j[1] + axis_vectors(1, 1), 0);
51  const GALS::CPU::Vec3<int> base_ip1_jp1 =
52  GALS::CPU::Vec3<int>(base_i_j[0] + axis_vectors(0, 0), base_i_j[1] + axis_vectors(1, 1), 0);
53 
54  const typename T_GRID::position_type x_base = grid(base_i_j);
55  const auto &dx = grid.dX();
56  const auto &one_over_dx = grid.oneOverDX();
57 
58  GALS::CPU::Vec3<T> eta = (x_interp - x_base) * one_over_dx;
59 
60  const ControlPoints<T> &control_points_bottom = GALS::INTERPOLATION::get_control_points(
61  levelset.phiPrev()(base_i_j), levelset.psiPrev()(base_i_j)[axis_x], levelset.phiPrev()(base_ip1_j),
62  levelset.psiPrev()(base_ip1_j)[axis_x], dx[axis_x], use_gradient_limiting);
64  levelset.phiPrev()(base_i_jp1), levelset.psiPrev()(base_i_jp1)[axis_x], levelset.phiPrev()(base_ip1_jp1),
65  levelset.psiPrev()(base_ip1_jp1)[axis_x], dx[axis_x], use_gradient_limiting);
66  const ControlPoints<T> &control_points_left = GALS::INTERPOLATION::get_control_points(
67  levelset.phiPrev()(base_i_j), levelset.psiPrev()(base_i_j)[axis_y], levelset.phiPrev()(base_i_jp1),
68  levelset.psiPrev()(base_i_jp1)[axis_y], dx[axis_y], use_gradient_limiting);
69  const ControlPoints<T> &control_points_right = GALS::INTERPOLATION::get_control_points(
70  levelset.phiPrev()(base_ip1_j), levelset.psiPrev()(base_ip1_j)[axis_y], levelset.phiPrev()(base_ip1_jp1),
71  levelset.psiPrev()(base_ip1_jp1)[axis_y], dx[axis_y], use_gradient_limiting);
72 
73  T c_1 =
74  levelset.psiPrev()(base_i_j)[axis_y] + dx[axis_x] * one_third * levelset.phiMixedDerivativesPrev()(base_i_j)[0];
75  T c_2 = levelset.psiPrev()(base_ip1_j)[axis_x] +
76  dx[axis_y] * one_third * levelset.phiMixedDerivativesPrev()(base_ip1_j)[0];
77  T c_2_prime = levelset.psiPrev()(base_ip1_jp1)[axis_x] -
78  dx[axis_y] * one_third * levelset.phiMixedDerivativesPrev()(base_ip1_jp1)[0];
79  T c_3 = levelset.psiPrev()(base_i_jp1)[axis_y] +
80  dx[axis_x] * one_third * levelset.phiMixedDerivativesPrev()(base_i_jp1)[0];
81 
82  T bx[] = {B0(eta[0]), B1(eta[0]), B2(eta[0]), B3(eta[0])};
83  T bx_prime[] = {B0_Prime(eta[0]), B1_Prime(eta[0]), B2_Prime(eta[0]), B3_Prime(eta[0])};
84  T by[] = {B0(eta[1]), B1(eta[1]), B2(eta[1]), B3(eta[1])};
85  T by_prime[] = {B0_Prime(eta[1]), B1_Prime(eta[1]), B2_Prime(eta[1]), B3_Prime(eta[1])};
86 
87  T control_points_all[] = {control_points_left.c_30,
88  control_points_left.c_21,
89  control_points_left.c_12,
90  control_points_left.c_03,
91  control_points_bottom.c_21,
92  control_points_bottom.c_21 + dx[axis_y] * one_third * c_1,
93  control_points_top.c_21 - dx[axis_y] * one_third * c_3,
94  control_points_top.c_21,
95  control_points_bottom.c_12,
96  control_points_right.c_21 - dx[axis_x] * one_third * c_2,
97  control_points_right.c_12 - dx[axis_x] * one_third * c_2_prime,
98  control_points_top.c_12,
99  control_points_right.c_30,
100  control_points_right.c_21,
101  control_points_right.c_12,
102  control_points_right.c_03};
103 
104  for (int i = 0, k = 0; i < 4; ++i)
105  for (int j = 0; j < 4; ++j) {
106  hermite_fields.phi_interpolated += bx[i] * by[j] * control_points_all[k];
107  ++k;
108  }
109 
110  for (int i = 0, k = 0; i < 4; ++i)
111  for (int j = 0; j < 4; ++j) {
112  hermite_fields.psi_interpolated[axis_x] += bx_prime[i] * by[j] * control_points_all[k];
113  ++k;
114  }
115  hermite_fields.psi_interpolated[axis_x] *= one_over_dx[axis_x];
116 
117  for (int i = 0, k = 0; i < 4; ++i)
118  for (int j = 0; j < 4; ++j) {
119  hermite_fields.psi_interpolated[axis_y] += bx[i] * by_prime[j] * control_points_all[k];
120  ++k;
121  }
122  hermite_fields.psi_interpolated[axis_y] *= one_over_dx[axis_y];
123 
124  return hermite_fields;
125 }
126 
127 template <typename T>
131 {
133 
134  const GALS::CPU::Vec3<int> num_cells_interp = x_interp.numCells();
135  const T_GRID &grid = levelset.grid();
138 
139  for (int i = 0; i < num_cells_interp[0]; ++i)
140  for (int j = 0; j < num_cells_interp[1]; ++j)
141  for (int k = 0; k < num_cells_interp[2]; ++k) {
142  const auto &hermite_fields = this->interpolate(grid, x_interp(i, j, k), levelset);
143 
144  levelset.phiInterpPrev()(i, j, k) = hermite_fields.phi_interpolated;
145  levelset.psiInterpPrev()(i, j, k) = hermite_fields.psi_interpolated;
146  }
147 }
148 
T B0(T eta)
cubic Bernstein polynomials
Definition: hermite.h:57
T B0_Prime(T eta)
Definition: hermite.h:81
const Vec3< T > & dX() const
Definition: grid.cc:144
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
T B1_Prime(T eta)
Definition: hermite.h:87