30 #ifndef __CRBElementsDB_H
31 #define __CRBElementsDB_H 1
35 #include <boost/filesystem/operations.hpp>
36 #include <boost/filesystem/convenience.hpp>
37 #include <boost/filesystem/fstream.hpp>
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>
45 #include <feel/feelcore/feel.hpp>
50 template<
typename ModelType>
51 class CRBElementsDB :
public CRBDB
58 typedef ModelType model_type;
59 typedef boost::shared_ptr<model_type> model_ptrtype;
62 typedef typename model_type::element_type element_type;
63 typedef typename model_type::element_ptrtype element_ptrtype;
66 typedef typename model_type::mesh_type mesh_type;
67 typedef boost::shared_ptr<mesh_type> mesh_ptrtype;
70 typedef typename model_type::space_type space_type;
71 typedef boost::shared_ptr<space_type> space_ptrtype;
73 typedef std::vector<element_type> wn_type;
82 CRBElementsDB( std::string prefixdir,
85 po::variables_map
const& vm,
86 model_ptrtype
const & model )
114 boost::tuple<wn_type, wn_type> wn()
116 return boost::make_tuple( M_WN , M_WNdu );
124 void setWn( boost::tuple< wn_type, wn_type > WN )
126 auto primal = WN.template get<0>();
127 auto dual = WN.template get<1>();
134 friend class boost::serialization::access;
138 template<
class Archive>
139 void save( Archive & ar,
const unsigned int version )
const;
141 template<
class Archive>
142 void load( Archive & ar,
const unsigned int version ) ;
144 BOOST_SERIALIZATION_SPLIT_MEMBER()
151 model_ptrtype M_model;
156 template<typename ModelType>
158 CRBElementsDB<ModelType>::saveDB()
165 boost::archive::text_oarchive oa( ofs );
172 template<
typename ModelType>
174 CRBElementsDB<ModelType>::loadDB()
176 if( option(_name=
"crb.rebuild-database").
template as<bool>() )
187 if ( !fs::exists( db ) )
191 fs::ifstream ifs( db );
195 boost::archive::text_iarchive ia( ifs );
199 this->setIsLoaded(
true );
208 template<
typename ModelType>
209 template<
class Archive>
211 CRBElementsDB<ModelType>::save( Archive & ar,
const unsigned int version )
const
213 auto mesh = mesh_type::New();
214 auto is_mesh_loaded = mesh->load( _name=
"mymesh",_path=this->
dbLocalPath(),_type=
"binary" );
216 if ( ! is_mesh_loaded )
218 auto first_element = M_WN[0];
219 mesh = first_element.functionSpace()->mesh() ;
220 mesh->save( _name=
"mymesh",_path=this->
dbLocalPath(),_type=
"binary" );
223 int size = M_WN.size();
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";
233 template<
typename ModelType>
234 template<
class Archive>
236 CRBElementsDB<ModelType>::load( Archive & ar,
const unsigned int version )
238 LOG( INFO ) <<
" loading Elements DB ... ";
241 M_WNdu.resize( M_N );
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";
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";
262 element_type temp = Xh->element();
264 for(
int i = 0 ; i < M_N ; i++ )
266 temp.setName( (boost::format(
"fem-primal-%1%" ) % ( i ) ).str() );
267 ar & BOOST_SERIALIZATION_NVP( temp );
271 for(
int i = 0 ; i < M_N ; i++ )
273 temp.setName( (boost::format(
"fem-dual-%1%" ) % ( i ) ).str() );
274 ar & BOOST_SERIALIZATION_NVP( temp );
277 LOG( INFO ) <<
" Elements DB loaded";
289 namespace serialization
291 template<
typename T>
292 struct version< Feel::CRBElementsDB<T> >
297 typedef mpl::int_<0> type;
298 typedef mpl::integral_c_tag tag;
299 static const unsigned int value = version::type::value;
301 template<
typename T>
const unsigned int version<Feel::CRBElementsDB<T> >::value;