forked from fmihpc/vlasiator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatareductionoperator.cpp
1793 lines (1536 loc) · 80.3 KB
/
datareductionoperator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of Vlasiator.
* Copyright 2010-2016 Finnish Meteorological Institute
*
* For details of usage, see the COPYING file and read the "Rules of the Road"
* at http://vlasiator.fmi.fi/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <cstdlib>
#include <mpi.h>
#include <iostream>
#include <limits>
#include <array>
#include "datareductionoperator.h"
#include "../object_wrapper.h"
using namespace std;
typedef Parameters P;
namespace DRO {
// ************************************************************
// ***** DEFINITIONS FOR DATAREDUCTIONOPERATOR BASE CLASS *****
// ************************************************************
/** DataReductionOperator base class constructor. The constructor is empty.*/
DataReductionOperator::DataReductionOperator() { }
/** DataReductionOperator base class virtual destructor. The destructor is empty.*/
DataReductionOperator::~DataReductionOperator() { }
/** Get info on the data the DataReductionOperator writes on disk. A DataReductionOperator writes
* an array on disk. Each element of the array is a vector with n elements. Finally, each
* vector element has a byte size, as given by the sizeof function.
* @param dataType Basic datatype, must be int, uint, float
* @param dataSize Byte size of written datatype, for example double-precision floating points
* have byte size of sizeof(double).
* @param vectorSize How many elements are in the vector returned by the DataReductionOperator.
* @return If true, DataReductionOperator returned sensible values.
*/
bool DataReductionOperator::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
cerr << "ERROR: DataReductionOperator::getDataVectorInfo called insted of derived class function!" << endl;
return false;
}
/** Get the name of the reduced data variable. The name is written to the disk as-is
* and is used in visualization.
* @return The name of the data. The base class function returns an empty string.
*/
std::string DataReductionOperator::getName() const {
cerr << "ERROR: DataReductionOperator::getName called instead of derived class function!" << endl;
return string("");
}
/** Check if this DataReductionOperator wants to take care of writing the
* data to the output file instead of letting it be handled in iowrite.cpp,
* i.e., one should call the writeData function.
* @return If true, then this DRO wants to write the data to file.
* @see DataReductionOperator::writeData.*/
bool DataReductionOperator::handlesWriting() const {return false;}
// TODO update this documentation snippet.
/** Reduce the data and write the data vector to the given buffer.
* @param N_blocks Number of velocity blocks in array avgs.
* @param avgs Array containing distribution function values for each velocity block.
* @param blockParams Array containing the parameters of each velocity block.
* @param buffer Buffer in which the reduced data is written.
* @return If true, DataReductionOperator reduced data successfully.
*/
bool DataReductionOperator::reduceData(const SpatialCell* cell,char* buffer) {
cerr << "ERROR: DataReductionOperator::reduceData called instead of derived class function!" << endl;
return false;
}
// TODO update this documentation snippet.
/** Reduce the data and write the data vector to the given variable.
* @param N_blocks Number of velocity blocks in array avgs.
* @param avgs Array containing distribution function values for each velocity block.
* @param blockParams Array containing the parameters of each velocity block.
* @param result Real variable in which the reduced data is written.
* @return If true, DataReductionOperator reduced data successfully.
*/
bool DataReductionOperator::reduceData(const SpatialCell* cell,Real* result) {
cerr << "ERROR: DataReductionOperator::reduceData called instead of derived class function!" << endl;
return false;
}
/** Set the SpatialCell whose data is going to be reduced by subsequent calls to
* DRO::DataReductionOperator::reduceData. This function is provided so that
* variables stored per SpatialCell can be accessed.
*
* Spatial cell variables are stored in array SpatialCell::cpu_cellParams.
* The contents of array elements are stored in namespace CellParams. For example,
* cell.cpu_cellParams[%CellParams::EX] contains the electric field.
* @param cell The SpatialCell whose data is to be reduced next.
* @return If true, the SpatialCell was set correctly.
*/
bool DataReductionOperator::setSpatialCell(const SpatialCell* cell) {
cerr << "ERROR: DataReductionOperator::setSpatialCell called instead of derived class function!" << endl;
return false;
}
/** Request the DataReductionOperator to write all its data to the output file.
* This function should only be called if handlesWriting function returned true.
* @param mpiGrid Parallel grid.
* @param cells List of spatial cells (ordered).
* @param meshName Name of the spatial mesh.
* @param vlsvWriter Output file writer.
* @return If true, then output data was written successfully.*/
bool DataReductionOperator::writeData(const dccrg::Dccrg<spatial_cell::SpatialCell,dccrg::Cartesian_Geometry>& mpiGrid,
const std::vector<CellID>& cells,const std::string& meshName,
vlsv::Writer& vlsvWriter) {
cerr << "ERROR: DataReductionOperator::writeData called instead of derived class function!" << endl;
return false;
}
DataReductionOperatorCellParams::DataReductionOperatorCellParams(const std::string& name,const unsigned int parameterIndex,const unsigned int vectorSize):
DataReductionOperator() {
_vectorSize=vectorSize;
_name=name;
_parameterIndex=parameterIndex;
}
DataReductionOperatorCellParams::~DataReductionOperatorCellParams() { }
bool DataReductionOperatorCellParams::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = _vectorSize;
return true;
}
std::string DataReductionOperatorCellParams::getName() const {return _name;}
bool DataReductionOperatorCellParams::reduceData(const SpatialCell* cell,char* buffer) {
const char* ptr = reinterpret_cast<const char*>(_data);
for (uint i = 0; i < _vectorSize*sizeof(Real); ++i){
buffer[i] = ptr[i];
}
return true;
}
bool DataReductionOperatorCellParams::reduceData(const SpatialCell* cell,Real* buffer){
//If _vectorSize is >1 it still works, we just give the first value and no other ones..
*buffer=_data[0];
return true;
}
bool DataReductionOperatorCellParams::setSpatialCell(const SpatialCell* cell) {
if(std::isinf(cell->parameters[_parameterIndex]) || std::isnan(cell->parameters[_parameterIndex])) {
string message = "The DataReductionOperator " + this->getName() + " returned a nan or an inf.";
bailout(true, message, __FILE__, __LINE__);
}
_data = &(cell->parameters[_parameterIndex]);
return true;
}
DataReductionOperatorDerivatives::DataReductionOperatorDerivatives(const std::string& name,const unsigned int parameterIndex,const unsigned int vectorSize):
DataReductionOperatorCellParams(name,parameterIndex,vectorSize) {
}
//a version with derivatives, this is the only function that is different
bool DataReductionOperatorDerivatives::setSpatialCell(const SpatialCell* cell) {
_data = &(cell->derivatives[_parameterIndex]);
return true;
}
DataReductionOperatorBVOLDerivatives::DataReductionOperatorBVOLDerivatives(const std::string& name,const unsigned int parameterIndex,const unsigned int vectorSize):
DataReductionOperatorCellParams(name,parameterIndex,vectorSize) {
}
//a version with derivatives, this is the only function that is different
bool DataReductionOperatorBVOLDerivatives::setSpatialCell(const SpatialCell* cell) {
_data = &(cell->derivativesBVOL[_parameterIndex]);
return true;
}
//------------------ total BVOL ---------------------------------------
VariableBVol::VariableBVol(): DataReductionOperator() { }
VariableBVol::~VariableBVol() { }
bool VariableBVol::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 3;
return true;
}
std::string VariableBVol::getName() const {return "B_vol";}
bool VariableBVol::reduceData(const SpatialCell* cell,char* buffer) {
const char* ptr = reinterpret_cast<const char*>(B);
for (uint i = 0; i < 3*sizeof(Real); ++i) buffer[i] = ptr[i];
return true;
}
bool VariableBVol::setSpatialCell(const SpatialCell* cell) {
B[0] = cell->parameters[CellParams::PERBXVOL] + cell->parameters[CellParams::BGBXVOL];
B[1] = cell->parameters[CellParams::PERBYVOL] + cell->parameters[CellParams::BGBYVOL];
B[2] = cell->parameters[CellParams::PERBZVOL] + cell->parameters[CellParams::BGBZVOL];
if(std::isinf(B[0]) || std::isnan(B[0]) ||
std::isinf(B[1]) || std::isnan(B[1]) ||
std::isinf(B[2]) || std::isnan(B[2])
) {
string message = "The DataReductionOperator " + this->getName() + " returned a nan or an inf.";
bailout(true, message, __FILE__, __LINE__);
}
return true;
}
//------------------ total B ---------------------------------------
VariableB::VariableB(): DataReductionOperator() { }
VariableB::~VariableB() { }
bool VariableB::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 3;
return true;
}
std::string VariableB::getName() const {return "B";}
bool VariableB::reduceData(const SpatialCell* cell,char* buffer) {
const char* ptr = reinterpret_cast<const char*>(B);
for (uint i = 0; i < 3*sizeof(Real); ++i) buffer[i] = ptr[i];
return true;
}
bool VariableB::setSpatialCell(const SpatialCell* cell) {
B[0] = cell->parameters[CellParams::PERBX] + cell->parameters[CellParams::BGBX];
B[1] = cell->parameters[CellParams::PERBY] + cell->parameters[CellParams::BGBY];
B[2] = cell->parameters[CellParams::PERBZ] + cell->parameters[CellParams::BGBZ];
if(std::isinf(B[0]) || std::isnan(B[0]) ||
std::isinf(B[1]) || std::isnan(B[1]) ||
std::isinf(B[2]) || std::isnan(B[2])
) {
string message = "The DataReductionOperator " + this->getName() + " returned a nan or an inf.";
bailout(true, message, __FILE__, __LINE__);
}
return true;
}
//MPI rank
MPIrank::MPIrank(): DataReductionOperator() { }
MPIrank::~MPIrank() { }
bool MPIrank::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "int";
dataSize = 4;
vectorSize = 1;
return true;
}
std::string MPIrank::getName() const {return "MPI_rank";}
bool MPIrank::reduceData(const SpatialCell* cell,char* buffer) {
const char* ptr = reinterpret_cast<const char*>(&mpiRank);
for (uint i = 0; i < sizeof(int); ++i) buffer[i] = ptr[i];
return true;
}
bool MPIrank::setSpatialCell(const SpatialCell* cell) {
int intRank;
MPI_Comm_rank(MPI_COMM_WORLD,&intRank);
rank = 1.0*intRank;
mpiRank = intRank;
return true;
}
// BoundaryType
BoundaryType::BoundaryType(): DataReductionOperator() { }
BoundaryType::~BoundaryType() { }
bool BoundaryType::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "int";
dataSize = sizeof(int);
vectorSize = 1;
return true;
}
std::string BoundaryType::getName() const {return "Boundary_type";}
bool BoundaryType::reduceData(const SpatialCell* cell,char* buffer) {
const char* ptr = reinterpret_cast<const char*>(&boundaryType);
for (uint i = 0; i < sizeof(int); ++i) buffer[i] = ptr[i];
return true;
}
bool BoundaryType::setSpatialCell(const SpatialCell* cell) {
boundaryType = (int)cell->sysBoundaryFlag;
return true;
}
// BoundaryLayer
BoundaryLayer::BoundaryLayer(): DataReductionOperator() { }
BoundaryLayer::~BoundaryLayer() { }
bool BoundaryLayer::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "int";
dataSize = sizeof(int);
vectorSize = 1;
return true;
}
std::string BoundaryLayer::getName() const {return "Boundary_layer";}
bool BoundaryLayer::reduceData(const SpatialCell* cell,char* buffer) {
const char* ptr = reinterpret_cast<const char*>(&boundaryLayer);
for (uint i = 0; i < sizeof(int); ++i) buffer[i] = ptr[i];
return true;
}
bool BoundaryLayer::setSpatialCell(const SpatialCell* cell) {
boundaryLayer = (int)cell->sysBoundaryLayer;
return true;
}
BoundaryLayerNew::BoundaryLayerNew(): DataReductionOperator() { }
BoundaryLayerNew::~BoundaryLayerNew() { }
bool BoundaryLayerNew::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "int";
dataSize = sizeof(int);
vectorSize = 1;
return true;
}
std::string BoundaryLayerNew::getName() const {return "Boundary_layer_new";}
bool BoundaryLayerNew::reduceData(const SpatialCell* cell,char* buffer) {
const char* ptr = reinterpret_cast<const char*>(&boundaryLayer);
for (uint i = 0; i < sizeof(int); ++i) buffer[i] = ptr[i];
return true;
}
bool BoundaryLayerNew::setSpatialCell(const SpatialCell* cell) {
boundaryLayer = cell->sysBoundaryLayerNew;
return true;
}
// Blocks
Blocks::Blocks(): DataReductionOperator() { }
Blocks::~Blocks() { }
bool Blocks::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "uint";
dataSize = 4;
vectorSize = 1;
return true;
}
std::string Blocks::getName() const {return "Blocks";}
bool Blocks::reduceData(const SpatialCell* cell,char* buffer) {
const char* ptr = reinterpret_cast<const char*>(&nBlocks);
for (uint i = 0; i < sizeof(int); ++i) buffer[i] = ptr[i];
return true;
}
bool Blocks::reduceData(const SpatialCell* cell,Real* buffer) {
*buffer = 1.0 * nBlocks;
return true;
}
bool Blocks::setSpatialCell(const SpatialCell* cell) {
nBlocks = cell->get_number_of_all_velocity_blocks();
return true;
}
// Scalar pressure
VariablePressure::VariablePressure(): DataReductionOperator() { }
VariablePressure::~VariablePressure() { }
std::string VariablePressure::getName() const {return "Pressure";}
bool VariablePressure::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 1;
return true;
}
// Adding pressure calculations to Vlasiator.
// p = m/3 * integral((v - <V>)^2 * f(r,v) dV), doing the sum of the x, y and z components.
bool VariablePressure::reduceData(const SpatialCell* cell,char* buffer) {
const Real HALF = 0.5;
const Real THIRD = 1.0/3.0;
Real thread_nvx2_sum = 0.0;
Real thread_nvy2_sum = 0.0;
Real thread_nvz2_sum = 0.0;
# pragma omp parallel reduction(+:thread_nvx2_sum,thread_nvy2_sum,thread_nvz2_sum)
{
for (int popID=0; popID<getObjectWrapper().particleSpecies.size(); ++popID) {
// Get velocity block parameters and distribution function of active population
const Real* parameters = cell->get_block_parameters(popID);
const Realf* block_data = cell->get_data(popID);
Real pop_nvx2_sum = 0.0;
Real pop_nvy2_sum = 0.0;
Real pop_nvz2_sum = 0.0;
#pragma omp for
for (vmesh::LocalID n=0; n<cell->get_number_of_velocity_blocks(popID); n++) {
for (uint k = 0; k < WID; ++k)
for (uint j = 0; j < WID; ++j)
for (uint i = 0; i < WID; ++i) {
const Real VX
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::VXCRD]
+ (i + HALF)*parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVX];
const Real VY
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::VYCRD]
+ (j + HALF)*parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVY];
const Real VZ
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::VZCRD]
+ (k + HALF)*parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVZ];
const Real DV3
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVX]
* parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVY]
* parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVZ];
pop_nvx2_sum += block_data[n * SIZE_VELBLOCK+cellIndex(i,j,k)] * (VX - averageVX) * (VX - averageVX) * DV3;
pop_nvy2_sum += block_data[n * SIZE_VELBLOCK+cellIndex(i,j,k)] * (VY - averageVY) * (VY - averageVY) * DV3;
pop_nvz2_sum += block_data[n * SIZE_VELBLOCK+cellIndex(i,j,k)] * (VZ - averageVZ) * (VZ - averageVZ) * DV3;
}
}
const Real mass = getObjectWrapper().particleSpecies[popID].mass;
thread_nvx2_sum += mass*pop_nvx2_sum;
thread_nvy2_sum += mass*pop_nvy2_sum;
thread_nvz2_sum += mass*pop_nvz2_sum;
}
}
Pressure = THIRD*(thread_nvx2_sum + thread_nvy2_sum + thread_nvz2_sum);
const char* ptr = reinterpret_cast<const char*>(&Pressure);
for (uint i = 0; i < sizeof(Real); ++i) buffer[i] = ptr[i];
return true;
}
bool VariablePressure::reduceData(const SpatialCell* cell,Real* buffer) {
reduceData(cell,(char*)buffer);
return true;
}
bool VariablePressure::setSpatialCell(const SpatialCell* cell) {
if(cell-> parameters[CellParams::RHO] != 0.0) {
averageVX = cell-> parameters[CellParams::RHOVX] / cell-> parameters[CellParams::RHO];
averageVY = cell-> parameters[CellParams::RHOVY] / cell-> parameters[CellParams::RHO];
averageVZ = cell-> parameters[CellParams::RHOVZ] / cell-> parameters[CellParams::RHO];
} else {
averageVX = 0.0;
averageVY = 0.0;
averageVZ = 0.0;
}
Pressure = 0.0;
return true;
}
// Scalar pressure from the solvers
VariablePressureSolver::VariablePressureSolver(): DataReductionOperator() { }
VariablePressureSolver::~VariablePressureSolver() { }
std::string VariablePressureSolver::getName() const {return "Pressure";}
bool VariablePressureSolver::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 1;
return true;
}
bool VariablePressureSolver::reduceData(const SpatialCell* cell,char* buffer) {
const char* ptr = reinterpret_cast<const char*>(&Pressure);
for (uint i = 0; i < sizeof(Real); ++i) buffer[i] = ptr[i];
return true;
}
bool VariablePressureSolver::setSpatialCell(const SpatialCell* cell) {
Pressure = 1.0/3.0 * (cell->parameters[CellParams::P_11] + cell->parameters[CellParams::P_22] + cell->parameters[CellParams::P_33]);
return true;
}
// YK Adding pressure calculations to Vlasiator.
// p_ij = m/3 * integral((v - <V>)_i(v - <V>)_j * f(r,v) dV)
// Pressure tensor 6 components (11, 22, 33, 23, 13, 12) added by YK
// Split into VariablePTensorDiagonal (11, 22, 33)
// and VariablePTensorOffDiagonal (23, 13, 12)
VariablePTensorDiagonal::VariablePTensorDiagonal(): DataReductionOperator() { }
VariablePTensorDiagonal::~VariablePTensorDiagonal() { }
std::string VariablePTensorDiagonal::getName() const {return "PTensorDiagonal";}
bool VariablePTensorDiagonal::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 3;
return true;
}
bool VariablePTensorDiagonal::reduceData(const SpatialCell* cell,char* buffer) {
const Real HALF = 0.5;
# pragma omp parallel
{
Real thread_nvxvx_sum = 0.0;
Real thread_nvyvy_sum = 0.0;
Real thread_nvzvz_sum = 0.0;
for (int popID=0; popID<getObjectWrapper().particleSpecies.size(); ++popID) {
const Real* parameters = cell->get_block_parameters(popID);
const Realf* block_data = cell->get_data(popID);
Real pop_nvxvx_sum = 0;
Real pop_nvyvy_sum = 0;
Real pop_nvzvz_sum = 0;
# pragma omp for
for (vmesh::LocalID n=0; n<cell->get_number_of_velocity_blocks(popID); n++) {
for (uint k = 0; k < WID; ++k)
for (uint j = 0; j < WID; ++j)
for (uint i = 0; i < WID; ++i) {
const Real VX
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::VXCRD]
+ (i + HALF)*parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVX];
const Real VY
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::VYCRD]
+ (j + HALF)*parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVY];
const Real VZ
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::VZCRD]
+ (k + HALF)*parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVZ];
const Real DV3
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVX]
* parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVY]
* parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVZ];
pop_nvxvx_sum += block_data[n * SIZE_VELBLOCK+cellIndex(i,j,k)] * (VX - averageVX) * (VX - averageVX) * DV3;
pop_nvyvy_sum += block_data[n * SIZE_VELBLOCK+cellIndex(i,j,k)] * (VY - averageVY) * (VY - averageVY) * DV3;
pop_nvzvz_sum += block_data[n * SIZE_VELBLOCK+cellIndex(i,j,k)] * (VZ - averageVZ) * (VZ - averageVZ) * DV3;
}
}
thread_nvxvx_sum += pop_nvxvx_sum * getObjectWrapper().particleSpecies[popID].mass;
thread_nvyvy_sum += pop_nvyvy_sum * getObjectWrapper().particleSpecies[popID].mass;
thread_nvzvz_sum += pop_nvzvz_sum * getObjectWrapper().particleSpecies[popID].mass;
}
// Accumulate contributions coming from this velocity block to the
// spatial cell velocity moments. If multithreading / OpenMP is used,
// these updates need to be atomic:
# pragma omp critical
{
PTensor[0] += thread_nvxvx_sum;
PTensor[1] += thread_nvyvy_sum;
PTensor[2] += thread_nvzvz_sum;
}
}
const char* ptr = reinterpret_cast<const char*>(&PTensor);
for (uint i = 0; i < 3*sizeof(Real); ++i) buffer[i] = ptr[i];
return true;
}
bool VariablePTensorDiagonal::setSpatialCell(const SpatialCell* cell) {
if (cell-> parameters[CellParams::RHO] != 0.0) {
averageVX = cell-> parameters[CellParams::RHOVX] / cell-> parameters[CellParams::RHO];
averageVY = cell-> parameters[CellParams::RHOVY] / cell-> parameters[CellParams::RHO];
averageVZ = cell-> parameters[CellParams::RHOVZ] / cell-> parameters[CellParams::RHO];
} else {
averageVX = 0.0;
averageVY = 0.0;
averageVZ = 0.0;
}
for(int i = 0; i < 3; i++) PTensor[i] = 0.0;
return true;
}
VariablePTensorOffDiagonal::VariablePTensorOffDiagonal(): DataReductionOperator() { }
VariablePTensorOffDiagonal::~VariablePTensorOffDiagonal() { }
std::string VariablePTensorOffDiagonal::getName() const {return "PTensorOffDiagonal";}
bool VariablePTensorOffDiagonal::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 3;
return true;
}
bool VariablePTensorOffDiagonal::reduceData(const SpatialCell* cell,char* buffer) {
const Real HALF = 0.5;
# pragma omp parallel
{
Real thread_nvxvy_sum = 0.0;
Real thread_nvzvx_sum = 0.0;
Real thread_nvyvz_sum = 0.0;
for (int popID=0; popID<getObjectWrapper().particleSpecies.size(); ++popID) {
const Real* parameters = cell->get_block_parameters(popID);
const Realf* block_data = cell->get_data(popID);
Real pop_nvxvy_sum = 0.0;
Real pop_nvzvx_sum = 0.0;
Real pop_nvyvz_sum = 0.0;
# pragma omp for
for (vmesh::LocalID n=0; n<cell->get_number_of_velocity_blocks(popID); n++) {
for (uint k = 0; k < WID; ++k)
for (uint j = 0; j < WID; ++j)
for (uint i = 0; i < WID; ++i) {
const Real VX
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::VXCRD]
+ (i + HALF)*parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVX];
const Real VY
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::VYCRD]
+ (j + HALF)*parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVY];
const Real VZ
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::VZCRD]
+ (k + HALF)*parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVZ];
const Real DV3
= parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVX]
* parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVY]
* parameters[n * BlockParams::N_VELOCITY_BLOCK_PARAMS + BlockParams::DVZ];
pop_nvxvy_sum += block_data[n * SIZE_VELBLOCK+cellIndex(i,j,k)] * (VX - averageVX) * (VY - averageVY) * DV3;
pop_nvzvx_sum += block_data[n * SIZE_VELBLOCK+cellIndex(i,j,k)] * (VZ - averageVZ) * (VX - averageVX) * DV3;
pop_nvyvz_sum += block_data[n * SIZE_VELBLOCK+cellIndex(i,j,k)] * (VY - averageVY) * (VZ - averageVZ) * DV3;
}
}
thread_nvxvy_sum += pop_nvxvy_sum * getObjectWrapper().particleSpecies[popID].mass;
thread_nvzvx_sum += pop_nvzvx_sum * getObjectWrapper().particleSpecies[popID].mass;
thread_nvyvz_sum += pop_nvyvz_sum * getObjectWrapper().particleSpecies[popID].mass;
}
// Accumulate contributions coming from this velocity block to the
// spatial cell velocity moments. If multithreading / OpenMP is used,
// these updates need to be atomic:
# pragma omp critical
{
PTensor[0] += thread_nvyvz_sum;
PTensor[1] += thread_nvzvx_sum;
PTensor[2] += thread_nvxvy_sum;
}
}
const char* ptr = reinterpret_cast<const char*>(&PTensor);
for (uint i = 0; i < 3*sizeof(Real); ++i) buffer[i] = ptr[i];
return true;
}
bool VariablePTensorOffDiagonal::setSpatialCell(const SpatialCell* cell) {
if(cell-> parameters[CellParams::RHO] != 0.0) {
averageVX = cell-> parameters[CellParams::RHOVX] / cell-> parameters[CellParams::RHO];
averageVY = cell-> parameters[CellParams::RHOVY] / cell-> parameters[CellParams::RHO];
averageVZ = cell-> parameters[CellParams::RHOVZ] / cell-> parameters[CellParams::RHO];
} else {
averageVX = 0.0;
averageVY = 0.0;
averageVZ = 0.0;
}
for(int i = 0; i < 3; i++) PTensor[i] = 0.0;
return true;
}
// Integrated divergence of magnetic field
// Integral of div B over the simulation volume =
// Integral of flux of B on simulation volume surface
DiagnosticFluxB::DiagnosticFluxB(): DataReductionOperator() { }
DiagnosticFluxB::~DiagnosticFluxB() { }
std::string DiagnosticFluxB::getName() const {return "FluxB";}
bool DiagnosticFluxB::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 1;
return true;
}
bool DiagnosticFluxB::reduceData(const SpatialCell* cell,Real * result) {
creal x = cell->parameters[CellParams::XCRD];
creal dx = cell->parameters[CellParams::DX];
creal y = cell->parameters[CellParams::YCRD];
creal dy = cell->parameters[CellParams::DY];
creal z = cell->parameters[CellParams::ZCRD];
creal dz = cell->parameters[CellParams::DZ];
creal cx = x + 0.5 * dx;
creal cy = y + 0.5 * dy;
creal cz = z + 0.5 * dz;
Real value = 0.0;
if(cx > Parameters::xmax - 2.0 * dx && cx < Parameters::xmax - dx) {
value += cell->parameters[CellParams::PERBX];
} else if (cx < Parameters::xmin + 2.0 * dx && cx > Parameters::xmin + dx) {
value += -1.0*cell->parameters[CellParams::PERBX];
}
if(cy > Parameters::ymax - 2.0 * dy && cy < Parameters::ymax - dy) {
value += cell->parameters[CellParams::PERBY];
} else if (cy < Parameters::ymin + 2.0 * dy && cy > Parameters::ymin + dy) {
value += -1.0*cell->parameters[CellParams::PERBY];
}
if(cz > Parameters::zmax - 2.0 * dz && cz < Parameters::zmax - dz) {
value += cell->parameters[CellParams::PERBZ];
} else if (cz < Parameters::zmin + 2.0 * dz && cz > Parameters::zmin + dz) {
value += -1.0*cell->parameters[CellParams::PERBZ];
}
*result = value;
return true;
}
bool DiagnosticFluxB::setSpatialCell(const SpatialCell* cell) {return true;}
// YK Integrated divergence of electric field
// Integral of div E over the simulation volume =
// Integral of flux of E on simulation volume surface
DiagnosticFluxE::DiagnosticFluxE(): DataReductionOperator() { }
DiagnosticFluxE::~DiagnosticFluxE() { }
std::string DiagnosticFluxE::getName() const {return "FluxE";}
bool DiagnosticFluxE::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 1;
return true;
}
bool DiagnosticFluxE::reduceData(const SpatialCell* cell,Real * result) {
creal x = cell->parameters[CellParams::XCRD];
creal dx = cell->parameters[CellParams::DX];
creal y = cell->parameters[CellParams::YCRD];
creal dy = cell->parameters[CellParams::DY];
creal z = cell->parameters[CellParams::ZCRD];
creal dz = cell->parameters[CellParams::DZ];
creal cx = x + 0.5 * dx;
creal cy = y + 0.5 * dy;
creal cz = z + 0.5 * dz;
Real value = 0.0;
if(cx > Parameters::xmax - 2.0 * dx && cx < Parameters::xmax - dx) {
value += cell->parameters[CellParams::EX];
} else if (cx < Parameters::xmin + 2.0 * dx && cx > Parameters::xmin + dx) {
value += -1.0*cell->parameters[CellParams::EX];
}
if(cy > Parameters::ymax - 2.0 * dy && cy < Parameters::ymax - dy) {
value += cell->parameters[CellParams::EY];
} else if (cy < Parameters::ymin + 2.0 * dy && cy > Parameters::ymin + dy) {
value += -1.0*cell->parameters[CellParams::EY];
}
if(cz > Parameters::zmax - 2.0 * dz && cz < Parameters::zmax - dz) {
value += cell->parameters[CellParams::EZ];
} else if (cz < Parameters::zmin + 2.0 * dz && cz > Parameters::zmin + dz) {
value += -1.0*cell->parameters[CellParams::EZ];
}
*result = value;
return true;
}
bool DiagnosticFluxE::setSpatialCell(const SpatialCell* cell) {return true;}
// YK maximum value of the distribution function
MaxDistributionFunction::MaxDistributionFunction(): DataReductionOperator() { }
MaxDistributionFunction::~MaxDistributionFunction() { }
std::string MaxDistributionFunction::getName() const {return "MaximumDistributionFunctionValue";}
bool MaxDistributionFunction::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 1;
return true;
}
bool MaxDistributionFunction::reduceData(const SpatialCell* cell,Real* buffer) {
const Real HALF = 0.5;
#pragma omp parallel
{
Real threadMax = std::numeric_limits<Real>::min();
for (int popID=0; popID<getObjectWrapper().particleSpecies.size(); ++popID) {
const Realf* block_data = cell->get_data(popID);
#pragma omp for
for (vmesh::LocalID n=0; n<cell->get_number_of_velocity_blocks(popID); ++n) {
for (uint k = 0; k < WID; ++k)
for (uint j = 0; j < WID; ++j)
for (uint i = 0; i < WID; ++i) {
const int celli=k*WID*WID+j*WID+i;
threadMax = max((Real)(block_data[n * SIZE_VELBLOCK+celli]), threadMax);
}
}
}
#pragma omp critical
{
maxF = max(threadMax, maxF);
}
}
*buffer = maxF;
return true;
}
bool MaxDistributionFunction::reduceData(const SpatialCell* cell,char* buffer) {
Real dummy;
reduceData(cell,&dummy);
const char* ptr = reinterpret_cast<const char*>(&dummy);
for (uint i = 0; i < sizeof(Real); ++i) buffer[i] = ptr[i];
return true;
}
bool MaxDistributionFunction::setSpatialCell(const SpatialCell* cell) {
return true;
}
// YK minimum value of the distribution function
MinDistributionFunction::MinDistributionFunction(): DataReductionOperator() { }
MinDistributionFunction::~MinDistributionFunction() { }
std::string MinDistributionFunction::getName() const {return "MinimumDistributionFunctionValue";}
bool MinDistributionFunction::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
dataType = "float";
dataSize = sizeof(Real);
vectorSize = 1;
return true;
}
bool MinDistributionFunction::reduceData(const SpatialCell* cell,Real* buffer) {
const Real HALF = 0.5;
#pragma omp parallel
{
Real threadMin = std::numeric_limits<Real>::max();
for (int popID=0; popID<getObjectWrapper().particleSpecies.size(); ++popID) {
const Realf* block_data = cell->get_data(popID);
#pragma omp for
for (vmesh::LocalID n=0; n<cell->get_number_of_velocity_blocks(popID); ++n) {
for (uint k = 0; k < WID; ++k)
for (uint j = 0; j < WID; ++j)
for (uint i = 0; i < WID; ++i) {
const int celli=k*WID*WID+j*WID+i;
threadMin = min((Real)(block_data[n * SIZE_VELBLOCK+celli]), threadMin);
}
}
}
#pragma omp critical
{
minF = min(threadMin, minF);
}
}
*buffer = minF;
return true;
}
bool MinDistributionFunction::reduceData(const SpatialCell* cell,char* buffer) {
Real dummy;
reduceData(cell,&dummy);
const char* ptr = reinterpret_cast<const char*>(&dummy);
for (uint i = 0; i < sizeof(Real); ++i) buffer[i] = ptr[i];
return true;
}
bool MinDistributionFunction::setSpatialCell(const SpatialCell* cell) {
return true;
}
//Helper function for getting the velocity cell ids that are a part of the backstream population:
static void getBackstreamVelocityCells(const Real* block_parameters, vector<uint64_t> & vCellIds ) {
const Real HALF = 0.5;
// Go through every velocity cell (i, j, k are indices)
for (uint k = 0; k < WID; ++k) for (uint j = 0; j < WID; ++j) for (uint i = 0; i < WID; ++i) {
// Get the vx, vy, vz coordinates of the velocity cell
const Real VX = block_parameters[BlockParams::VXCRD] + (i + HALF) * block_parameters[BlockParams::DVX];
const Real VY = block_parameters[BlockParams::VYCRD] + (j + HALF) * block_parameters[BlockParams::DVY];
const Real VZ = block_parameters[BlockParams::VZCRD] + (k + HALF) * block_parameters[BlockParams::DVZ];
// Compare the distance of the velocity cell from the center of the maxwellian distribution to the radius of the maxwellian distribution
if( ( (P::backstreamvx - VX) * (P::backstreamvx - VX)
+ (P::backstreamvy - VY) * (P::backstreamvy - VY)
+ (P::backstreamvz - VZ) * (P::backstreamvz - VZ) )
>
P::backstreamradius*P::backstreamradius ) {
//The velocity cell is a part of the backstream population:
vCellIds.push_back(cellIndex(i,j,k));
}
}
}
//Helper function for getting the velocity cell ids that are a part of the backstream population:
static void getNonBackstreamVelocityCells(const Real* block_parameters, vector<uint64_t> & vCellIds ) {
const Real HALF = 0.5;
for (uint k = 0; k < WID; ++k) for (uint j = 0; j < WID; ++j) for (uint i = 0; i < WID; ++i) {
const Real VX = block_parameters[BlockParams::VXCRD] + (i + HALF) * block_parameters[BlockParams::DVX];
const Real VY = block_parameters[BlockParams::VYCRD] + (j + HALF) * block_parameters[BlockParams::DVY];
const Real VZ = block_parameters[BlockParams::VZCRD] + (k + HALF) * block_parameters[BlockParams::DVZ];
if( ( (P::backstreamvx - VX) * (P::backstreamvx - VX)
+ (P::backstreamvy - VY) * (P::backstreamvy - VY)
+ (P::backstreamvz - VZ) * (P::backstreamvz - VZ) )
<=
P::backstreamradius*P::backstreamradius ) {
//The velocity cell is a part of the backstream population:
vCellIds.push_back(cellIndex(i,j,k));
}
}
}
//Helper function for getting the velocity cell indices that are a part of the backstream population:
static void getBackstreamVelocityCellIndices(
const Real* block_parameters,
vector<array<uint, 3>> & vCellIndices
) {
const Real HALF = 0.5;
// Go through a block's every velocity cell
for (uint k = 0; k < WID; ++k) for (uint j = 0; j < WID; ++j) for (uint i = 0; i < WID; ++i) {
// Get the coordinates of the velocity cell (e.g. VX = block_vx_min_coordinates + (velocity_cell_indice_x+0.5)*length_of_velocity_cell_in_x_direction
const Real VX = block_parameters[BlockParams::VXCRD] + (i + HALF) * block_parameters[BlockParams::DVX];
const Real VY = block_parameters[BlockParams::VYCRD] + (j + HALF) * block_parameters[BlockParams::DVY];
const Real VZ = block_parameters[BlockParams::VZCRD] + (k + HALF) * block_parameters[BlockParams::DVZ];
// Calculate the distance of the velocity cell from the center of the maxwellian distribution and compare it to the approximate radius of the maxwellian distribution
if( ( (P::backstreamvx - VX) * (P::backstreamvx - VX)
+ (P::backstreamvy - VY) * (P::backstreamvy - VY)
+ (P::backstreamvz - VZ) * (P::backstreamvz - VZ) )
>
P::backstreamradius*P::backstreamradius ) {
//The velocity cell is a part of the backstream population because it is not within the radius:
const array<uint, 3> indices{{i, j, k}};
vCellIndices.push_back( indices );
}
}
}
//Helper function for getting the velocity cell indices that are not a part of the backstream population:
static void getNonBackstreamVelocityCellIndices(const Real* block_parameters,
vector<array<uint, 3>> & vCellIndices ) {
const Real HALF = 0.5;
// Go through a block's every velocity cell
for (uint k = 0; k < WID; ++k) for (uint j = 0; j < WID; ++j) for (uint i = 0; i < WID; ++i) {
// Get the coordinates of the velocity cell (e.g. VX = block_vx_min_coordinates + (velocity_cell_indice_x+0.5)*length_of_velocity_cell_in_x_direction
const Real VX = block_parameters[BlockParams::VXCRD] + (i + HALF) * block_parameters[BlockParams::DVX];
const Real VY = block_parameters[BlockParams::VYCRD] + (j + HALF) * block_parameters[BlockParams::DVY];
const Real VZ = block_parameters[BlockParams::VZCRD] + (k + HALF) * block_parameters[BlockParams::DVZ];
// Calculate the distance of the velocity cell from the center of the maxwellian distribution and compare it to the approximate radius of the maxwellian distribution
if( ( (P::backstreamvx - VX) * (P::backstreamvx - VX)
+ (P::backstreamvy - VY) * (P::backstreamvy - VY)
+ (P::backstreamvz - VZ) * (P::backstreamvz - VZ) )
<=
P::backstreamradius*P::backstreamradius ) {
//The velocity cell is a part of the backstream population because it is within the radius:
const array<uint, 3> indices{{i, j, k}};
vCellIndices.push_back( indices );
}
}
}
//Calculates rho backstream or rho non backstream
static void rhoBackstreamCalculation( const SpatialCell * cell, const bool calculateBackstream, Real & rho ) {
const Real HALF = 0.5;
# pragma omp parallel
{
Real thread_n_sum = 0.0;
for (int popID=0; popID<getObjectWrapper().particleSpecies.size(); ++popID) {
const Real* parameters = cell->get_block_parameters(popID);
const Realf* block_data = cell->get_data(popID);
# pragma omp for
for (vmesh::LocalID n=0; n<cell->get_number_of_velocity_blocks(popID); ++n) {