TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Domaine_PolyMAC_HFV.cpp
1/****************************************************************************
2* Copyright (c) 2025, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#include <Linear_algebra_tools_impl.h>
17#include <Champ_implementation_P1.h>
18#include <Connectivite_som_elem.h>
19#include <MD_Vector_composite.h>
20#include <Domaine_Cl_PolyMAC_family.h>
21#include <MD_Vector_tools.h>
22#include <Comm_Group_MPI.h>
23#include <communications.h>
24#include <Poly_geom_base.h>
25#include <Matrix_tools.h>
26#include <Domaine_PolyMAC_HFV.h>
27#include <unordered_map>
28#include <Array_tools.h>
29#include <TRUSTLists.h>
30#include <PE_Groups.h>
31#include <Rectangle.h>
32#include <Tetraedre.h>
33#include <EFichier.h>
34#include <SFichier.h>
35#include <Hexaedre.h>
36#include <Triangle.h>
37#include <Segment.h>
38#include <Domaine.h>
39#include <Scatter.h>
40#include <EChaine.h>
41#include <Lapack.h>
42#include <unistd.h>
43#include <numeric>
44#include <cfloat>
45#include <vector>
46#include <tuple>
47#include <cmath>
48#include <cfenv>
49#include <set>
50#include <map>
51
52Implemente_instanciable(Domaine_PolyMAC_HFV, "Domaine_PolyMAC_HFV", Domaine_PolyMAC_CDO);
53
55
57
64
66{
67 const double tol_r = 1e-12;
68 const IntTab& fsom = face_sommets();
69 const DoubleTab& xs = domaine().coord_sommets();
70
71 //volumes_entrelaces_ and volumes_entrelaces_dir: by projection of the upstream/downstream element onto the face normal
72 for (int f = 0, e; f < nb_faces(); f++)
73 {
74 const bool axis_face = bidim_axi && (face_voisins_(f, 0) < 0 || face_voisins_(f, 1) < 0) && std::fabs(xv_(f, 0)) <= tol_r;
75 double axis_length = 0.;
76
77 if (axis_face)
78 {
79 assert(fsom.dimension(1) == 2);
80 const int s0 = fsom(f, 0);
81 const int s1 = fsom(f, 1);
82 const double dr = xs(s1, 0) - xs(s0, 0);
83 const double dz = xs(s1, 1) - xs(s0, 1);
84 axis_length = std::sqrt(dr * dr + dz * dz);
85 }
86
87 for (int i = 0; i < 2 && (e = face_voisins_(f, i)) >= 0; i++)
88 {
89 volumes_entrelaces_dir_(f, i) = axis_face
90 ? volume_entrelace_axi(std::fabs(xv_(f, 0)), std::fabs(xp_(e, 0)), axis_length)
91 : std::fabs(dot(&xp_(e, 0), &face_normales_(f, 0), &xv_(f, 0)));
92
94 }
95 }
96 volumes_entrelaces_.echange_espace_virtuel(), volumes_entrelaces_dir_.echange_espace_virtuel();
97}
98
100{
101 if (surf_elem_arete_.nb_dim() == 2) return surf_elem_arete_; //already done
102 int e, f, a, s, sb, i, j, k, d, D = dimension, sgn;
103 const IntTab& ea_d = elem_arete_d(), e_f = elem_faces(), &f_s = face_sommets(), &e_a = D < 3 ? e_f : domaine().elem_aretes();//in 2D, edges are faces!
104 const DoubleTab& xe = xp(), &xf = xv(), &xs = domaine().coord_sommets();
105 surf_elem_arete_.resize(ea_d(nb_elem_tot()), D);
106 double vecz[3] = { 0, 0, 1 };
107 for (e = 0; e < nb_elem_tot(); e++)
108 if (D < 3)
109 for (i = 0, j = ea_d(e); j < ea_d(e + 1); i++, j++) //2D: edge <-> face, oriented from the first to the second vertex of f_s
110 {
111 auto vec = cross(D, 3, &xf(f = e_f(e, i), 0), vecz, &xe(e, 0)); //rotation of (xf - xe)
112 for (sgn = dot(&xs(f_s(f, 1), 0), &vec[0], &xs(f_s(f, 0), 0)) > 0 ? 1 : -1, d = 0; d < D; d++) surf_elem_arete_(j, d) = sgn * vec[d]; //with correct orientation
113 }
114 else for (i = 0; i < e_f.dimension(1) && (f = e_f(e, i)) >= 0; i++)
115 for (j = 0; j < f_s.dimension(1) && (s = f_s(f, j)) >= 0; j++) //3D: one contribution per (f, a) pair
116 {
117 sb = f_s(f, j + 1 < f_s.dimension(1) && f_s(f, j + 1) >= 0 ? j + 1 : 0); //other vertex
118 a = som_arete[s].at(sb), k = (int)(std::find(&e_a(e, 0), &e_a(e, 0) + ea_d(e + 1) - ea_d(e), a) - &e_a(e, 0) + ea_d(e)); //edge and its index in e_a
119 auto vec = cross(D, D, &xf(f, 0), &xa_(a, 0), &xe(e, 0), &xe(e, 0)); //unoriented
120 for (sgn = dot(&vec[0], &ta_(a, 0)) >= 0 ? 1 : -1, d = 0; d < D; d++) surf_elem_arete_(k, d) += sgn * vec[d] / 2;
121 }
122 return surf_elem_arete_;
123}
124/* "clamping" to 0 of small coefficients in M1/W1/M2/W2 */
125inline void clamp(DoubleTab& m)
126{
127 for (int i = 0; i < m.dimension(0); i++)
128 for (int j = 0; j < m.dimension(1); j++)
129 for (int n = 0; n < m.dimension(2); n++)
130 if (1e6 * std::abs(m(i, j, n)) < std::abs(m(i, i, n)) + std::abs(m(j, j, n))) m(i, j, n) = 0;
131}
132
133//dual edge normals -> edge tangentials: (nu|a|t_a.v) = m1 (S_ea.v)
134void Domaine_PolyMAC_HFV::M1(const DoubleTab *nu, int e, DoubleTab& m1) const
135{
136 const IntTab& e_f = elem_faces(), &f_s = face_sommets(), &e_a = dimension < 3 ? e_f : domaine().elem_aretes(), &a_s = dimension < 3 ? f_s : domaine().aretes_som(), &ea_d = elem_arete_d(); //in 2D, edges are faces!
137 const DoubleTab& xs = domaine().coord_sommets(), &S_ea = surf_elem_arete();
138 const DoubleVect& ve = volumes();
139 int i, j, k, a, s, sb, n, N = nu ? nu->dimension(1) : 1, e_nu = nu && nu->dimension_tot(0) == 1 ? 0 : e, n_a = ea_d(e + 1) - ea_d(e), d, D = dimension, idx;
140 double prefac, fac, beta = 1 || n_a == 3 * D - 3 ? 1. / D : D == 2 ? 1. / sqrt(2) : 1. / sqrt(3); //stabilization: DGA on simplices, SUSHI otherwise
141 m1.resize(n_a, n_a, N), m1 = 0;
142 DoubleTrav v_e(n_a, D), v_ea(n_a, n_a, D); //non-stabilized / stabilized interpolations
143 //v_e (non-stabilized interpolation)
144 for (i = 0; i < n_a; i++)
145 for (a = e_a(e, i), s = a_s(a, 0), sb = a_s(a, 1), d = 0; d < D; d++) v_e(i, d) = (xs(sb, d) - xs(s, d)) / ve(e);
146 //v_ea (stabilized interpolation)
147 for (i = 0, idx = ea_d(e); i < n_a; i++, idx++)
148 for (a = e_a(e, i), s = a_s(a, 0), sb = a_s(a, 1), prefac = D * beta / dot(&xs(sb, 0), &S_ea(idx, 0), &xs(s, 0)), j = 0; j < n_a; j++)
149 for (fac = prefac * ((j == i) - dot(&S_ea(idx, 0), &v_e(j, 0))), d = 0; d < D; d++)
150 v_ea(i, j, d) = v_e(j, d) + fac * (xs(sb, d) - xs(s, d));
151 //matrix!
152 for (i = 0; i < n_a; i++)
153 for (j = 0; j < n_a; j++)
154 if (j < i)
155 for (n = 0; n < N; n++) m1(i, j, n) = m1(j, i, n); //below diagonal -> copy from the other side
156 else for (k = 0, idx = ea_d(e); k < n_a; k++, idx++)
157 for (a = e_a(e, k), s = a_s(a, 0), sb = a_s(a, 1), fac = dot(&xs(sb, 0), &S_ea(idx, 0), &xs(s, 0)) / D, n = 0; n < N; n++)
158 m1(i, j, n) += fac * nu_dot(nu, e_nu, n, &v_ea(k, i, 0), &v_ea(k, j, 0));
159 clamp(m1);
160}
161
162//edge tangentials -> dual edge normals: (nuS_ea.v) = w1 (|a|t_a.v)
163void Domaine_PolyMAC_HFV::W1(const DoubleTab *nu, int e, DoubleTab& w1, DoubleTab& v_e, DoubleTab& v_ea) const
164{
165 const IntTab& e_f = elem_faces(), &f_s = face_sommets(), &e_a = dimension < 3 ? e_f : domaine().elem_aretes(), &a_s = dimension < 3 ? f_s : domaine().aretes_som(), &ea_d = elem_arete_d(); //in 2D, edges are faces!
166 const DoubleTab& xs = domaine().coord_sommets(), &S_ea = surf_elem_arete();
167 const DoubleVect& ve = volumes();
168 int i, j, k, a, s, sb, n, N = nu ? nu->dimension(1) : 1, e_nu = nu && nu->dimension_tot(0) == 1 ? 0 : e, n_a = ea_d(e + 1) - ea_d(e), d, D = dimension, idx;
169 double prefac, fac, beta = n_a == 3 * D - 3 ? 1. / D : D == 2 ? 1. / sqrt(2) : 1. / sqrt(3); //stabilization: DGA on simplices, SUSHI otherwise
170 w1.resize(n_a, n_a, N), w1 = 0, v_e.resize(n_a, D), v_ea.resize(n_a, n_a, D); //non-stabilized / stabilized interpolations
171 //v_e (non-stabilized interpolation)
172 for (i = 0, idx = ea_d(e); i < n_a; i++, idx++)
173 for (d = 0; d < D; d++) v_e(i, d) = S_ea(idx, d) / ve(e);
174 //v_ea (stabilized interpolation)
175 for (i = 0, idx = ea_d(e); i < n_a; i++, idx++)
176 for (a = e_a(e, i), s = a_s(a, 0), sb = a_s(a, 1), prefac = D * beta / dot(&xs(sb, 0), &S_ea(idx, 0), &xs(s, 0)), j = 0; j < n_a; j++)
177 for (fac = prefac * ((j == i) - dot(&xs(sb, 0), &v_e(j, 0), &xs(s, 0))), d = 0; d < D; d++)
178 v_ea(i, j, d) = v_e(j, d) + fac * S_ea(idx, d);
179 //matrix!
180 for (i = 0; i < n_a; i++)
181 for (j = 0; j < n_a; j++)
182 if (j < i)
183 for (n = 0; n < N; n++) w1(i, j, n) = w1(j, i, n); //below diagonal -> copy from the other side
184 else for (k = 0, idx = ea_d(e); k < n_a; k++, idx++)
185 for (a = e_a(e, k), s = a_s(a, 0), sb = a_s(a, 1), fac = dot(&xs(sb, 0), &S_ea(idx, 0), &xs(s, 0)) / D, n = 0; n < N; n++)
186 w1(i, j, n) += fac * nu_dot(nu, e_nu, n, &v_ea(k, i, 0), &v_ea(k, j, 0));
187 clamp(w1);
188}
const IntTab_t & aretes_som() const
returns the connectivity array edges/vertices.
Definition Domaine.h:156
const DoubleTab_t & coord_sommets() const
Definition Domaine.h:112
int_t elem_aretes(int_t i, int j) const
returns the number of the j-th edge of the i-th element.
Definition Domaine.h:154
double dot(const double *a, const double *b, const double *ma=nullptr, const double *mb=nullptr) const
void W1(const DoubleTab *nu, int e, DoubleTab &w1, DoubleTab &v_e, DoubleTab &v_ea) const
const DoubleTab & surf_elem_arete() const
void M1(const DoubleTab *nu, int e, DoubleTab &m1) const
void calculer_volumes_entrelaces() override
std::vector< std::map< int, int > > som_arete
void discretiser() override
double nu_dot(const DoubleTab *nu, int e, int n, const double *a, const double *b, const double *ma=nullptr, const double *mb=nullptr) const
const IntTab & elem_arete_d() const
int nb_faces() const
Returns the total number of faces.
Definition Domaine_VF.h:471
DoubleTab xp_
Definition Domaine_VF.h:218
IntTab & face_sommets() override
Returns the face/vertex connectivity array.
Definition Domaine_VF.h:590
DoubleVect volumes_entrelaces_
Definition Domaine_VF.h:210
DoubleTab xa_
Definition Domaine_VF.h:225
double volume_entrelace_axi(double r_face, double r_elem, double axis_length) const
Definition Domaine_VF.h:702
DoubleTab xv_
Definition Domaine_VF.h:219
DoubleTab & xv()
Definition Domaine_VF.h:93
IntTab & elem_faces()
Returns the element/face connectivity array.
Definition Domaine_VF.h:550
IntTab face_voisins_
Definition Domaine_VF.h:216
DoubleVect & volumes()
Definition Domaine_VF.h:119
std::array< double, 3 > cross(int dima, int dimb, const double *a, const double *b, const double *ma=nullptr, const double *mb=nullptr) const
Definition Domaine_VF.h:710
DoubleTab face_normales_
Definition Domaine_VF.h:212
DoubleTab volumes_entrelaces_dir_
Definition Domaine_VF.h:211
DoubleTab & xp()
Definition Domaine_VF.h:95
int nb_elem_tot() const
const Domaine & domaine() const
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
static int dimension
Definition Objet_U.h:94
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
static int bidim_axi
Definition Objet_U.h:97
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
Base class for output streams.
Definition Sortie.h:52
void resize(_SIZE_ n, RESIZE_OPTIONS opt=RESIZE_OPTIONS::COPY_INIT)
Definition TRUSTTab.tpp:469
_SIZE_ dimension_tot(int) const override
Definition TRUSTTab.tpp:160
_SIZE_ dimension(int d) const
Definition TRUSTTab.tpp:133