Compare commits

...

2 Commits

4 changed files with 132 additions and 81 deletions

3
.gitignore vendored
View File

@@ -636,4 +636,7 @@ FodyWeavers.xsd
*.msix
*.msm
*.msp
*.txt
!CMakeLists.txt
*.png

View File

@@ -1,4 +1,4 @@
#include "Header.h"
#include "Header.h"
#include <Eigen/Dense>
using namespace Eigen;
@@ -16,10 +16,10 @@ void Solver::Execute_Linear(double val1, double val2) {
// Локальный вектор нагрузки
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(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.;
@@ -80,81 +80,93 @@ void Solver::Execute_Linear(double val1, double val2) {
file << std::endl;
}
// TODO: переделать под себя
void Solver::Execute_Cubic(double val1, double val2) {
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);
Amat.setZero();
b.setZero();
VectorXd global_load = VectorXd::Zero(mat_dim);
// 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;
// Ансамблирование
for (int elem = 0; elem < N; ++elem) {
int node_i = elem * 3;
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;
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);
}
}
// 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;
}
#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
Amat.row(0).setZero();
Amat(0, 0) = L / 3.0 + 1;
Amat(0, 1) = -1;
b(0) = 0;
// Граничные условия
double u_right = val2;
Amat.row(mat_dim - 1).setZero();
Amat(mat_dim - 1, mat_dim - 1) = 1;
b(mat_dim - 1) = val2;
ansamb.row(0).setZero();
ansamb(0, 0) = L / 3.0 + 1;
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 << 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;
}
}

View File

@@ -2,13 +2,13 @@
class Solver {
private:
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><>
// Коэффициенты в ДУ
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;
// <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;
// <EFBFBD><EFBFBD><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);

View File

@@ -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"
]
},
{
@@ -78,8 +106,8 @@
" 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=\"orange\")\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"
},