Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
dro_populations.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://vlasiator.fmi.fi/
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 DRO_POPULATIONS_H
24#define DRO_POPULATIONS_H
25
26#include <string>
27
29#include "../object_wrapper.h"
31
32namespace DRO {
33
34 template <typename T> class DataReductionOperatorPopulations: public DataReductionOperator {
35 public:
36 DataReductionOperatorPopulations(const std::string& name,const uint popID, const unsigned int byteOffset,const unsigned int vectorSize) :
37 DataReductionOperator(), _byteOffset {byteOffset}, _vectorSize {vectorSize}, _popID {popID}, _name {name} {}
39
40 virtual bool getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
41 std::cerr << "Error! Trying to perform population-based data reducer on unspecialized template!" << std::endl;
42 return false;
43 };
44 virtual std::string getName() const {return _name;};
45
46 virtual bool reduceData(const spatial_cell::SpatialCell* cell,char* buffer) {
47 // First, get a byte-sized pointer to this populations' struct within this cell.
48 const char* population_struct = reinterpret_cast<const char*>(&cell->get_population(_popID));
49
50 // Find the actual data at the specified offset
51 const char* ptr = population_struct + _byteOffset;
52
53 for (uint i = 0; i < _vectorSize*sizeof(T); ++i){
54 buffer[i] = ptr[i];
55 }
56 return true;
57 }
58
59 virtual bool reduceDiagnostic(const spatial_cell::SpatialCell* cell, Real* target) {
60 if(_vectorSize > 1) {
61 std::cerr << "Warning: trying to use variable " << getName() << " as a diagnostic reducer, but it's vectorSize is " << _vectorSize << " > 1" << std::endl;
62 return false;
63 }
64
65 // First, get a byte-sized pointer to this populations' struct within this cell.
66 const char* population_struct = reinterpret_cast<const char*>(&cell->get_population(_popID));
67
68 // Find the actual data at the specified offset
69 const char* ptr = population_struct + _byteOffset;
70
71 *target = *reinterpret_cast<const Real*>(ptr);
72 return true;
73 }
74
75 virtual bool setSpatialCell(const spatial_cell::SpatialCell* cell) {
76
77 // First, get a byte-sized pointer to this populations' struct within this cell.
78 const char* population_struct = reinterpret_cast<const char*>(&cell->get_population(_popID));
79
80 // Find the actual data at the specified offset
81 const T* ptr = reinterpret_cast<const T*>(population_struct + _byteOffset);
82
83 for (uint i=0; i<_vectorSize; i++) {
84 if(!std::isfinite(ptr[i])) {
85 std::string message = "The DataReductionOperator " + this->getName() + " returned a nan or an inf.";
86 bailout(true, message, __FILE__, __LINE__);
87 }
88 }
89
90 return true;
91 }
92
93 protected:
96 uint _popID;
97 std::string _name;
98 };
99
100
101 // Partial specialization for int- and real datatypes
102 template<> bool DataReductionOperatorPopulations<Real>::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
103 dataType = "float";
104 dataSize = sizeof(Real);
105 vectorSize = _vectorSize;
106 return true;
107 }
108 template<> bool DataReductionOperatorPopulations<int>::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
109 dataType = "int";
110 dataSize = sizeof(int);
111 vectorSize = _vectorSize;
112 return true;
113 }
114 template<> bool DataReductionOperatorPopulations<uint>::getDataVectorInfo(std::string& dataType,unsigned int& dataSize,unsigned int& vectorSize) const {
115 dataType = "uint";
116 dataSize = sizeof(uint);
117 vectorSize = _vectorSize;
118 return true;
119 }
120
121} // namespace DRO
122
123#endif /* DRO_POPULATIONS_H */
124
for i
Definition Dispersion.m:24
virtual bool getDataVectorInfo(std::string &dataType, unsigned int &dataSize, unsigned int &vectorSize) const
virtual bool reduceData(const spatial_cell::SpatialCell *cell, char *buffer)
virtual bool reduceDiagnostic(const spatial_cell::SpatialCell *cell, Real *target)
virtual std::string getName() const
virtual bool setSpatialCell(const spatial_cell::SpatialCell *cell)
DataReductionOperatorPopulations(const std::string &name, const uint popID, const unsigned int byteOffset, const unsigned int vectorSize)
Population & get_population(const uint popID)
void bailout(const bool condition, const std::string &message, const char *const file, const int line)
A function to stop the simulation if the boolean condition is true. Raises a flag which gets MPI_Redu...
Definition common.cpp:36
float Real
Definition definitions.h:41