Reference documentation for deal.II version 9.1.0-pre
utilities.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/gmsh/utilities.h>
17 
18 #include <deal.II/grid/grid_in.h>
19 #include <deal.II/grid/grid_out.h>
20 
21 #include <deal.II/opencascade/utilities.h>
22 
23 #include <cstdio>
24 
25 #ifdef DEAL_II_WITH_GMSH
26 
27 DEAL_II_NAMESPACE_OPEN
28 
29 namespace Gmsh
30 {
32  const double characteristic_length,
33  const std::string &output_base_name)
34  : characteristic_length(characteristic_length)
35  , output_base_name(output_base_name)
36  {}
37 
38 
39 
40  void
42  {
43  prm.add_parameter("Characteristic length", characteristic_length);
44  prm.add_parameter("Intermediate file name base",
46  "Keep empty, if you want the program to generate "
47  "temporary files, and then remove them when they "
48  "are no longer used.");
49  }
50 
51 
52 
53 # ifdef DEAL_II_WITH_OPENCASCADE
54  template <int spacedim>
55  void
56  create_triangulation_from_boundary_curve(const TopoDS_Edge & boundary,
58  const AdditionalParameters &prm)
59  {
60  std::string base_name = prm.output_base_name;
61  char dir_template[] = "ctfbc-XXXXXX";
62  if (base_name == "")
63  {
64  const char *temp = mkdtemp(dir_template);
65  AssertThrow(temp != nullptr,
66  ExcMessage("Creating temporary directory failed!"));
67  base_name = temp;
68  base_name += "tmp";
69  }
70 
71  const std::string iges_file_name = base_name + ".iges";
72  const std::string geo_file_name = base_name + ".geo";
73  const std::string msh_file_name = base_name + ".msh";
74  const std::string log_file_name = base_name + ".log";
75  const std::string warnings_file_name = base_name + "_warn.log";
76 
77  ::OpenCASCADE::write_IGES(boundary, iges_file_name);
78 
79  ofstream geofile;
80  geofile.open(geo_file_name);
81  geofile << "Merge \"" << iges_file_name << "\";" << std::endl
82  << "Line Loop (2) = {1};" << std::endl
83  << "Plane Surface (3) = {2};" << std::endl
84  << "Characteristic Length { 1 } = " << prm.characteristic_length
85  << ";" << std::endl
86  << "Mesh.RecombineAll = 1;" << std::endl
87  << "Mesh.SubdivisionAlgorithm = 1;" << std::endl;
88  geofile.close();
89 
90  std::stringstream command;
91  command << DEAL_II_GMSH_EXECUTABLE_PATH << " -2 " << geo_file_name << " 1> "
92  << log_file_name << " 2> " << warnings_file_name;
93 
94  const auto ret_value = std::system(command.str().c_str());
95  AssertThrow(ret_value == 0,
96  ExcMessage("Gmsh failed to run. Check the " + log_file_name +
97  " file."));
98 
99  std::ifstream grid_file(msh_file_name);
100  Assert(grid_file, ExcIO());
101 
102  GridIn<2, spacedim> gridin;
103  gridin.attach_triangulation(tria);
104  gridin.read_msh(grid_file);
105 
106  if (base_name != prm.output_base_name)
107  {
108  // declaring the list without a type, i.e.,
109  //
110  // auto filenames = {{iges_file_name, geo_file_name, ...}})
111  //
112  // causes internal compiler errors with GCC's concepts implementation,
113  // so give it an explicit type:
114  const std::array<const std::string *, 5> filenames{
115  {&iges_file_name,
116  &geo_file_name,
117  &msh_file_name,
118  &log_file_name,
119  &warnings_file_name}};
120  for (const std::string *filename : filenames)
121  {
122  const auto ret_value = std::remove(filename->c_str());
123  AssertThrow(ret_value == 0,
124  ExcMessage("Failed to remove " + *filename));
125  }
126  const auto ret_value = std::remove(dir_template);
127  AssertThrow(ret_value == 0,
128  ExcMessage("Failed to remove " +
129  std::string(dir_template)));
130  }
131  }
132 # endif
133 
134  // explicit instantiations
135 # ifdef DEAL_II_WITH_OPENCASCADE
136 # include "utilities.inst"
137 # endif
138 } // namespace Gmsh
139 
140 DEAL_II_NAMESPACE_CLOSE
141 
142 #endif
void add_parameter(const std::string &entry, ParameterType &parameter, const std::string &documentation=std::string(), const Patterns::PatternBase &pattern=*Patterns::Tools::Convert< ParameterType >::to_pattern())
void create_triangulation_from_boundary_curve(const TopoDS_Edge &boundary, Triangulation< 2, spacedim > &tria, const AdditionalParameters &prm=AdditionalParameters())
Definition: utilities.cc:56
static::ExceptionBase & ExcIO()
void add_parameters(ParameterHandler &prm)
Definition: utilities.cc:41
#define AssertThrow(cond, exc)
Definition: exceptions.h:1329
std::string output_base_name
Definition: utilities.h:80
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1227
Definition: utilities.h:44
void read_msh(std::istream &in)
Definition: grid_in.cc:1303
void attach_triangulation(Triangulation< dim, spacedim > &tria)
Definition: grid_in.cc:105
AdditionalParameters(const double characteristic_length=1.0, const std::string &output_base_name="")
Definition: utilities.cc:31