Skip to content

Commit a8ed47e

Browse files
authored
Merge pull request #19 from BlackAngel2108/lubaw
chebyshev test
2 parents 818db92 + 9635d72 commit a8ed47e

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

Poisson_Equation/headers/Solvers.hpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ namespace numcpp
388388
FP Mmin;
389389
FP Mmax;
390390
public:
391-
ChebyshevIteration(std::vector<FP> initial_approximation, size_t max_iterations, FP required_precision, std::unique_ptr<IMatrix> system_matrix, std::vector<FP> b) :
392-
ISolver(std::move(initial_approximation), max_iterations, required_precision, std::move(system_matrix), std::move(b)) {}
391+
ChebyshevIteration(std::vector<FP> initial_approximation, size_t max_iterations, FP required_precision, std::unique_ptr<IMatrix> system_matrix, std::vector<FP> b, FP min = 0, FP max = 0) :
392+
ISolver(std::move(initial_approximation), max_iterations, required_precision, std::move(system_matrix), std::move(b)), Mmin(min), Mmax(max) {}
393393

394394
ChebyshevIteration() = default;
395395
~ChebyshevIteration() = default;
@@ -402,28 +402,38 @@ namespace numcpp
402402
std::vector<FP> solve() const override
403403
{
404404
std::vector<FP> approximation = std::move(initial_approximation);
405-
double size = (*system_matrix).size();
406-
//FP h = sqrt(1.0 / (*system_matrix).at(0, 1));
407-
//FP k = sqrt(1.0 / (*system_matrix).at(0, sqrt(size)));
408405

409406
FP k_cheb = 2.0;
410407
FP tau0 = 1.0 / ((Mmin + Mmax) / 2.0 + (Mmax - Mmin) / 2 * cos(PI / (2.0 * k_cheb) * (1.0 + 2.0 * 0.0)));
411408
FP tau1 = 1.0 / ((Mmin + Mmax) / 2.0 + (Mmax - Mmin) / 2 * cos(PI / (2.0 * k_cheb) * (1.0 + 2.0 * 1.0)));
412409

413-
410+
std::cout<<"tau1 = "<<tau0<<" tau2 = "<<tau1<<"\n";
414411
for (size_t i = 0; i < max_iterations; ++i)
415412
{
416413
std::vector<FP> residual = (*system_matrix) * approximation - b;
417-
418-
FP residual_norm = norm(residual);
419-
if (residual_norm <= required_precision) break;
414+
std::vector<FP> saved_approximation = approximation;
420415

421416
if (i % 2 == 0)
422-
approximation = vector_FMA(residual, -tau0, approximation);
417+
approximation = vector_FMA(residual, tau0, approximation);
423418
else
424-
approximation = vector_FMA(residual, -tau1, approximation);
419+
approximation = vector_FMA(residual, tau1, approximation);
420+
421+
FP approximation_error = error(saved_approximation, approximation);
422+
if (approximation_error <= required_precision) break;
425423
}
426-
424+
// for (size_t i = 0; i < max_iterations; ++i)
425+
// {
426+
// std::vector<FP> residual = (*system_matrix) * approximation - b;
427+
428+
// FP residual_norm = norm(residual);
429+
// if (residual_norm <= required_precision) break;
430+
431+
// if (i % 2 == 0)
432+
// approximation = vector_FMA(residual, -tau0, approximation);
433+
// else
434+
// approximation = vector_FMA(residual, -tau1, approximation);
435+
// std::cout << residual_norm<< " "<< required_precision <<"\n";
436+
// }
427437
return approximation;
428438
}
429439
};

Poisson_Equation/src/test.cpp

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,53 @@ void test_task_custom_grid(size_t m, size_t n, std::unique_ptr<numcpp::ISolver>
118118
auto [duration, solution] = estimate_time(solver);
119119
}
120120

121+
void test_Chebyshev(size_t n, size_t m) {
122+
// size_t n = 2048;
123+
// size_t m = 2048;
124+
std::cout << "n = " << n << " m = " << m << '\n';
125+
126+
std::array<double, 4> corners = { -1.0, -1.0, 1.0, 1.0 };
127+
std::vector<FP> init_app((n - 1) * (m - 1), 0.0);
128+
129+
FP h = (corners[2] - corners[0]) / n;
130+
FP k = (corners[3] - corners[1]) / m;
131+
132+
FP Mmin = 4.0 / pow(h, 2) * pow(sin(numcpp::PI / 2.0 / n), 2) + 4.0 / pow(k, 2) * pow(sin(numcpp::PI / 2.0 / m), 2);
133+
FP Mmax = 4.0 / pow(h, 2) * pow(sin(numcpp::PI * (n - 1) / 2.0 / n), 2) + 4.0 / pow(k, 2) * pow(sin(numcpp::PI * (m - 1) / 2.0 / m), 2);
134+
std::cout << "Mmin = " << Mmin << " Mmax = " << Mmax << '\n';
135+
auto u = [](double x, double y) { return exp(1 - pow(x, 2) - pow(y, 2)); };
136+
auto f = [](double x, double y) { return -4 * exp(1 - pow(x, 2) - pow(y, 2)) * (pow(y, 2) + pow(x, 2) - 1); };
137+
138+
auto mu1 = [](double y) { return exp(-pow(y, 2)); };
139+
auto mu2 = [](double y) { return exp(-pow(y, 2)); };
140+
auto mu3 = [](double x) { return exp(-pow(x, 2)); };
141+
auto mu4 = [](double x) { return exp(-pow(x, 2)); };
142+
143+
numcpp::DirichletProblemSolver<numcpp::Regular> dirichlet_task;
144+
dirichlet_task.set_fraction(m, n);
145+
dirichlet_task.set_corners(corners);
146+
dirichlet_task.set_u(u);
147+
dirichlet_task.set_f(f);
148+
dirichlet_task.set_boundary_conditions({ mu1, mu2, mu3, mu4 });
149+
150+
dirichlet_task.set_solver(std::make_unique<numcpp::ChebyshevIteration>(init_app, 1000000000, 0.0000000000001, nullptr, std::vector<FP>(), Mmin, Mmax));
151+
152+
auto [duration, solution] = estimate_time(dirichlet_task);
153+
}
154+
121155
int main()
122156
{
123-
size_t m1 = 1024;
124-
size_t n1 = 1024;
157+
// size_t m1 = 1024;
158+
// size_t n1 = 1024;
159+
160+
// auto LS_solver1 = std::make_unique<numcpp::ConGrad>(std::vector<FP>(), 1000000, 0.000000001, nullptr, std::vector<FP>());
161+
// test_task_custom_grid(m1, n1, std::move(LS_solver1));
125162

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));
163+
// size_t m2 = 200;
164+
// size_t n2 = 200;
128165

129-
size_t m2 = 200;
130-
size_t n2 = 200;
166+
// auto LS_solver2 = std::make_unique<numcpp::MinRes>(std::vector<FP>(), 1000000, 0.000001, nullptr, std::vector<FP>());
167+
// test_task(m2, n2, std::move(LS_solver2));
131168

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));
169+
test_Chebyshev(1024,1024);
134170
}

0 commit comments

Comments
 (0)