RINGMesh  Version 5.0.0
A programming library for geological model meshes
io_aster.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2017, Association Scientifique pour la Geologie et ses
3  * Applications (ASGA). All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ASGA nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASGA BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * http://www.ring-team.org
28  *
29  * RING Project
30  * Ecole Nationale Superieure de Geologie - GeoRessources
31  * 2 Rue du Doyen Marcel Roubault - TSA 70605
32  * 54518 VANDOEUVRE-LES-NANCY
33  * FRANCE
34  */
35 
36 namespace {
37  static const std::string tet_name_in_aster_mail_file = "TETRA4";
38  static const std::string hex_name_in_aster_mail_file = "HEXA10";
39  static const std::string prism_name_in_aster_mail_file = "PENTA6";
40  static const std::string pyr_name_in_aster_mail_file = "PYRAM5";
41 
42  static const std::string* cell_name_in_aster_mail_file[4] = {
43  &tet_name_in_aster_mail_file, &hex_name_in_aster_mail_file,
44  &prism_name_in_aster_mail_file, &pyr_name_in_aster_mail_file };
45 
46  static const std::string triangle_name_in_aster_mail_file = "TRIA3";
47  static const std::string quad_name_in_aster_mail_file = "QUAD4";
48 
49  static const std::string* polygon_name_in_aster_mail_file[2] = {
50  &triangle_name_in_aster_mail_file, &quad_name_in_aster_mail_file };
67  class AsterIOHandler final: public GeoModelIOHandler< 3 > {
68  public:
69  void load( const std::string& filename, GeoModel3D& geomodel ) final
70  {
71  ringmesh_unused( filename );
72  ringmesh_unused( geomodel );
73  throw RINGMeshException( "I/O",
74  "Loading of a GeoModel from Code_Aster mesh not implemented yet" );
75  }
76  void save( const GeoModel3D& geomodel, const std::string& filename ) final
77  {
78  std::ofstream out( filename.c_str() );
79  out.precision( 16 );
80  const RINGMesh::GeoModelMesh3D& geomodel_mesh = geomodel.mesh;
81 
82  write_title( out, geomodel );
83  write_vertices( out, geomodel_mesh );
84  write_cells( geomodel, out );
85  write_polygons( geomodel, out );
86  write_regions( geomodel, out );
87  write_interfaces( geomodel, out );
88  out << "FIN" << EOL;
89  out << std::flush;
90  }
91 
92  private:
93 
94  void write_title( std::ofstream& out, const GeoModel3D& geomodel ) const
95  {
96  out << "TITRE" << EOL;
97  out << geomodel.name() << EOL;
98  out << "FINSF" << EOL;
99  }
100  void write_vertices(
101  std::ofstream& out,
102  const RINGMesh::GeoModelMesh3D& geomodel_mesh ) const
103  {
104  out << "COOR_3D" << EOL;
105  for( auto v : range( geomodel_mesh.vertices.nb() ) ) {
106  out << "V" << v << " " << geomodel_mesh.vertices.vertex( v ) << EOL;
107  }
108  out << "FINSF" << EOL;
109  }
110 
111  void write_cells( const GeoModel3D& geomodel, std::ofstream& out ) const
112  {
113  const GeoModelMesh3D& geomodel_mesh = geomodel.mesh;
114  for( auto r : range( geomodel.nb_regions() ) ) {
115  // -1 Because connectors doesn't exist in aster
116  for( auto ct : range( GEO::MESH_NB_CELL_TYPES - 1 ) ) {
117  if( geomodel_mesh.cells.nb_cells( r, CellType( ct ) ) > 0 ) {
118  write_cells_in_region( CellType( ct ), r, geomodel_mesh,
119  out );
120  }
121  }
122  }
123  }
124 
125  void write_polygons( const GeoModel3D& geomodel, std::ofstream& out ) const
126  {
127  const GeoModelMesh3D& geomodel_mesh = geomodel.mesh;
128  for( const auto& surface : surface_range < 3 > ( geomodel ) ) {
129  // -1 because polygons doesn' t exist in aster
130  for( auto pt : range(
131  to_underlying_type( PolygonType::UNDEFINED ) - 1 ) ) {
132  if( geomodel_mesh.polygons.nb_polygons( surface.index(),
133  PolygonType( pt ) ) > 0 ) {
134  write_polygons_in_interface( PolygonType( pt ),
135  surface.index(), geomodel_mesh, out );
136  }
137  }
138  }
139  }
140  void write_cells_in_region(
141  const CellType& cell_type,
142  index_t region,
143  const GeoModelMesh3D& geomodel_mesh,
144  std::ofstream& out ) const
145  {
146  out << *cell_name_in_aster_mail_file[to_underlying_type( cell_type )]
147  << EOL;
148  for( auto c : range(
149  geomodel_mesh.cells.nb_cells( region, cell_type ) ) ) {
150  index_t global_id = geomodel_mesh.cells.cell( region, c, cell_type );
151  out << "C" << global_id << " ";
152  for( auto v : range( geomodel_mesh.cells.nb_vertices( c ) ) ) {
153  out << "V"
154  << geomodel_mesh.cells.vertex(
155  ElementLocalVertex( global_id, v ) ) << " ";
156  }
157  out << EOL;
158  }
159  out << "FINSF" << EOL;
160  }
161 
162  void write_polygons_in_interface(
163  const PolygonType& polygon_type,
164  index_t surface,
165  const RINGMesh::GeoModelMesh3D& mesh,
166  std::ofstream& out ) const
167  {
168  out
169  << *polygon_name_in_aster_mail_file[to_underlying_type(
170  polygon_type )] << EOL;
171  for( auto p : range(
172  mesh.polygons.nb_polygons( surface, polygon_type ) ) ) {
173  index_t global_id = mesh.polygons.polygon( surface, p,
174  polygon_type );
175  out << "F" << global_id << " ";
176  for( auto v : range( mesh.polygons.nb_vertices( p ) ) ) {
177  out << "V"
178  << mesh.polygons.vertex( ElementLocalVertex( global_id, v ) )
179  << " ";
180  }
181  out << EOL;
182  }
183  out << "FINSF" << EOL;
184  }
185 
186  void write_regions( const GeoModel3D& geomodel, std::ofstream& out ) const
187  {
188  for( const auto& region : region_range < 3 > ( geomodel ) ) {
189  if( region.is_meshed() ) {
190  out << "GROUP_MA" << EOL;
191  out << region.name() << EOL;
192  for( auto c : range(
193  geomodel.mesh.cells.nb_cells( region.index() ) ) ) {
194  out << "C" << geomodel.mesh.cells.cell( region.index(), c )
195  << EOL;
196  }
197  out << "FINSF" << EOL;
198  }
199  }
200  }
201 
202  void write_interfaces( const GeoModel3D& geomodel, std::ofstream& out ) const
203  {
204  for( auto& cur_interface : geomodel.geol_entities(
205  Interface3D::type_name_static() ) ) {
206  for( auto s : range( cur_interface.nb_children() ) ) {
207  index_t surface_id = cur_interface.child( s ).index();
208  out << "GROUP_MA" << EOL;
209  out << cur_interface.name() << "_" << s << EOL;
210  for( auto p : range(
211  geomodel.mesh.polygons.nb_polygons( surface_id ) ) ) {
212  out << "F" << geomodel.mesh.polygons.polygon( surface_id, p )
213  << EOL;
214  }
215  out << "FINSF" << EOL;
216  }
217 
218  out << "GROUP_MA" << EOL;
219  out << cur_interface.name() << EOL;
220  for( auto s : range( cur_interface.nb_children() ) ) {
221  index_t surface_id = cur_interface.child( s ).index();
222  for( auto p : range(
223  geomodel.mesh.polygons.nb_polygons( surface_id ) ) ) {
224  out << "F" << geomodel.mesh.polygons.polygon( surface_id, p )
225  << EOL;
226  }
227  }
228  out << "FINSF" << EOL;
229  }
230  }
231  };
232 
233 }
void ringmesh_unused(const T &)
Definition: common.h:105
auto to_underlying_type(Enum e) -> typename std::underlying_type< Enum >::type
Definition: types.h:114
CellType
Definition: types.h:89
PolygonType
Definition: types.h:105
const char EOL
Definition: io.h:45