Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
utilities.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2018 - 2019 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 
22 
23 #include <cstdio>
24 
25 #ifdef DEAL_II_WITH_GMSH
26 
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.empty())
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  std::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 
141 
142 #endif
StandardExceptions::ExcIO
static ::ExceptionBase & ExcIO()
Gmsh
Definition: utilities.h:44
Gmsh::AdditionalParameters::output_base_name
std::string output_base_name
Definition: utilities.h:80
GridIn::attach_triangulation
void attach_triangulation(Triangulation< dim, spacedim > &tria)
Definition: grid_in.cc:106
Triangulation
Definition: tria.h:1109
grid_in.h
utilities.h
Gmsh::AdditionalParameters
Definition: utilities.h:51
StandardExceptions::ExcMessage
static ::ExceptionBase & ExcMessage(std::string arg1)
Gmsh::create_triangulation_from_boundary_curve
void create_triangulation_from_boundary_curve(const TopoDS_Edge &boundary, Triangulation< 2, spacedim > &tria, const AdditionalParameters &prm=AdditionalParameters())
Definition: utilities.cc:56
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
Gmsh::AdditionalParameters::add_parameters
void add_parameters(ParameterHandler &prm)
Definition: utilities.cc:41
Gmsh::AdditionalParameters::AdditionalParameters
AdditionalParameters(const double characteristic_length=1.0, const std::string &output_base_name="")
Definition: utilities.cc:31
Assert
#define Assert(cond, exc)
Definition: exceptions.h:1419
Gmsh::AdditionalParameters::characteristic_length
double characteristic_length
Definition: utilities.h:72
GridIn
Definition: grid_in.h:307
ParameterHandler::add_parameter
void add_parameter(const std::string &entry, ParameterType &parameter, const std::string &documentation="", const Patterns::PatternBase &pattern= *Patterns::Tools::Convert< ParameterType >::to_pattern(), const bool has_to_be_set=false)
Definition: parameter_handler.h:2328
GridIn::read_msh
void read_msh(std::istream &in)
Definition: grid_in.cc:1427
ParameterHandler
Definition: parameter_handler.h:845
OpenCASCADE::write_IGES
void write_IGES(const TopoDS_Shape &shape, const std::string &filename)
Definition: utilities.cc:267
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
grid_out.h
DEAL_II_GMSH_EXECUTABLE_PATH
#define DEAL_II_GMSH_EXECUTABLE_PATH
Definition: config.h:234
AssertThrow
#define AssertThrow(cond, exc)
Definition: exceptions.h:1531
utilities.h