Compare commits

..

4 Commits

Author SHA1 Message Date
af1950982b Start solver with new arguments 2025-09-21 22:29:38 +03:00
5a8c3357ca Add Debug preprocessor 2025-09-21 22:29:10 +03:00
4ab15cb13b Ready linear element eval 2025-09-21 22:28:44 +03:00
dc0df855d7 Temporary way of cubic elements coordinates eval 2025-09-21 22:28:08 +03:00
5 changed files with 155 additions and 105 deletions

View File

@@ -1,11 +1,12 @@
#include "Header.h"
#include "Solver.h"
#include <Eigen/Dense>
using namespace Eigen;
int main() {
Solver slv(3., 2., 5., 20, 1, 6);
Solver slv(5., 4., 0., 1., 30, 0, 10);
std::cout << "Linear element:" << std::endl;
slv.Execute_Linear(-5, -10);
std::cout << "\nCubic element:" << std::endl;
slv.Execute_Cubic(-5, -10);
slv.Execute_Linear(0, 5);
//std::cout << "\nCubic element:" << std::endl;
//slv.Execute_Cubic(0, 5);
return 0;
}

View File

@@ -1,18 +1,24 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36310.24 d17.14
VisualStudioVersion = 17.14.36310.24
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MemMAPR-MKE", "MemMAPR-MKE.vcxproj", "{5D2C9737-E669-4AF1-9B8C-4339EF6E4EB0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
DEBUG_THIRD|x64 = DEBUG_THIRD|x64
DEBUG_THIRD|x86 = DEBUG_THIRD|x86
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5D2C9737-E669-4AF1-9B8C-4339EF6E4EB0}.DEBUG_THIRD|x64.ActiveCfg = DEBUG_FIRST|x64
{5D2C9737-E669-4AF1-9B8C-4339EF6E4EB0}.DEBUG_THIRD|x64.Build.0 = DEBUG_FIRST|x64
{5D2C9737-E669-4AF1-9B8C-4339EF6E4EB0}.DEBUG_THIRD|x86.ActiveCfg = DEBUG_FIRST|Win32
{5D2C9737-E669-4AF1-9B8C-4339EF6E4EB0}.DEBUG_THIRD|x86.Build.0 = DEBUG_FIRST|Win32
{5D2C9737-E669-4AF1-9B8C-4339EF6E4EB0}.Debug|x64.ActiveCfg = Debug|x64
{5D2C9737-E669-4AF1-9B8C-4339EF6E4EB0}.Debug|x64.Build.0 = Debug|x64
{5D2C9737-E669-4AF1-9B8C-4339EF6E4EB0}.Debug|x86.ActiveCfg = Debug|Win32

View File

@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="DEBUG_FIRST|Win32">
<Configuration>DEBUG_FIRST</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="DEBUG_FIRST|x64">
<Configuration>DEBUG_FIRST</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
@@ -32,6 +40,12 @@
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DEBUG_FIRST|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
@@ -45,6 +59,12 @@
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DEBUG_FIRST|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
@@ -60,12 +80,18 @@
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='DEBUG_FIRST|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='DEBUG_FIRST|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
@@ -82,6 +108,18 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DEBUG_FIRST|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
@@ -100,8 +138,24 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>Default</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DEBUG_FIRST|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>DEBUG;_CONSOLE;THIRD;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>Default</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -116,6 +170,8 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>Default</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

View File

@@ -2,165 +2,151 @@
#include <Eigen/Dense>
using namespace Eigen;
Solver::Solver(double _A, double _B, double _C, int _N, int _l, int _u) {
A = _A, B = _B, C = _C, N = _N;
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 = upper - lower;
dx = L / N;
}
void Solver::Execute_Linear(double val1, double val2) {
MatrixXd local = MatrixXd::Zero(2, 2);
local(0, 0) = A / dx - B / 2.;
local(0, 1) = -A / dx + B / 2.;
local(1, 0) = -A / dx - B / 2.;
local(1, 1) = A / dx + B / 2.;
std::cout << "Local matrix:\n" << local << std::endl;
VectorXd local_load(2);
local_load(0) = C * dx / 2;
local_load(1) = C * dx / 2;
// Ансаблированные матрицы и вектор
local(0, 0) = -A / dx - B / 2. + C * dx / 3.;
local(0, 1) = A / dx + B / 2. + C * dx / 6.;
local(1, 0) = A / dx - B / 2. + C * dx / 6.;
local(1, 1) = -A / dx + B / 2. + C * dx / 3.;
local_load(0) = -D * dx / 2.;
local_load(1) = -D * dx / 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 << "Before:" << std::endl;
std::cout << "Ansamb matrix:\n" << ansamb << std::endl;
std::cout << "Ansamb load vector:\n" << global_load << std::endl;
#endif
// ГУ 1-го рода
double u_left = val1; // u(1) = 5
double u_right = val2; // u(6) = 15
double u_right = val2;
// Обнуляем первую строку и столбец матрицы, устанавливаем диагональ = 1
// Clear first and last rows
ansamb.row(0).setZero();
ansamb.col(0).setZero();
ansamb(0, 0) = 1.0;
global_load(0) = u_left;
// Обнуляем последнюю строку и столбец матрицы, устанавливаем диагональ = 1
ansamb.row(N).setZero();
ansamb.col(N).setZero();
ansamb(N, N) = 1.0;
// u'(0) = u(0)
ansamb(0, 0) = dx + 1;
ansamb(0, 1) = -1;
global_load(0) = 0;
// u(10) = u_right = 5
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_2.txt");
for (int i = 0; i < N + 1; i++) {
std::ofstream file("matrix_linear.txt");
for (int i = 0; i < N; i++) {
file << solution(i) << ' ';
}
file << std::endl;
}
void Solver::Execute_Cubic(double val1, double val2) {
// Локальная матрица жесткости (4x4)
MatrixXd local = MatrixXd::Zero(4, 4);
local(0, 0) = -37. * A / (10 * dx) - B / 2.;
local(0, 1) = 189. * A / (40 * dx) + 57. * B / 80.;
local(0, 2) = -27. * A / (20 * dx) - 3. * B / 10.;
local(0, 3) = 13. * A / (40. * dx) + 7. * B / 10.;
local(1, 0) = 189. * A / (40 * dx) - 57. * B / 80.;
local(1, 1) = -54. * A / (5 * dx);
local(1, 2) = 297. * A / (40. * dx) + 81. * B / 80.;
local(1, 3) = -27 * A / (20. * dx) - 3. * B / 10.;
int mat_dim = 1 + N * 3;
Eigen::MatrixXd Amat(mat_dim, mat_dim);
Eigen::VectorXd b(mat_dim);
Amat.setZero();
b.setZero();
local(2, 0) = -27. * A / (20. * dx) + 3. * B / 10.;
local(2, 1) = 297. * A / (40. * dx) - 81. * B / 80.;
local(2, 2) = -54. * A / (5. * dx);
local(2, 3) = 189. * A / (40. * dx) + 57. * B / 80.;
// Assemble matrix
for (int i = 0; i < mat_dim - 3; i += 3) {
Amat(i, i + 0) -= A * 37.0 / 10.0 / dx;
Amat(i, i + 1) -= A * (-189.0) / 40.0 / dx;
Amat(i, i + 2) -= A * 27.0 / 20.0 / dx;
Amat(i, i + 3) -= A * (-13.0) / 40.0 / dx;
Amat(i + 1, i + 0) -= A * (-189.0) / 40.0 / dx;
Amat(i + 1, i + 1) -= A * 54.0 / 5.0 / dx;
Amat(i + 1, i + 2) -= A * (-297.0) / 40.0 / dx;
Amat(i + 1, i + 3) -= A * 27.0 / 20.0 / dx;
Amat(i + 2, i + 0) -= A * 27.0 / 20.0 / dx;
Amat(i + 2, i + 1) -= A * (-297.0) / 40.0 / dx;
Amat(i + 2, i + 2) -= A * 54.0 / 5.0 / dx;
Amat(i + 2, i + 3) -= A * (-189.0) / 40.0 / dx;
Amat(i + 3, i + 0) -= A * (-13.0) / 40.0 / dx;
Amat(i + 3, i + 1) -= A * 27.0 / 20.0 / dx;
Amat(i + 3, i + 2) -= A * (-189.0) / 40.0 / dx;
Amat(i + 3, i + 3) -= A * 37.0 / 10.0 / dx;
local(3, 0) = 13. * A / (40. * dx) - 7. * B / 80.;
local(3, 1) = -27. * A / (20. * dx) + 3. * B / 10.;
local(3, 2) = 189. * A / (40. * dx) - 57. * B / 80.;
local(3, 3) = -37. * A / (10. * dx) + B / 2.;
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;
// Локальный вектор нагрузки (4x1)
VectorXd local_load(4);
local_load(0) = -C * dx / 8.;
local_load(1) = -3. * C * dx / 8.;
local_load(2) = -3. * C * dx / 8.;
local_load(3) = -C * dx / 8.;
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;
std::cout << "Local matrix:\n" << local << std::endl;
std::cout << "Local load vector:\n" << local_load << std::endl;
// Размер глобальной системы: для кубических элементов (4 узла на элемент, перекрытие по 2 узла)
int ndof = 2 * N + 2;
MatrixXd ansamb = MatrixXd::Zero(ndof, ndof);
VectorXd global_load = VectorXd::Zero(ndof);
// АНСАМБЛИРОВАНИЕ
for (int elem = 0; elem < N; ++elem) {
int node_start = 2 * elem; // Начальный индекс для текущего элемента
// Добавляем локальную матрицу
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
ansamb(node_start + i, node_start + j) += local(i, j);
}
}
// Добавляем локальный вектор нагрузки
for (int i = 0; i < 4; ++i) {
global_load(node_start + i) += local_load(i);
}
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;
}
std::cout << "Before boundary conditions:" << std::endl;
std::cout << "Ansamb matrix:\n" << ansamb << std::endl;
std::cout << "Ansamb load vector:\n" << global_load << std::endl;
// Assembdxe vector
for (int i = 0; i < mat_dim - 3; i += 3) {
b(i) -= D * dx / 8.0;
b(i + 1) -= D * 3.0 * dx / 8.0;
b(i + 2) -= D * 3.0 * dx / 8.0;
b(i + 3) -= D * dx / 8.0;
}
// ГРАНИЧНЫЕ УСЛОВИЯ
double u_left = val1; // u(1) = 5
double u_right = val2; // u(6) = 15
ansamb.row(0).setZero();
ansamb.col(0).setZero();
ansamb(0, 0) = 1.0;
global_load(0) = u_left;
Amat.row(0).setZero();
Amat(0, 0) = dx / 3.0 + 1;
Amat(0, 1) = -1;
b(0) = 0;
ansamb.row(ndof - 1).setZero();
ansamb.col(ndof - 1).setZero();
ansamb(ndof - 1, ndof - 1) = 1.0;
global_load(ndof - 1) = u_right;
std::cout << "\nAfter boundary conditions:" << std::endl;
std::cout << "Modified matrix:\n" << ansamb << std::endl;
std::cout << "Modified load vector:\n" << global_load << std::endl;
Amat.row(mat_dim - 1).setZero();
Amat(mat_dim - 1, mat_dim - 1) = 1;
b(mat_dim - 1) = val2;
// Решение системы
VectorXd solution = ansamb.fullPivLu().solve(global_load);
VectorXd solution = Amat.colPivHouseholderQr().solve(b);
std::cout << "\nSolution:" << std::endl;
std::cout << solution << std::endl;
// Сохранение результатов (берем только значения функции в узлах, шаг 2)
std::ofstream file("matrix_cubic.txt");
for (int i = 0; i < ndof; i += 2) {
for (int i = 0; i < solution.size(); i++) {
file << solution(i) << ' ';
}
file << std::endl;

View File

@@ -1,10 +1,11 @@
#pragma once
class Solver {
private:
double A, B, C, L, dx;
double A, B, C, D, L, dx;
int N, upper, lower;
public:
Solver(double _A, double _B, double _C, int _N, int _l, int _u);
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);
};