TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Init_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 <Init_spectral.h>
17#include <IJK_Field_vector.h>
18#include <string>
19
20Implemente_instanciable( Init_spectral, "Init_spectral", Objet_U ) ;
21
23{
24 Objet_U::printOn( os );
25 return os;
26}
27
29{
30 Objet_U::readOn( is );
31 return is;
32}
33
34double randInRange(double min, double max)
35{
36 return min + ((double)(rand())/(double)(RAND_MAX)) * (max-min);
37}
38
39void compute_inital_velocity_spectral(IJK_Field_vector3_double& velocity)
40{
41 Cout << finl;
42 Cout << " = Initialisation du champ de vitesse =" << finl;
43 Cout << finl;
44
45 fftw_plan plan_ux;
46 fftw_plan plan_uy;
47 fftw_plan plan_uz;
48
49 unsigned int seed = 1;
50 int kmax = velocity[0].ni();
51 // double kpic = 10;
52 // double s = 2;
53 // DoubleTab coord_velocity_fourier(kmax,2);
54 DoubleTab angles(kmax,3); // (theta_1, theta_2 et phi)
55
56 // Tirage aleatoire
57 srand(seed);
58 for(int i=0; i<kmax; i++)
59 {
60 angles(i,0) = randInRange(0.0, 2.*M_PI);
61 angles(i,1) = randInRange(0.0, 2.*M_PI);
62 angles(i,2) = randInRange(0.0, 2.*M_PI);
63 }
64
65 // On définit le tableau des nombres d'ondes de 0 à Kmax/2-1 puis de -Kmax/2 à -1
66 DoubleTab kx(kmax, 1);
67 for (int i=0 ; i<kmax/2; i++)
68 {
69 kx[i] = i;
70 }
71 for (int i=-kmax/2; i<0; i++)
72 {
73 kx[i+kmax] = i;
74 }
75 kx[0] = 1; // pour la division par zéro
76
77
78 fftw_complex *ux_f = (fftw_complex*) fftw_malloc ( sizeof(fftw_complex) * kmax*kmax*kmax );
79 fftw_complex *uy_f = (fftw_complex*) fftw_malloc ( sizeof(fftw_complex) * kmax*kmax*kmax );
80 fftw_complex *uz_f = (fftw_complex*) fftw_malloc ( sizeof(fftw_complex) * kmax*kmax*kmax );
81 // On va de -16 à 16 (on ne passe pas par zéro)
82 // Les données sont stockés de 1 à 16 puis de -16 à -1
83 for(int i = 0; i<kmax; i++)
84 {
85 for(int j = 0; j<kmax; j++)
86 {
87 // double Sk1k2 = sqrt(kx[i]*kx[i] + kx[j]*kx[j]);
88 for(int k = 0; k<kmax; k++)
89 {
90 // double normK = sqrt(kx[i]*kx[i] + kx[j]*kx[j] + kx[k]*kx[k]);
91 // double Ek = 1/kpic * pow(normK/kpic, s) * exp(-s/2. * pow(normK/kpic, 2.));
92 // double sqrt_Ek_4piK2 = sqrt(Ek/(4.0*M_PI*normK*normK));
93 // const double theta1_k = angles((int)floor(normK), 0);
94 // const double theta2_k = angles((int)floor(normK), 1);
95 // const double phi_k = angles((int)floor(normK), 2);
96 // double cTh1 = cos(theta1_k);
97 // double sTh1 = sin(theta1_k);
98 // double cTh2 = cos(theta2_k);
99 // double sTh2 = sin(theta2_k);
100 // double cPhi = cos(phi_k);
101 // double sPhi = sin(phi_k);
102 // double alphaKr = sqrt_Ek_4piK2 * cTh1 * cPhi;
103 // double alphaKi = sqrt_Ek_4piK2 * sTh1 * cPhi;
104 // double betaKr = sqrt_Ek_4piK2 * cTh2 * sPhi;
105 // double betaKi = sqrt_Ek_4piK2 * sTh2 * sPhi;
106 ux_f[(i*kmax+j)*kmax+k][0] = 0; // (alphaKr*normK*j + betaKr*i*k)/(normK*Sk1k2);
107 ux_f[(i*kmax+j)*kmax+k][1] = 0; // (alphaKi*normK*j + betaKi*i*k)/(normK*Sk1k2);
108 uy_f[(i*kmax+j)*kmax+k][0] = 0; // (betaKr*j*k - alphaKr*normK*i)/(normK*Sk1k2);
109 uy_f[(i*kmax+j)*kmax+k][1] = 0; // (betaKi*j*k - alphaKi*normK*i)/(normK*Sk1k2);
110 uz_f[(i*kmax+j)*kmax+k][0] = 0; // betaKr*Sk1k2/normK;
111 uz_f[(i*kmax+j)*kmax+k][1] = 0; // betaKi*Sk1k2/normK;
112 }
113 }
114 }
115 // ux_f[(0*kmax+1)*kmax+0][0] = 1;
116 // ux_f[(0*kmax+1)*kmax+0][1] = 1;
117 ux_f[((kmax/2+1)*kmax+kmax/2+1)*kmax+kmax/2+1][0] = 1;
118 uy_f[((kmax/2+1)*kmax+kmax/2+1)*kmax+kmax/2+1][0] = 1;
119
120 // Create output fields in real space
121 fftw_complex *ux_r = (fftw_complex*) fftw_malloc ( sizeof(fftw_complex) * kmax*kmax*kmax );
122 fftw_complex *uy_r = (fftw_complex*) fftw_malloc ( sizeof(fftw_complex) * kmax*kmax*kmax );
123 fftw_complex *uz_r = (fftw_complex*) fftw_malloc ( sizeof(fftw_complex) * kmax*kmax*kmax );
124
125 // And fill them with (inverse?) Fourier Transform 3D
126 plan_ux = fftw_plan_dft_3d(kmax, kmax, kmax, ux_f, ux_r, FFTW_BACKWARD, FFTW_ESTIMATE);
127 plan_uy = fftw_plan_dft_3d(kmax, kmax, kmax, uy_f, uy_r, FFTW_BACKWARD, FFTW_ESTIMATE);
128 plan_uz = fftw_plan_dft_3d(kmax, kmax, kmax, uz_f, uz_r, FFTW_BACKWARD, FFTW_ESTIMATE);
129
130 // And execute Fourier 3D
131 fftw_execute(plan_ux);
132 fftw_execute(plan_uy);
133 fftw_execute(plan_uz);
134
135 //for (int dir = 0; dir < 3; dir++)
136 {
137 //const IJK_Field_double& v = velocity[dir];
138 IJK_Field_double& vx = velocity[0];
139 IJK_Field_double& vy = velocity[1];
140 IJK_Field_double& vz = velocity[2];
141
142 const int ni = vx.ni();
143 const int nj = vx.nj();
144 const int nk = vx.nk();
145 if ((ni != nj) || (nj != nk))
147
148 for (int k = 0; k < nk; k++)
149 {
150 for (int j = 0; j < nj; j++)
151 {
152 for (int i = 0; i < ni; i++)
153 {
154 // On ne prend que la partie réelle [0]
155 vx(i,j,k) =ux_r[(i*kmax +j)*kmax+k][0]/sqrt(2);
156 vy(i,j,k) =uy_r[(i*kmax +j)*kmax+k][0]/sqrt(2);
157 vz(i,j,k) =uz_r[(i*kmax +j)*kmax+k][0]/sqrt(2);
158 }
159 }
160 }
161
162 // Vérification
163 // double *Ek_x = new double [kmax];
164 // double *Ek_y = new double [kmax];
165 // double *Ek_z = new double [kmax];
166 // double *nn = new double [kmax];
167 // std::fill_n(Ek_x, kmax, static_cast<double>(0));
168 // std::fill_n(Ek_y, kmax, static_cast<double>(0));
169 // std::fill_n(Ek_z, kmax, static_cast<double>(0));
170 // std::fill_n(nk, kmax, static_cast<double>(0));
171 // for (int i = 0; i < nk; i++)
172 // {
173 // for (int j = 0; j < nj; j++)
174 // {
175 // for (int k = 0; k < ni; k++)
176 // {
177 // // Test: calcul des spectres
178 // int k2 = (int)(i*i+j*j+k*k);
179 // int indice = (int)(sqrt(k2));
180 // if (indice <= kmax)
181 // {
182 // Ek_x[indice] += 4.*M_PI*k2*(pow(ux_f[((i*kmax)+j)*kmax+k][0], 2.)+pow(ux_f[((i*kmax)+j)*kmax+k][1], 2.));
183 // Ek_y[indice] += 4.*M_PI*k2*(pow(uy_f[((i*kmax)+j)*kmax+k][0], 2.)+pow(uy_f[((i*kmax)+j)*kmax+k][1], 2.));
184 // Ek_z[indice] += 4.*M_PI*k2*(pow(uz_f[((i*kmax)+j)*kmax+k][0], 2.)+pow(uz_f[((i*kmax)+j)*kmax+k][1], 2.));
185 // nn[indice] += 1;
186 // }
187 // }
188 // }
189 // }
190
191 // Écriture vérif (ne marche pas :( )
192 // std::string nomfic("spectres.dat");
193 // std::ofstream sortie(nomfic.c_str(), ios::out | ios::trunc);
194 // for (int i = 0; i < kmax; i++)
195 // {
196 // Ek_x[i] /= nn[i];
197 // Ek_y[i] /= nn[i];
198 // Ek_z[i] /= nn[i];
199 // sortie << i << ", " << Ek_x[i] << ", " << Ek_y[i] << ", " << Ek_z[i] << std::endl;
200 // }
201 // sortie.close();
202 }
203}
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
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
Classe de base des flux de sortie.
Definition Sortie.h:52