19#include "BDSArray2DCoords.hh"
21#include "BDSException.hh"
22#include "BDSFieldLoaderPoisson.hh"
23#include "BDSFieldValue.hh"
27#include "CLHEP/Units/SystemOfUnits.h"
38#include "src-external/gzstream/gzstream.h"
56 bool validFile = file.rdbuf()->is_open();
58 bool validFile = file.is_open();
61 {
throw BDSException(__METHOD_NAME__,
"Invalid file name or no such file named \"" + fileName +
"\"");}
63 {G4cout <<
"Loading \"" << fileName <<
"\"" << G4endl;}
75 std::vector<G4String> keys;
76 std::vector<G4String> units;
79 G4bool intoData =
false;
80 G4bool pastNStep =
false;
81 G4bool dataFinished =
false;
86 while (std::getline(file, line))
90 if (std::all_of(line.begin(), line.end(), isspace))
99 std::istringstream liness(line);
101 std::vector<G4float> lineData(nColumns);
102 for (G4int i = 0; i < nColumns; ++i)
112 (*result)(indX,indY) = fv;
132 std::regex min(
"^.*?\\(Xmin,Ymin\\)\\s*=\\s*", std::regex_constants::icase);
133 if (std::regex_search(line, min))
135 std::regex minValues(
"\\(([0-9eE.+-]+),([0-9eE.+-]+)\\)");
137 std::regex_search(line, match, minValues);
138 xMin = std::stod(match[1])*CLHEP::cm;
139 yMin = std::stod(match[2])*CLHEP::cm;
144 std::regex max(
"^.*?\\(Xmax,Ymax\\)\\s*=\\s*", std::regex_constants::icase);
145 if (std::regex_search(line, max))
147 std::regex maxValues(
"\\(([0-9eE.+-]+),([0-9eE.+-]+)\\)");
149 std::regex_search(line, match, maxValues);
150 xMax = std::stod(match[1])*CLHEP::cm;
151 yMax = std::stod(match[2])*CLHEP::cm;
156 std::regex incr(
"\\s*X\\s*and\\s*Y", std::regex_constants::icase);
157 if (std::regex_search(line, incr))
159 std::regex nStepValues(
":\\s*([0-9eE.+-]+)\\s*([0-9eE.+-]+)");
161 std::regex_search(line, match, nStepValues);
162 nX = std::stoi(match[1]) + 1;
163 nY = std::stoi(match[2]) + 1;
169 std::regex noNumbers(
"^(\\D)*$");
170 G4bool noNumbersR = std::regex_search(line, noNumbers);
171 G4bool hasParenthesis = (line.find(
"(") != std::string::npos) || (line.find(
")") != std::string::npos);
172 G4bool hasNoParenthesis = !hasParenthesis;
173 if (pastNStep && noNumbersR && hasNoParenthesis)
175 std::istringstream liness(line);
176 for (G4String key; liness >> key;)
177 {keys.push_back(key);}
178 nColumns = G4int(std::distance(keys.begin(), keys.end()));
183 if (pastNStep && noNumbersR && hasParenthesis)
186 std::regex unitsRE(
"[\\(](\\w+\\-*\\/*\\w*)[\\)]");
188 std::regex_search(line, match, unitsRE);
189 for (
const auto& u : match)
190 {units.push_back(G4String(u));}
200 {
throw BDSException(__METHOD_NAME__,
"Invalid file contents - no column header found");}
2D array with spatial mapping derived from BDSArray4DCoords.
General exception with possible name of object and message.
Loader for 2D Poisson SuperFish SF7 files.
BDSArray2DCoords * LoadMag2D(G4String fileName)
Load the 2D array of 3-Vector field values.