Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
map_test.cpp
Go to the documentation of this file.
1#include <stdio.h>
2#include "common.h"
3#include "vlasovsolver/vec.h"
4//#include "vlasovsolver/cpu_1d_ppm.hpp"
5//#include "vlasovsolver/cpu_1d_ppm_nonuniform.hpp"
7#include <random>
8#include <boost/random/mersenne_twister.hpp>
9#include <boost/random/uniform_real.hpp>
10#include <boost/random/variate_generator.hpp>
11#include <iostream>
12
13const int fluxlimiterscalingfactor=1.e-15;
14// Used for better calculation of flux limiters at extreme values.
15// In vlasiator, the value of spatial_cell->getVelocityBlockMinValue(popID)
16// is used here.
17
18/*print all values in the vector valued values array. In this array
19 there are blocks_per_dim blocks with a width of WID*/
20void print_values(int step, Vec *values, uint blocks_per_dim, Real v_min, Real dv){
21 char name[256];
22 sprintf(name,"dist_%03d.dat",step);
23
24 FILE* fp=fopen(name,"w");
25 for(uint i=0; i < blocks_per_dim * WID; i++){
26 Real v=v_min + (i + 0.5)*dv;
27 fprintf(fp,"%20.12g %20.12g %20.12g %20.12g %20.12g\n", v, values[i + WID][0], values[i + WID][1], values[i + WID][2], values[i + WID][3]);
28 }
29 fclose(fp);
30}
31
32void propagate(Vec dr[], Vec values[], Real z_translation, uint blocks_per_dim ) {
33
34 // Determine direction of translation
35 // part of density goes here (cell index change along spatial direcion)
36 const int target_scell_index = (z_translation > 0) ? 1: -1;
37
38 // Vector buffer where we write data, initialized to 0*/
39 Vec targetValues[(blocks_per_dim + 2) * WID];
40
41 for (uint k_block = 0; k_block < blocks_per_dim; k_block++){
42
43 for (uint k_cell=0; k_cell < WID; ++k_cell) {
44
45 uint gid = k_block * WID + k_cell + WID;
46 // init target_values
47 targetValues[gid] = 0.0;
48
49 }
50 }
51 for (uint k_block = 0; k_block < blocks_per_dim; k_block++){
52
53 for (uint k_cell=0; k_cell < WID; ++k_cell){
54
55 uint gid = k_block * WID + k_cell + WID;
56 //uint gid = (blocks_per_dim + 2) * WID - (k_block * WID + k_cell + WID);
57
58 // Calculate normalized coordinates in current cell.
59 // The coordinates (scaled units from 0 to 1) between which we will
60 // integrate to put mass in the target neighboring cell.
61 // Normalize the coordinates to the origin cell. Then we scale with the difference
62 // in volume between target and origin later when adding the integrated value.
63 Realf z_1,z_2;
64 if ( z_translation < 0 ) {
65 z_1 = 0;
66 z_2 = -z_translation / dr[gid][0];
67 } else {
68 z_1 = 1.0 - z_translation / dr[gid][0];
69 z_2 = 1.0;
70 }
71
72 if( abs(z_1) > 1.0 || abs(z_2) > 1.0 ) {
73 std::cout << "Error, CFL condition violated\n";
74 std::cout << "Exiting\n";
75 std::exit(1);
76 }
77
78 // Compute polynomial coefficients
79 Vec a[3];
80 //compute_ppm_coeff_nonuniform(dr, values, h4, gid + target_scell_index, a);
82
83 // Compute integral
84 const Vec ngbr_target_density =
85 z_2 * ( a[0] + z_2 * ( a[1] + z_2 * a[2] ) ) -
86 z_1 * ( a[0] + z_1 * ( a[1] + z_1 * a[2] ) );
87
88 // Store mapped density in two target cells
89 // in the neighbor cell we will put this density
90 targetValues[gid + target_scell_index] += ngbr_target_density * dr[gid] / dr[gid + target_scell_index];
91 // in the current original cells we will put the rest of the original density
92 targetValues[gid] += values[gid] - ngbr_target_density;
93 }
94 }
95
96 // Store target data into source data
97 for (uint k_block = 0; k_block<blocks_per_dim;k_block++){
98
99 for (uint k_cell=0; k_cell<WID; ++k_cell){
100
101 uint gid = k_block * WID + k_cell + WID;
102 //uint gid = (blocks_per_dim + 2) * WID - (k_block * WID + k_cell + WID);
103 values[gid] = targetValues[gid];
104
105 }
106
107 }
108
109}
110
111void print_reconstruction(int step, Vec dr[], Vec values[], uint blocks_per_dim, Real r_min){
112 char name[256];
113 sprintf(name,"reconstructions_%05d.dat",step);
114 FILE* fp=fopen(name,"w");
115
116 Vec r0 = r_min;
117 const int subcells = 50;
118 /*loop through all blocks in column and divide into subcells. Print value of reconstruction*/
119 for (unsigned int k_block = 0; k_block<blocks_per_dim;k_block++){
120 for (uint k_cell=0; k_cell<WID; ++k_cell){
121#ifdef ACC_SEMILAG_PPM
122 Vec a[3];
123 //compute_ppm_coeff( values, h4, (k_block + 1) * WID + k_cell, a);
124 compute_ppm_coeff_nonuniform(dr, values, h4, (k_block + 1) * WID + k_cell, a, fluxlimiterscalingfactor);
125#endif
126
127 int iend = k_block * WID + k_cell;
128 if (iend > 0)
129 r0 += dr[iend-1+WID];
130
131
132 for (uint k_subcell=0; k_subcell< subcells; ++k_subcell){
133 Vec r_norm = (Real)(k_subcell + 0.5)/subcells; //normalized r of subcell in source cell
134 Vec r = r0 + r_norm * dr[k_block * WID + k_cell + WID];
135
136#ifdef ACC_SEMILAG_PPM
137 Vec target =
138 a[0] +
139 2.0 * r_norm * a[1] +
140 3.0 * r_norm * r_norm * a[2];
141#endif
142
143 fprintf(fp,"%20.12g %20.12e %20.12e\n", r[0], values[k_block * WID + k_cell + WID][0], target[0]);
144 }
145 //fprintf(fp,"\n"); //empty line to deay wgments in gnuplot
146 }
147 }
148
149 fclose(fp);
150}
151
152void refine(Vec dr[], int ir, int max_refinement, int cells_per_level) {
153
154 for (uint k=0; k < max_refinement * cells_per_level; ++k) {
155 dr[ir + k] = dr[ir + k]/pow(2,(max_refinement - k / cells_per_level));
156 if (k > 0)
157 dr[ir - k] = dr[ir - k]/pow(2,(max_refinement - k / cells_per_level));
158 }
159
160}
161
162int main(void) {
163
164 const Real dr0 = 20000;
165 const int blocks_per_dim = 100;
166 const int i_block = 0; //x index of block, fixed in this simple test
167 const int j_block = 0; //y index of block, fixed in this simple test
168 const int j_cell = 0; // y index of cell within block (0..WID-1)
169
170 Vec dr[(blocks_per_dim+2)*WID];
171 Vec values[(blocks_per_dim+2)*WID];
172
173 boost::mt19937 rng;
174 boost::uniform_real<Real> u(0.0, 2.0 * dr0);
175 boost::variate_generator<boost::mt19937&, boost::uniform_real<Real> > gen(rng, u);
176 gen.distribution().reset();
177 gen.engine().seed(12345);
178
179 /*initial values*/
180 /*clear target & values array*/
181 for (uint k=0; k<WID* (blocks_per_dim + 2); ++k){
182 values[k] = 0.0;
183 dr[k] = dr0;
184 //dr[k] = gen();
185 }
186
187 int ir = (blocks_per_dim + 2) * WID / 2;
188 int ir2 = (blocks_per_dim + 2) * WID / 3;
189 int max_refinement = 5;
190 int cells_per_level = 2;
191
192 refine(dr,ir,max_refinement,cells_per_level);
193 // refine(dr,ir2,max_refinement,cells_per_level);
194
195 Real r_min = 0.0;
196 for (uint k=WID;k < (blocks_per_dim + 2) * WID / 2; ++k) {
197 r_min -= dr[k][0];
198 }
199
200 Real T = 500000;
201 Real rho = 1.0e18;
202 Real r = r_min;
203 Real r1 = -10.0 * dr0;
204
205 for(uint i=0; i < blocks_per_dim * WID; i++){
206
207 // Evaluate the function at the middle of the cell
208 r = r + 0.5 * dr[i + WID][0];
209 values[i + WID] = rho * pow(physicalconstants::MASS_PROTON / (2.0 * M_PI * physicalconstants::K_B * T), 1.5) *
210 exp(- physicalconstants::MASS_PROTON * (r - r1) * (r - r1) / (2.0 * physicalconstants::K_B * T));
211
212 // if (r < 0.0 && r_min - 10.0 * r < 0.0) {
213 // values[i + WID] = abs(r_min - 10.0 * r);
214 // } else {
215 // values[i + WID] = 0.0;
216 // }
217
218 // Move r to the end of the cell for the next iteration
219 r = r + 0.5 * dr[i + WID][0];
220 }
221
222 print_reconstruction(0, dr, values, blocks_per_dim, r_min);
223
224 uint nstep = 1000;
225 Real step = 500.0;
226
227 for (uint istep=0; istep < nstep; ++istep) {
228 propagate(dr, values, step, blocks_per_dim);
229 if ((istep+1) % 10 == 0)
230 print_reconstruction(istep+1, dr, values, blocks_per_dim, r_min);
231 }
232
233}
fclose(file)
for i
Definition Dispersion.m:24
const int fluxlimiterscalingfactor
Definition map_test.cpp:8
void propagate(Vec dr[], Vec values[], Real z_translation, uint blocks_per_dim)
Definition map_test.cpp:32
void print_values(int step, Vec *values, uint blocks_per_dim, Real v_min, Real dv)
Definition map_test.cpp:20
void print_reconstruction(int step, Vec dr[], Vec values[], uint blocks_per_dim, Real r_min)
Definition map_test.cpp:111
int main(void)
Definition map_test.cpp:162
void refine(Vec dr[], int ir, int max_refinement, int cells_per_level)
Definition map_test.cpp:152
#define WID
Definition common.h:514
void compute_ppm_coeff_nonuniform(const Realf *const dv, const Vec *const values, const face_estimate_order order, const uint k, Vec a[3], const Realf threshold)
float Real
Definition definitions.h:41
float Realf
Definition definitions.h:33
__global__ void vmesh::VelocityMesh **__restrict__ ColumnOffsets split::SplitVector< vmesh::GlobalID > Hashinator::Hashmap< vmesh::GlobalID, vmesh::LocalID > const uint *__restrict__ const Realf const int const int const Realf v_min
__global__ void vmesh::VelocityMesh **__restrict__ ColumnOffsets split::SplitVector< vmesh::GlobalID > Hashinator::Hashmap< vmesh::GlobalID, vmesh::LocalID > const uint *__restrict__ const Realf const int const int const Realf const Realf dv
const int k
const Real K_B
Definition common.h:571
const Real MASS_PROTON
Definition common.h:574
An interface to a type with floating point values.
static ARCH_HOSTDEV VecSimple< T > abs(const VecSimple< T > &l)