LifeV
DOFInterfaceIO.cpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5 Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6 Copyright (C) 2010, 2011, 2012 EPFL, Politecnico di Milano, Emory University
7 
8 This file is part of LifeV.
9 
10 LifeV is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14 
15 LifeV is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief Class that handles I/O of fluid-solid DOF interfaces in FSI
30 
31  @date 2013-04-30
32  @author Radu Popescu <radu.popescu@epfl.ch>
33  @maintainer Radu Popescu <radu.popescu@epfl.ch>
34 */
35 
36 #include <sstream>
37 #include <map>
38 #include <algorithm>
39 
40 #include <lifev/fsi_blocks/filter/DOFInterfaceIO.hpp>
41 
42 #ifdef LIFEV_HAS_HDF5
43 #ifdef HAVE_MPI
44 
45 namespace LifeV
46 {
47 
48 DOFInterfaceIO::DOFInterfaceIO (const std::string& fileName,
49  const commPtr_Type& comm) :
50  M_comm (comm),
51  M_fileName (fileName)
52 {
53  M_myRank = M_comm->MyPID();
54 }
55 
56 void DOFInterfaceIO::write (const interfaceVectorPtr_Type& interfaces)
57 {
58  M_HDF5IO.openFile (M_fileName, M_comm, false);
59 
60  const UInt numParts = interfaces->size();
61  std::vector<UInt> sizes (numParts, 0);
62 
63  UInt maxPoints = 0;
64  for (UInt i = 0; i < numParts; ++i)
65  {
66  interface_Type& curIf = * (interfaces->at (i) );
67  UInt curSize = curIf.localDofMap().size();
68  sizes[i] = curSize;
69  if (maxPoints < curSize)
70  {
71  maxPoints = curSize;
72  }
73  }
74 
75  hsize_t sizeSpaceDims[2] = {numParts, 1};
76  hsize_t sizeCount[2] = {1, 1};
77 
78  hsize_t currentSpaceDims[2];
79  hsize_t currentCount[2];
80  currentSpaceDims[0] = numParts;
81  currentSpaceDims[1] = maxPoints;
82  currentCount[0] = 1;
83  currentCount[1] = currentSpaceDims[1];
84 
85  // Create a mini table storing the size of each interface map
86  M_HDF5IO.createTable ("Sizes", H5T_STD_U32BE, sizeSpaceDims);
87 
88  M_HDF5IO.createTable ("Keys", H5T_STD_U32BE, currentSpaceDims);
89  M_HDF5IO.createTable ("Values", H5T_STD_U32BE, currentSpaceDims);
90 
91  M_keyBuffer.resize (currentCount[0] * currentCount[1], 0);
92  M_valueBuffer.resize (currentCount[0] * currentCount[1], 0);
93 
94  for (UInt i = 0; i < numParts; ++i)
95  {
96  const std::map<UInt, UInt>& curMap =
97  interfaces->at (i)->localDofMap();
98  UInt j = 0;
99  for (std::map<UInt, UInt>::const_iterator it = curMap.begin();
100  it != curMap.end(); ++it)
101  {
102  M_keyBuffer[j] = it->first;
103  M_valueBuffer[j] = it->second;
104  ++j;
105  }
106 
107  hsize_t sizeOffset[2] = {i, 0};
108  M_HDF5IO.write ("Sizes", H5T_NATIVE_UINT, sizeCount,
109  sizeOffset, &sizes[i]);
110 
111  hsize_t currentOffset[2] = {i* currentCount[0], 0};
112  M_HDF5IO.write ("Keys", H5T_NATIVE_UINT, currentCount,
113  currentOffset, &M_keyBuffer[0]);
114  M_HDF5IO.write ("Values", H5T_NATIVE_UINT, currentCount,
115  currentOffset, &M_valueBuffer[0]);
116  }
117 
118  M_HDF5IO.closeTable ("Sizes");
119  M_HDF5IO.closeTable ("Keys");
120  M_HDF5IO.closeTable ("Values");
121 
122  M_HDF5IO.closeFile();
123 
124  M_keyBuffer.resize (0);
125  M_valueBuffer.resize (0);
126 }
127 
128 void DOFInterfaceIO::read (dofMapPtr_Type& interface)
129 {
130  interface.reset (new dofMap_Type() );
131 
132  M_HDF5IO.openFile (M_fileName, M_comm, true);
133 
134  hsize_t sizeSpaceDims[2];
135 
136  M_HDF5IO.openTable ("Sizes", sizeSpaceDims);
137 
138  UInt mySize;
139  hsize_t sizeCount[2] = {1, 1};
140  hsize_t sizeOffset[2] = {M_myRank, 0};
141  M_HDF5IO.read ("Sizes", H5T_NATIVE_UINT, sizeCount, sizeOffset,
142  &mySize);
143 
144  M_HDF5IO.closeTable ("Sizes");
145 
146  hsize_t currentSpaceDims[2];
147  hsize_t currentCount[2];
148  hsize_t currentOffset[2];
149 
150  M_HDF5IO.openTable ("Keys", currentSpaceDims);
151  M_HDF5IO.openTable ("Values", currentSpaceDims);
152 
153  currentCount[0] = 1;
154  currentCount[1] = currentSpaceDims[1];
155  currentOffset[0] = currentCount[0] * M_myRank;
156  currentOffset[1] = 0;
157 
158  M_keyBuffer.resize (currentCount[0] * currentCount[1], 0);
159  M_valueBuffer.resize (currentCount[0] * currentCount[1], 0);
160 
161  M_HDF5IO.read ("Keys", H5T_NATIVE_UINT, currentCount, currentOffset,
162  &M_keyBuffer[0]);
163  M_HDF5IO.read ("Values", H5T_NATIVE_UINT, currentCount, currentOffset,
164  &M_valueBuffer[0]);
165 
166  M_HDF5IO.closeTable ("Keys");
167  M_HDF5IO.closeTable ("Values");
168 
169  M_HDF5IO.closeFile();
170 
171  for (UInt i = 0; i < mySize; ++i)
172  {
173  interface->insert (std::make_pair (M_keyBuffer[i], M_valueBuffer[i]) );
174  }
175 
176  M_keyBuffer.resize (0);
177  M_valueBuffer.resize (0);
178 }
179 
180 } /* namespace LifeV */
181 
182 #endif /* HAVE_MPI */
183 #endif /* LIFEV_HAS_HDF5 */