ViennaLS
Loading...
Searching...
No Matches
lsEnquistOsher.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <hrleSparseStarIterator.hpp>
4#include <hrleVectorType.hpp>
5
6#include <lsDomain.hpp>
7#include <lsExpand.hpp>
8#include <lsVelocityField.hpp>
9
10#include <vcVectorUtil.hpp>
11
12namespace lsInternal {
13
14using namespace viennacore;
15
19template <class T, int D, int order> class EnquistOsher {
20 SmartPointer<viennals::Domain<T, D>> levelSet;
21 SmartPointer<viennals::VelocityField<T>> velocities;
22 hrleSparseStarIterator<hrleDomain<T, D>, order> neighborIterator;
23 bool calculateNormalVectors = true;
24
25 static T pow2(const T &value) { return value * value; }
26
27public:
28 static void prepareLS(SmartPointer<viennals::Domain<T, D>> passedlsDomain) {
29 assert(order == 1 || order == 2);
30 viennals::Expand<T, D>(passedlsDomain, 2 * order + 1).apply();
31 }
32
33 EnquistOsher(SmartPointer<viennals::Domain<T, D>> passedlsDomain,
34 SmartPointer<viennals::VelocityField<T>> vel,
35 bool calcNormal = true)
36 : levelSet(passedlsDomain), velocities(vel),
37 neighborIterator(hrleSparseStarIterator<hrleDomain<T, D>, order>(
38 levelSet->getDomain())),
39 calculateNormalVectors(calcNormal) {}
40
41 std::pair<T, T> operator()(const hrleVectorType<hrleIndexType, D> &indices,
42 int material) {
43 auto &grid = levelSet->getGrid();
44 double gridDelta = grid.getGridDelta();
45
46 hrleVectorType<T, 3> coordinate(0., 0., 0.);
47 for (unsigned i = 0; i < D; ++i) {
48 coordinate[i] = indices[i] * gridDelta;
49 }
50
51 // move neighborIterator to current position
52 neighborIterator.goToIndicesSequential(indices);
53
54 T gradPos[D];
55 T gradNeg[D];
56
57 T gradPosTotal = 0;
58 T gradNegTotal = 0;
59
60 for (int i = 0; i < D; i++) {
61 const T deltaPos = gridDelta;
62 const T deltaNeg = -gridDelta;
63
64 const T phi0 = neighborIterator.getCenter().getValue();
65 const T phiPos = neighborIterator.getNeighbor(i).getValue();
66 const T phiNeg = neighborIterator.getNeighbor(i + D).getValue();
67
68 T diffPos = (phiPos - phi0) / deltaPos;
69 T diffNeg = (phiNeg - phi0) / deltaNeg;
70
71 if (order == 2) { // if second order time integration scheme is used
72 const T deltaPosPos = 2 * gridDelta;
73 const T deltaNegNeg = -2 * gridDelta;
74
75 const T phiPosPos =
76 neighborIterator.getNeighbor((D * order) + i).getValue();
77 const T phiNegNeg =
78 neighborIterator.getNeighbor((D * order) + D + i).getValue();
79
80 const T diff00 =
81 (((deltaNeg * phiPos - deltaPos * phiNeg) / (deltaPos - deltaNeg) +
82 phi0)) /
83 (deltaPos * deltaNeg);
84 const T diffNegNeg = (((deltaNeg * phiNegNeg - deltaNegNeg * phiNeg) /
85 (deltaNegNeg - deltaNeg) +
86 phi0)) /
87 (deltaNegNeg * deltaNeg);
88 const T diffPosPos = (((deltaPos * phiPosPos - deltaPosPos * phiPos) /
89 (deltaPosPos - deltaPos) +
90 phi0)) /
91 (deltaPosPos * deltaPos);
92
93 if (std::signbit(diff00) == std::signbit(diffPosPos)) {
94 if (std::abs(diffPosPos * deltaPos) < std::abs(diff00 * deltaNeg)) {
95 diffPos -= deltaPos * diffPosPos;
96 } else {
97 diffPos += deltaNeg * diff00;
98 }
99 }
100
101 if (std::signbit(diff00) == std::signbit(diffNegNeg)) {
102 if (std::abs(diffNegNeg * deltaNeg) < std::abs(diff00 * deltaPos)) {
103 diffNeg -= deltaNeg * diffNegNeg;
104 } else {
105 diffNeg += deltaPos * diff00;
106 }
107 }
108 }
109
110 gradPos[i] = diffNeg;
111 gradNeg[i] = diffPos;
112
113 gradPosTotal +=
114 pow2(std::max(diffNeg, T(0))) + pow2(std::min(diffPos, T(0)));
115 gradNegTotal +=
116 pow2(std::min(diffNeg, T(0))) + pow2(std::max(diffPos, T(0)));
117 }
118
119 T vel_grad = 0.;
120
121 // Calculate normal vector for velocity calculation
122 // use std::array since it will be exposed to interface
123 Vec3D<T> normalVector = {};
124 if (calculateNormalVectors) {
125 T denominator = 0;
126 for (int i = 0; i < D; i++) {
127 T pos = neighborIterator.getNeighbor(i).getValue() -
128 neighborIterator.getCenter().getValue();
129 T neg = neighborIterator.getCenter().getValue() -
130 neighborIterator.getNeighbor(i + D).getValue();
131 normalVector[i] = (pos + neg) * 0.5; // = 0;
132 denominator += normalVector[i] * normalVector[i];
133 }
134 denominator = std::sqrt(denominator);
135 for (unsigned i = 0; i < D; ++i) {
136 normalVector[i] /= denominator;
137 }
138 }
139
140 // convert coordinate to std array for interface
141 Vec3D<T> coordArray = {coordinate[0], coordinate[1], coordinate[2]};
142
143 double scalarVelocity = velocities->getScalarVelocity(
144 coordArray, material, normalVector,
145 neighborIterator.getCenter().getPointId());
146 Vec3D<T> vectorVelocity = velocities->getVectorVelocity(
147 coordArray, material, normalVector,
148 neighborIterator.getCenter().getPointId());
149
150 if (scalarVelocity > 0) {
151 vel_grad += std::sqrt(gradPosTotal) * scalarVelocity;
152 } else {
153 vel_grad += std::sqrt(gradNegTotal) * scalarVelocity;
154 }
155
156 for (int w = 0; w < D; w++) {
157 if (vectorVelocity[w] > 0.) {
158 vel_grad += vectorVelocity[w] * gradPos[w];
159 } else {
160 vel_grad += vectorVelocity[w] * gradNeg[w];
161 }
162 }
163
164 return {vel_grad, 0.};
165 }
166
167 void reduceTimeStepHamiltonJacobi(double &MaxTimeStep,
168 hrleCoordType gridDelta) {}
169};
170
171} // namespace lsInternal
EnquistOsher(SmartPointer< viennals::Domain< T, D > > passedlsDomain, SmartPointer< viennals::VelocityField< T > > vel, bool calcNormal=true)
Definition lsEnquistOsher.hpp:33
std::pair< T, T > operator()(const hrleVectorType< hrleIndexType, D > &indices, int material)
Definition lsEnquistOsher.hpp:41
void reduceTimeStepHamiltonJacobi(double &MaxTimeStep, hrleCoordType gridDelta)
Definition lsEnquistOsher.hpp:167
static void prepareLS(SmartPointer< viennals::Domain< T, D > > passedlsDomain)
Definition lsEnquistOsher.hpp:28
Class containing all information about the level set, including the dimensions of the domain,...
Definition lsDomain.hpp:27
Expands the leveleSet to the specified number of layers. The largest value in the levelset is thus wi...
Definition lsExpand.hpp:16
void apply()
Apply the expansion to the specified width.
Definition lsExpand.hpp:44
Abstract class defining the interface for the velocity field used during advection using lsAdvect.
Definition lsVelocityField.hpp:13
Definition lsCurvatureFormulas.hpp:9
constexpr int D
Definition pyWrap.cpp:66
double T
Definition pyWrap.cpp:64