forked from fmihpc/vlasiator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioread.cpp
1350 lines (1177 loc) · 60.9 KB
/
ioread.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://www.physics.helsinki.fi/vlasiator/
*
* 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 <iostream>
#include <iomanip> // for setprecision()
#include <cmath>
#include <vector>
#include <sstream>
#include <ctime>
#include <array>
#include <sys/types.h>
#include <sys/stat.h>
#include "ioread.h"
#include "phiprof.hpp"
#include "parameters.h"
#include "logger.h"
#include "vlsv_reader_parallel.h"
#include "vlasovmover.h"
#include "object_wrapper.h"
using namespace std;
using namespace phiprof;
extern Logger logFile, diagnostic;
typedef Parameters P;
/*!
* \brief Checks for command files written to the local directory.
* If a file STOP was written and is readable, then a bailout with restart writing is initiated.
* If a file KILL was written and is readable, then a bailout without a restart is initiated.
* If a file SAVE was written and is readable, then restart writing without a bailout is initiated.
* If a file DOLB was written and is readable, then a new load balancing is initiated.
* To avoid bailing out upfront on a new run the files are renamed with the date to keep a trace.
* The function should only be called by MASTER_RANK. This ensures that resetting P::bailout_write_restart works.
*/
void checkExternalCommands() {
struct stat tempStat;
if (stat("STOP", &tempStat) == 0) {
bailout(true, "Received an external STOP command. Setting bailout.write_restart to true.");
P::bailout_write_restart = true;
char newName[80];
// Get the current time.
const time_t rawTime = time(NULL);
const struct tm * timeInfo = localtime(&rawTime);
strftime(newName, 80, "STOP_%F_%H-%M-%S", timeInfo);
rename("STOP", newName);
return;
}
if(stat("KILL", &tempStat) == 0) {
bailout(true, "Received an external KILL command. Setting bailout.write_restart to false.");
P::bailout_write_restart = false;
char newName[80];
// Get the current time.
const time_t rawTime = time(NULL);
const struct tm * timeInfo = localtime(&rawTime);
strftime(newName, 80, "KILL_%F_%H-%M-%S", timeInfo);
rename("KILL", newName);
return;
}
if(stat("SAVE", &tempStat) == 0) {
cerr << "Received an external SAVE command. Writing a restart file." << endl;
globalflags::writeRestart = true;
char newName[80];
// Get the current time.
const time_t rawTime = time(NULL);
const struct tm * timeInfo = localtime(&rawTime);
strftime(newName, 80, "SAVE_%F_%H-%M-%S", timeInfo);
rename("SAVE", newName);
return;
}
if(stat("DOLB", &tempStat) == 0) {
cerr << "Received an external DOLB command. Balancing load." << endl;
globalflags::balanceLoad = true;
char newName[80];
// Get the current time.
const time_t rawTime = time(NULL);
const struct tm * timeInfo = localtime(&rawTime);
strftime(newName, 80, "DOLB_%F_%H-%M-%S", timeInfo);
rename("DOLB", newName);
return;
}
}
/*!
\brief Collective exit on error functions
If any process(es) have a false success values then the program will
abort and write out the message to the logfile
\param success This process' value for successful execution
\param message Error message to be shown upon failure
\param comm MPI comm
\return Returns true if the operation was successful
*/
bool exitOnError(bool success,string message,MPI_Comm comm) {
int successInt;
int globalSuccessInt;
if(success)
successInt=1;
else
successInt=0;
MPI_Allreduce(&successInt,&globalSuccessInt,1,MPI_INT,MPI_MIN,comm);
if(globalSuccessInt==1) {
return true;
}
else{
logFile << message << endl<<write ;
exit(1);
}
}
/*!
\brief Read cell ID's
Read in cell ID's from file. Note: Uses the newer version of vlsv parallel reader
\param file Some vlsv reader with a file open
\param fileCells Vector in whic to store the cell ids
\param masterRank The simulation's master rank id (Vlasiator uses 0, which should be the default)
\param comm MPI comm (MPI_COMM_WORLD should be the default)
*/
bool readCellIds(vlsv::ParallelReader & file,
vector<CellID>& fileCells, const int masterRank,MPI_Comm comm){
// Get info on array containing cell Ids:
uint64_t arraySize = 0;
uint64_t vectorSize;
vlsv::datatype::type dataType;
uint64_t byteSize;
list<pair<string,string> > attribs;
bool success=true;
int rank;
MPI_Comm_rank(comm,&rank);
if (rank==masterRank) {
const short int readFromFirstIndex = 0;
//let's let master read cellId's, we anyway have at max ~1e6 cells
attribs.push_back(make_pair("name","CellID"));
attribs.push_back(make_pair("mesh","SpatialGrid"));
if (file.getArrayInfoMaster("VARIABLE",attribs,arraySize,vectorSize,dataType,byteSize) == false) {
logFile << "(RESTART) ERROR: Failed to read cell ID array info!" << endl << write;
return false;
}
//Make a routine error check:
if( vectorSize != 1 ) {
logFile << "(RESTART) ERROR: Bad vectorsize at " << __FILE__ << " " << __LINE__ << endl << write;
return false;
}
// Read cell Ids:
char* IDbuffer = new char[arraySize*vectorSize*byteSize];
if (file.readArrayMaster("VARIABLE",attribs,readFromFirstIndex,arraySize,IDbuffer) == false) {
logFile << "(RESTART) ERROR: Failed to read cell Ids!" << endl << write;
success = false;
}
// Convert global Ids into our local DCCRG 64 bit uints
const uint64_t& numberOfCells = arraySize;
fileCells.resize(numberOfCells);
if (dataType == vlsv::datatype::type::UINT && byteSize == 4) {
uint32_t* ptr = reinterpret_cast<uint32_t*>(IDbuffer);
//Input cell ids
for (uint64_t i=0; i<numberOfCells; ++i) {
const CellID cellID = ptr[i];
fileCells[i] = cellID;
}
} else if (dataType == vlsv::datatype::type::UINT && byteSize == 8) {
uint64_t* ptr = reinterpret_cast<uint64_t*>(IDbuffer);
for (uint64_t i=0; i<numberOfCells; ++i) {
const CellID cellID = ptr[i];
fileCells[i] = cellID;
}
} else {
logFile << "(RESTART) ERROR: ParallelReader returned an unsupported datatype for cell Ids!" << endl << write;
success = false;
}
delete[] IDbuffer;
}
//broadcast cellId's to everybody
MPI_Bcast(&arraySize,1,MPI_UINT64_T,masterRank,comm);
fileCells.resize(arraySize);
MPI_Bcast(&(fileCells[0]),arraySize,MPI_UINT64_T,masterRank,comm);
return success;
}
/* Read the total number of velocity blocks per spatial cell in the spatial mesh.
* The returned value for each cell is a sum of the velocity blocks associated in each particle species.
* The value is used to calculate an initial load balance after restart.
* @param file Some vlsv reader with a file open (can be old or new vlsv reader)
* @param nBlocks Vector for holding information on cells and the number of blocks in them -- this function saves data here
* @param masterRank The master rank of this process (Vlasiator uses masterRank = 0 and so it should be the default)
* @param comm MPI comm
* @return Returns true if the operation was successful
@ @see exec_readGrid
*/
bool readNBlocks(vlsv::ParallelReader& file,const std::string& meshName,
std::vector<size_t>& nBlocks,int masterRank,MPI_Comm comm) {
bool success = true;
// Get info on array containing cell IDs:
uint64_t arraySize;
uint64_t vectorSize;
vlsv::datatype::type dataType;
uint64_t byteSize;
// Read mesh bounding box to all processes, the info in bbox contains
// the number of spatial cells in the mesh.
// (This is *not* the physical coordinate bounding box.)
uint64_t bbox[6];
uint64_t* bbox_ptr = bbox;
list<pair<string,string> > attribsIn;
map<string,string> attribsOut;
attribsIn.push_back(make_pair("mesh",meshName));
// Read number of domains and domain sizes
uint64_t N_domains;
file.getArrayAttributes("MESH_DOMAIN_SIZES",attribsIn,attribsOut);
auto it = attribsOut.find("arraysize");
if (it == attribsOut.end()) {
cerr << "VLSV\t\t ERROR: Array 'MESH_DOMAIN_SIZES' XML tag does not have attribute 'arraysize'" << endl;
return false;
} else {
N_domains = atoi(it->second.c_str());
}
uint64_t N_spatialCells = 0;
int64_t* domainInfo = NULL;
if (file.read("MESH_DOMAIN_SIZES",attribsIn,0,N_domains,domainInfo) == false) return false;
for (uint i_domain = 0; i_domain < N_domains; ++i_domain) {
N_spatialCells += domainInfo[2*i_domain];
}
nBlocks.resize(N_spatialCells);
#pragma omp parallel for
for (size_t i=0; i<nBlocks.size(); ++i) nBlocks[i] = 0;
// Note: the input file contains N particle species, which are also
// defined in the configuration file. We need to read the BLOCKSPERCELL
// array for each species, and sum the values for each spatial cell.
set<string> speciesNames;
if (file.getUniqueAttributeValues("BLOCKSPERCELL","name",speciesNames) == false) return false;
// Iterate over all particle species and read in BLOCKSPERCELL array
// to all processes, and add the values to nBlocks
uint64_t* buffer = new uint64_t[N_spatialCells];
for (set<string>::const_iterator s=speciesNames.begin(); s!=speciesNames.end(); ++s) {
attribsIn.clear();
attribsIn.push_back(make_pair("mesh",meshName));
attribsIn.push_back(make_pair("name",*s));
if (file.getArrayInfo("BLOCKSPERCELL",attribsIn,arraySize,vectorSize,dataType,byteSize) == false) return false;
if (file.read("BLOCKSPERCELL",attribsIn,0,arraySize,buffer) == false) {
delete [] buffer; buffer = NULL;
return false;
}
#pragma omp parallel for
for (size_t i=0; i<N_spatialCells; ++i) {
nBlocks[i] += buffer[i];
}
}
delete [] buffer; buffer = NULL;
return success;
}
/** Read velocity block mesh data and distribution function data belonging to this process
* for the given particle species. This function must be called simultaneously by all processes.
* @param file VLSV reader with input file open.
* @param spatMeshName Name of the spatial mesh.
* @param fileCells List of all spatial cell IDs.
* @param localCellStartOffset The offset from which to start reading cells.
* @param localCells How many spatial cells after the offset to read.
* @param blocksPerCell Number of velocity blocks for this particle species in each spatial cell belonging to this process.
* @param localBlockStartOffset Offset into velocity block data arrays from which to start reading data.
* @param localBlocks Number of velocity blocks for this species assigned to this process.
* @param mpiGrid Parallel grid library.
* @param popID ID of the particle species who's data is to be read.
* @return If true, velocity block data was read successfully.*/
template <typename fileReal>
bool _readBlockData(
vlsv::ParallelReader & file,
const std::string& spatMeshName,
const std::vector<uint64_t>& fileCells,
const uint64_t localCellStartOffset,
const uint64_t localCells,
const vmesh::LocalID* blocksPerCell,
const uint64_t localBlockStartOffset,
const uint64_t localBlocks,
dccrg::Dccrg<SpatialCell,dccrg::Cartesian_Geometry>& mpiGrid,
std::function<vmesh::GlobalID(vmesh::GlobalID)> blockIDremapper,
const uint popID
) {
uint64_t arraySize;
uint64_t avgVectorSize;
uint64_t cellParamsVectorSize;
vlsv::datatype::type dataType;
uint64_t byteSize;
list<pair<string,string> > avgAttribs;
bool success=true;
const string popName = getObjectWrapper().particleSpecies[popID].name;
const string tagName = "BLOCKIDS";
avgAttribs.push_back(make_pair("mesh",spatMeshName));
avgAttribs.push_back(make_pair("name",popName));
//Get block id array info and store them into blockIdAttribs, lockIdByteSize, blockIdDataType, blockIdVectorSize
list<pair<string,string> > blockIdAttribs;
uint64_t blockIdVectorSize, blockIdByteSize;
vlsv::datatype::type blockIdDataType;
blockIdAttribs.push_back( make_pair("mesh", spatMeshName));
blockIdAttribs.push_back( make_pair("name", popName));
if (file.getArrayInfo("BLOCKIDS",blockIdAttribs,arraySize,blockIdVectorSize,blockIdDataType,blockIdByteSize) == false ){
logFile << "(RESTART) ERROR: Failed to read BLOCKCOORDINATES array info " << endl << write;
return false;
}
if(file.getArrayInfo("BLOCKVARIABLE",avgAttribs,arraySize,avgVectorSize,dataType,byteSize) == false ){
logFile << "(RESTART) ERROR: Failed to read BLOCKVARIABLE array info " << endl << write;
return false;
}
//Some routine error checks:
if( avgVectorSize!=WID3 ){
logFile << "(RESTART) ERROR: Blocksize does not match in restart file " << endl << write;
return false;
}
if( byteSize != sizeof(fileReal) ) {
logFile << "(RESTART) ERROR: Bad avgs bytesize at " << __FILE__ << " " << __LINE__ << endl << write;
return false;
}
if( blockIdByteSize != sizeof(vmesh::GlobalID)) {
logFile << "(RESTART) ERROR: BlockID data size does not match " << __FILE__ << " " << __LINE__ << endl << write;
return false;
}
fileReal* avgBuffer = new fileReal[avgVectorSize * localBlocks]; //avgs data for all cells
vmesh::GlobalID * blockIdBuffer = new vmesh::GlobalID[blockIdVectorSize * localBlocks]; //blockids of all cells
//Read block ids and data
if (file.readArray("BLOCKIDS", blockIdAttribs, localBlockStartOffset, localBlocks, (char*)blockIdBuffer ) == false) {
cerr << "ERROR, failed to read BLOCKIDS in " << __FILE__ << ":" << __LINE__ << endl;
success = false;
}
if (file.readArray("BLOCKVARIABLE", avgAttribs, localBlockStartOffset, localBlocks, (char*)avgBuffer) == false) {
cerr << "ERROR, failed to read BLOCKVARIABLE in " << __FILE__ << ":" << __LINE__ << endl;
success = false;
}
uint64_t blockBufferOffset=0;
//Go through all spatial cells
vector<vmesh::GlobalID> blockIdsInCell; //blockIds in a particular cell, temporary usage
for(uint64_t i=0; i<localCells; i++) {
CellID cell = fileCells[localCellStartOffset + i]; //spatial cell id
vmesh::LocalID nBlocksInCell = blocksPerCell[i];
//copy blocks in this cell to vector blockIdsInCell, size of read in data has been checked earlier
blockIdsInCell.reserve(nBlocksInCell);
blockIdsInCell.assign(blockIdBuffer + blockBufferOffset, blockIdBuffer + blockBufferOffset + nBlocksInCell);
for(auto& id : blockIdsInCell) {
id = blockIDremapper(id);
}
mpiGrid[cell]->add_velocity_blocks(blockIdsInCell,popID); //allocate space for all blocks and create them
//copy avgs data, here a conversion may happen between float and double
Realf *cellBlockData=mpiGrid[cell]->get_data(popID);
for(uint64_t i = 0; i< WID3 * nBlocksInCell ; i++){
cellBlockData[i] = avgBuffer[blockBufferOffset*WID3 + i];
}
blockBufferOffset += nBlocksInCell; //jump to location of next local cell
}
delete[] avgBuffer;
delete[] blockIdBuffer;
return success;
}
/** Read velocity block data of all existing particle species.
* @param file VLSV reader.
* @param meshName Name of the spatial mesh.
* @param fileCells Vector containing spatial cell IDs.
* @param localCellStartOffset Offset into fileCells, determines where the cells belonging
* to this process start.
* @param localCells Number of spatial cells assigned to this process.
* @param mpiGrid Parallel grid library.
* @return If true, velocity block data was read successfully.*/
bool readBlockData(
vlsv::ParallelReader& file,
const string& meshName,
const vector<CellID>& fileCells,
const uint64_t localCellStartOffset,
const uint64_t localCells,
dccrg::Dccrg<SpatialCell,dccrg::Cartesian_Geometry>& mpiGrid
) {
bool success = true;
const uint64_t bytesReadStart = file.getBytesRead();
int N_processes;
MPI_Comm_size(MPI_COMM_WORLD,&N_processes);
uint64_t arraySize;
uint64_t vectorSize;
vlsv::datatype::type dataType;
uint64_t byteSize;
uint64_t* offsetArray = new uint64_t[N_processes];
for (uint popID=0; popID<getObjectWrapper().particleSpecies.size(); ++popID) {
const string& popName = getObjectWrapper().particleSpecies[popID].name;
// Create a cellID remapping lambda that can renumber our velocity space, should it's size have changed.
// By default, this is a no-op that keeps the blockIDs untouched.
std::function<vmesh::GlobalID(vmesh::GlobalID)> blockIDremapper = [](vmesh::GlobalID oldID) -> vmesh::GlobalID {return oldID;};
// Check that velocity space extents and DV matches the grids we have created
list<pair<string,string> > attribs;
attribs.push_back(make_pair("mesh",popName));
std::array<unsigned int, 6> fileMeshBBox;
unsigned int* bufferpointer = &fileMeshBBox[0];
if (file.read("MESH_BBOX",attribs,0,6,bufferpointer,false) == false) {
logFile << "(RESTART) ERROR: Failed to read MESH_BBOX at " << __FILE__ << ":" << __LINE__ << endl << write;
success = false;
}
const size_t meshID = getObjectWrapper().particleSpecies[popID].velocityMesh;
const vmesh::MeshParameters& ourMeshParams = getObjectWrapper().velocityMeshes[meshID];
if(fileMeshBBox[0] != ourMeshParams.gridLength[0] ||
fileMeshBBox[1] != ourMeshParams.gridLength[1] ||
fileMeshBBox[2] != ourMeshParams.gridLength[2]) {
logFile << "(RESTART) INFO: velocity mesh sizes don't match:" << endl
<< " restart file has " << fileMeshBBox[0] << " x " << fileMeshBBox[1] << " x " << fileMeshBBox[2] << "," << endl
<< " config specifies " << ourMeshParams.gridLength[0] << " x " << ourMeshParams.gridLength[1] << " x " << ourMeshParams.gridLength[2] << endl << write;
if(ourMeshParams.gridLength[0] < fileMeshBBox[0] ||
ourMeshParams.gridLength[1] < fileMeshBBox[1] ||
ourMeshParams.gridLength[2] < fileMeshBBox[2]) {
logFile << "(RESTART) ERROR: trying to shrink velocity space." << endl << write;
abort();
}
// If we are mismatched, we have to iterate through the velocity coords to see if we have a
// chance at renumbering.
std::vector<Real> fileVelCoordsX(fileMeshBBox[0]*fileMeshBBox[3]+1);
std::vector<Real> fileVelCoordsY(fileMeshBBox[1]*fileMeshBBox[4]+1);
std::vector<Real> fileVelCoordsZ(fileMeshBBox[2]*fileMeshBBox[5]+1);
Real* tempPointer = fileVelCoordsX.data();
if (file.read("MESH_NODE_CRDS_X",attribs,0,fileMeshBBox[0]*fileMeshBBox[3]+1,tempPointer,false) == false) {
logFile << "(RESTART) ERROR: Failed to read MESH_NODE_CRDS_X at " << __FILE__ << ":" << __LINE__ << endl << write;
success = false;
}
tempPointer = fileVelCoordsY.data();
if (file.read("MESH_NODE_CRDS_Y",attribs,0,fileMeshBBox[1]*fileMeshBBox[4]+1,tempPointer,false) == false) {
logFile << "(RESTART) ERROR: Failed to read MESH_NODE_CRDS_X at " << __FILE__ << ":" << __LINE__ << endl << write;
success = false;
}
tempPointer = fileVelCoordsZ.data();
if (file.read("MESH_NODE_CRDS_Z",attribs,0,fileMeshBBox[2]*fileMeshBBox[5]+1,tempPointer,false) == false) {
logFile << "(RESTART) ERROR: Failed to read MESH_NODE_CRDS_X at " << __FILE__ << ":" << __LINE__ << endl << write;
success = false;
}
const Real dVx = getObjectWrapper().velocityMeshes[meshID].cellSize[0];
for(const auto& c : fileVelCoordsX) {
Real cellindex = (c - getObjectWrapper().velocityMeshes[meshID].meshMinLimits[0]) / dVx;
if(fabs(nearbyint(cellindex) - cellindex) > 1./10000.) {
logFile << "(RESTART) ERROR: Can't resize velocity space as cell coordinates don't match." << endl
<< " (X coordinate " << c << " = " << cellindex <<" * " << dVx << " + " << getObjectWrapper().velocityMeshes[meshID].meshMinLimits[0] << endl
<< " coordinate = cellindex * dV + meshMinLimits)" << endl << write;
abort();
}
}
const Real dVy = getObjectWrapper().velocityMeshes[meshID].cellSize[1];
for(const auto& c : fileVelCoordsY) {
Real cellindex = (c - getObjectWrapper().velocityMeshes[meshID].meshMinLimits[1]) / dVy;
if(fabs(nearbyint(cellindex) - cellindex) > 1./10000.) {
logFile << "(RESTART) ERROR: Can't resize velocity space as cell coordinates don't match." << endl
<< " (Y coordinate " << c << " = " << cellindex <<" * " << dVy << " + " << getObjectWrapper().velocityMeshes[meshID].meshMinLimits[1] << endl
<< " coordinate = cellindex * dV + meshMinLimits)" << endl << write;
abort();
}
}
const Real dVz = getObjectWrapper().velocityMeshes[meshID].cellSize[2];
for(const auto& c : fileVelCoordsY) {
Real cellindex = (c - getObjectWrapper().velocityMeshes[meshID].meshMinLimits[2]) / dVz;
if(fabs(nearbyint(cellindex) - cellindex) > 1./10000.) {
logFile << "(RESTART) ERROR: Can't resize velocity space as cell coordinates don't match." << endl
<< " (Z coordinate " << c << " = " << cellindex <<" * " << dVz << " + " << getObjectWrapper().velocityMeshes[meshID].meshMinLimits[2] << endl
<< " coordinate = cellindex * dV + meshMinLimits)" << endl << write;
abort();
}
}
// If we haven't aborted above, we can apparently renumber our
// cellIDs. Build an approprita blockIDremapper lambda for this purpose.
std::array<int, 3> velGridOffset;
velGridOffset[0] = (fileVelCoordsX[0] - getObjectWrapper().velocityMeshes[meshID].meshMinLimits[0]) / dVx;
velGridOffset[1] = (fileVelCoordsY[0] - getObjectWrapper().velocityMeshes[meshID].meshMinLimits[1]) / dVy;
velGridOffset[2] = (fileVelCoordsZ[0] - getObjectWrapper().velocityMeshes[meshID].meshMinLimits[2]) / dVz;
if((velGridOffset[0] % ourMeshParams.blockLength[0] != 0) ||
(velGridOffset[1] % ourMeshParams.blockLength[1] != 0) ||
(velGridOffset[2] % ourMeshParams.blockLength[2] != 0)) {
logFile << "(RESTART) ERROR: resizing velocity space on restart must end up with the old velocity space" << endl
<< " at a block boundary of the new space!" << endl
<< " (It now starts at cell [" << velGridOffset[0] << ", " << velGridOffset[1] << "," << velGridOffset[2] << "])" << endl << write;
abort();
}
velGridOffset[0] /= ourMeshParams.blockLength[0];
velGridOffset[1] /= ourMeshParams.blockLength[1];
velGridOffset[2] /= ourMeshParams.blockLength[2];
blockIDremapper = [fileMeshBBox,velGridOffset,ourMeshParams](vmesh::GlobalID oldID) -> vmesh::GlobalID {
unsigned int x,y,z;
x = oldID % fileMeshBBox[0];
y = (oldID / fileMeshBBox[0]) % fileMeshBBox[1];
z = oldID / (fileMeshBBox[0] * fileMeshBBox[1]);
x += velGridOffset[0];
y += velGridOffset[1];
z += velGridOffset[2];
//logFile << " Remapping " << oldID << "(" << x << "," << y << "," << z << ") to " << x + y * ourMeshParams.gridLength[0] + z* ourMeshParams.gridLength[0] * ourMeshParams.gridLength[1] << endl << write;
return x + y * ourMeshParams.gridLength[0] + z* ourMeshParams.gridLength[0] * ourMeshParams.gridLength[1];
};
logFile << " => Resizing velocity space by renumbering GlobalIDs." << endl << endl << write;
}
// In restart files each spatial cell has an entry in CELLSWITHBLOCKS.
// Each process calculates how many velocity blocks it has for this species.
attribs.clear();
attribs.push_back(make_pair("mesh",meshName));
attribs.push_back(make_pair("name",popName));
vmesh::LocalID* blocksPerCell = NULL;
if (file.read("BLOCKSPERCELL",attribs,localCellStartOffset,localCells,blocksPerCell,true) == false) {
logFile << "(RESTART) ERROR: Failed to read BLOCKSPERCELL at " << __FILE__ << ":" << __LINE__ << endl << write;
success = false;
}
// Count how many velocity blocks this process gets
uint64_t blockSum = 0;
for (uint64_t i=0; i<localCells; ++i){
blockSum += blocksPerCell[i];
}
// Gather all block sums to master process who will them broadcast
// the values to everyone
MPI_Allgather(&blockSum,1,MPI_Type<uint64_t>(),offsetArray,1,MPI_Type<uint64_t>(),MPI_COMM_WORLD);
// Calculate the offset from which this process starts reading block data
uint64_t myOffset = 0;
for (int64_t i=0; i<mpiGrid.get_rank(); ++i) myOffset += offsetArray[i];
if (file.getArrayInfo("BLOCKVARIABLE",attribs,arraySize,vectorSize,dataType,byteSize) == false) {
logFile << "(RESTART) ERROR: Failed to read BLOCKVARIABLE INFO" << endl << write;
return false;
}
// Call _readBlockData
if (dataType == vlsv::datatype::type::FLOAT) {
switch (byteSize) {
case sizeof(double):
if (_readBlockData<double>(file,meshName,fileCells,localCellStartOffset,localCells,blocksPerCell,
myOffset,blockSum,mpiGrid,blockIDremapper,popID) == false) success = false;
break;
case sizeof(float):
if (_readBlockData<float>(file,meshName,fileCells,localCellStartOffset,localCells,blocksPerCell,
myOffset,blockSum,mpiGrid,blockIDremapper,popID) == false) success = false;
break;
}
} else if (dataType == vlsv::datatype::type::UINT) {
switch (byteSize) {
case sizeof(uint32_t):
if (_readBlockData<uint32_t>(file,meshName,fileCells,localCellStartOffset,localCells,blocksPerCell,
myOffset,blockSum,mpiGrid,blockIDremapper,popID) == false) success = false;
break;
case sizeof(uint64_t):
if (_readBlockData<uint64_t>(file,meshName,fileCells,localCellStartOffset,localCells,blocksPerCell,
myOffset,blockSum,mpiGrid,blockIDremapper,popID) == false) success = false;
break;
}
} else if (dataType == vlsv::datatype::type::INT) {
switch (byteSize) {
case sizeof(int32_t):
if (_readBlockData<int32_t>(file,meshName,fileCells,localCellStartOffset,localCells,blocksPerCell,
myOffset,blockSum,mpiGrid,blockIDremapper,popID) == false) success = false;
break;
case sizeof(int64_t):
if (_readBlockData<int64_t>(file,meshName,fileCells,localCellStartOffset,localCells,blocksPerCell,
myOffset,blockSum,mpiGrid,blockIDremapper,popID) == false) success = false;
break;
}
} else {
logFile << "(RESTART) ERROR: Failed to read data type at readCellParamsVariable" << endl << write;
success = false;
}
delete [] blocksPerCell; blocksPerCell = NULL;
} // for-loop over particle species
delete [] offsetArray; offsetArray = NULL;
const uint64_t bytesReadEnd = file.getBytesRead() - bytesReadStart;
logFile << "Velocity meshes and data read, approximate data rate is ";
logFile << vlsv::printDataRate(bytesReadEnd,file.getReadTime()) << endl << write;
return success;
}
/*! Reads cell parameters from the file and saves them in the right place in mpiGrid
\param file Some parallel vlsv reader with a file open
\param fileCells List of all cell ids
\param localCellStartOffset Offset in the fileCells list for this process ( calculated so that the amount of blocks is distributed somewhat evenly between processes)
\param localCells The amount of cells to read in this process after localCellStartOffset
\param cellParamsIndex The parameter of the cell index e.g. CellParams::RHOM
\param expectedVectorSize The amount of elements in the parameter (parameter can be a scalar or a vector of size N)
\param mpiGrid Vlasiator's grid (the parameters are saved here)
\return Returns true if the operation is successful
*/
template <typename fileReal>
static bool _readCellParamsVariable(
vlsv::ParallelReader& file,
const vector<uint64_t>& fileCells,
const uint64_t localCellStartOffset,
const uint64_t localCells,
const string& variableName,
const size_t cellParamsIndex,
const size_t expectedVectorSize,
dccrg::Dccrg<SpatialCell,dccrg::Cartesian_Geometry>& mpiGrid
) {
uint64_t arraySize;
uint64_t vectorSize;
vlsv::datatype::type dataType;
uint64_t byteSize;
list<pair<string,string> > attribs;
fileReal *buffer;
bool success=true;
attribs.push_back(make_pair("name",variableName));
attribs.push_back(make_pair("mesh","SpatialGrid"));
if (file.getArrayInfo("VARIABLE",attribs,arraySize,vectorSize,dataType,byteSize) == false) {
logFile << "(RESTART) ERROR: Failed to read " << endl << write;
return false;
}
if(vectorSize!=expectedVectorSize){
logFile << "(RESTART) vectorsize wrong " << endl << write;
return false;
}
buffer=new fileReal[vectorSize*localCells];
if(file.readArray("VARIABLE",attribs,localCellStartOffset,localCells,(char *)buffer) == false ) {
logFile << "(RESTART) ERROR: Failed to read " << variableName << endl << write;
return false;
}
for(uint i=0;i<localCells;i++){
uint cell=fileCells[localCellStartOffset+i];
for(uint j=0;j<vectorSize;j++){
mpiGrid[cell]->parameters[cellParamsIndex+j]=buffer[i*vectorSize+j];
}
}
delete[] buffer;
return success;
}
/*! Reads cell parameters from the file and saves them in the right place in mpiGrid
\param file Some parallel vlsv reader with a file open
\param fileCells List of all cell ids
\param localCellStartOffset Offset in the fileCells list for this process ( calculated so that the amount of blocks is distributed somewhat evenly between processes)
\param localCells The amount of cells to read in this process after localCellStartOffset
\param cellParamsIndex The parameter of the cell index e.g. CellParams::RHOM
\param expectedVectorSize The amount of elements in the parameter (parameter can be a scalar or a vector of size N)
\param mpiGrid Vlasiator's grid (the parameters are saved here)
\return Returns true if the operation is successful
*/
bool readCellParamsVariable(
vlsv::ParallelReader& file,
const vector<CellID>& fileCells,
const uint64_t localCellStartOffset,
const uint64_t localCells,
const string& variableName,
const size_t cellParamsIndex,
const size_t expectedVectorSize,
dccrg::Dccrg<SpatialCell,dccrg::Cartesian_Geometry>& mpiGrid
) {
uint64_t arraySize;
uint64_t vectorSize;
vlsv::datatype::type dataType;
uint64_t byteSize;
list<pair<string,string> > attribs;
attribs.push_back(make_pair("name",variableName));
attribs.push_back(make_pair("mesh","SpatialGrid"));
if (file.getArrayInfo("VARIABLE",attribs,arraySize,vectorSize,dataType,byteSize) == false) {
logFile << "(RESTART) ERROR: Failed to read " << endl << write;
return false;
}
// Call _readCellParamsVariable
if( dataType == vlsv::datatype::type::FLOAT ) {
switch (byteSize) {
case sizeof(double):
return _readCellParamsVariable<double>( file, fileCells, localCellStartOffset, localCells, variableName, cellParamsIndex, expectedVectorSize, mpiGrid );
break;
case sizeof(float):
return _readCellParamsVariable<float>( file, fileCells, localCellStartOffset, localCells, variableName, cellParamsIndex, expectedVectorSize, mpiGrid );
break;
}
} else if( dataType == vlsv::datatype::type::UINT ) {
switch (byteSize) {
case sizeof(uint32_t):
return _readCellParamsVariable<uint32_t>( file, fileCells, localCellStartOffset, localCells, variableName, cellParamsIndex, expectedVectorSize, mpiGrid );
break;
case sizeof(uint64_t):
return _readCellParamsVariable<uint64_t>( file, fileCells, localCellStartOffset, localCells, variableName, cellParamsIndex, expectedVectorSize, mpiGrid );
break;
}
} else if( dataType == vlsv::datatype::type::INT ) {
switch (byteSize) {
case sizeof(int32_t):
return _readCellParamsVariable<int32_t>( file, fileCells, localCellStartOffset, localCells, variableName, cellParamsIndex, expectedVectorSize, mpiGrid );
break;
case sizeof(int64_t):
return _readCellParamsVariable<int64_t>( file, fileCells, localCellStartOffset, localCells, variableName, cellParamsIndex, expectedVectorSize, mpiGrid );
break;
}
} else {
logFile << "(RESTART) ERROR: Failed to read data type at readCellParamsVariable" << endl << write;
return false;
}
// For compiler purposes
return false;
}
/*! Read a fsgrid variable (consinting of N real values) from the given vlsv file.
* \param file VLSV parallel reader with a file open.
* \param variableName Name of the variable in the file
* \param numWritingRanks Number of mpi ranks that were used to write this file (used for reconstruction of the spatial order)
* \param targetGrid target location where the data will be stored.
*/
template<unsigned long int N> bool readFsGridVariable(
vlsv::ParallelReader& file, const string& variableName, int numWritingRanks, FsGrid<std::array<Real, N>,FS_STENCIL_WIDTH> & targetGrid) {
uint64_t arraySize;
uint64_t vectorSize;
vlsv::datatype::type dataType;
uint64_t byteSize;
list<pair<string,string> > attribs;
bool convertFloatType = false;
attribs.push_back(make_pair("name",variableName));
attribs.push_back(make_pair("mesh","fsgrid"));
if (file.getArrayInfo("VARIABLE",attribs,arraySize,vectorSize,dataType,byteSize) == false) {
logFile << "(RESTART) ERROR: Failed to read " << endl << write;
return false;
}
if(! (dataType == vlsv::datatype::type::FLOAT && byteSize == sizeof(Real))) {
logFile << "(RESTART) Converting floating point format of fsgrid variable " << variableName << " from " << byteSize * 8 << " bits to " << sizeof(Real) * 8 << " bits." << endl << write;
convertFloatType = true;
}
// Are we restarting from the same number of tasks, or a different number?
int size, myRank;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
std::array<int32_t,3>& localSize = targetGrid.getLocalSize();
std::array<int32_t,3>& localStart = targetGrid.getLocalStart();
std::array<int32_t,3>& globalSize = targetGrid.getGlobalSize();
// Determine our tasks storage size
size_t storageSize = localSize[0]*localSize[1]*localSize[2];
if(size == numWritingRanks) {
// Easy case: same number of tasks => slurp it in.
//
std::array<int,3> decomposition;
targetGrid.computeDomainDecomposition(globalSize, size, decomposition);
// Determine offset in file by summing up all the previous tasks' sizes.
size_t localStartOffset = 0;
for(int task = 0; task < myRank; task++) {
std::array<int32_t,3> thatTasksSize;
thatTasksSize[0] = targetGrid.calcLocalSize(globalSize[0], decomposition[0], task/decomposition[2]/decomposition[1]);
thatTasksSize[1] = targetGrid.calcLocalSize(globalSize[1], decomposition[1], (task/decomposition[2])%decomposition[1]);
thatTasksSize[2] = targetGrid.calcLocalSize(globalSize[2], decomposition[2], task%decomposition[2]);
localStartOffset += thatTasksSize[0] * thatTasksSize[1] * thatTasksSize[2];
}
// Read into buffer
std::vector<Real> buffer(storageSize*N);
if(!convertFloatType) {
if(file.readArray("VARIABLE",attribs, localStartOffset, storageSize, (char*)buffer.data()) == false) {
logFile << "(RESTART) ERROR: Failed to read fsgrid variable " << variableName << endl << write;
return false;
}
} else {
// Read to temporary float buffer
std::vector<float> readBuffer(storageSize*N);
if(file.readArray("VARIABLE",attribs, localStartOffset, storageSize, (char*)readBuffer.data()) == false) {
logFile << "(RESTART) ERROR: Failed to read fsgrid variable " << variableName << endl << write;
return false;
}
for(uint64_t i=0; i<storageSize*N; i++) {
buffer[i] = readBuffer[i];
}
}
// Assign buffer into fsgrid
int index=0;
for(int z=0; z<localSize[2]; z++) {
for(int y=0; y<localSize[1]; y++) {
for(int x=0; x<localSize[0]; x++) {
memcpy(targetGrid.get(x,y,z), &buffer[index], N*sizeof(Real));
index += N;
}
}
}
} else {
// More difficult case: different number of tasks.
// In this case, our own fsgrid domain overlaps (potentially many) domains in the file.
// We read the whole source rank into a temporary buffer, and transfer the overlapping
// part.
//
// +------------+----------------+
// | | |
// | . . . . . . . . . . . . |
// | .<----->|<----------->. |
// | .<----->|<----------->. |
// | .<----->|<----------->. |
// +----+-------+-------------+--|
// | .<----->|<----------->. |
// | .<----->|<----------->. |
// | .<----->|<----------->. |
// | . . . . . . . . . . . . |
// | | |
// +------------+----------------+
// Determine the decomposition in the file and the one in RAM for our restart
std::array<int,3> fileDecomposition;
targetGrid.computeDomainDecomposition(globalSize, numWritingRanks, fileDecomposition);
// Iterate through tasks and find their overlap with our domain.
size_t fileOffset = 0;
for(int task = 0; task < numWritingRanks; task++) {
std::array<int32_t,3> thatTasksSize;
std::array<int32_t,3> thatTasksStart;
thatTasksSize[0] = targetGrid.calcLocalSize(globalSize[0], fileDecomposition[0], task/fileDecomposition[2]/fileDecomposition[1]);
thatTasksSize[1] = targetGrid.calcLocalSize(globalSize[1], fileDecomposition[1], (task/fileDecomposition[2])%fileDecomposition[1]);
thatTasksSize[2] = targetGrid.calcLocalSize(globalSize[2], fileDecomposition[2], task%fileDecomposition[2]);
thatTasksStart[0] = targetGrid.calcLocalStart(globalSize[0], fileDecomposition[0], task/fileDecomposition[2]/fileDecomposition[1]);
thatTasksStart[1] = targetGrid.calcLocalStart(globalSize[1], fileDecomposition[1], (task/fileDecomposition[2])%fileDecomposition[1]);
thatTasksStart[2] = targetGrid.calcLocalStart(globalSize[2], fileDecomposition[2], task%fileDecomposition[2]);
// Iterate through overlap area
std::array<int,3> overlapStart,overlapEnd,overlapSize;
overlapStart[0] = max(localStart[0],thatTasksStart[0]);
overlapStart[1] = max(localStart[1],thatTasksStart[1]);
overlapStart[2] = max(localStart[2],thatTasksStart[2]);
overlapEnd[0] = min(localStart[0]+localSize[0], thatTasksStart[0]+thatTasksSize[0]);
overlapEnd[1] = min(localStart[1]+localSize[1], thatTasksStart[1]+thatTasksSize[1]);
overlapEnd[2] = min(localStart[2]+localSize[2], thatTasksStart[2]+thatTasksSize[2]);
overlapSize[0] = max(overlapEnd[0]-overlapStart[0],0);
overlapSize[1] = max(overlapEnd[1]-overlapStart[1],0);
overlapSize[2] = max(overlapEnd[2]-overlapStart[2],0);
// Read into buffer
std::vector<Real> buffer(thatTasksSize[0]*thatTasksSize[1]*thatTasksSize[2]*N);
file.startMultiread("VARIABLE", attribs);
// Read every source rank that we have an overlap with.
if(overlapSize[0]*overlapSize[1]*overlapSize[2] > 0) {
if(!convertFloatType) {
if(file.addMultireadUnit((char*)buffer.data(), thatTasksSize[0]*thatTasksSize[1]*thatTasksSize[2])==false) {
logFile << "(RESTART) ERROR: Failed to read fsgrid variable " << variableName << endl << write;
return false;
}
file.endMultiread(fileOffset);
} else {
std::vector<float> readBuffer(thatTasksSize[0]*thatTasksSize[1]*thatTasksSize[2]*N);
if(file.addMultireadUnit((char*)readBuffer.data(), thatTasksSize[0]*thatTasksSize[1]*thatTasksSize[2])==false) {
logFile << "(RESTART) ERROR: Failed to read fsgrid variable " << variableName << endl << write;
return false;
}
file.endMultiread(fileOffset);
for(uint64_t i=0; i< thatTasksSize[0]*thatTasksSize[1]*thatTasksSize[2]*N; i++) {
buffer[i]=readBuffer[i];
}
}
// Copy continuous stripes in x direction.
for(int z=overlapStart[2]; z<overlapEnd[2]; z++) {
for(int y=overlapStart[1]; y<overlapEnd[1]; y++) {
for(int x=overlapStart[0]; x<overlapEnd[0]; x++) {
int index = (z - thatTasksStart[2]) * thatTasksSize[0]*thatTasksSize[1]
+ (y - thatTasksStart[1]) * thatTasksSize[0]
+ (x - thatTasksStart[0]);
memcpy(targetGrid.get(x - localStart[0], y - localStart[1], z - localStart[2]), &buffer[index*N], N*sizeof(Real));
}
}
}
} else {
// If we don't overlap, just perform a dummy read.
file.endMultiread(fileOffset);
}
fileOffset += thatTasksSize[0] * thatTasksSize[1] * thatTasksSize[2];
}
}
phiprof::start("updateGhostCells");
targetGrid.updateGhostCells();
phiprof::stop("updateGhostCells");
return true;
}
/*! Read an ionosphere variable from the given vlsv file.
* Note that only singular floating point values (no vectors) can be read at this time.
* \param file VLSV parallel reader with a file open.
* \param variableName Name of the variable in the file
* \param grid the ionosphere grid that data will be deposited into
* \param index index into the nodes' parameters array, where the data will end up.
*/
bool readIonosphereNodeVariable(
vlsv::ParallelReader& file, const string& variableName, SBC::SphericalTriGrid& grid, ionosphereParameters index) {
uint64_t arraySize;
uint64_t vectorSize;
vlsv::datatype::type dataType;
uint64_t byteSize;
list<pair<string,string> > attribs;
bool convertFloatType = false;
attribs.push_back(make_pair("name",variableName));
attribs.push_back(make_pair("mesh","ionosphere"));
// If we don't have an ionosphere (zero nodes), we simply skip trying to read any restart data for this.
if(grid.nodes.size() == 0) {
return true;
}
if (file.getArrayInfo("VARIABLE",attribs,arraySize,vectorSize,dataType,byteSize) == false) {
logFile << "(RESTART) ERROR: Failed to read array info for " << variableName << endl << write;
return false;
}
if(! (dataType == vlsv::datatype::type::FLOAT && byteSize == sizeof(Real))) {
logFile << "(RESTART) Converting floating point format of ionosphere variable " << variableName << " from " << byteSize * 8 << " bits to " << sizeof(Real) * 8 << " bits." << endl << write;
convertFloatType = true;
}
// Verify that this is a scalar variable
if(vectorSize != 1) {
logFile << "(RESTART) ERROR: Trying to read vector valued (" << vectorSize << " components) ionosphere parameter from restart file. Only scalars are supported." << endl << write;