LOOS 4.1.0
The Lightweight Object Oriented Structural analysis library/toolkit
Loading...
Searching...
No Matches
amber_netcdf.hpp
1// (c) 2012 Tod D. Romo, Grossfield Lab, URMC
2
3#if !defined(LOOS_AMBER_NETCDF_HPP)
4#define LOOS_AMBER_NETCDF_HPP
5
6
7#include <istream>
8#include <string>
9#include <netcdf.h>
10
11#include <loos_defs.hpp>
12#include <Coord.hpp>
13#include <Trajectory.hpp>
14#include <exceptions.hpp>
15
16#include <amber_traj.hpp>
17
18namespace loos {
19
20
22 bool isFileNetCDF(const std::string& fname);
23
24
25
26
27
28 namespace {
29
30
31 // These classes handle the template specialization for
32 // determining which nc_get_vara function to call depending on the
33 // desired output type. NetCDF will handle any necessary
34 // conversion from the variable's native format.
35 template<typename T>
36 class VarTypeDecider {
37
38 // This is private to keep arbitrary types from compiling
39 static int read(const int id, const int var, const size_t* st, const size_t *co, T* ip) { return(0); }
40 };
41
42
43
44 // The following are the supported types (based on what GCoord
45 // typically holds...
46
47 template<> class VarTypeDecider<float> {
48 public:
49 static int read(const int id, const int var, const size_t* st, const size_t* co, float* ip) {
50 return(nc_get_vara_float(id, var, st, co, ip));
51 }
52 };
53
54 template<> class VarTypeDecider<double> {
55 public:
56 static int read(const int id, const int var, const size_t* st, const size_t* co, double* ip) {
57 return(nc_get_vara_double(id, var, st, co, ip));
58 }
59 };
60
61
62 }
63
64
65
67 class AmberNetcdf : public Trajectory {
68 public:
69
70
71 // Note: we don't call the base class constructor because we need
72 // to keep it from trying to use an istream (since the C netcdf API
73 // doesn't support this)
74
75 explicit AmberNetcdf(const std::string& s, const uint na)
76 : Trajectory(s),
77 _coord_data(new GCoord::element_type[na*3]),
78 _velocity_data(new GCoord::element_type[na*3]),
79 _box_data(new GCoord::element_type[3]),
80 _periodic(false),
81 _velocities(false),
82 _timestep(1e-12)
83 {
84 cached_first = false;
85 init(s.c_str(), na);
86 }
87
88
89 ~AmberNetcdf() {
90 // ignore the return code since throwing in destructors is bad...
91 nc_close(_ncid);
92
93 delete[] _coord_data;
94 delete[] _box_data;
95 }
96
97 std::string description() const { return("Amber trajectory (netCDF)"); }
98 static pTraj create(const std::string& fname, const AtomicGroup& model) {
99 if (isFileNetCDF(fname))
100 return(pTraj(new AmberNetcdf(fname, model.size())));
101
102 return(pTraj(new AmberTraj(fname, model.size())));
103 }
104
105 uint natoms() const { return(_natoms); }
106 uint nframes() const { return(_nframes); }
107 float timestep() const { return(_timestep); }
108
109 bool hasPeriodicBox() const { return(_periodic); }
110 GCoord periodicBox() const { return(GCoord(_box_data[0], _box_data[1], _box_data[2])); }
111
112 virtual bool hasVelocities() const { return(_velocities); }
113 virtual double velocityConversionFactor() const { return(1.0); }
114
115
116 std::vector<GCoord> coords() const {
117 std::vector<GCoord> res;
118 for (uint i=0; i<_natoms; i += 3)
119 res.push_back(GCoord(_coord_data[i], _coord_data[i+1], _coord_data[i+2]));
120 return(res);
121 }
122
123
124
125 private:
126
127
128
129 void init(const char* name, const uint natoms);
130 void readGlobalAttributes();
131 std::string readGlobalAttribute(const std::string& name);
132 void readRawFrame(const uint frameno);
133
134 void updateGroupCoordsImpl(AtomicGroup& g);
135 void updateGroupVelocitiesImpl(AtomicGroup& g);
136 bool parseFrame();
137 void seekNextFrameImpl() { }
138 void seekFrameImpl(const uint frame) { }
139 void rewindImpl() { }
140
141 std::vector<GCoord> velocitiesImpl() const;
142
143
144 private:
145 GCoord::element_type* _coord_data;
146 GCoord::element_type* _velocity_data;
147 GCoord::element_type* _box_data;
148 bool _periodic;
149 bool _velocities;
150 float _timestep;
151 int _ncid;
152 size_t _nframes;
153 size_t _natoms;
154 int _coord_id;
155 size_t _coord_size;
156 int _cell_lengths_id;
157 int _velocities_id;
158 std::string _title, _application, _program, _programVersion, _conventions, _conventionVersion;
159 };
160
161
162}
163
164
165
166#endif
Class for reading Amber Trajectories in NetCDF format.
Definition amber_netcdf.hpp:67
std::string description() const
Return a string describing trajectory format.
Definition amber_netcdf.hpp:97
uint natoms() const
Definition amber_netcdf.hpp:105
virtual bool hasVelocities() const
Whether or not the trajectory format supports velocities.
Definition amber_netcdf.hpp:112
virtual double velocityConversionFactor() const
Conversion applied to velocities to get to \AA/ps.
Definition amber_netcdf.hpp:113
uint nframes() const
Number of frames in the trajectory.
Definition amber_netcdf.hpp:106
bool hasPeriodicBox() const
Definition amber_netcdf.hpp:109
float timestep() const
Timestep per frame.
Definition amber_netcdf.hpp:107
GCoord periodicBox() const
Returns the periodic box for the current frame/trajectory.
Definition amber_netcdf.hpp:110
std::vector< GCoord > coords() const
Returns the current frames coordinates as a vector of GCoords.
Definition amber_netcdf.hpp:116
Class for reading amber coordinate trajectories.
Definition amber_traj.hpp:48
Class for handling groups of Atoms (pAtoms, actually)
Definition AtomicGroup.hpp:108
Base-class for polymorphic trajectories.
Definition Trajectory.hpp:65
-picker
Namespace for most things not already encapsulated within a class.
Definition version.cpp:3
bool isFileNetCDF(const std::string &fname)
Returns true if the file is a NetCDF file.
Definition amber_netcdf.cpp:9