TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
forcage_spectral.cpp
1/****************************************************************************
2* Copyright (c) 2019, 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 <forcage_spectral.h>
17#include <string>
18#include <fstream>
19#include <algorithm> // std::random_shuffle
20
21// GAB pour avoir une distribution Gaussienne, il nous faut la librairie :
22#include <random>
23
24Implemente_instanciable( forcage_spectral, "forcage_spectral", Objet_U ) ;
25
27{
28 Objet_U::printOn( os );
29 return os;
30}
31
33{
34 Objet_U::readOn( is );
35 return is;
36}
37
38double my_function_d_2(const int i, const int j, const int k)
39{
40 double b(2.9);
41 return b;
42 // return i/(j*k);
43}
44
45// 6.5 Multi-dimensional MPI DFTs of Real Data
47{
48 const ptrdiff_t L = 7, M = 8, N = 9;
49 fftw_plan plan;
50 double *rin;
51 fftw_complex *c_out;
52 ptrdiff_t alloc_local, local_n0, local_0_start;
53 int i, j, k;
54
55 // fftw_mpi_init();
56
57 /* get local data size and allocate */
58 alloc_local = fftw_mpi_local_size_3d(L, M, N/2+1, MPI_COMM_WORLD,
59 &local_n0, &local_0_start);
60 rin = fftw_alloc_real(2 * alloc_local);
61// double[2]
62 c_out = fftw_alloc_complex(alloc_local);
63
64 // fftw_complex *ux_f = (fftw_complex*) fftw_malloc ( sizeof(fftw_complex)*kmax*kmax*kmax );
65
66 /* create plan for out-of-place r2c DFT */
67 plan = fftw_mpi_plan_dft_r2c_3d(L, M, N, rin, c_out, MPI_COMM_WORLD,
68 FFTW_MEASURE);
69 // int rank(3);
70 // int n_pointeur[3];
71 // n_pointeur[0]=5,n_pointeur[1]=10,n_pointeur[2]=15;
72 // fftw_complex in_pointer;
73 // fftw_complex out_pointer;
74 // plan2 = fftw_mpi_plan_dft_r2c(rank, n_pointer, in_pointer, out_pointer, flags)
75
76 /* initialize rin to some function my_func(x,y,z) */
77 for (i = 0; i < local_n0; ++i)
78 {
79 compteur[0]+=1;
80 for (j = 0; j < M; ++j)
81 {
82 compteur[1]+=1;
83 for (k = 0; k < N; ++k)
84 {
85 compteur[2]+=1;
86 // ux_f[][0] =
87 // ux_f[][1] =
88 rin[(i*M + j) * (2*(N/2+1)) + k]= my_function_d_2((int)local_0_start+i, j, k);
89 // rin[(i*M + j) * (2*(N/2+1)) + k][1] = my_function_d_2(local_0_start+i, j, k);
90 }
91 }
92 }
93 /* compute transforms as many times as desired */
94 fftw_free(rin); // Dans la doc, ils disent que c'est que si on utilise malloc, mais bon, la ca plante s'il n'y est pas...
95 // fftw_free(c_out);
96 fftw_execute(plan);
97 // fftw_free(rin); // Dans la doc, ils disent que c'est que si on utilise malloc, mais bon, la ca plante s'il n'y est pas...
98
99 fftw_destroy_plan(plan);
100}
101
103 const double kmin, const double kmax)
104{
105 nk = number_k;
106 k_max = kmax;
107 k_min = kmin;
108}
109
110static int myPow(int x, unsigned int p)
111{
112 if (p == 0) return 1;
113 if (p == 1) return x;
114
115 int tmp = myPow(x, p/2);
116 if (p%2 == 0) return tmp * tmp;
117 else return x * tmp * tmp;
118}
119
121{
122 int i,j,k,ind;
123 nk_tot = myPow(nk,3)-1;
124 kx.resize_array(nk_tot);
125 ky.resize_array(nk_tot);
126 kz.resize_array(nk_tot);
127 for (i=0; i<nk; i++)
128 {
129 for (j=0; j<nk; j++)
130 {
131 for (k=0; k<nk; k++)
132 {
133 ind = i*nk*nk+j*nk+k;
134 kx[ind] = k_min + i*(k_max/nk);
135 ky[ind] = k_min + j*(k_max/nk);
136 kz[ind] = k_min + k*(k_max/nk);
137 }
138 }
139 }
140}
141
143{
144 const double pi(3.14159265358979);
145 int i,j,k,ind;
146 nk_tot = myPow(nk,3)-1;
147 fx.resize_array(nk_tot);
148 fy.resize_array(nk_tot);
149 fz.resize_array(nk_tot);
150 for (i=0; i<nk; i++)
151 {
152 for (j=0; j<nk; j++)
153 {
154 for (k=0; k<nk; k++)
155 {
156 ind = i*nk*nk+j*nk+k;
157 fx[ind] = cos(2*pi*kx[ind]);
158 fy[ind] = cos(2*pi*ky[ind]);
159 fz[ind] = cos(2*pi*kz[ind]);
160 }
161 }
162 }
163
164}
165////////////////////////////////////////////////////////////////////////////////
166///////////// RELATED FUNCTIONS ////////////////////////////////////////////////
167////////////////////////////////////////////////////////////////////////////////
168
169void Create_file(std::string const& fichname, std::string const& name)
170{
171
172 std::ofstream myFlux(fichname.c_str());
173 if(myFlux)
174 {
175 myFlux << "----------- " << name << " --------" << endl;
176 }
177}
178
179void Add_in_file(std::string const& fichname, std::string const& information)
180{
181
182 std::ofstream myFlux(fichname.c_str(), ios::app);
183 if(myFlux)
184 {
185 myFlux << information << endl;
186 }
187}
188void Add_in_file(std::string const& fichname, int const information)
189{
190
191 std::ofstream myFlux(fichname.c_str(), ios::app);
192 if(myFlux)
193 {
194 myFlux << information << endl;
195 }
196}
197void Add_in_file(std::string const& fichname, std::string const& info, int const information)
198{
199
200 std::ofstream myFlux(fichname.c_str(), ios::app);
201 if(myFlux)
202 {
203 myFlux << info << " : "<<information << endl;
204 }
205}
206
207
208
209
210// int fftw_org_unidentified_function(int argc, char **argv)
211std::string fftw_org_unidentified_function()
212{
213 const ptrdiff_t N0 = 2, N1 = 5;
214 fftw_plan plan;
215 fftw_complex *data;
216 ptrdiff_t alloc_local, local_n0, local_0_start;
217
218 // MPI_Init(&argc, &argv);
219 // fftw_mpi_init();
220
221 /* get local data size and allocate */
222 // you call fftw_mpi_local_size_2d to find out what portion of the array resides
223 // on each processor, and how much space to allocate. Here, the portion of the
224 // array on each process is a local_n0 by N1 slice of the total array, starting
225 // at index local_0_start. The total number of fftw_complex numbers to
226 // allocate is given by the alloc_local return value, which may be greater than
227 // local_n0 * N1
228 // //////////////////////////////
229 alloc_local = fftw_mpi_local_size_2d(N0, N1, MPI_COMM_WORLD,
230 &local_n0, &local_0_start);
231 data = fftw_alloc_complex(alloc_local);
232
233 /* create plan for in-place forward DFT */
234 plan = fftw_mpi_plan_dft_2d(N0, N1, data, data, MPI_COMM_WORLD,
235 FFTW_FORWARD, FFTW_ESTIMATE);
236
237 /* initialize data to some function my_function(x,y) */
238 // for (i = 0; i < local_n0; ++i) for (j = 0; j < N1; ++j)
239 // {}
240
241 /* compute transforms, in-place, as many times as desired */
242 fftw_execute(plan);
243
244
245 // One good way to output a large multi-dimensional distributed array in MPI
246 // to a portable binary file is to use the free HDF5 library; see the HDF home
247 // page.
248 fftw_free(data);
249 fftw_destroy_plan(plan);
250
251 std::string msg("MPI unditentified");
252 return msg;
253}
254
255double my_function_d(const int i, const int j)
256{
257 return i/j;
258}
259
260// void fftw_org_real_data_MPI_transform(int argc, char **argv)
261std::string fftw_org_real_data_MPI_transform()
262{
263 const ptrdiff_t L = 6, M = 9;
264 fftw_plan plan;
265 double *data;
266 ptrdiff_t alloc_local, local_n0, local_0_start;
267 int i, j;
268
269 fftw_mpi_init();
270 /* get local data size and allocate */
271 alloc_local = fftw_mpi_local_size_2d(L, M, MPI_COMM_WORLD,
272 &local_n0, &local_0_start);
273 data = fftw_alloc_real(alloc_local);
274
275 /* create plan for in-place REDFT10 x RODFT10 */
276 plan = fftw_mpi_plan_r2r_2d(L, M, data, data, MPI_COMM_WORLD,
277 FFTW_REDFT10, FFTW_RODFT10, FFTW_MEASURE);
278
279 /* initialize data to some function my_function(x,y) */
280 for (i = 0; i < local_n0; ++i)
281 for (j = 0; j < M; ++j)
282 data[i*M + j] = my_function_d((int)local_0_start + i, j);
283
284 /* compute transforms, in-place, as many times as desired */
285 fftw_execute(plan);
286 fftw_free(data);
287 fftw_destroy_plan(plan);
288
289 std::string msg("MPI r2r_2d");
290 return msg;
291}
292
293
295{
296 return compteur[0];
297}
299{
300 return compteur[1];
301}
303{
304 return compteur[2];
305}
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
classe Objet_U Cette classe est la classe de base des Objets de TRUST
Definition Objet_U.h:73
virtual Entree & readOn(Entree &)
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
Definition Objet_U.cpp:293
virtual Sortie & printOn(Sortie &) const
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
Definition Objet_U.cpp:282
Classe de base des flux de sortie.
Definition Sortie.h:52
void fftw_org_multi_D_MPI_DFT_real_data()
void set_nk_kmin_kmax(const int number_k, const double kmin, const double kmax)