aboutsummaryrefslogtreecommitdiff
path: root/CarpetDev/CarpetIOF5/src/data_region.cc
blob: 66f61e8cc6492dccc1064e2a1cdcf11f4349e2eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <cassert>
#include <cstdlib>
#include <sstream>
#include <string>

#include "cctk.h"

#include "carpet.hh"

#include "data_region.hh"
#include "utils.hh"



namespace CarpetIOF5 {
  
  namespace F5 {
    
    using std::ostringstream;
    
    
    
    data_region_t::
    data_region_t (tensor_component_t & tensor_component,
                   bbox<int, dim> const & region)
      : m_tensor_component (tensor_component),
        m_region (region)
    {
      assert (! region.empty());
      
      ostringstream namebuf;
      namebuf << "map=" << Carpet::map << " "
              << "region=" << m_region;
      string const namestr = namebuf.str();
      char const * const name = namestr.c_str();
      assert (name != 0);
      
      int const vartype = CCTK_VarTypeI (m_tensor_component.get_variable());
      assert (vartype >= 0);
      hid_t const hdf5_datatype = hdf5_datatype_from_cactus_datatype (vartype);
      assert (hdf5_datatype >= 0);
      
      m_properties = H5Pcreate (H5P_DATASET_CREATE);
      assert (m_properties >= 0);
      
      vect<hsize_t, dim> const dims
        = (region.shape() / region.stride()).reverse();
      m_dataspace = H5Screate_simple (dim, & dims [0], & dims [0]);
      assert (m_dataspace >= 0);
      
      m_dataset
        = H5Dcreate (m_tensor_component.get_hdf5_tensor_component(), name,
                     hdf5_datatype, m_dataspace, m_properties);
      assert (m_dataset >= 0);
      
      write_or_check_attribute
        (m_dataset, "iorigin", region.lower() / region.stride());
      
      assert (invariant());
    }
    
    
    
    data_region_t::
    ~ data_region_t ()
    {
      herr_t herr;
      
      herr = H5Dclose (m_dataset);
      assert (! herr);
      
      herr = H5Sclose (m_dataspace);
      assert (! herr);
      
      herr = H5Pclose (m_properties);
      assert (! herr);
    }
    
    
    
    tensor_component_t & data_region_t::
    get_tensor_component ()
      const
    {
      return m_tensor_component;
    }
    
    
    
    void data_region_t::
    write (void const * const data,
           int const cactus_datatype)
      const
    {
      hid_t const memory_hdf5_datatype
        = hdf5_datatype_from_cactus_datatype (cactus_datatype);
      assert (memory_hdf5_datatype >= 0);
      
      vect<hsize_t, dim> const dims
        = (m_region.shape() / m_region.stride()).reverse();
      hid_t const memory_dataspace
        = H5Screate_simple (dim, & dims [0], & dims [0]);
      assert (memory_dataspace >= 0);
      
      hid_t const transfer_properties = H5Pcreate (H5P_DATASET_XFER);
      assert (transfer_properties >= 0);
      
      herr_t herr;
      herr
        = H5Dwrite (m_dataset, memory_hdf5_datatype, memory_dataspace,
                    m_dataspace, transfer_properties, data);
      assert (! herr);
      
      herr = H5Pclose (transfer_properties);
      assert (! herr);
      
      herr = H5Sclose (memory_dataspace);
      assert (! herr);
    }
    
    
    
    bool data_region_t::
    invariant ()
      const
    {
      return (! m_region.empty()
              and m_properties >= 0
              and m_dataset >= 0
              and m_dataspace >= 0);
    }
    
  } // namespace F5

} // namespace CarpetIOF5