Compare commits
2 Commits
4fe90394ec
...
cac280deb1
| Author | SHA1 | Date | |
|---|---|---|---|
| cac280deb1 | |||
| b8bc3f705a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -636,4 +636,7 @@ FodyWeavers.xsd
|
|||||||
*.msix
|
*.msix
|
||||||
*.msm
|
*.msm
|
||||||
*.msp
|
*.msp
|
||||||
|
|
||||||
*.txt
|
*.txt
|
||||||
|
!CMakeLists.txt
|
||||||
|
*.png
|
||||||
|
|||||||
134
Solver.cpp
134
Solver.cpp
@@ -1,4 +1,4 @@
|
|||||||
#include "Header.h"
|
#include "Header.h"
|
||||||
#include <Eigen/Dense>
|
#include <Eigen/Dense>
|
||||||
using namespace Eigen;
|
using namespace Eigen;
|
||||||
|
|
||||||
@@ -16,10 +16,10 @@ void Solver::Execute_Linear(double val1, double val2) {
|
|||||||
// Локальный вектор нагрузки
|
// Локальный вектор нагрузки
|
||||||
VectorXd local_load(2);
|
VectorXd local_load(2);
|
||||||
|
|
||||||
local(0, 0) = -A / L - B / 2. + C * L / 3.;
|
local(0, 0) = -A / L - B / 2.;// +C * L / 3.;
|
||||||
local(0, 1) = A / L + B / 2. + C * L / 6.;
|
local(0, 1) = A / L + B / 2.; // +C * L / 6.;
|
||||||
local(1, 0) = 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(1, 1) = -A / L + B / 2.;// +C * L / 3.;
|
||||||
|
|
||||||
local_load(0) = -D * L / 2.;
|
local_load(0) = -D * L / 2.;
|
||||||
local_load(1) = -D * L / 2.;
|
local_load(1) = -D * L / 2.;
|
||||||
@@ -80,81 +80,93 @@ void Solver::Execute_Linear(double val1, double val2) {
|
|||||||
file << std::endl;
|
file << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: переделать под себя
|
|
||||||
void Solver::Execute_Cubic(double val1, double val2) {
|
void Solver::Execute_Cubic(double val1, double val2) {
|
||||||
|
|
||||||
int mat_dim = 1 + N * 3;
|
int mat_dim = 1 + N * 3;
|
||||||
|
|
||||||
|
// Локальная матрица жёсткости
|
||||||
|
MatrixXd local = MatrixXd::Zero(4, 4);
|
||||||
|
|
||||||
|
// Локальный вектор нагрузки
|
||||||
|
VectorXd local_load(4);
|
||||||
|
|
||||||
|
// Формирование локальной матрицы жёсткости
|
||||||
|
local(0, 0) = -A * 37.0 / 10.0 / L + B * (-1.0) / 2.0;
|
||||||
|
local(0, 1) = -A * (-189.0) / 40.0 / L + B * 57.0 / 80.0;
|
||||||
|
local(0, 2) = -A * 27.0 / 20.0 / L + B * (-3.0) / 10.0;
|
||||||
|
local(0, 3) = -A * (-13.0) / 40.0 / L + B * 7.0 / 80.0;
|
||||||
|
|
||||||
|
local(1, 0) = -A * (-189.0) / 40.0 / L + B * (-57.0) / 80.0;
|
||||||
|
local(1, 1) = -A * 54.0 / 5.0 / L;
|
||||||
|
local(1, 2) = -A * (-297.0) / 40.0 / L + B * 81.0 / 80.0;
|
||||||
|
local(1, 3) = -A * 27.0 / 20.0 / L + B * (-3.0) / 10.0;
|
||||||
|
|
||||||
|
local(2, 0) = -A * 27.0 / 20.0 / L + B * 3.0 / 10.0;
|
||||||
|
local(2, 1) = -A * (-297.0) / 40.0 / L + B * (-81.0) / 80.0;
|
||||||
|
local(2, 2) = -A * 54.0 / 5.0 / L;
|
||||||
|
local(2, 3) = -A * (-189.0) / 40.0 / L + B * 57.0 / 80.0;
|
||||||
|
|
||||||
|
local(3, 0) = -A * (-13.0) / 40.0 / L + B * (-7.0) / 80.0;
|
||||||
|
local(3, 1) = -A * 27.0 / 20.0 / L + B * 3.0 / 10.0;
|
||||||
|
local(3, 2) = -A * (-189.0) / 40.0 / L + B * (-57.0) / 80.0;
|
||||||
|
local(3, 3) = -A * 37.0 / 10.0 / L + B * 1.0 / 2.0;
|
||||||
|
|
||||||
|
// Формирование локального вектора нагрузки
|
||||||
|
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;
|
||||||
|
|
||||||
// Глобальная матрица жёсткости
|
// Глобальная матрица жёсткости
|
||||||
Eigen::MatrixXd Amat(mat_dim, mat_dim);
|
MatrixXd ansamb = MatrixXd::Zero(mat_dim, mat_dim);
|
||||||
|
|
||||||
// Глобальный вектор нагрузок
|
// Глобальный вектор нагрузок
|
||||||
Eigen::VectorXd b(mat_dim);
|
VectorXd global_load = VectorXd::Zero(mat_dim);
|
||||||
Amat.setZero();
|
|
||||||
b.setZero();
|
|
||||||
|
|
||||||
// Assemble matrix
|
// Ансамблирование
|
||||||
for (int i = 0; i < mat_dim - 3; i += 3) {
|
for (int elem = 0; elem < N; ++elem) {
|
||||||
Amat(i, i + 0) -= A * 37.0 / 10.0 / L;
|
int node_i = elem * 3;
|
||||||
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;
|
for (int i = 0; i < 4; i++) {
|
||||||
Amat(i + 0, i + 1) += B * 57.0 / 80.0;
|
for (int j = 0; j < 4; j++) {
|
||||||
Amat(i + 0, i + 2) += B * (-3.0) / 10.0;
|
ansamb(node_i + i, node_i + j) += local(i, j);
|
||||||
Amat(i + 0, i + 3) += B * 7.0 / 80.0;
|
}
|
||||||
Amat(i + 1, i + 0) += B * (-57.0) / 80.0;
|
global_load(node_i + i) += local_load(i);
|
||||||
|
}
|
||||||
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
|
#if DEBUG
|
||||||
for (int i = 0; i < mat_dim - 3; i += 3) {
|
std::cout << std::endl << "Before:" << std::endl;
|
||||||
b(i) -= D * L / 8.0;
|
std::cout << "Ansamb matrix:\n" << ansamb << std::endl;
|
||||||
b(i + 1) -= D * 3.0 * L / 8.0;
|
std::cout << "Ansamb load vector:\n" << global_load << std::endl;
|
||||||
b(i + 2) -= D * 3.0 * L / 8.0;
|
#endif
|
||||||
b(i + 3) -= D * L / 8.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Amat.row(0).setZero();
|
// Граничные условия
|
||||||
Amat(0, 0) = L / 3.0 + 1;
|
double u_right = val2;
|
||||||
Amat(0, 1) = -1;
|
|
||||||
b(0) = 0;
|
|
||||||
|
|
||||||
Amat.row(mat_dim - 1).setZero();
|
ansamb.row(0).setZero();
|
||||||
Amat(mat_dim - 1, mat_dim - 1) = 1;
|
ansamb(0, 0) = L / 3.0 + 1;
|
||||||
b(mat_dim - 1) = val2;
|
ansamb(0, 1) = -1;
|
||||||
|
global_load(0) = 0;
|
||||||
|
|
||||||
|
ansamb.row(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 = Amat.fullPivLu().solve(b);
|
VectorXd solution = ansamb.fullPivLu().solve(global_load);
|
||||||
std::cout << "\nSolution:" << std::endl;
|
std::cout << "\nSolution:" << std::endl;
|
||||||
std::cout << solution << std::endl;
|
std::cout << solution << std::endl;
|
||||||
|
|
||||||
std::ofstream file("matrix_cubic_" + std::to_string(N) + ".txt");
|
std::ofstream file("matrix_cubic_" + std::to_string(N) + ".txt");
|
||||||
for (int i = 0; i < solution.size(); i++) {
|
for (int i = 0; i < solution.size(); i++) {
|
||||||
file << solution(i) << ' ';
|
file << solution(i) << ' ';
|
||||||
}
|
}
|
||||||
file << std::endl;
|
file << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
8
Solver.h
8
Solver.h
@@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
class Solver {
|
class Solver {
|
||||||
private:
|
private:
|
||||||
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><>
|
// Коэффициенты в ДУ
|
||||||
double A, B, C, D;
|
double A, B, C, D;
|
||||||
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
// Длина конечного элемента
|
||||||
double L;
|
double L;
|
||||||
// <EFBFBD><EFBFBD><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;
|
int N;
|
||||||
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>
|
// Границы частного решения ДУ
|
||||||
int upper, lower;
|
int upper, lower;
|
||||||
public:
|
public:
|
||||||
Solver(double _A, double _B, double _C, double _D, int _N, int _l, int _u);
|
Solver(double _A, double _B, double _C, double _D, int _N, int _l, int _u);
|
||||||
|
|||||||
@@ -8,6 +8,34 @@
|
|||||||
"# Solution of second-order linear ordinary differential equation"
|
"# 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",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
@@ -37,7 +65,7 @@
|
|||||||
"id": "8af07a56",
|
"id": "8af07a56",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"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",
|
"id": "efac514a",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Linear element"
|
"### Linear element"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -78,8 +106,8 @@
|
|||||||
" plt.plot(x, y_real, label=\"Numeral solution\", color='black')\n",
|
" plt.plot(x, y_real, label=\"Numeral solution\", color='black')\n",
|
||||||
" plt.title(f\"Linear element, elements = {elements}\")\n",
|
" plt.title(f\"Linear element, elements = {elements}\")\n",
|
||||||
" plt.grid(True)\n",
|
" plt.grid(True)\n",
|
||||||
" plt.xlabel(\"X\")\n",
|
" plt.xlabel(\"x\")\n",
|
||||||
" plt.ylabel(\"Y\")\n",
|
" plt.ylabel(\"u(x)\")\n",
|
||||||
" plt.legend()\n",
|
" plt.legend()\n",
|
||||||
" plt.savefig(f\"linear_{elements}.png\", dpi=300)\n",
|
" plt.savefig(f\"linear_{elements}.png\", dpi=300)\n",
|
||||||
" plt.show()\n",
|
" plt.show()\n",
|
||||||
@@ -92,7 +120,7 @@
|
|||||||
"id": "d9a38740",
|
"id": "d9a38740",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Cubic element"
|
"### Cubic element"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -110,11 +138,11 @@
|
|||||||
" y = np.fromstring(data_str, sep=' ')\n",
|
" y = np.fromstring(data_str, sep=' ')\n",
|
||||||
" y_real = u(x)\n",
|
" y_real = u(x)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" plt.plot(x, y, label=\"Cubic element solution\", color=\"green\")\n",
|
" plt.plot(x, y, label=\"Cubic element solution\", color=\"orange\")\n",
|
||||||
" plt.plot(x, y_real, label=\"Numeral solution\", color='black')\n",
|
" plt.plot(x, y_real, label=\"Numeral solution\", color='black')\n",
|
||||||
" plt.title(f\"Cubic element, elements = {elements} \")\n",
|
" plt.title(f\"Cubic element, elements = {elements} \")\n",
|
||||||
" plt.xlabel(\"X\")\n",
|
" plt.xlabel(\"x\")\n",
|
||||||
" plt.ylabel(\"Y\")\n",
|
" plt.ylabel(\"u(x)\")\n",
|
||||||
" plt.grid(True)\n",
|
" plt.grid(True)\n",
|
||||||
" plt.legend()\n",
|
" plt.legend()\n",
|
||||||
" plt.savefig(f\"cubic_{elements}.png\", dpi=300)\n",
|
" plt.savefig(f\"cubic_{elements}.png\", dpi=300)\n",
|
||||||
@@ -138,9 +166,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"N = 20\n",
|
"show_plot_linear(linear_file, N)\n",
|
||||||
"show_plot_linear(\"matrix_linear.txt\", N)\n",
|
"show_plot_cubic(cubic_file, N)"
|
||||||
"show_plot_cubic(\"matrix_cubic.txt\", N)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -154,7 +181,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"id": "e0e9266d",
|
"id": "31de1104",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -162,8 +189,16 @@
|
|||||||
" with open(filename, 'r') as file:\n",
|
" with open(filename, 'r') as file:\n",
|
||||||
" content = file.read().strip()\n",
|
" content = file.read().strip()\n",
|
||||||
" data = np.array([float(x) for x in content.split()])\n",
|
" data = np.array([float(x) for x in content.split()])\n",
|
||||||
" return data\n",
|
" return data"
|
||||||
"\n",
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "e0e9266d",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
"def show_error(program_file: str, analytical_file: str):\n",
|
"def show_error(program_file: str, analytical_file: str):\n",
|
||||||
" # Считывание данных из файлов\n",
|
" # Считывание данных из файлов\n",
|
||||||
" real = read_data_from_file(analytical_file)\n",
|
" real = read_data_from_file(analytical_file)\n",
|
||||||
@@ -192,13 +227,14 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"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": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": ".venv",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user