Alsvinn  0.5.3
The fast FVM simulator with UQ support
netcdf_utils.hpp
Go to the documentation of this file.
1 /* Copyright (c) 2018 ETH Zurich, Kjetil Olsen Lye
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #pragma once
17 #include "alsfvm/types.hpp"
19 #include <netcdf.h>
20 
21 #define NETCDF_SAFE_CALL(x) {\
22  auto error = x; \
23  if (error) { \
24  THROW("NetCDF error in call to\n\t" << #x << "\n\nError code: " << error \
25  <<"\n\nError message: " << nc_strerror(error)); \
26  } \
27 }
28 
29 namespace alsfvm {
30 namespace io {
31 typedef int netcdf_raw_ptr;
32 
33 
34 
35 template<class RealType>
36 struct NetCDFType {
37  using type = double;
38 };
39 
40 
41 template<>
42 struct NetCDFType<float> {
43  using type = float;
44 };
45 
47 inline netcdf_raw_ptr getNetcdfRealType() {
48  if (std::is_same<NetCDFType<real>::type, double>::value) {
49  return NC_DOUBLE;
50  } else if ((std::is_same<NetCDFType<real>::type, float>::value)) {
51  return NC_FLOAT;
52  }
53 
54  return NC_DOUBLE;
55 }
56 
58 template<class RealType>
59 typename std::enable_if<std::is_same<RealType, double>::value, int>::type
61  int ncid, int varid, const RealType* op) {
62  return nc_put_var_double(ncid, varid, op);
63 
64 }
65 
67 template<class RealType>
68 typename std::enable_if<std::is_same<RealType, float>::value, int>::type
70  int ncid, int varid, const RealType* op) {
71  return nc_put_var_float(ncid, varid, op);
72 
73 }
74 
75 }
76 
77 }
78 
79 
int netcdf_raw_ptr
Definition: netcdf_utils.hpp:31
Definition: netcdf_utils.hpp:36
netcdf_raw_ptr getNetcdfRealType()
Gets the type corresponding to the alsfvm::real type.
Definition: netcdf_utils.hpp:47
std::enable_if< std::is_same< RealType, double >::value, int >::type nc_put_var_real(int ncid, int varid, const RealType *op)
Wrapper function for nc_put_var_double.
Definition: netcdf_utils.hpp:60
double type
Definition: netcdf_utils.hpp:37
Various utility functions to implement the tecno flux.
Definition: types.hpp:30
float type
Definition: netcdf_utils.hpp:43