Chaste  Release::3.4
CellVecData.hpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef CELLVECDATA_HPP_
37 #define CELLVECDATA_HPP_
38 
39 #include <boost/shared_ptr.hpp>
40 #include <map>
41 #include <string>
42 #include <vector>
43 
44 #include <petscvec.h>
45 
46 #include "AbstractCellProperty.hpp"
47 #include "ChasteSerialization.hpp"
48 #include <boost/serialization/base_object.hpp>
49 #include <boost/serialization/map.hpp>
50 #include <boost/serialization/array.hpp>
51 #include "Exception.hpp"
52 #include "ArchiveLocationInfo.hpp"
53 
54 
63 {
64 private:
65 
69  std::map<std::string, Vec> mCellVecData;
70 
74  bool mFreeVecs;
75 
84  template<class Archive>
85  void serialize(Archive & archive, const unsigned int version)
86  {
87  archive & boost::serialization::base_object<AbstractCellProperty>(*this);
88  }
89 
90 public:
91 
95  CellVecData();
96 
101  CellVecData(const CellVecData& rAnotherCellVecData);
102 
107  CellVecData(const std::map<std::string, Vec>& rCellVecDataMap);
108 
113  virtual ~CellVecData();
114 
121  void SetItem(const std::string& rVariableName, Vec data);
122 
130  Vec GetItem(const std::string& rVariableName) const;
131 
135  unsigned GetNumItems() const;
136 
142  std::vector<std::string> GetKeys() const;
143 };
144 
146 // Declare identifier for the serializer
148 
149 
150 namespace boost
151 {
152 namespace serialization
153 {
154 template<class Archive>
155 inline void save_construct_data(
156  Archive & ar, const CellVecData * t, const unsigned int file_version)
157 {
158  unsigned map_size = t->GetNumItems();
159  ar << map_size;
160 
161  std::vector<std::string> keys = t->GetKeys();
162 
163  for (std::vector<std::string>::iterator iter = keys.begin(); iter != keys.end(); ++iter)
164  {
165  std::string key = *iter;
166  ar << key;
167  Vec vec_data = t->GetItem(key);
168 
169  // Machinery for archiving a sequential PETSc Vec as a boost make_array.
170  double *p_vec_data;
171  VecGetArray(vec_data, &p_vec_data);
172  PetscInt size, local_size;
173  VecGetSize(vec_data, &size);
174  VecGetLocalSize(vec_data, &local_size);
175  // This will fail if we run in parallel and the vector is shared MPI (size is global)
176  assert( local_size == size);
177  ar << size;
178  ar << make_array(p_vec_data, size);
179  VecRestoreArray(vec_data, &p_vec_data);
180 
181  //std::string archive_filename = ArchiveLocationInfo::GetArchiveDirectory() + key + ".vec";
182  //PetscTools::DumpPetscObject(t->GetItem(key), archive_filename);
183  }
184 
185 }
186 
187 template<class Archive>
188 inline void load_construct_data(
189  Archive & ar, CellVecData * t, const unsigned int file_version)
190 {
191  unsigned map_size;
192  ar >> map_size;
193 
194  std::map<std::string, Vec> archived_cell_vec_data;
195  for (unsigned map_entry = 0; map_entry < map_size; ++map_entry)
196  {
197  std::string key;
198  ar >> key;
199 
200  // Un-archive vector data and construct a new PETSc Vec for it
201  PetscInt size;
202  ar >> size;
203  Vec archived_vec = PetscTools::CreateVec(size);
204  double *p_archived_vec;
205  VecGetArray(archived_vec, &p_archived_vec);
206  ar >> make_array<double>(p_archived_vec, size);
207  VecRestoreArray(archived_vec, &p_archived_vec);
208 
209  //std::string archive_filename = ArchiveLocationInfo::GetArchiveDirectory() + key + ".vec";
210  //PetscTools::ReadPetscObject(archived_vec, archive_filename);
211 
212  archived_cell_vec_data[key] = archived_vec;
213  }
214 
215  ::new(t)CellVecData(archived_cell_vec_data);
216 }
217 }
218 }
219 
220 #endif /* CELLVECDATA_HPP_ */
std::map< std::string, Vec > mCellVecData
Definition: CellVecData.hpp:69
Vec GetItem(const std::string &rVariableName) const
Definition: CellVecData.cpp:84
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
Definition: PetscTools.cpp:214
void serialize(Archive &archive, const unsigned int version)
Definition: CellVecData.hpp:85
friend class boost::serialization::access
Definition: CellVecData.hpp:77
void SetItem(const std::string &rVariableName, Vec data)
Definition: CellVecData.cpp:79
std::vector< std::string > GetKeys() const
virtual ~CellVecData()
Definition: CellVecData.cpp:67
#define CHASTE_CLASS_EXPORT(T)
unsigned GetNumItems() const