ViennaLS
Loading...
Searching...
No Matches
lsEngquistOsher.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <hrleSparseStarIterator.hpp>
4
5#include <lsDomain.hpp>
6#include <lsExpand.hpp>
7#include <lsVelocityField.hpp>
8
9#include <vcVectorType.hpp>
10
11namespace lsInternal {
12
13using namespace viennacore;
14
18template <class T, int D, int order> class EngquistOsher {
19 SmartPointer<viennals::Domain<T, D>> levelSet;
20 SmartPointer<viennals::VelocityField<T>> velocities;
21 viennahrle::ConstSparseStarIterator<viennahrle::Domain<T, D>, order>
22 neighborIterator;
23 const bool calculateNormalVectors;
24 const double gridDelta;
25
26 static T pow2(const T &value) { return value * value; }
27
28public:
29 static void prepareLS(SmartPointer<viennals::Domain<T, D>> &passedlsDomain) {
30 assert(order == 1 || order == 2);
31 viennals::Expand<T, D>(passedlsDomain, 2 * order + 1).apply();
32 }
33
34 EngquistOsher(SmartPointer<viennals::Domain<T, D>> passedlsDomain,
35 SmartPointer<viennals::VelocityField<T>> vel,
36 bool calcNormal = true)
37 : levelSet(passedlsDomain), velocities(vel),
38 neighborIterator(levelSet->getDomain()),
39 calculateNormalVectors(calcNormal),
40 gridDelta(levelSet->getGrid().getGridDelta()) {}
41
42 std::pair<T, T> operator()(const viennahrle::Index<D> &indices,
43 int material) {
44
45 VectorType<T, 3> coordinate{0., 0., 0.};
46 for (unsigned i = 0; i < D; ++i) {
47 coordinate[i] = indices[i] * gridDelta;
48 }
49
50 // move neighborIterator to current position
51 neighborIterator.goToIndicesSequential(indices);
52
53 T gradPos[D];
54 T gradNeg[D];
55
56 T gradPosTotal = 0;
57 T gradNegTotal = 0;
58
59 for (int i = 0; i < D; i++) {
60 const T deltaPos = gridDelta;
61 const T deltaNeg = -gridDelta;
62
63 const T phi0 = neighborIterator.getCenter().getValue();
64 const T phiPos = neighborIterator.getNeighbor(i).getValue();
65 const T phiNeg = neighborIterator.getNeighbor(i + D).getValue();
66
67 T diffPos = (phiPos - phi0) / deltaPos;
68 T diffNeg = (phiNeg - phi0) / deltaNeg;
69
70 if constexpr (order == 2) { // if second order spatial discretization
71 // 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 = 1. / std::sqrt(denominator);
135 for (unsigned i = 0; i < D; ++i) {
136 normalVector[i] *= denominator;
137 }
138 }
139
140 T scalarVelocity = velocities->getScalarVelocity(
141 coordinate, material, normalVector,
142 neighborIterator.getCenter().getPointId());
143 Vec3D<T> vectorVelocity = velocities->getVectorVelocity(
144 coordinate, material, normalVector,
145 neighborIterator.getCenter().getPointId());
146
147 if (scalarVelocity > 0) {
148 vel_grad += std::sqrt(gradPosTotal) * scalarVelocity;
149 } else {
150 vel_grad += std::sqrt(gradNegTotal) * scalarVelocity;
151 }
152
153 for (int w = 0; w < D; w++) {
154 if (vectorVelocity[w] > 0.) {
155 vel_grad += vectorVelocity[w] * gradPos[w];
156 } else {
157 vel_grad += vectorVelocity[w] * gradNeg[w];
158 }
159 }
160
161 return {vel_grad, 0.};
162 }
163
164 void reduceTimeStepHamiltonJacobi(double &MaxTimeStep,
165 double gridDelta) const {}
166};
167
168} // namespace lsInternal
constexpr int D
Definition Epitaxy.cpp:12
double T
Definition Epitaxy.cpp:13
void reduceTimeStepHamiltonJacobi(double &MaxTimeStep, double gridDelta) const
Definition lsEngquistOsher.hpp:164
static void prepareLS(SmartPointer< viennals::Domain< T, D > > &passedlsDomain)
Definition lsEngquistOsher.hpp:29
std::pair< T, T > operator()(const viennahrle::Index< D > &indices, int material)
Definition lsEngquistOsher.hpp:42
EngquistOsher(SmartPointer< viennals::Domain< T, D > > passedlsDomain, SmartPointer< viennals::VelocityField< T > > vel, bool calcNormal=true)
Definition lsEngquistOsher.hpp:34
Class containing all information about the level set, including the dimensions of the domain,...
Definition lsDomain.hpp:27
Expands the levelSet to the specified number of layers. The largest value in the levelset is thus wid...
Definition lsExpand.hpp:17
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:11
Definition lsAdvectIntegrationSchemes.hpp:41