Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
crbelementsdb.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*-
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <christophe.prudhomme@feelpp.org>
6  Date: 2013-06-16
7 
8  Copyright (C) 2011 Université Joseph Fourier (Grenoble I)
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library 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
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
30 #ifndef __CRBElementsDB_H
31 #define __CRBElementsDB_H 1
32 
33 #include <string>
34 
35 #include <boost/filesystem/operations.hpp>
36 #include <boost/filesystem/convenience.hpp>
37 #include <boost/filesystem/fstream.hpp>
38 
39 #include <boost/serialization/vector.hpp>
40 #include <boost/serialization/list.hpp>
41 #include <boost/serialization/string.hpp>
42 #include <boost/serialization/version.hpp>
43 #include <boost/serialization/split_member.hpp>
44 
45 #include <feel/feelcore/feel.hpp>
46 
47 namespace Feel
48 {
49 
50 template<typename ModelType>
51 class CRBElementsDB : public CRBDB
52 {
53 
54  typedef CRBDB super;
55 
56 public :
57 
58  typedef ModelType model_type;
59  typedef boost::shared_ptr<model_type> model_ptrtype;
60 
62  typedef typename model_type::element_type element_type;
63  typedef typename model_type::element_ptrtype element_ptrtype;
64 
66  typedef typename model_type::mesh_type mesh_type;
67  typedef boost::shared_ptr<mesh_type> mesh_ptrtype;
68 
70  typedef typename model_type::space_type space_type;
71  typedef boost::shared_ptr<space_type> space_ptrtype;
72 
73  typedef std::vector<element_type> wn_type;
74 
76  CRBElementsDB()
77  :
78  super()
79  {
80  }
81 
82  CRBElementsDB( std::string prefixdir,
83  std::string name,
84  std::string dbprefix,
85  po::variables_map const& vm,
86  model_ptrtype const & model )
87  :
88  super( prefixdir,
89  name,
90  dbprefix,
91  vm ),
92  M_N( 0 )
93  {
94  M_model = model;
95  }
96 
97 
99  ~CRBElementsDB()
100  {}
101 
102 
106  void saveDB();
107 
111  bool loadDB();
112 
113 
114  boost::tuple<wn_type, wn_type> wn()
115  {
116  return boost::make_tuple( M_WN , M_WNdu );
117  }
118 
119  void setMN( size_type MN )
120  {
121  M_N = MN;
122  }
123 
124  void setWn( boost::tuple< wn_type, wn_type > WN )
125  {
126  auto primal = WN.template get<0>();
127  auto dual = WN.template get<1>();
128  M_WN = primal;
129  M_WNdu = dual;
130  }
131 
132 private :
133 
134  friend class boost::serialization::access;
135  // When the class Archive corresponds to an output archive, the
136  // & operator is defined similar to <<. Likewise, when the class Archive
137  // is a type of input archive the & operator is defined similar to >>.
138  template<class Archive>
139  void save( Archive & ar, const unsigned int version ) const;
140 
141  template<class Archive>
142  void load( Archive & ar, const unsigned int version ) ;
143 
144  BOOST_SERIALIZATION_SPLIT_MEMBER()
145 
146  size_type M_N;
147 
148  wn_type M_WN;
149  wn_type M_WNdu;
150 
151  model_ptrtype M_model;
152 
153 
154 };//class CRBElementsDB
155 
156 template<typename ModelType>
157 void
158 CRBElementsDB<ModelType>::saveDB()
159 {
160 
161  fs::ofstream ofs( this->dbLocalPath() / this->dbFilename() );
162 
163  if ( ofs )
164  {
165  boost::archive::text_oarchive oa( ofs );
166  // write class instance to archive
167  oa << *this;
168  // archive and stream closed when destructors are called
169  }
170 }
171 
172 template<typename ModelType>
173 bool
174 CRBElementsDB<ModelType>::loadDB()
175 {
176  if( option(_name="crb.rebuild-database").template as<bool>() )
177  return false;
178 
179  if( this->isDBLoaded() )
180  return true;
181 
182  fs::path db = this->lookForDB();
183 
184  if ( db.empty() )
185  return false;
186 
187  if ( !fs::exists( db ) )
188  return false;
189 
190  //std::cout << "Loading " << db << "...\n";
191  fs::ifstream ifs( db );
192 
193  if ( ifs )
194  {
195  boost::archive::text_iarchive ia( ifs );
196  // write class instance to archive
197  ia >> *this;
198  //std::cout << "Loading " << db << " done...\n";
199  this->setIsLoaded( true );
200  // archive and stream closed when destructors are called
201  return true;
202  }
203 
204  return false;
205 }
206 
207 
208 template<typename ModelType>
209 template<class Archive>
210 void
211 CRBElementsDB<ModelType>::save( Archive & ar, const unsigned int version ) const
212 {
213  auto mesh = mesh_type::New();
214  auto is_mesh_loaded = mesh->load( _name="mymesh",_path=this->dbLocalPath(),_type="binary" );
215 
216  if ( ! is_mesh_loaded )
217  {
218  auto first_element = M_WN[0];
219  mesh = first_element.functionSpace()->mesh() ;
220  mesh->save( _name="mymesh",_path=this->dbLocalPath(),_type="binary" );
221  }
222 
223  int size = M_WN.size();
224 
225  LOG( INFO ) << "saving Elements DB";
226  for(int i=0; i<size; i++)
227  ar & BOOST_SERIALIZATION_NVP( M_WN[i] );
228  for(int i=0; i<size; i++)
229  ar & BOOST_SERIALIZATION_NVP( M_WNdu[i] );
230  LOG( INFO ) << "Elements DB saved";
231 }
232 
233 template<typename ModelType>
234 template<class Archive>
235 void
236 CRBElementsDB<ModelType>::load( Archive & ar, const unsigned int version )
237 {
238  LOG( INFO ) << " loading Elements DB ... ";
239 
240  M_WN.resize( M_N );
241  M_WNdu.resize( M_N );
242 
243  mesh_ptrtype mesh;
244  space_ptrtype Xh;
245 
246  if ( !M_model )
247  {
248  LOG(INFO) << "[load] model not initialized, loading fdb files...\n";
249  mesh = mesh_type::New();
250  bool is_mesh_loaded = mesh->load( _name="mymesh",_path=this->dbLocalPath(),_type="binary" );
251  Xh = space_type::New( mesh );
252  LOG(INFO) << "[load] loading fdb files done.\n";
253  }
254  else
255  {
256  LOG(INFO) << "[load] get mesh/Xh from model...\n";
257  mesh = M_model->functionSpace()->mesh();
258  Xh = M_model->functionSpace();
259  LOG(INFO) << "[load] get mesh/Xh from model done.\n";
260  }
261 
262  element_type temp = Xh->element();
263 
264  for( int i = 0 ; i < M_N ; i++ )
265  {
266  temp.setName( (boost::format( "fem-primal-%1%" ) % ( i ) ).str() );
267  ar & BOOST_SERIALIZATION_NVP( temp );
268  M_WN[i] = temp;
269  }
270 
271  for( int i = 0 ; i < M_N ; i++ )
272  {
273  temp.setName( (boost::format( "fem-dual-%1%" ) % ( i ) ).str() );
274  ar & BOOST_SERIALIZATION_NVP( temp );
275  M_WNdu[i] = temp;
276  }
277  LOG( INFO ) << " Elements DB loaded";
278 }
279 
280 
281 
282 
283 }//Feel
284 
285 
286 
287 namespace boost
288 {
289 namespace serialization
290 {
291 template< typename T>
292 struct version< Feel::CRBElementsDB<T> >
293 {
294  // at the moment the version of the CRB DB is 0. if any changes is done
295  // to the format it is mandatory to increase the version number below
296  // and use the new version number of identify the new entries in the DB
297  typedef mpl::int_<0> type;
298  typedef mpl::integral_c_tag tag;
299  static const unsigned int value = version::type::value;
300 };
301 template<typename T> const unsigned int version<Feel::CRBElementsDB<T> >::value;
302 }
303 }
304 
305 #endif /* __CRBElementsDB_H */

Generated on Sun Oct 20 2013 08:24:55 for Feel++ by doxygen 1.8.4