-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuccessiveOverRelaxationSpace.cc
45 lines (34 loc) · 1.2 KB
/
SuccessiveOverRelaxationSpace.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) 2021 Chanjung Kim. All rights reserved.
// Licensed under the MIT License.
#include <leth/SuccessiveOverRelaxationSpace.hh>
#include <cmath>
#include <cstdio>
char const* SuccessiveOverRelaxationSpace::GetName() noexcept
{
return "SOR";
}
void SuccessiveOverRelaxationSpace::SolveEquation(std::vector<float> const& A,
std::vector<float>& x,
std::vector<float> const& b) noexcept
{
constexpr float omega { 1.12f };
size_t const numVars { x.size() };
for (size_t iter { 0 }; iter < 10000; ++iter)
{
bool converge { true };
for (size_t i { 0 }; i < numVars; ++i)
{
float const before { x[i] };
x[i] = b[i];
for (size_t j { 0 }; j < i; ++j) x[i] -= (A[i * numVars + j] * x[j]);
for (size_t j { i + 1 }; j < numVars; ++j) x[i] -= (A[i * numVars + j] * x[j]);
x[i] *= omega;
x[i] /= A[i * numVars + i];
x[i] += (1 - omega) * before;
if (std::abs(x[i] - before) >= 0.001)
converge = false;
}
if (converge)
break;
}
}