Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
Fluctuations.cpp
Go to the documentation of this file.
1/*
2 * This file is part of Vlasiator.
3 * Copyright 2010-2016 Finnish Meteorological Institute
4 *
5 * For details of usage, see the COPYING file and read the "Rules of the Road"
6 * at http://www.physics.helsinki.fi/vlasiator/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include <cstdlib>
24#include <iostream>
25#include <iomanip>
26#include <cmath>
27
28#include "../../common.h"
34
35#include "Fluctuations.h"
36
37using namespace spatial_cell;
38
40
41
42namespace projects {
46
48 typedef Readparameters RP;
49 RP::add("Fluctuations.BX0", "Background field value (T)", 1.0e-9);
50 RP::add("Fluctuations.BY0", "Background field value (T)", 2.0e-9);
51 RP::add("Fluctuations.BZ0", "Background field value (T)", 3.0e-9);
52 RP::add("Fluctuations.magXPertAbsAmp", "Amplitude of the magnetic perturbation along x", 1.0e-9);
53 RP::add("Fluctuations.magYPertAbsAmp", "Amplitude of the magnetic perturbation along y", 1.0e-9);
54 RP::add("Fluctuations.magZPertAbsAmp", "Amplitude of the magnetic perturbation along z", 1.0e-9);
55
56 // Per-population parameters
57 for(uint i=0; i< getObjectWrapper().particleSpecies.size(); i++) {
58 const std::string& pop = getObjectWrapper().particleSpecies[i].name;
59
60 RP::add(pop + "_Fluctuations.rho", "Number density (m^-3)", 1.0e7);
61 RP::add(pop + "_Fluctuations.TemperatureX", "Temperature (K)", 2.0e6);
62 RP::add(pop + "_Fluctuations.TemperatureY", "Temperature (K)", 2.0e6);
63 RP::add(pop + "_Fluctuations.TemperatureZ", "Temperature (K)", 2.0e6);
64 RP::add(pop + "_Fluctuations.densityPertRelAmp", "Amplitude factor of the density perturbation", 0.1);
65 RP::add(pop + "_Fluctuations.velocityPertAbsAmp", "Amplitude of the velocity perturbation", 1.0e6);
66 RP::add(pop + "_Fluctuations.maxwCutoff", "Cutoff for the maxwellian distribution", 1e-12);
67 }
68 }
69
72 typedef Readparameters RP;
74 RP::get("Fluctuations.BX0", this->BX0);
75 RP::get("Fluctuations.BY0", this->BY0);
76 RP::get("Fluctuations.BZ0", this->BZ0);
77 RP::get("Fluctuations.magXPertAbsAmp", this->magXPertAbsAmp);
78 RP::get("Fluctuations.magYPertAbsAmp", this->magYPertAbsAmp);
79 RP::get("Fluctuations.magZPertAbsAmp", this->magZPertAbsAmp);
80
81 // Per-population parameters
82 for(uint i=0; i< getObjectWrapper().particleSpecies.size(); i++) {
83 const std::string& pop = getObjectWrapper().particleSpecies[i].name;
85 RP::get(pop + "_Fluctuations.rho", sP.DENSITY);
86 RP::get(pop + "_Fluctuations.TemperatureX", sP.TEMPERATUREX);
87 RP::get(pop + "_Fluctuations.TemperatureY", sP.TEMPERATUREY);
88 RP::get(pop + "_Fluctuations.TemperatureZ", sP.TEMPERATUREZ);
89 RP::get(pop + "_Fluctuations.densityPertRelAmp", sP.densityPertRelAmp);
90 RP::get(pop + "_Fluctuations.velocityPertAbsAmp", sP.velocityPertAbsAmp);
91 RP::get(pop + "_Fluctuations.maxwCutoff", sP.maxwCutoff);
92
93 speciesParams.push_back(sP);
94 }
95 }
96
98 const uint popID,
99 const uint nRequested
100 ) const {
102 // Fetch spatial cell center coordinates
103 // const Real x = cell->parameters[CellParams::XCRD] + 0.5*cell->parameters[CellParams::DX];
104 // const Real y = cell->parameters[CellParams::YCRD] + 0.5*cell->parameters[CellParams::DY];
105 // const Real z = cell->parameters[CellParams::ZCRD] + 0.5*cell->parameters[CellParams::DZ];
106
107 const Real mass = getObjectWrapper().particleSpecies[popID].mass;
108 Real initRho = sP.DENSITY * (1.0 + sP.densityPertRelAmp * (0.5 - rndRho));
109 Real initTx = sP.TEMPERATUREX;
110 Real initTy = sP.TEMPERATUREY;
111 Real initTz = sP.TEMPERATUREZ;
112 const Real initV0X = sP.velocityPertAbsAmp * (0.5 - rndVel[0] );
113 const Real initV0Y = sP.velocityPertAbsAmp * (0.5 - rndVel[1] );
114 const Real initV0Z = sP.velocityPertAbsAmp * (0.5 - rndVel[2] );
115
116 #ifdef USE_GPU
119 #else
122 #endif
123 // Loop over blocks
124 Realf rhosum = 0;
126 {WID, WID, WID, nRequested},
127 ARCH_LOOP_LAMBDA (const uint i, const uint j, const uint k, const uint initIndex, Realf *lsum ) {
128 vmesh::GlobalID *GIDlist = vmesh->getGrid()->data();
129 Realf* bufferData = VBC->getData();
130 const vmesh::GlobalID blockGID = GIDlist[initIndex];
131 // Calculate parameters for new block
132 Real blockCoords[6];
133 vmesh->getBlockInfo(blockGID,&blockCoords[0]);
134 creal vxBlock = blockCoords[0];
135 creal vyBlock = blockCoords[1];
136 creal vzBlock = blockCoords[2];
137 creal dvxCell = blockCoords[3];
138 creal dvyCell = blockCoords[4];
139 creal dvzCell = blockCoords[5];
140 ARCH_INNER_BODY(i, j, k, initIndex, lsum) {
141 creal vx = vxBlock + (i+0.5)*dvxCell - initV0X;
142 creal vy = vyBlock + (j+0.5)*dvyCell - initV0Y;
143 creal vz = vzBlock + (k+0.5)*dvzCell - initV0Z;
144 const Realf value = TriMaxwellianPhaseSpaceDensity(vx,vy,vz,initTx,initTy,initTz,initRho,mass);
145 bufferData[initIndex*WID3 + k*WID2 + j*WID + i] = value;
146 //lsum[0] += value;
147 };
148 }, rhosum);
149 return rhosum;
150 }
151
152 /* Evaluates local SpatialCell properties for the project and population,
153 then evaluates the phase-space density at the given coordinates.
154 Used as a probe for projectTriAxisSearch.
155 */
157 const uint popID,
158 Real vx_in, Real vy_in, Real vz_in
159 ) const {
161 // Fetch spatial cell center coordinates
162 // const Real x = cell->parameters[CellParams::XCRD] + 0.5*cell->parameters[CellParams::DX];
163 // const Real y = cell->parameters[CellParams::YCRD] + 0.5*cell->parameters[CellParams::DY];
164 // const Real z = cell->parameters[CellParams::ZCRD] + 0.5*cell->parameters[CellParams::DZ];
165
166 const Real mass = getObjectWrapper().particleSpecies[popID].mass;
167 Real initRho = sP.DENSITY * (1.0 + sP.densityPertRelAmp * (0.5 - rndRho));
168 Real initTx = sP.TEMPERATUREX;
169 Real initTy = sP.TEMPERATUREY;
170 Real initTz = sP.TEMPERATUREZ;
171 const Real initV0X = sP.velocityPertAbsAmp * (0.5 - rndVel[0] );
172 const Real initV0Y = sP.velocityPertAbsAmp * (0.5 - rndVel[1] );
173 const Real initV0Z = sP.velocityPertAbsAmp * (0.5 - rndVel[2] );
174 creal vx = vx_in - initV0X;
175 creal vy = vy_in - initV0Y;
176 creal vz = vz_in - initV0Z;
177 const Realf value = TriMaxwellianPhaseSpaceDensity(vx,vy,vz,initTx,initTy,initTz,initRho,mass);
178 return value;
179 }
180
182
183 std::default_random_engine rndState;
184 setRandomCellSeed(cell,rndState);
185
186 this->rndRho=getRandomNumber(rndState);
187
188 this->rndVel[0]=getRandomNumber(rndState);
189 this->rndVel[1]=getRandomNumber(rndState);
190 this->rndVel[2]=getRandomNumber(rndState);
191 }
192
197 ) {
198 ConstantField bgField;
199 bgField.initialize(this->BX0,
200 this->BY0,
201 this->BZ0);
202
203 setBackgroundField(bgField, bgb, technical, fsgrid);
204
205 if(!P::isRestart) {
206 // local copies for lambda capture
207 const auto magXPertAbsAmp_l = this->magXPertAbsAmp;
208 const auto magYPertAbsAmp_l = this->magYPertAbsAmp;
209 const auto magZPertAbsAmp_l = this->magZPertAbsAmp;
210 const auto seed = this->seed;
211
212 fsgrid.parallel_for([](int timerId) -> phiprof::Timer { return phiprof::Timer{timerId}; },
213 phiprof::initializeTimer("setProjectBField"), technical,
214 [=](const fsgrid::Coordinates &coordinates, const fsgrid::FsStencil& stencil, cuint sysBoundaryFlag, cuint sysBoundaryLayer) {
215 auto& cell = perb[stencil.ooo()];
216
217 const auto seedmodifier = coordinates.globalIDFromLocalCoordinates(stencil.i, stencil.j, stencil.k);
218 std::default_random_engine rndState_l;
219 rndState_l.seed(seed+seedmodifier);
220 Real rndBuffer[3];
221 rndBuffer[0] = std::uniform_real_distribution<>(-0.5,0.5)(rndState_l);
222 rndBuffer[1] = std::uniform_real_distribution<>(-0.5,0.5)(rndState_l);
223 rndBuffer[2] = std::uniform_real_distribution<>(-0.5,0.5)(rndState_l);
224
225 cell[fsgrids::bfield::PERBX] = magXPertAbsAmp_l * rndBuffer[0];
226 cell[fsgrids::bfield::PERBY] = magYPertAbsAmp_l * rndBuffer[1];
227 cell[fsgrids::bfield::PERBZ] = magZPertAbsAmp_l * rndBuffer[2];
228 });
229 }
230 }
231
232 std::vector<std::array<Real, 3> > Fluctuations::getV0(
233 creal x,
234 creal y,
235 creal z,
236 const uint popID
237 ) const {
238 std::array<Real, 3> V0 {{0.0, 0.0, 0.0}};
239 std::vector<std::array<Real, 3> > centerPoints;
240 centerPoints.push_back(V0);
241 return centerPoints;
242 }
243
244} // namespace projects
for i
Definition Dispersion.m:24
#define ARCH_INNER_BODY(...)
#define ARCH_LOOP_LAMBDA
void setBackgroundField(const FieldFunction &bgFunction, fsgrids::bgbspan bgb, fsgrids::technicalspan technical, FieldSolverGrid &fsgrid, bool append)
void initialize(const double Bx, const double By, const double Bz)
virtual std::vector< std::array< Real, 3 > > getV0(creal x, creal y, creal z, const uint popID) const override
Return a vector containing the velocity coordinate of the centre of each ion population in the distri...
virtual bool initialize(void) override
virtual void calcCellParameters(spatial_cell::SpatialCell *cell, creal &t) override
static Real rndVel[3]
virtual void getParameters(void) override
std::vector< FluctuationsSpeciesParameters > speciesParams
virtual Realf probePhaseSpace(spatial_cell::SpatialCell *cell, const uint popID, Real vx_in, Real vy_in, Real vz_in) const override
virtual Realf fillPhaseSpace(spatial_cell::SpatialCell *cell, const uint popID, const uint nRequested) const override
static void addParameters(void)
virtual void setProjectBField(fsgrids::perbspan perb, fsgrids::bgbspan bgb, fsgrids::technicalspan technical, FieldSolverGrid &fsgrid) override
Real getRandomNumber(std::default_random_engine &randGen) const
Definition project.cpp:317
virtual bool initialize()
Definition project.cpp:113
virtual void getParameters()
Definition project.cpp:103
void setRandomCellSeed(spatial_cell::SpatialCell *cell, std::default_random_engine &randGen) const
Definition project.cpp:336
vmesh::VelocityMesh * get_velocity_mesh(const size_t &popID)
vmesh::VelocityBlockContainer * get_velocity_blocks(const size_t &popID)
vmesh::VelocityBlockContainer * dev_get_velocity_blocks(const size_t &popID)
vmesh::VelocityMesh * dev_get_velocity_mesh(const size_t &popID)
#define WID
Definition common.h:514
const int WID3
Definition common.h:517
const int WID2
Definition common.h:516
const uint32_t cuint
Definition definitions.h:50
float Real
Definition definitions.h:41
float Realf
Definition definitions.h:33
fsgrid::FsGrid< FS_STENCIL_WIDTH > FieldSolverGrid
Definition definitions.h:78
const float creal
Definition definitions.h:42
const int j
const int k
ObjectWrapper & getObjectWrapper()
Definition main.cpp:33
static void parallel_reduce(const uint(&limits)[NDim], Lambda loop_body, T &sum)
std::span< std::array< Real, fsgrids::bfield::N_BFIELD > > perbspan
Definition common.h:434
std::span< technical > technicalspan
Definition common.h:452
std::span< std::array< Real, bgbfield::N_BGB > > bgbspan
Definition common.h:444
@ PERBY
Definition common.h:276
@ PERBZ
Definition common.h:277
@ PERBX
Definition common.h:275
ARCH_HOSTDEV Realf TriMaxwellianPhaseSpaceDensity(creal &vx, creal &vy, creal &vz, creal &Tx, creal &Ty, creal &Tz, creal &rho, creal &mass)
Definition project.h:66
uint32_t GlobalID
Definition definitions.h:59
std::vector< species::Species > particleSpecies
static bool isRestart
Definition parameters.h:176