Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
CellVecData.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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"
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{
64private:
65
69 std::map<std::string, Vec> mCellVecData;
70
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
90public:
91
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
150namespace boost
151{
152namespace serialization
153{
154template<class Archive>
155inline 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
187template<class Archive>
188inline 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_ */
gcov doesn't like this file...
#define CHASTE_CLASS_EXPORT(T)
void serialize(Archive &archive, const unsigned int version)
std::map< std::string, Vec > mCellVecData
virtual ~CellVecData()
unsigned GetNumItems() const
std::vector< std::string > GetKeys() const
Vec GetItem(const std::string &rVariableName) const
void SetItem(const std::string &rVariableName, Vec data)
friend class boost::serialization::access
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)