forked from ParkSuMin/MemMAPR-MKE
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b306b77af | |||
| 9b80340998 | |||
| 5b2331445c | |||
| e3c7b88dfb | |||
| 64cb04388a | |||
| b07c9e6cea | |||
| 1585c76980 | |||
| 1fc2925f8f | |||
| 8957f65064 | |||
| cac280deb1 | |||
| b8bc3f705a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -636,4 +636,7 @@ FodyWeavers.xsd
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
*.txt
|
||||
!CMakeLists.txt
|
||||
*.png
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#include "Header.h"
|
||||
#include "Solver.h"
|
||||
#include <Eigen/Dense>
|
||||
using namespace Eigen;
|
||||
int main() {
|
||||
Solver slv(5., 4., 0., 1., 30, 0, 10);
|
||||
std::cout << "Linear element:";
|
||||
slv.Execute_Linear(0, 5);
|
||||
std::cout << "\nCubic element:";
|
||||
slv.Execute_Cubic(0, 5);
|
||||
return 0;
|
||||
}
|
||||
@@ -142,6 +142,7 @@
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>Default</LanguageStandard>
|
||||
<LanguageStandard_C>Default</LanguageStandard_C>
|
||||
<AdditionalIncludeDirectories>include/</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -156,6 +157,7 @@
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>Default</LanguageStandard>
|
||||
<LanguageStandard_C>Default</LanguageStandard_C>
|
||||
<AdditionalIncludeDirectories>include/</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -172,6 +174,7 @@
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>Default</LanguageStandard>
|
||||
<LanguageStandard_C>Default</LanguageStandard_C>
|
||||
<AdditionalIncludeDirectories>include/</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -179,15 +182,15 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="MemMAPR-MKE.cpp" />
|
||||
<ClCompile Include="Solver.cpp" />
|
||||
<ClCompile Include="src\MemMAPR-MKE.cpp" />
|
||||
<ClCompile Include="src\Solver.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Header.h" />
|
||||
<ClInclude Include="Solver.h" />
|
||||
<ClInclude Include="include\Header.h" />
|
||||
<ClInclude Include="include\Solver.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -18,22 +18,22 @@
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="MemMAPR-MKE.cpp">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Solver.cpp">
|
||||
<ClCompile Include="src\Solver.cpp">
|
||||
<Filter>Solver</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\MemMAPR-MKE.cpp">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Header.h">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Solver.h">
|
||||
<ClInclude Include="include\Solver.h">
|
||||
<Filter>Solver</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\Header.h">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
160
Solver.cpp
160
Solver.cpp
@@ -1,160 +0,0 @@
|
||||
#include "Header.h"
|
||||
#include <Eigen/Dense>
|
||||
using namespace Eigen;
|
||||
|
||||
Solver::Solver(double _A, double _B, double _C, double _D, int _N, int _l, int _u) {
|
||||
A = _A, B = _B, C = _C, D = _D, N = _N;
|
||||
upper = _u, lower = _l;
|
||||
L = (double)(upper - lower) / N;
|
||||
}
|
||||
|
||||
void Solver::Execute_Linear(double val1, double val2) {
|
||||
|
||||
// Локальная матрица жёсткости
|
||||
MatrixXd local = MatrixXd::Zero(2, 2);
|
||||
|
||||
// Локальный вектор нагрузки
|
||||
VectorXd local_load(2);
|
||||
|
||||
local(0, 0) = -A / L - B / 2. + C * L / 3.;
|
||||
local(0, 1) = A / L + B / 2. + C * L / 6.;
|
||||
local(1, 0) = A / L - B / 2. + C * L / 6.;
|
||||
local(1, 1) = -A / L + B / 2. + C * L / 3.;
|
||||
|
||||
local_load(0) = -D * L / 2.;
|
||||
local_load(1) = -D * L / 2.;
|
||||
|
||||
// Глобальная матрица жёсткости
|
||||
MatrixXd ansamb = MatrixXd::Zero(N + 1, N + 1);
|
||||
|
||||
// Глобальный вектор нагрузок
|
||||
VectorXd global_load = VectorXd::Zero(N + 1);
|
||||
|
||||
// Ансамблирование
|
||||
for (int elem = 0; elem < N; ++elem) {
|
||||
int node_i = elem;
|
||||
int node_j = elem + 1;
|
||||
|
||||
ansamb(node_i, node_i) += local(0, 0);
|
||||
ansamb(node_i, node_j) += local(0, 1);
|
||||
ansamb(node_j, node_i) += local(1, 0);
|
||||
ansamb(node_j, node_j) += local(1, 1);
|
||||
|
||||
global_load(node_i) += local_load(0);
|
||||
global_load(node_j) += local_load(1);
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
std::cout << std::endl << "Before:" << std::endl;
|
||||
std::cout << "Ansamb matrix:\n" << ansamb << std::endl;
|
||||
std::cout << "Ansamb load vector:\n" << global_load << std::endl;
|
||||
#endif
|
||||
|
||||
// Граничные условия
|
||||
double u_right = val2;
|
||||
ansamb.row(0).setZero();
|
||||
ansamb.row(N).setZero();
|
||||
|
||||
ansamb(0, 0) = L + 1;
|
||||
ansamb(0, 1) = -1;
|
||||
global_load(0) = 0;
|
||||
|
||||
ansamb(N, N) = 1;
|
||||
global_load(N) = u_right;
|
||||
|
||||
#if DEBUG
|
||||
std::cout << "\nAfter:" << std::endl;
|
||||
std::cout << "Modified matrix:\n" << ansamb << std::endl;
|
||||
std::cout << "Modified load vector:\n" << global_load << std::endl;
|
||||
#endif
|
||||
|
||||
// Решение системы
|
||||
VectorXd solution = ansamb.fullPivLu().solve(global_load);
|
||||
std::cout << "\nSolution:" << std::endl;
|
||||
std::cout << solution << std::endl;
|
||||
|
||||
std::ofstream file("matrix_linear_" + std::to_string(N) + ".txt");
|
||||
for (int i = 0; i < solution.size(); i++) {
|
||||
file << solution(i) << ' ';
|
||||
}
|
||||
file << std::endl;
|
||||
}
|
||||
|
||||
// TODO: переделать под себя
|
||||
void Solver::Execute_Cubic(double val1, double val2) {
|
||||
|
||||
int mat_dim = 1 + N * 3;
|
||||
|
||||
// Глобальная матрица жёсткости
|
||||
Eigen::MatrixXd Amat(mat_dim, mat_dim);
|
||||
|
||||
// Глобальный вектор нагрузок
|
||||
Eigen::VectorXd b(mat_dim);
|
||||
Amat.setZero();
|
||||
b.setZero();
|
||||
|
||||
// Assemble matrix
|
||||
for (int i = 0; i < mat_dim - 3; i += 3) {
|
||||
Amat(i, i + 0) -= A * 37.0 / 10.0 / L;
|
||||
Amat(i, i + 1) -= A * (-189.0) / 40.0 / L;
|
||||
Amat(i, i + 2) -= A * 27.0 / 20.0 / L;
|
||||
Amat(i, i + 3) -= A * (-13.0) / 40.0 / L;
|
||||
Amat(i + 1, i + 0) -= A * (-189.0) / 40.0 / L;
|
||||
Amat(i + 1, i + 1) -= A * 54.0 / 5.0 / L;
|
||||
Amat(i + 1, i + 2) -= A * (-297.0) / 40.0 / L;
|
||||
Amat(i + 1, i + 3) -= A * 27.0 / 20.0 / L;
|
||||
Amat(i + 2, i + 0) -= A * 27.0 / 20.0 / L;
|
||||
Amat(i + 2, i + 1) -= A * (-297.0) / 40.0 / L;
|
||||
Amat(i + 2, i + 2) -= A * 54.0 / 5.0 / L;
|
||||
Amat(i + 2, i + 3) -= A * (-189.0) / 40.0 / L;
|
||||
Amat(i + 3, i + 0) -= A * (-13.0) / 40.0 / L;
|
||||
Amat(i + 3, i + 1) -= A * 27.0 / 20.0 / L;
|
||||
Amat(i + 3, i + 2) -= A * (-189.0) / 40.0 / L;
|
||||
Amat(i + 3, i + 3) -= A * 37.0 / 10.0 / L;
|
||||
|
||||
Amat(i + 0, i + 0) += B * (-1.0) / 2.0;
|
||||
Amat(i + 0, i + 1) += B * 57.0 / 80.0;
|
||||
Amat(i + 0, i + 2) += B * (-3.0) / 10.0;
|
||||
Amat(i + 0, i + 3) += B * 7.0 / 80.0;
|
||||
Amat(i + 1, i + 0) += B * (-57.0) / 80.0;
|
||||
|
||||
Amat(i + 1, i + 2) += B * 81.0 / 80.0;
|
||||
Amat(i + 1, i + 3) += B * (-3.0) / 10;
|
||||
Amat(i + 2, i + 0) += B * 3.0 / 10.0;
|
||||
Amat(i + 2, i + 1) += B * (-81.0) / 80.0;
|
||||
|
||||
Amat(i + 2, i + 3) += B * 57.0 / 80.0;
|
||||
Amat(i + 3, i + 0) += B * (-7.0) / 80.0;
|
||||
Amat(i + 3, i + 1) += B * 3.0 / 10.0;
|
||||
Amat(i + 3, i + 2) += B * (-57.0) / 80.0;
|
||||
Amat(i + 3, i + 3) += B * 1.0 / 2.0;
|
||||
}
|
||||
|
||||
// AssembLe vector
|
||||
for (int i = 0; i < mat_dim - 3; i += 3) {
|
||||
b(i) -= D * L / 8.0;
|
||||
b(i + 1) -= D * 3.0 * L / 8.0;
|
||||
b(i + 2) -= D * 3.0 * L / 8.0;
|
||||
b(i + 3) -= D * L / 8.0;
|
||||
}
|
||||
|
||||
Amat.row(0).setZero();
|
||||
Amat(0, 0) = L / 3.0 + 1;
|
||||
Amat(0, 1) = -1;
|
||||
b(0) = 0;
|
||||
|
||||
Amat.row(mat_dim - 1).setZero();
|
||||
Amat(mat_dim - 1, mat_dim - 1) = 1;
|
||||
b(mat_dim - 1) = val2;
|
||||
|
||||
// Решение системы
|
||||
VectorXd solution = Amat.fullPivLu().solve(b);
|
||||
std::cout << "\nSolution:" << std::endl;
|
||||
std::cout << solution << std::endl;
|
||||
|
||||
std::ofstream file("matrix_cubic_" + std::to_string(N) + ".txt");
|
||||
for (int i = 0; i < solution.size(); i++) {
|
||||
file << solution(i) << ' ';
|
||||
}
|
||||
file << std::endl;
|
||||
}
|
||||
17
Solver.h
17
Solver.h
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
class Solver {
|
||||
private:
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><>
|
||||
double A, B, C, D;
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
double L;
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
int N;
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>
|
||||
int upper, lower;
|
||||
public:
|
||||
Solver(double _A, double _B, double _C, double _D, int _N, int _l, int _u);
|
||||
void Execute_Linear(double value_1, double value_2);
|
||||
void Execute_Cubic(double value_1, double value_2);
|
||||
};
|
||||
@@ -8,6 +8,34 @@
|
||||
"# Solution of second-order linear ordinary differential equation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a4b21c72",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Input vars"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6653253d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"N = 20\n",
|
||||
"linear_file = f\"matrix_linear_{N}.txt\"\n",
|
||||
"cubic_file = f\"matrix_cubic_{N}.txt\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "781047cc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## System cells"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -37,7 +65,7 @@
|
||||
"id": "8af07a56",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Solution of $5u'' + 4u' + 1 = 0, u'(0) = u(0), u(10) = 5$"
|
||||
"### Solution of $5u'' + 4u' + 1 = 0, u'(0) = u(0), u(10) = 5$"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -56,7 +84,7 @@
|
||||
"id": "efac514a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Linear element"
|
||||
"### Linear element"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -74,12 +102,12 @@
|
||||
" y = np.fromstring(data_str, sep=' ')\n",
|
||||
" y_real = u(x)\n",
|
||||
"\n",
|
||||
" plt.plot(x, y, label=\"Linear element solution\", color='red')\n",
|
||||
" plt.plot(x, y, label=\"Linear element solution\", color='blue')\n",
|
||||
" plt.plot(x, y_real, label=\"Numeral solution\", color='black')\n",
|
||||
" plt.title(f\"Linear element, elements = {elements}\")\n",
|
||||
" plt.grid(True)\n",
|
||||
" plt.xlabel(\"X\")\n",
|
||||
" plt.ylabel(\"Y\")\n",
|
||||
" plt.xlabel(\"x\")\n",
|
||||
" plt.ylabel(\"u(x)\")\n",
|
||||
" plt.legend()\n",
|
||||
" plt.savefig(f\"linear_{elements}.png\", dpi=300)\n",
|
||||
" plt.show()\n",
|
||||
@@ -92,7 +120,7 @@
|
||||
"id": "d9a38740",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Cubic element"
|
||||
"### Cubic element"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -110,11 +138,11 @@
|
||||
" y = np.fromstring(data_str, sep=' ')\n",
|
||||
" y_real = u(x)\n",
|
||||
"\n",
|
||||
" plt.plot(x, y, label=\"Cubic element solution\", color=\"green\")\n",
|
||||
" plt.plot(x, y, label=\"Cubic element solution\", color=\"red\")\n",
|
||||
" plt.plot(x, y_real, label=\"Numeral solution\", color='black')\n",
|
||||
" plt.title(f\"Cubic element, elements = {elements} \")\n",
|
||||
" plt.xlabel(\"X\")\n",
|
||||
" plt.ylabel(\"Y\")\n",
|
||||
" plt.xlabel(\"x\")\n",
|
||||
" plt.ylabel(\"u(x)\")\n",
|
||||
" plt.grid(True)\n",
|
||||
" plt.legend()\n",
|
||||
" plt.savefig(f\"cubic_{elements}.png\", dpi=300)\n",
|
||||
@@ -138,9 +166,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"N = 20\n",
|
||||
"show_plot_linear(\"matrix_linear.txt\", N)\n",
|
||||
"show_plot_cubic(\"matrix_cubic.txt\", N)"
|
||||
"show_plot_linear(linear_file, N)\n",
|
||||
"show_plot_cubic(cubic_file, N)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -154,7 +181,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e0e9266d",
|
||||
"id": "31de1104",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -162,8 +189,16 @@
|
||||
" with open(filename, 'r') as file:\n",
|
||||
" content = file.read().strip()\n",
|
||||
" data = np.array([float(x) for x in content.split()])\n",
|
||||
" return data\n",
|
||||
"\n",
|
||||
" return data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e0e9266d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def show_error(program_file: str, analytical_file: str):\n",
|
||||
" # Считывание данных из файлов\n",
|
||||
" real = read_data_from_file(analytical_file)\n",
|
||||
@@ -192,13 +227,14 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"show_error(\"matrix_linear.txt\", \"linear_real_y_20.txt\")"
|
||||
"show_error(linear_file, f\"linear_real_y_{N}.txt\")\n",
|
||||
"show_error(cubic_file, f\"cubic_real_y_{N}.txt\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": ".venv",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
||||
31
include/Solver.h
Normal file
31
include/Solver.h
Normal file
@@ -0,0 +1,31 @@
|
||||
// Особенности MSVC
|
||||
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
|
||||
|
||||
#pragma once
|
||||
#include <experimental/filesystem>
|
||||
using namespace std;
|
||||
using namespace std::experimental::filesystem;
|
||||
|
||||
class Solver {
|
||||
private:
|
||||
// Коэффициенты в ДУ
|
||||
double A, B, C, D;
|
||||
// Длина конечного элемента
|
||||
double L;
|
||||
// Количество конечных элементов
|
||||
int N;
|
||||
// Границы частного решения ДУ
|
||||
int upper, lower;
|
||||
public:
|
||||
Solver(double _A, double _B, double _C, double _D, int _N, int _l, int _u);
|
||||
void Execute_Linear(double value_1, double value_2);
|
||||
void Execute_Cubic(double value_1, double value_2);
|
||||
|
||||
bool check_path(string path) {
|
||||
return exists(path);
|
||||
}
|
||||
|
||||
void make_path(string path) {
|
||||
create_directory(path);
|
||||
}
|
||||
};
|
||||
18
src/MemMAPR-MKE.cpp
Normal file
18
src/MemMAPR-MKE.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "Solver.h"
|
||||
#include "Header.h"
|
||||
#include <Eigen/Dense>
|
||||
#define A 5.
|
||||
#define B 4.
|
||||
#define C 0.
|
||||
#define D 1.
|
||||
|
||||
using namespace Eigen;
|
||||
int main() {
|
||||
std::cout << A << "u''" << " + " << B << "u'" << "+ " << C << "u + " << D << " = 0" << std::endl;
|
||||
Solver slv(A, B, C, D, 20, 0, 10);
|
||||
std::cout << "Linear element:";
|
||||
slv.Execute_Linear(0, 5);
|
||||
std::cout << "\nCubic element:";
|
||||
slv.Execute_Cubic(0, 5);
|
||||
return 0;
|
||||
}
|
||||
186
src/Solver.cpp
Normal file
186
src/Solver.cpp
Normal file
@@ -0,0 +1,186 @@
|
||||
#include "Header.h"
|
||||
#include <Eigen/Dense>
|
||||
using namespace Eigen;
|
||||
|
||||
Solver::Solver(double _A, double _B, double _C, double _D, int _N, int _l, int _u) {
|
||||
if (_N < 1)
|
||||
throw std::runtime_error("N CAN BE OVER THAN 1!");
|
||||
|
||||
A = _A, B = _B, C = _C, D = _D, N = _N;
|
||||
upper = _u, lower = _l;
|
||||
L = (double)(upper - lower) / N;
|
||||
}
|
||||
|
||||
void Solver::Execute_Linear(double val1, double val2) {
|
||||
|
||||
// Локальная матрица жёсткости
|
||||
MatrixXd local = MatrixXd::Zero(2, 2);
|
||||
|
||||
// Локальный вектор нагрузки
|
||||
VectorXd local_load(2);
|
||||
|
||||
local(0, 0) = -A / L - B / 2. + C * L / 2.;
|
||||
local(0, 1) = A / L + B / 2. + C * L / 2.;
|
||||
local(1, 0) = A / L - B / 2. + C * L / 2.;
|
||||
local(1, 1) = -A / L + B / 2. + C * L / 2.;
|
||||
|
||||
local_load(0) = -D * L / 2.;
|
||||
local_load(1) = -D * L / 2.;
|
||||
|
||||
// Глобальная матрица жёсткости
|
||||
MatrixXd ansamb = MatrixXd::Zero(N + 2, N + 2);
|
||||
|
||||
// Глобальный вектор нагрузок
|
||||
VectorXd global_load = VectorXd::Zero(N + 2);
|
||||
|
||||
// Ансамблирование (с учётом смещения на 1)
|
||||
for (int elem = 0; elem < N; ++elem) {
|
||||
int node_i = elem + 1;
|
||||
int node_j = elem + 2;
|
||||
|
||||
ansamb(node_i, node_i) += local(0, 0);
|
||||
ansamb(node_i, node_j) += local(0, 1);
|
||||
ansamb(node_j, node_i) += local(1, 0);
|
||||
ansamb(node_j, node_j) += local(1, 1);
|
||||
|
||||
global_load(node_i) += local_load(0);
|
||||
global_load(node_j) += local_load(1);
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
std::cout << std::endl << "Before:" << std::endl;
|
||||
std::cout << "Ansamb matrix:\n" << ansamb << std::endl;
|
||||
std::cout << "Ansamb load vector:\n" << global_load << std::endl;
|
||||
#endif
|
||||
|
||||
// Boundary conditions
|
||||
double u_right = val2;
|
||||
|
||||
// 3rd type of boundary condition
|
||||
ansamb.row(0).setZero();
|
||||
ansamb(0, 0) = 1;
|
||||
ansamb(0, 1) = -1;
|
||||
global_load(0) = 0;
|
||||
ansamb(1, 1) -= A;
|
||||
|
||||
// 1st type of boundary condition
|
||||
for (int i = 0; i < global_load.size(); ++i) {
|
||||
global_load(i) = global_load(i) - u_right * ansamb(i, N + 1);
|
||||
}
|
||||
ansamb.row(N + 1).setZero();
|
||||
ansamb.col(N + 1).setZero();
|
||||
ansamb(N + 1, N + 1) = 1;
|
||||
global_load(N + 1) = u_right;
|
||||
|
||||
#if DEBUG
|
||||
std::cout << "\nAfter:" << std::endl;
|
||||
std::cout << "Modified matrix:\n" << ansamb << std::endl;
|
||||
std::cout << "Modified load vector:\n" << global_load << std::endl;
|
||||
#endif
|
||||
|
||||
// Решение системы
|
||||
VectorXd solution = ansamb.fullPivLu().solve(global_load);
|
||||
std::cout << "\nSolution:" << std::endl;
|
||||
std::cout << solution << std::endl;
|
||||
|
||||
std::ofstream file("matrix_linear_" + std::to_string(N) + ".txt");
|
||||
for (int i = 1; i < solution.size(); i++) {
|
||||
file << solution(i) << ' ';
|
||||
}
|
||||
file << std::endl;
|
||||
}
|
||||
|
||||
void Solver::Execute_Cubic(double val1, double val2) {
|
||||
|
||||
int mat_dim = N * 3 + 2; // +2 для граничных узлов, как в линейном случае
|
||||
|
||||
// Локальная матрица жёсткости
|
||||
MatrixXd local = MatrixXd::Zero(4, 4);
|
||||
|
||||
// Локальный вектор нагрузки
|
||||
VectorXd local_load(4);
|
||||
|
||||
// Формирование локальной матрицы жёсткости
|
||||
local(0, 0) = -A * 37.0 / 10.0 / L + B * (-1.0) / 2.0 + C * 8. * L / 105.;
|
||||
local(0, 1) = -A * (-189.0) / 40.0 / L + B * 57.0 / 80.0 + C * 33. * L / 560.;
|
||||
local(0, 2) = -A * 27.0 / 20.0 / L + B * (-3.0) / 10.0 - C * 3. * L / 140.;
|
||||
local(0, 3) = -A * (-13.0) / 40.0 / L + B * 7.0 / 80.0 + C * 19. * L / 1680.;
|
||||
|
||||
local(1, 0) = -A * (-189.0) / 40.0 / L + B * (-57.0) / 80.0 + C * 33. * L / 560.;
|
||||
local(1, 1) = -A * 54.0 / 5.0 / L + C * 27. * L / 70.;
|
||||
local(1, 2) = -A * (-297.0) / 40.0 / L + B * 81.0 / 80.0 - C * 27. * L / 560.;
|
||||
local(1, 3) = -A * 27.0 / 20.0 / L + B * (-3.0) / 10.0 - C * 3. * L / 140.;
|
||||
|
||||
local(2, 0) = -A * 27.0 / 20.0 / L + B * 3.0 / 10.0 - C * 3. * L / 140.;
|
||||
local(2, 1) = -A * (-297.0) / 40.0 / L + B * (-81.0) / 80.0 - C * 27. * L / 560.;
|
||||
local(2, 2) = -A * 54.0 / 5.0 / L + C * 27. * L / 70.;
|
||||
local(2, 3) = -A * (-189.0) / 40.0 / L + B * 57.0 / 80.0 + C * 33. * L / 560.;
|
||||
|
||||
local(3, 0) = -A * (-13.0) / 40.0 / L + B * (-7.0) / 80.0 + C * 19. * L / 1680.;
|
||||
local(3, 1) = -A * 27.0 / 20.0 / L + B * 3.0 / 10.0 - C * 3. * L / 140.;
|
||||
local(3, 2) = -A * (-189.0) / 40.0 / L + B * (-57.0) / 80.0 + C * 33. * L / 560.;
|
||||
local(3, 3) = -A * 37.0 / 10.0 / L + B * 1.0 / 2.0 + C * 8. * L / 105.;
|
||||
|
||||
// Локальный вектор нагрузки
|
||||
local_load(0) = -D * L / 8.0;
|
||||
local_load(1) = -D * 3.0 * L / 8.0;
|
||||
local_load(2) = -D * 3.0 * L / 8.0;
|
||||
local_load(3) = -D * L / 8.0;
|
||||
|
||||
// Глобальные матрицы
|
||||
MatrixXd ansamb = MatrixXd::Zero(mat_dim, mat_dim);
|
||||
VectorXd global_load = VectorXd::Zero(mat_dim);
|
||||
|
||||
// Ансамблирование (со смещением на 1, как в линейной версии)
|
||||
for (int elem = 0; elem < N; ++elem) {
|
||||
int node_i = 1 + elem * 3; // смещение на 1
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
ansamb(node_i + i, node_i + j) += local(i, j);
|
||||
}
|
||||
global_load(node_i + i) += local_load(i);
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
std::cout << std::endl << "Before:" << std::endl;
|
||||
std::cout << "Ansamb matrix:\n" << ansamb << std::endl;
|
||||
std::cout << "Ansamb load vector:\n" << global_load << std::endl;
|
||||
#endif
|
||||
|
||||
// Граничные условия
|
||||
double u_right = val2;
|
||||
|
||||
// 3rd type of boundary condition
|
||||
ansamb.row(0).setZero();
|
||||
ansamb(0, 0) = 1;
|
||||
ansamb(0, 1) = -1;
|
||||
global_load(0) = 0;
|
||||
ansamb(1, 1) -= A;
|
||||
|
||||
// 1st type of boundary condition
|
||||
for (int i = 0; i < global_load.size(); ++i) {
|
||||
global_load(i) = global_load(i) - u_right * ansamb(i, mat_dim - 1);
|
||||
}
|
||||
ansamb.row(mat_dim - 1).setZero();
|
||||
ansamb.col(mat_dim - 1).setZero();
|
||||
ansamb(mat_dim - 1, mat_dim - 1) = 1;
|
||||
global_load(mat_dim - 1) = u_right;
|
||||
|
||||
#if DEBUG
|
||||
std::cout << "\nAfter:" << std::endl;
|
||||
std::cout << "Modified matrix:\n" << ansamb << std::endl;
|
||||
std::cout << "Modified load vector:\n" << global_load << std::endl;
|
||||
#endif
|
||||
|
||||
// Решение системы
|
||||
VectorXd solution = ansamb.fullPivLu().solve(global_load);
|
||||
std::cout << "\nSolution:" << std::endl;
|
||||
std::cout << solution << std::endl;
|
||||
|
||||
std::ofstream file("matrix_cubic_" + std::to_string(N) + ".txt");
|
||||
for (int i = 1; i < solution.size(); i++) {
|
||||
file << solution(i) << ' ';
|
||||
}
|
||||
file << std::endl;
|
||||
}
|
||||
Reference in New Issue
Block a user