Skip to content

Commit 35f113e

Browse files
authored
Add applySystemOperator(result, x) to Residual class for PCG support (#186)
1 parent a5f97e6 commit 35f113e

File tree

8 files changed

+112
-87
lines changed

8 files changed

+112
-87
lines changed

include/Residual/ResidualGive/residualGive.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class ResidualGive : public Residual
1212

1313
void computeResidual(Vector<double> result, ConstVector<double> rhs, ConstVector<double> x) const override;
1414

15+
void applySystemOperator(Vector<double> result, ConstVector<double> x) const override;
16+
1517
private:
1618
void applyCircleSection(const int i_r, Vector<double> result, ConstVector<double> x) const;
1719
void applyRadialSection(const int i_theta, Vector<double> result, ConstVector<double> x) const;

include/Residual/ResidualTake/residualTake.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ class ResidualTake : public Residual
1212

1313
void computeResidual(Vector<double> result, ConstVector<double> rhs, ConstVector<double> x) const override;
1414

15+
void applySystemOperator(Vector<double> result, ConstVector<double> x) const override;
16+
1517
private:
16-
void applyCircleSection(const int i_r, Vector<double> result, ConstVector<double> rhs, ConstVector<double> x) const;
17-
void applyRadialSection(const int i_theta, Vector<double> result, ConstVector<double> rhs,
18-
ConstVector<double> x) const;
18+
void applyCircleSection(const int i_r, Vector<double> result, ConstVector<double> x) const;
19+
void applyRadialSection(const int i_theta, Vector<double> result, ConstVector<double> x) const;
1920
};

include/Residual/residual.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Residual
2525

2626
virtual void computeResidual(Vector<double> result, ConstVector<double> rhs, ConstVector<double> x) const = 0;
2727

28+
virtual void applySystemOperator(Vector<double> result, ConstVector<double> x) const = 0;
29+
2830
protected:
2931
/* ------------------- */
3032
/* Constructor members */

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ set(RESIDUAL_SOURCES
104104
${CMAKE_CURRENT_SOURCE_DIR}/Residual/ResidualGive/residualGive.cpp
105105

106106
# ResidualTake
107-
${CMAKE_CURRENT_SOURCE_DIR}/Residual/ResidualTake/applyResidualTake.cpp
107+
${CMAKE_CURRENT_SOURCE_DIR}/Residual/ResidualTake/applyATake.cpp
108108
${CMAKE_CURRENT_SOURCE_DIR}/Residual/ResidualTake/residualTake.cpp
109109
)
110110

src/Residual/ResidualGive/applyAGive.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,30 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
3434
const int top = grid.index(i_r, i_theta_P1);
3535

3636
/* Fill result(i,j) */
37-
result[center] -= (0.25 * (h1 + h2) * (k1 + k2) * coeff_beta * std::fabs(detDF) * x[center] /* beta_{i,j} */
37+
result[center] += (0.25 * (h1 + h2) * (k1 + k2) * coeff_beta * std::fabs(detDF) * x[center] /* beta_{i,j} */
3838
- coeff1 * arr * x[left] /* Left */
3939
- coeff2 * arr * x[right] /* Right */
4040
- coeff3 * att * x[bottom] /* Bottom */
4141
- coeff4 * att * x[top] /* Top */
4242
+ ((coeff1 + coeff2) * arr + (coeff3 + coeff4) * att) *
4343
x[center]) /* Center: (Left, Right, Bottom, Top) */;
4444
/* Fill result(i-1,j) */
45-
result[left] -= (-coeff1 * arr * x[center] /* Right */
45+
result[left] += (-coeff1 * arr * x[center] /* Right */
4646
+ coeff1 * arr * x[left] /* Center: (Right) */
4747
- 0.25 * art * x[top] /* Top Right */
4848
+ 0.25 * art * x[bottom]); /* Bottom Right */
4949
/* Fill result(i+1,j) */
50-
result[right] -= (-coeff2 * arr * x[center] /* Left */
50+
result[right] += (-coeff2 * arr * x[center] /* Left */
5151
+ coeff2 * arr * x[right] /* Center: (Left) */
5252
+ 0.25 * art * x[top] /* Top Left */
5353
- 0.25 * art * x[bottom]); /* Bottom Left */
5454
/* Fill result(i,j-1) */
55-
result[bottom] -= (-coeff3 * att * x[center] /* Top */
55+
result[bottom] += (-coeff3 * att * x[center] /* Top */
5656
+ coeff3 * att * x[bottom] /* Center: (Top) */
5757
- 0.25 * art * x[right] /* Top Right */
5858
+ 0.25 * art * x[left]); /* Top Left */
5959
/* Fill result(i,j+1) */
60-
result[top] -= (-coeff4 * att * x[center] /* Bottom */
60+
result[top] += (-coeff4 * att * x[center] /* Bottom */
6161
+ coeff4 * att * x[top] /* Center: (Bottom) */
6262
+ 0.25 * art * x[right] /* Bottom Right */
6363
- 0.25 * art * x[left]); /* Bottom Left */
@@ -71,7 +71,7 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
7171
/* ------------------------------------------------ */
7272
if (DirBC_Interior) {
7373
/* Fill result(i,j) */
74-
result[center] -= x[center];
74+
result[center] += x[center];
7575

7676
/* Give value to the interior nodes! */
7777
double h2 = grid.radialSpacing(i_r);
@@ -88,7 +88,7 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
8888
const int top = grid.index(i_r, i_theta_P1);
8989

9090
/* Fill result(i+1,j) */
91-
result[right] -= (-coeff2 * arr * x[center] /* Left */
91+
result[right] += (-coeff2 * arr * x[center] /* Left */
9292
+ coeff2 * arr * x[right] /* Center: (Left) */
9393
+ 0.25 * art * x[top] /* Top Left */
9494
- 0.25 * art * x[bottom]); /* Bottom Left */
@@ -120,7 +120,7 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
120120
const int top = grid.index(i_r, i_theta_P1);
121121

122122
/* Fill result(i,j) */
123-
result[center] -= (0.25 * (h1 + h2) * (k1 + k2) * coeff_beta * std::fabs(detDF) * x[center] /* beta_{i,j} */
123+
result[center] += (0.25 * (h1 + h2) * (k1 + k2) * coeff_beta * std::fabs(detDF) * x[center] /* beta_{i,j} */
124124
- coeff1 * arr * x[left] /* Left */
125125
- coeff2 * arr * x[right] /* Right */
126126
- coeff3 * att * x[bottom] /* Bottom */
@@ -129,22 +129,22 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
129129
x[center]); /* Center: (Left, Right, Bottom, Top) */
130130
/* Fill result(i-1,j) */
131131
/* From view the view of the across origin node, the directions are roatated by 180 degrees in the stencil! */
132-
result[left] -= (-coeff1 * arr * x[center] /* Right -> Left */
132+
result[left] += (-coeff1 * arr * x[center] /* Right -> Left */
133133
+ coeff1 * arr * x[left]); /* Center: (Right) -> Center: (Left)*/
134134
/* + 0.25 * art * x[top]; // Top Right -> Bottom Left: REMOVED DUE TO ARTIFICAL 7 POINT STENCIL */
135135
/* - 0.25 * art * x[bottom]; // Bottom Right -> Top Left: REMOVED DUE TO ARTIFICAL 7 POINT STENCIL */
136136
/* Fill result(i+1,j) */
137-
result[right] -= (-coeff2 * arr * x[center] /* Left */
137+
result[right] += (-coeff2 * arr * x[center] /* Left */
138138
+ coeff2 * arr * x[right] /* Center: (Left) */
139139
+ 0.25 * art * x[top] /* Top Left */
140140
- 0.25 * art * x[bottom]); /* Bottom Left */
141141
/* Fill result(i,j-1) */
142-
result[bottom] -= (-coeff3 * att * x[center] /* Top */
142+
result[bottom] += (-coeff3 * att * x[center] /* Top */
143143
+ coeff3 * att * x[bottom] /* Center: (Top) */
144144
- 0.25 * art * x[right]); /* Top Right */
145145
/* + 0.25 * art * x[left]; // Top Left: REMOVED DUE TO ARTIFICAL 7 POINT STENCIL */
146146
/* Fill result(i,j+1) */
147-
result[top] -= (-coeff4 * att * x[center] /* Bottom */
147+
result[top] += (-coeff4 * att * x[center] /* Bottom */
148148
+ coeff4 * att * x[top] /* Center: (Bottom) */
149149
+ 0.25 * art * x[right]); /* Bottom Right */
150150
/* - 0.25 * art * x[left]; // Bottom Left: REMOVED DUE TO ARTIFICAL 7 POINT STENCIL */
@@ -173,7 +173,7 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
173173
const int top = grid.index(i_r, i_theta_P1);
174174

175175
/* Fill result(i,j) */
176-
result[center] -= (0.25 * (h1 + h2) * (k1 + k2) * coeff_beta * std::fabs(detDF) * x[center] /* beta_{i,j} */
176+
result[center] += (0.25 * (h1 + h2) * (k1 + k2) * coeff_beta * std::fabs(detDF) * x[center] /* beta_{i,j} */
177177
- coeff1 * arr * x[left] /* Left */
178178
- coeff2 * arr * x[right] /* Right */
179179
- coeff3 * att * x[bottom] /* Bottom */
@@ -182,23 +182,23 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
182182
x[center]); /* Center: (Left, Right, Bottom, Top) */
183183
/* Fill result(i-1,j) */
184184
if (!DirBC_Interior) { /* Don't give to the inner dirichlet boundary! */
185-
result[left] -= (-coeff1 * arr * x[center] /* Right */
185+
result[left] += (-coeff1 * arr * x[center] /* Right */
186186
+ coeff1 * arr * x[left] /* Center: (Right) */
187187
- 0.25 * art * x[top] /* Top Right */
188188
+ 0.25 * art * x[bottom]); /* Bottom Right */
189189
}
190190
/* Fill result(i+1,j) */
191-
result[right] -= (-coeff2 * arr * x[center] /* Left */
191+
result[right] += (-coeff2 * arr * x[center] /* Left */
192192
+ coeff2 * arr * x[right] /* Center: (Left) */
193193
+ 0.25 * art * x[top] /* Top Left */
194194
- 0.25 * art * x[bottom]); /* Bottom Left */
195195
/* Fill result(i,j-1) */
196-
result[bottom] -= (-coeff3 * att * x[center] /* Top */
196+
result[bottom] += (-coeff3 * att * x[center] /* Top */
197197
+ coeff3 * att * x[bottom] /* Center: (Top) */
198198
- 0.25 * art * x[right] /* Top Right */
199199
+ 0.25 * art * x[left]); /* Top Left */
200200
/* Fill result(i,j+1) */
201-
result[top] -= (-coeff4 * att * x[center] /* Bottom */
201+
result[top] += (-coeff4 * att * x[center] /* Bottom */
202202
+ coeff4 * att * x[top] /* Center: (Bottom) */
203203
+ 0.25 * art * x[right] /* Bottom Right */
204204
- 0.25 * art * x[left]); /* Bottom Left */
@@ -226,15 +226,15 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
226226
const int top = grid.index(i_r, i_theta_P1);
227227

228228
/* Fill result(i,j) */
229-
result[center] -= (0.25 * (h1 + h2) * (k1 + k2) * coeff_beta * std::fabs(detDF) * x[center] /* beta_{i,j} */
229+
result[center] += (0.25 * (h1 + h2) * (k1 + k2) * coeff_beta * std::fabs(detDF) * x[center] /* beta_{i,j} */
230230
- coeff1 * arr * x[left] /* Left */
231231
- coeff2 * arr * x[right] /* Right */
232232
- coeff3 * att * x[bottom] /* Bottom */
233233
- coeff4 * att * x[top] /* Top */
234234
+ ((coeff1 + coeff2) * arr + (coeff3 + coeff4) * att) *
235235
x[center]); /* Center: (Left, Right, Bottom, Top) */
236236
/* Fill result(i-1,j) */
237-
result[left] -= (-coeff1 * arr * x[center] /* Right */
237+
result[left] += (-coeff1 * arr * x[center] /* Right */
238238
+ coeff1 * arr * x[left] /* Center: (Right) */
239239
- 0.25 * art * x[top] /* Top Right */
240240
+ 0.25 * art * x[bottom]); /* Bottom Right */
@@ -246,12 +246,12 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
246246
/* + 0.25 * art * x[top] // Top Left */
247247
/* - 0.25 * art * x[bottom]); // Bottom Left */
248248
/* Fill result(i,j-1) */
249-
result[bottom] -= (-coeff3 * att * x[center] /* Top */
249+
result[bottom] += (-coeff3 * att * x[center] /* Top */
250250
+ coeff3 * att * x[bottom] /* Center: (Top) */
251251
- 0.25 * art * x[right] /* Top Right */
252252
+ 0.25 * art * x[left]); /* Top Left */
253253
/* Fill result(i,j+1) */
254-
result[top] -= (-coeff4 * att * x[center] /* Bottom */
254+
result[top] += (-coeff4 * att * x[center] /* Bottom */
255255
+ coeff4 * att * x[top] /* Center: (Bottom) */
256256
+ 0.25 * art * x[right] /* Bottom Right */
257257
- 0.25 * art * x[left]); /* Bottom Left */
@@ -261,7 +261,7 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
261261
/* ----------------------------- */
262262
else if (i_r == grid.nr() - 1) {
263263
/* Fill result of (i,j) */
264-
result[center] -= x[center];
264+
result[center] += x[center];
265265

266266
/* Give value to the interior nodes! */
267267
double h1 = grid.radialSpacing(i_r - 1);
@@ -278,7 +278,7 @@ static inline void node_apply_a_give(int i_r, int i_theta, double r, double thet
278278
const int top = grid.index(i_r, i_theta_P1);
279279

280280
/* Fill result(i-1,j) */
281-
result[left] -= (-coeff1 * arr * x[center] /* Right */
281+
result[left] += (-coeff1 * arr * x[center] /* Right */
282282
+ coeff1 * arr * x[left] /* Center: (Right) */
283283
- 0.25 * art * x[top] /* Top Right */
284284
+ 0.25 * art * x[bottom]); /* Bottom Right */

src/Residual/ResidualGive/residualGive.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ ResidualGive::ResidualGive(const PolarGrid& grid, const LevelCache& level_cache,
88
{
99
}
1010

11-
/* ------------------ */
12-
/* result = rhs - A*x */
11+
/* ------------ */
12+
/* result = A*x */
1313

1414
// clang-format off
15-
void ResidualGive::computeResidual(Vector<double> result, ConstVector<double> rhs, ConstVector<double> x) const
15+
void ResidualGive::applySystemOperator(Vector<double> result, ConstVector<double> x) const
1616
{
1717
assert(result.size() == x.size());
1818

19-
Kokkos::deep_copy(result, rhs);
19+
assign(result, 0.0);
2020

2121
/* Single-threaded execution */
2222
if (num_omp_threads_ == 1) {
@@ -101,3 +101,19 @@ void ResidualGive::computeResidual(Vector<double> result, ConstVector<double> rh
101101
}
102102
}
103103
// clang-format on
104+
105+
/* ------------------ */
106+
/* result = rhs - A*x */
107+
void ResidualGive::computeResidual(Vector<double> result, ConstVector<double> rhs, ConstVector<double> x) const
108+
{
109+
assert(result.size() == x.size());
110+
111+
applySystemOperator(result, x);
112+
113+
// Subtract A*x from rhs to get the residual.
114+
const int n = result.size();
115+
#pragma omp parallel for num_threads(num_omp_threads_)
116+
for (int i = 0; i < n; i++) {
117+
result[i] = rhs[i] - result[i];
118+
}
119+
}

0 commit comments

Comments
 (0)