TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Coarsen_Operator_Uniform.tpp
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#ifndef Coarsen_Operator_Uniform_TPP_H
16#define Coarsen_Operator_Uniform_TPP_H
17
18#include <Domaine_IJK.h>
19#include <Perf_counters.h>
20
21template<typename _TYPE_>
22void Coarsen_Operator_Uniform::initialize_grid_data_(const Grid_Level_Data_template<_TYPE_>& fine,
24 int additional_k_layers)
25{
26 const Domaine_IJK& src_grid_geom = fine.get_domaine();
27 VECT(ArrOfDouble) coarse_delta(3);
28 ArrOfInt nlocal(3);
29
30 for (int dir = 0; dir < 3; dir++)
31 {
32 const int coarsen_factor = coarsen_factors_[dir];
33 const ArrOfDouble& fine_delta = src_grid_geom.get_delta(dir);
34
35 const int src_n = fine_delta.size_array();
36 if (src_n % coarsen_factor != 0)
37 {
38 Cerr << "Coarsen_Operator_Uniform::initialize_grid_data: source grid has " << src_n
39 << " elements in direction " << dir
40 << " and cannot be refined by factor " << coarsen_factor << finl;
42 }
43 nlocal[dir] = fine.get_rho().nb_elem_local(dir);
44 if (nlocal[dir] % coarsen_factor != 0)
45 {
46 Cerr << "Coarsen_Operator_Uniform::initialize_grid_data: local source grid processor has " << nlocal[dir]
47 << " elements in direction " << dir
48 << " and cannot be refined by factor " << coarsen_factor << finl;
50 }
51 nlocal[dir] /= coarsen_factor;
52 const int n = src_n / coarsen_factor;
53 ArrOfDouble& delta = coarse_delta[dir];
54 delta.resize_array(n);
55
56 int src_index = 0;
57 delta.resize_array(n);
58 for (int dest_index = 0; dest_index < n; dest_index++)
59 {
60 double d = 0;
61 for (int j = 0; j < coarsen_factor; j++)
62 {
63 d += fine_delta[src_index];
64 src_index++;
65 }
66 delta[dest_index] = d;
67 }
68 }
69
70 Domaine_IJK grid_geom;
71 grid_geom.initialize_origin_deltas(src_grid_geom.get_origin(0),
72 src_grid_geom.get_origin(1),
73 src_grid_geom.get_origin(2),
74 coarse_delta[0],
75 coarse_delta[1],
76 coarse_delta[2],
77 src_grid_geom.get_periodic_flag(0),
78 src_grid_geom.get_periodic_flag(1),
79 src_grid_geom.get_periodic_flag(2));
80
81 Domaine_IJK coarse_splitting;
82 // Same processor mapping as fine mesh
83 IntTab processor_mapping;
84 fine.get_domaine().get_processor_mapping(processor_mapping);
85 // Splitting is identical, divide ncells by the coarsening factor
86 VECT(ArrOfInt) slice_sizes(3);
87 for (int dir = 0; dir < 3; dir++)
88 {
89 fine.get_domaine().get_slice_size(dir, Domaine_IJK::ELEM, slice_sizes[dir]);
90 const int n = slice_sizes[dir].size_array();
91 for (int i = 0; i < n; i++)
92 slice_sizes[dir][i] /= coarsen_factors_[dir];
93 }
94 coarse_splitting.initialize_mapping(grid_geom, slice_sizes[0], slice_sizes[1], slice_sizes[2],
95 processor_mapping);
96 const int ghost_domaine_size = fine.get_ghost_size();
97 coarse.initialize(coarse_splitting, ghost_domaine_size, additional_k_layers);
98}
99
100template <typename _TYPE_, typename _TYPE_ARRAY_>
101void Coarsen_Operator_Uniform::coarsen_(const IJK_Field_template<_TYPE_,_TYPE_ARRAY_>& fine,
103 int compute_weighted_average) const
104{
105 statistics().create_custom_counter("multigrid: uniform coarsening",2,"IJK");
106 statistics().begin_count("multigrid: uniform coarsening",statistics().get_last_opened_counter_level()+1);
107 const int ni2 = coarse.ni();
108 const int nj2 = coarse.nj();
109 const int nk2 = coarse.nk();
110
111 _TYPE_ coef = 1;
112 if (compute_weighted_average)
113 coef = (_TYPE_)(1. / (coarsen_factors_[0] * coarsen_factors_[1] * coarsen_factors_[2]));
114
115 const int Kstart = 0;
116 const int Kend = nk2;
117 const int deltaK = 1;
118 for (int K = Kstart; K != Kend; K += deltaK)
119 {
120 const int k = K*coarsen_factors_[2];
121 for (int J = 0; J < nj2; J++)
122 {
123 const int j = J*coarsen_factors_[1];
124 for (int I = 0; I < ni2; I++)
125 {
126 const int i = I*coarsen_factors_[0];
127 _TYPE_ sum = 0.;
128 for (int ii = 0; ii < coarsen_factors_[0]; ii++)
129 for (int jj = 0; jj < coarsen_factors_[1]; jj++)
130 for (int kk = 0; kk < coarsen_factors_[2]; kk++)
131 sum += fine(i + ii, j + jj, k + kk);
132 coarse(I,J,K) = sum * coef;
133 }
134 }
135 }
136
137 statistics().end_count("multigrid: uniform coarsening");
138 return;
139}
140
141template <typename _TYPE_, typename _TYPE_ARRAY_>
142void Coarsen_Operator_Uniform::interpolate_sub_shiftk_(const IJK_Field_template<_TYPE_, _TYPE_ARRAY_>& coarse,
144 const int kshift) const
145{
146 if (kshift == 1)
147 {
148 Cerr << "error Coarsen_Operator_Uniform: kshift=1 will not work" << finl;
150 }
151
152 statistics().create_custom_counter("multigrid: interpolate uniform",2,"IJK");
153 statistics().begin_count("multigrid: interpolate uniform",statistics().get_last_opened_counter_level()+1);
154
155 const int ni2 = coarse.ni();
156 const int nj2 = coarse.nj();
157 const int nk2 = coarse.nk();
158
159 const int Kstart = kshift <= 0 ? 0 : nk2 - 1;
160 const int Kend = kshift <= 0 ? nk2 : -1;
161 const int deltaK = kshift <= 0 ? 1 : -1;
162
163 for (int K = Kstart; K != Kend; K += deltaK)
164 {
165 const int k = K*coarsen_factors_[2];
166 for (int J = 0; J < nj2; ++J)
167 {
168 const int j = J*coarsen_factors_[1];
169 for (int I = 0; I < ni2; ++I)
170 {
171 const int i = I*coarsen_factors_[0];
172 const _TYPE_ val = coarse(I, J, K);
173 for (int ii = 0; ii < coarsen_factors_[0]; ++ii)
174 for (int jj = 0; jj < coarsen_factors_[1]; ++jj)
175 for (int kk = 0; kk < coarsen_factors_[2]; ++kk)
176 fine.get_in_allocated_area(i + ii, j + jj, k + kk + kshift) = fine(i + ii, j + jj, k + kk) - val;
177 }
178 }
179 }
180
181 fine.shift_k_origin(kshift);
182 statistics().end_count("multigrid: interpolate uniform");
183 return;
184}
185
186#endif
bool get_periodic_flag(int direction) const
Method returns true if periodic in this direction.
void get_processor_mapping(IntTab &mapping) const
Fills an array containing the mapping of processors.
const ArrOfDouble & get_delta(int direction) const
Returns the array of mesh cell sizes in requested direction.
double get_origin(int direction) const
Returns the coordinate of the first node (global) of the mesh in the requested direction.
void initialize_mapping(Domaine_IJK &dom, const ArrOfInt &slice_size_i, const ArrOfInt &slice_size_j, const ArrOfInt &slice_size_k, const IntTab &processor_mapping)
Creates a splitting of the domain by specifying the slice sizes and the processor mapping.
void get_slice_size(int direction, Localisation loc, ArrOfInt &tab) const
Returns the number of items of given location (elements, nodes, faces...) for all slices in the reque...
void initialize_origin_deltas(double x0, double y0, double z0, const ArrOfDouble &delta_x, const ArrOfDouble &delta_y, const ArrOfDouble &delta_z, bool perio_x, bool perio_y, bool perio_z)
Initializes class elements given dataset's parameters.
const IJK_Field_template< _TYPE_, TRUSTArray< _TYPE_ > > & get_rho() const
void initialize(const Domaine_IJK &, int ghost, int additional_k_layers)
const Domaine_IJK & get_domaine() const
_TYPE_ & get_in_allocated_area(int i, int j, int k)
: This class is an IJK_Field_local with parallel informations.
void begin_count(const STD_COUNTERS &std_cnt, int counter_lvl=-100000)
void end_count(const std::string &custom_count_name, int count_increment=1, long int quantity_increment=0)
End the count of a counter and update the counter values.
void create_custom_counter(std::string counter_description, int counter_level, std::string counter_family="None", bool is_comm=false, bool is_gpu=false)
Create a new counter and add it to the map of custom counters.
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
_SIZE_ size_array() const
void resize_array(_SIZE_ new_size, RESIZE_OPTIONS opt=RESIZE_OPTIONS::COPY_INIT)