Athena++/Atmosphere
Planetary Atmosphere Simulator
utils.hpp
Go to the documentation of this file.
1 #ifndef UTILS_UTILS_HPP_
2 #define UTILS_UTILS_HPP_
3 //========================================================================================
4 // Athena++ astrophysical MHD code
5 // Copyright(C) 2014 James M. Stone <jmstone@princeton.edu> and other code contributors
6 // Licensed under the 3-clause BSD License, see LICENSE file for details
7 //========================================================================================
9 // \brief prototypes of functions and class definitions for utils/*.cpp files
10 
11 // C headers
12 
13 // C++ headers
14 #include <csignal> // sigset_t POSIX C extension
15 #include <cstdint> // std::int64_t
16 #include <vector>
17 #include <string>
18 #include <cstring>
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 
23 // Athena++ headers
24 #include "../athena.hpp"
25 
26 void ChangeRunDir(const char *pdir);
27 double ran2(std::int64_t *idum);
28 void ShowConfig();
29 
30 //----------------------------------------------------------------------------------------
32 // \brief static data and functions that implement a simple signal handling system
33 
34 namespace SignalHandler {
35 const int nsignal = 3;
36 static volatile int signalflag[nsignal];
37 const int ITERM = 0, IINT = 1, IALRM = 2;
38 static sigset_t mask;
41 int GetSignalFlag(int s);
42 void SetSignalFlag(int s);
43 void SetWallTimeAlarm(int t);
45 } // namespace SignalHandler
46 
48 bool FileExists(std::string fname);
49 
51 bool IsBlankLine(char const* line);
52 bool IsBlankLine(std::string const& line);
53 
55 std::string DecommentFile(std::string fname);
56 
58 int GetNumCols(std::string fname, char c = ' ');
59 
61 int GetNumRows(std::string fname);
62 
64 template<typename A>
65 std::vector<A> Vectorize(const char* cstr, const char *delimiter = " ")
66 {
67  std::vector<A> arr;
68  char str[1028], *p;
69  strcpy(str, cstr);
70  p = std::strtok(str, delimiter);
71  while (p != NULL) {
72  arr.push_back(static_cast<A>(std::stof(p)));
73  p = std::strtok(NULL, delimiter);
74  }
75  return arr;
76 }
77 
78 template<>
79 std::vector<std::string> Vectorize(const char* cstr, const char *delimiter);
80 
82 void replaceChar(char* buf, char c_old, char c_new);
83 
84 template<typename T>
85 void NewCArray(T** &a, int n1, int n2)
86 {
87  a = new T* [n1];
88  a[0] = new T [n1*n2];
89 
90  for (int i = 0; i < n1; ++i)
91  a[i] = a[0] + i*n2;
92 }
93 
94 template<typename T>
95 void FreeCArray(T **a)
96 {
97  delete[] a[0];
98  delete[] a;
99 }
100 
101 template<typename T>
102 void FreeCArray2(T **a)
103 {
104  delete[] a[0];
105  delete[] a;
106 }
107 
108 template<typename T>
109 void NewCArray(T*** &a, int n1, int n2, int n3)
110 {
111  a = new T** [n1];
112  a[0] = new T* [n1*n2];
113  a[0][0] = new T [n1*n2*n3];
114 
115  for (int i = 0; i < n1; ++i) {
116  a[i] = a[0] + i*n2;
117  for (int j = 0; j < n2; ++j)
118  a[i][j] = a[0][0] + i*n2*n3 + j*n3;
119  }
120 }
121 
122 template<typename T>
123 void FreeCArray(T ***a)
124 {
125  delete[] a[0][0];
126  delete[] a[0];
127  delete[] a;
128 }
129 
130 template<typename T>
131 void FreeCArray3(T ***a)
132 {
133  delete[] a[0][0];
134  delete[] a[0];
135  delete[] a;
136 }
137 
138 template<typename T>
139 void NewCArray(T**** &a, int n1, int n2, int n3, int n4)
140 {
141  a = new T*** [n1];
142  a[0] = new T** [n1*n2];
143  a[0][0] = new T* [n1*n2*n3];
144  a[0][0][0] = new T [n1*n2*n3*n4];
145 
146  for (int i = 0; i < n1; ++i) {
147  a[i] = a[0] + i*n2;
148  for (int j = 0; j < n2; ++j) {
149  a[i][j] = a[0][0] + i*n2*n3 + j*n3;
150  for (int k = 0; k < n3; ++k)
151  a[i][j][k] = a[0][0][0] + i*n2*n3*n4 + j*n3*n4 + k*n4;
152  }
153  }
154 }
155 
156 template<typename T>
157 void FreeCArray(T ****a)
158 {
159  delete[] a[0][0][0];
160  delete[] a[0][0];
161  delete[] a[0];
162  delete[] a;
163 }
164 
165 template<typename T>
166 void FreeCArray4(T ****a)
167 {
168  delete[] a[0][0][0];
169  delete[] a[0][0];
170  delete[] a[0];
171  delete[] a;
172 }
173 
174 template <typename T> class AthenaArray;
175 
176 char* StripLine(char *line);
177 char* NextLine(char *line, int num, FILE* stream);
178 void read_data_table(char const *fname, double** data, int *rows, int *cols);
179 void ReadDataTable(AthenaArray<Real> &data, std::string fname, char c = ' ');
180 
181 #endif // UTILS_UTILS_HPP_
#define A(i, j)
Definition: band_back_sub.c:13
SignalHandler.
Definition: utils.hpp:34
const int nsignal
Definition: utils.hpp:35
void SetSignalFlag(int s)
int GetSignalFlag(int s)
const int ITERM
Definition: utils.hpp:37
static sigset_t mask
Definition: utils.hpp:38
const int IALRM
Definition: utils.hpp:37
void SetWallTimeAlarm(int t)
void SignalHandlerInit()
const int IINT
Definition: utils.hpp:37
int CheckSignalFlags()
static volatile int signalflag[nsignal]
Definition: utils.hpp:36
void CancelWallTimeAlarm()
void FreeCArray3(T ***a)
Definition: utils.hpp:131
std::string DecommentFile(std::string fname)
decomment a file
Definition: utils.cpp:29
void FreeCArray(T **a)
Definition: utils.hpp:95
std::vector< A > Vectorize(const char *cstr, const char *delimiter=" ")
split a string to a vector
Definition: utils.hpp:65
void ChangeRunDir(const char *pdir)
void FreeCArray4(T ****a)
Definition: utils.hpp:166
int GetNumRows(std::string fname)
get number of rows in a data table
Definition: utils.cpp:64
bool FileExists(std::string fname)
test file existance
Definition: utils.cpp:9
char * StripLine(char *line)
Definition: utils.cpp:96
void replaceChar(char *buf, char c_old, char c_new)
replace a character in a string
Definition: utils.cpp:88
void FreeCArray2(T **a)
Definition: utils.hpp:102
char * NextLine(char *line, int num, FILE *stream)
Definition: utils.cpp:112
double ran2(std::int64_t *idum)
int GetNumCols(std::string fname, char c=' ')
get number of columns in a data table
Definition: utils.cpp:51
void NewCArray(T **&a, int n1, int n2)
Definition: utils.hpp:85
bool IsBlankLine(char const *line)
test a blank line
Definition: utils.cpp:15
void ReadDataTable(AthenaArray< Real > &data, std::string fname, char c=' ')
void ShowConfig()
void read_data_table(char const *fname, double **data, int *rows, int *cols)