Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
vlsvreaderinterface.h
Go to the documentation of this file.
1/*
2 * This file is part of Vlasiator.
3 * Copyright 2010-2016 Finnish Meteorological Institute
4 *
5 * For details of usage, see the COPYING file and read the "Rules of the Road"
6 * at http://www.physics.helsinki.fi/vlasiator/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef VLSVREADER_INTERFACE_H
24#define VLSVREADER_INTERFACE_H
25
26#include <map>
27#include <vector>
28#include <unordered_map>
29#include <array>
30#include <vlsv_reader.h>
31
32// Returns the vlsv file's version number. Returns 0 if the version does not have a version mark (The old vlsv format does not have it)
33//Input: File name
34extern float checkVersion( const std::string & fname );
35
36namespace vlsvinterface {
37 class Reader : public vlsv::Reader {
38 private:
39 std::unordered_map<uint64_t, uint64_t> cellIdLocations;
40 std::unordered_map<uint64_t, std::pair<uint64_t, uint32_t> > cellsWithBlocksLocations;
43 public:
44 Reader();
45 virtual ~Reader();
46 bool getMeshNames( std::list<std::string> & meshNames ); //Function for getting mesh names
47 bool getMeshNames( std::set<std::string> & meshNames );
48 bool getVariableNames( const std::string&, std::list<std::string> & meshNames );
49 bool getVariableNames( std::set<std::string> & meshNames );
50 bool getCellIds( std::vector<uint64_t> & cellIds,const std::string& meshName="SpatialGrid");
51 //Reads in a variable:
52 template <typename T, size_t N>
53 bool getVariable( const std::string & variableName, const uint64_t & cellId, std::array<T, N> & variable );
54 bool getBlockIds( const uint64_t& cellId,std::vector<uint64_t>& blockIds,const std::string& popName );
55 bool setCellIds();
56 inline void clearCellIds() {
57 cellIdLocations.clear();
58 cellIdsSet = false;
59 }
60 bool setCellsWithBlocks(const std::string& meshName,const std::string& popName);
61 inline void clearCellsWithBlocks() {
63 cellsWithBlocksSet = false;
64 }
65 bool getVelocityBlockVariables( const std::string & variableName, const uint64_t & cellId, char*& buffer, bool allocateMemory = true );
66
67 inline uint64_t getBlockOffset( const uint64_t & cellId ) {
68 //Check if the cell id can be found:
69 std::unordered_map<uint64_t, std::pair<uint64_t,uint32_t> >::const_iterator it = cellsWithBlocksLocations.find( cellId );
70 if( it == cellsWithBlocksLocations.end() ) {
71 std::cerr << "COULDNT FIND CELL ID " << cellId << " AT " << __FILE__ << " " << __LINE__ << std::endl;
72 exit(1);
73 }
74 //Get offset:
75 return std::get<0>(it->second);
76 }
77 inline uint32_t getNumberOfBlocks( const uint64_t & cellId ) {
78 //Check if the cell id can be found:
79 std::unordered_map<uint64_t, std::pair<uint64_t,uint32_t> >::const_iterator it = cellsWithBlocksLocations.find( cellId );
80 if( it == cellsWithBlocksLocations.end() ) {
81 std::cerr << "COULDNT FIND CELL ID " << cellId << " AT " << __FILE__ << " " << __LINE__ << std::endl;
82 exit(1);
83 }
84 //Get number of blocks:
85 return std::get<1>(it->second);
86 }
87 };
88
89 template <typename T, size_t N> inline
90 bool Reader::getVariable( const std::string & variableName, const uint64_t & cellId, std::array<T, N> & variable ) {
91 if( cellIdsSet == false ) {
92 std::cerr << "ERROR, CELL IDS NOT SET AT " << __FILE__ << " " << __LINE__ << std::endl;
93 return false;
94 }
95 //Check if the cell id is in the list:
96 std::unordered_map<uint64_t, uint64_t>::const_iterator findCell = cellIdLocations.find(cellId);
97 if( findCell == cellIdLocations.end() ) {
98 std::cerr << "ERROR, CELL ID NOT FOUND AT " << __FILE__ << " " << __LINE__ << std::endl;
99 return false;
100 }
101 uint64_t vectorSize, byteSize;
102 uint64_t arraySize;
103 vlsv::datatype::type dataType;
104 std::list< std::pair<std::string, std::string> > xmlAttributes;
105 xmlAttributes.push_back( std::make_pair( "name", variableName ) );
106 xmlAttributes.push_back( std::make_pair( "mesh", "SpatialGrid" ) );
107 if( getArrayInfo( "VARIABLE", xmlAttributes, arraySize, vectorSize, dataType, byteSize ) == false ) return false;
108 if( vectorSize != N ) {
109 std::cerr << "ERROR, BAD VECTORSIZE AT " << __FILE__ << " " << __LINE__ << std::endl;
110 return false;
111 }
112 const uint64_t amountToReadIn = 1;
113 char * buffer = new char[vectorSize * amountToReadIn * byteSize];
114 //Read in variable to the buffer:
115 const uint64_t begin = findCell->second;
116 if( readArray( "VARIABLE", xmlAttributes, begin, amountToReadIn, buffer ) == false ) return false;
117 float * buffer_float = reinterpret_cast<float*>(buffer);
118 double * buffer_double = reinterpret_cast<double*>(buffer);
119 uint32_t * buffer_uint_small = reinterpret_cast<uint32_t*>(buffer);
120 uint64_t * buffer_uint_large = reinterpret_cast<uint64_t*>(buffer);
121 int32_t * buffer_int_small = reinterpret_cast<int32_t*>(buffer);
122 int64_t * buffer_int_large = reinterpret_cast<int64_t*>(buffer);
123 //Input the variable:
124 if( dataType == vlsv::datatype::type::FLOAT ) {
125 if( byteSize == sizeof(double) ) {
126 for( uint i = 0; i < N; ++i ) {
127 const double var = buffer_double[i];
128 variable[i] = var;
129 }
130 } else if( byteSize == sizeof(float) ) {
131 for( uint i = 0; i < N; ++i ) {
132 const float var = buffer_float[i];
133 variable[i] = var;
134 }
135 } else {
136 std::cerr << "BAD BYTESIZE AT " << __FILE__ << " " << __LINE__ << std::endl;
137 delete [] buffer;
138 return false;
139 }
140 } else if( dataType == vlsv::datatype::type::UINT ) {
141 if( byteSize == sizeof(uint64_t) ) {
142 for( uint i = 0; i < N; ++i ) {
143 const uint64_t var = buffer_uint_large[i];
144 variable[i] = var;
145 }
146 } else if( byteSize == sizeof(uint32_t) ) {
147 for( uint i = 0; i < N; ++i ) {
148 const uint32_t var = buffer_uint_small[i];
149 variable[i] = var;
150 }
151 } else {
152 std::cerr << "BAD BYTESIZE AT " << __FILE__ << " " << __LINE__ << std::endl;
153 delete [] buffer;
154 return false;
155 }
156 } else if( dataType == vlsv::datatype::type::INT ) {
157 if( byteSize == sizeof(int64_t) ) {
158 for( uint i = 0; i < N; ++i ) {
159 const int64_t var = buffer_int_large[i];
160 variable[i] = var;
161 }
162 } else if( byteSize == sizeof(int32_t) ) {
163 for( uint i = 0; i < N; ++i ) {
164 const int32_t var = buffer_int_small[i];
165 variable[i] = var;
166 }
167 } else {
168 std::cerr << "BAD BYTESIZE AT " << __FILE__ << " " << __LINE__ << std::endl;
169 delete [] buffer;
170 return false;
171 }
172 } else {
173 std::cerr << "BAD DATATYPE AT " << __FILE__ << " " << __LINE__ << std::endl;
174 delete [] buffer;
175 return false;
176 }
177 delete [] buffer;
178 return true;
179 }
180}
181
182#endif
for i
Definition Dispersion.m:24
uint64_t getBlockOffset(const uint64_t &cellId)
bool getBlockIds(const uint64_t &cellId, std::vector< uint64_t > &blockIds, const std::string &popName)
bool getMeshNames(std::list< std::string > &meshNames)
bool setCellsWithBlocks(const std::string &meshName, const std::string &popName)
uint32_t getNumberOfBlocks(const uint64_t &cellId)
bool getVariableNames(std::set< std::string > &meshNames)
std::unordered_map< uint64_t, std::pair< uint64_t, uint32_t > > cellsWithBlocksLocations
bool getVariable(const std::string &variableName, const uint64_t &cellId, std::array< T, N > &variable)
bool getMeshNames(std::set< std::string > &meshNames)
bool getVariableNames(const std::string &, std::list< std::string > &meshNames)
bool getCellIds(std::vector< uint64_t > &cellIds, const std::string &meshName="SpatialGrid")
bool getVelocityBlockVariables(const std::string &variableName, const uint64_t &cellId, char *&buffer, bool allocateMemory=true)
std::unordered_map< uint64_t, uint64_t > cellIdLocations
float checkVersion(const std::string &fname)