VM2D 1.14
Vortex methods for 2D flows simulation
Loading...
Searching...
No Matches
BarnesHut.cpp
Go to the documentation of this file.
1/*--------------------------------*- VM2D -*-----------------*---------------*\
2| ## ## ## ## #### ##### | | Version 1.14 |
3| ## ## ### ### ## ## ## ## | VM2D: Vortex Method | 2026/03/06 |
4| ## ## ## # ## ## ## ## | for 2D Flow Simulation *----------------*
5| #### ## ## ## ## ## | Open Source Code |
6| ## ## ## ###### ##### | https://www.github.com/vortexmethods/VM2D |
7| |
8| Copyright (C) 2017-2026 I. Marchevsky, K. Sokol, E. Ryatina, A. Kolganova |
9*-----------------------------------------------------------------------------*
10| File name: BarnesHut.cpp |
11| Info: Source code of VM2D |
12| |
13| This file is part of VM2D. |
14| VM2D is free software: you can redistribute it and/or modify it |
15| under the terms of the GNU General Public License as published by |
16| the Free Software Foundation, either version 3 of the License, or |
17| (at your option) any later version. |
18| |
19| VM2D is distributed in the hope that it will be useful, but WITHOUT |
20| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21| FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
22| for more details. |
23| |
24| You should have received a copy of the GNU General Public License |
25| along with VM2D. If not, see <http://www.gnu.org/licenses/>. |
26\*---------------------------------------------------------------------------*/
27
39#include <fstream>
40
41#include "BarnesHut.h"
42#include "knnCPU.h"
43
44namespace BH
45{
46 //Конструктор для вычисления скоростей частиц pointsVrt, влияющих самих на себя
47 BarnesHut::BarnesHut(const params& prm_, const std::vector<Vortex2D>& pointsVrt)
48 : prm(prm_)
49 {
50 pointsCopyVrt.insert(pointsCopyVrt.end(), pointsVrt.begin(), pointsVrt.end());
51 }//BarnesHut(...)
52
53
54
55 //Построение одного дерева
56 void BarnesHut::BuildOneTree(std::unique_ptr<MortonTree>& tree, int maxTreeLevel, std::vector<PointsCopy>& pointsCopy, double& time)
57 {
58 tree = std::make_unique<MortonTree>(prm, maxTreeLevel, pointsCopy);
59
60 double t1 = omp_get_wtime();
61 tree->MakeRootMortonTree();
62 tree->BuildMortonInternalTree();
63 tree->BuildMortonParticlesTree();
64
65 // Один из двух вариантов заполнения нижних вершин
66 //tree->FillMortonLowCellsA(); // Рекурсивный
67 tree->FillMortonLowCells(); // Нерекурсивный
68
69 double t2 = omp_get_wtime();
70
71 time += t2-t1;
72 }
73
74
75 //Построение всех нужных деревьев на основе заданных точек pointsCopy
77 {
78#ifdef needTreeVrt
79 BuildOneTree(treeVrt, prm.NumOfLevelsVortex, pointsCopyVrt, time); // дерево вихрей
80#endif
81 }
82
83 //габаритные прямоугольники для листовых ячеек и объединение их при подъеме
85 {
86 double t1 = omp_get_wtime();
87
88#ifdef needTreeVrt
89 //treeVrt->MinMaxCellsSearch(0); //габаритные прямоугольники для листовых ячеек
90
91 for (auto& cell : treeVrt->mortonLowCells)
92 treeVrt->MinMaxCellsSearch(cell);
93
94 treeVrt->GabsPyr(0); //расчет габаритных прямоугольников при подъеме от листов к корню (но на самом деле наоборот)
95#endif
96
97 double t2 = omp_get_wtime();
98 time += t2 - t1;
99
100
101
102 }
103
104 // Расчет влияния
105#ifndef CALCSHEET
106 void BarnesHut::InfluenceComputation(std::vector<Point2D>& result, std::vector<double>& epsast, double& timeParams, double& timeInfl, bool calcRadius)
107 {
108 double tTreeParamsStart = omp_get_wtime();
109
110#ifdef OLD_OMP
111 omp_set_nested(1);
112#else
113 omp_set_max_active_levels(3);
114 std::cout << "OMP_LEVEL = 2" << std::endl;
115 //omp_set_max_active_levels(prm.maxLevelOmp + 1);
116#endif
117
118
119 auto& treeContr = treeVrt;
120 auto& pointsCopy = pointsCopyVrt;
121
122
123 treeVrt->CalculateMortonTreeParams(0, 0);
124
125 double tTreeParamsFinish = omp_get_wtime();
126 timeParams += tTreeParamsFinish - tTreeParamsStart;
127
128 double tInflStart = omp_get_wtime();
129
130#pragma omp parallel for schedule(dynamic, 10) //reduction(+:t1, t2, t3)
131 for (int i = 0; i < (int)treeContr->mortonLowCells.size(); ++i)
132 {
133 auto& lci = treeContr->mortonLowCells[i];
134 auto& lowCell = treeContr->mortonTree[lci];
135
136 for (auto& e : lowCell.E)
137 e.toZero();
138
139 treeContr->CalcLocalCoeffToLowLevel(lci, treeVrt, 0, true);
140 treeContr->CalcVeloBiotSavart(lci, treeVrt);
141 treeContr->CalcVeloTaylorExpansion(lci);
142 }
143
144
145 //CPU - neib
146 const size_t knb = 3;
147 std::vector<std::vector<std::pair<double, size_t>>> initdist(pointsCopy.size());
148 for (auto& d : initdist)
149 d.resize(2 * knb, { -1.0, -1 });
150 double timeKnn = -omp_get_wtime();
151 WakekNNnewForEpsast(pointsCopy, knb, initdist);//CPU
152
153#pragma omp parallel for
154 for (int j = 0; j < pointsCopy.size(); ++j)
155 {
156 double sd2 = (initdist[j][0].first + initdist[j][1].first + initdist[j][2].first) / 3.0;
157 pointsCopy[j].epsast = (sd2 > 0) ? sqrt(sd2) : 1000.0;
158 }
159 timeKnn += omp_get_wtime();
160
161 double tInflStop = omp_get_wtime();
162 timeInfl += tInflStop - tInflStart;
163
164 int n = (int)pointsCopy.size();
165
166#pragma omp parallel for
167 for (int i = 0; i < n; ++i)
168 result[i] = IDPI * pointsCopy[i].veloCopy;
169
170 if (calcRadius)
171 {
172#pragma omp parallel for
173 for (int i = 0; i < n; ++i)
174 epsast[i] = pointsCopy[i].epsast;
175 }
176 }//InfluenceComputation(...)
177#endif
178
179}//namespace BH
Заголовок основного класса BarnesHut.
void BuildOneTree(std::unique_ptr< MortonTree > &tree, int maxTreeLevel, std::vector< PointsCopy > &pointsCopy, double &time)
Построение одного дерева tree на основе заданных точек pointsCopy
Definition BarnesHut.cpp:56
void InfluenceComputation(std::vector< Point2D > &result, std::vector< double > &epsast, double &timeParams, double &timeInfl, bool calcRadius)
Расчет влияния в точках дерева, характерных для решаемой задачи (определяется внутри функции)
const params & prm
Ссылка на параметры, считываемые из файла
Definition BarnesHut.h:62
BarnesHut(const params &prm_, const std::vector< Vortex2D > &pointsVrt)
Конструктор для решения задачи NBODY о вычислении скоростей вихревых частиц
Definition BarnesHut.cpp:47
void BuildNecessaryTrees(double &time)
Построение всех нужных деревьев на основе заданных точек pointsCopy
Definition BarnesHut.cpp:76
std::unique_ptr< MortonTree > treeVrt
Умный yказатель на дерево вихрей
Definition BarnesHut.h:68
void BuildEnclosingRectangle(double &time)
Definition BarnesHut.cpp:84
std::vector< PointsCopy > pointsCopyVrt
Список оберток положений вихрей
Definition BarnesHut.h:65
Класс, содержащий параметры метода Баонса - Хата для CPU.
Definition ParamsBH.h:65
int NumOfLevelsVortex
Максимальное количество уровней дерева
Definition ParamsBH.h:81
void WakekNNnewForEpsast(const std::vector< BH::PointsCopy > &vtx, const size_t k, std::vector< std::vector< std::pair< double, size_t > > > &initdist)
Definition knnCPU.cpp:458
knn CPU.
static const double IDPI
Definition defsBH.h:60