Python-визуализация сетки

This commit is contained in:
2025-05-15 18:00:18 +03:00
parent 9325bbc747
commit 2933868ef9
6 changed files with 52 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ public:
Mesh(Object&, double); Mesh(Object&, double);
~Mesh(); ~Mesh();
//void ShowLinks(); //void ShowLinks();
void VisualizeMesh(std::string);
std::vector<std::vector<Node*>>& Nodes() { return mesh; } std::vector<std::vector<Node*>>& Nodes() { return mesh; }
std::vector<Node*>& LineX() { return hlines; } std::vector<Node*>& LineX() { return hlines; }

View File

@@ -11,6 +11,7 @@ class System{
public: public:
System(Object& obj, double step = 10., double a1 = 1.): _obj(obj), mesh(obj, step), _a(a1), _step(step) {} System(Object& obj, double step = 10., double a1 = 1.): _obj(obj), mesh(obj, step), _a(a1), _step(step) {}
void DefineBounds(int, int, int, int); void DefineBounds(int, int, int, int);
void export_mesh(std::string);
std::vector<std::vector<Node*>>& Nodes(); std::vector<std::vector<Node*>>& Nodes();
std::vector<Node*>& LineX(); std::vector<Node*>& LineX();
std::vector<Node*>& LineY(); std::vector<Node*>& LineY();

View File

@@ -1,5 +1,6 @@
#include "Mesh.hpp" #include "Mesh.hpp"
#include <iostream> #include <iostream>
#include <fstream>
#include <Object.hpp> #include <Object.hpp>
Mesh::Mesh(Object& _obj, double _step) : obj(_obj), step(_step) { Mesh::Mesh(Object& _obj, double _step) : obj(_obj), step(_step) {
@@ -14,6 +15,21 @@ Mesh::Mesh(Object& _obj, double _step) : obj(_obj), step(_step) {
Adapt(); Adapt();
} }
void Mesh::VisualizeMesh(std::string name) {
std::ofstream data_file(name);
data_file << "# Coordinates\n";
data_file << "# Format: x y\n";
for (const auto& row : mesh) {
for (const auto& node : row) {
data_file << node->X() << " " << node->Y() << "\n";
}
}
data_file.close();
}
void Mesh::LinkX() { void Mesh::LinkX() {
for (int i = 0; i < mesh.size(); i++) { for (int i = 0; i < mesh.size(); i++) {
mesh[i][0]->LinkX(nullptr, mesh[i][1]); mesh[i][0]->LinkX(nullptr, mesh[i][1]);

View File

@@ -1,5 +1,9 @@
#include "System.hpp" #include "System.hpp"
void System::export_mesh(std::string name) {
mesh.VisualizeMesh(name);
}
void System::DefineBounds(int l, int t, int r, int b) { void System::DefineBounds(int l, int t, int r, int b) {
Node* cur = mesh.LineX().front(); Node* cur = mesh.LineX().front();
while (cur) { while (cur) {

View File

@@ -73,6 +73,10 @@ int main()
System implicit5(obj, step_5, CONDUCTIVITY); System implicit5(obj, step_5, CONDUCTIVITY);
System implicit10(obj, step_10, CONDUCTIVITY); System implicit10(obj, step_10, CONDUCTIVITY);
/* Экспорт сеток */
explicit5.export_mesh("mesh5.txt");
explicit10.export_mesh("mesh10.txt");
explicit5.DefineBounds(left, top, right, bottom); explicit5.DefineBounds(left, top, right, bottom);
explicit10.DefineBounds(left, top, right, bottom); explicit10.DefineBounds(left, top, right, bottom);

26
visual_mesh.py Normal file
View File

@@ -0,0 +1,26 @@
import matplotlib.pyplot as plt
x_coords = []
y_coords = []
try:
filename = input("Input name: ")
with open(f'{filename}', 'r') as file:
lines = file.readlines()
for line in lines[2:]:
if line.strip():
x, y = map(float, line.strip().split())
x_coords.append(x)
y_coords.append(y)
plt.figure(figsize=(10, 6))
plt.scatter(x_coords, y_coords, s=10, c='blue', alpha=0.5)
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)
plt.axis('equal')
plt.savefig(f"{filename[:-4]}.png")
plt.show()
except FileNotFoundError:
print(f"{filename} not found. ABORT!")