Skip to content

Commit 818db92

Browse files
authored
Merge pull request #18 from solarcalf/update_accuracy
Update accuracy
2 parents 8115cb4 + 0a2fb2d commit 818db92

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

Poisson_Equation/headers/Solvers.hpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ namespace numcpp
6060
return norm;
6161
}
6262

63+
FP error(const std::vector<FP>& v1, const std::vector<FP>& v2)
64+
{
65+
FP error = 0.0;
66+
67+
#pragma omp parallel for reduction(max: error)
68+
for (size_t i = 0; i < v1.size(); ++i)
69+
{
70+
error = std::max(std::abs(v1[i] - v2[i]), error);
71+
}
72+
return error;
73+
}
74+
6375
std::vector<FP> vector_FMA(const std::vector<FP>& lhs, FP coef, const std::vector<FP>& rhs)
6476
{
6577
const size_t size = lhs.size();
@@ -210,15 +222,19 @@ namespace numcpp
210222

211223
for (size_t i = 0; i < max_iterations; ++i)
212224
{
225+
std::vector<FP> saved_approximation = approximation;
226+
213227
std::vector<FP> residual = (*system_matrix) * approximation - b;
214228

215-
FP residual_norm = norm(residual);
216-
if (residual_norm <= required_precision) break;
229+
//FP residual_norm = norm(residual);
217230

218231
std::vector<FP> Ar = (*system_matrix) * residual;
219232
FP tau = scalar_product(Ar, residual) / scalar_product(Ar, Ar);
220233

221234
approximation = vector_FMA(residual, -tau, approximation);
235+
236+
FP approximation_error = error(saved_approximation, approximation);
237+
if (approximation_error <= required_precision) break;
222238
}
223239

224240
return approximation;
@@ -439,10 +455,11 @@ namespace numcpp
439455

440456
for (size_t i = 1; i < max_iterations; ++i)
441457
{
458+
std::vector<FP> saved_approximation = approximation;
459+
442460
residual = (*system_matrix) * approximation - b;
443461

444-
FP residual_norm = norm(residual);
445-
if (residual_norm <= required_precision) break;
462+
//FP residual_norm = norm(residual);
446463

447464
FP beta = scalar_product(Ah, residual) / scalar_product(Ah, h);
448465

@@ -453,6 +470,9 @@ namespace numcpp
453470
alpha = -scalar_product(residual, h) / scalar_product(Ah, h);
454471

455472
approximation = vector_FMA(h, alpha, approximation);
473+
474+
FP approximation_error = error(saved_approximation, approximation);
475+
if (approximation_error <= required_precision) break;
456476
}
457477

458478
return approximation;

Poisson_Equation/src/test.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,15 @@ void test_task_custom_grid(size_t m, size_t n, std::unique_ptr<numcpp::ISolver>
120120

121121
int main()
122122
{
123-
size_t m = 1024;
124-
size_t n = 1024;
123+
size_t m1 = 1024;
124+
size_t n1 = 1024;
125125

126-
auto LS_solver = std::make_unique<numcpp::ConGrad>(std::vector<FP>(), 1000000, 0.00001, nullptr, std::vector<FP>());
126+
auto LS_solver1 = std::make_unique<numcpp::ConGrad>(std::vector<FP>(), 1000000, 0.000000001, nullptr, std::vector<FP>());
127+
test_task_custom_grid(m1, n1, std::move(LS_solver1));
127128

128-
test_task_custom_grid(m, n, std::move(LS_solver));
129+
size_t m2 = 200;
130+
size_t n2 = 200;
131+
132+
auto LS_solver2 = std::make_unique<numcpp::MinRes>(std::vector<FP>(), 1000000, 0.000001, nullptr, std::vector<FP>());
133+
test_task(m2, n2, std::move(LS_solver2));
129134
}

0 commit comments

Comments
 (0)