Reference documentation for deal.II version GIT d77e5ebb0a 2023-01-27 22:35:02+00:00
\(\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 - 2022 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 
17 
18 #include <deal.II/gmsh/utilities.h>
19 
20 #include <deal.II/grid/grid_in.h>
21 #include <deal.II/grid/grid_out.h>
22 
24 
25 #include <cstdio>
26 
27 #ifdef DEAL_II_WITH_GMSH
28 
30 
31 namespace Gmsh
32 {
34  const double characteristic_length,
35  const std::string &output_base_name)
36  : characteristic_length(characteristic_length)
37  , output_base_name(output_base_name)
38  {}
39 
40 
41 
42  void
44  {
45  prm.add_parameter("Characteristic length", characteristic_length);
46  prm.add_parameter("Intermediate file name base",
48  "Keep empty, if you want the program to generate "
49  "temporary files, and then remove them when they "
50  "are no longer used.");
51  }
52 
53 
54 
55 # ifdef DEAL_II_WITH_OPENCASCADE
56  template <int spacedim>
57  void
58  create_triangulation_from_boundary_curve(const TopoDS_Edge & boundary,
60  const AdditionalParameters &prm)
61  {
62  std::string base_name = prm.output_base_name;
63  char dir_template[] = "ctfbc-XXXXXX";
64  if (base_name.empty())
65  {
66  const char *temp = mkdtemp(dir_template);
67  AssertThrow(temp != nullptr,
68  ExcMessage("Creating temporary directory failed!"));
69  base_name = temp;
70  base_name += "tmp";
71  }
72 
73  const std::string iges_file_name = base_name + ".iges";
74  const std::string geo_file_name = base_name + ".geo";
75  const std::string msh_file_name = base_name + ".msh";
76  const std::string log_file_name = base_name + ".log";
77  const std::string warnings_file_name = base_name + "_warn.log";
78 
79  ::OpenCASCADE::write_IGES(boundary, iges_file_name);
80 
81  std::ofstream geofile;
82  geofile.open(geo_file_name);
83  geofile << "Merge \"" << iges_file_name << "\";" << std::endl
84  << "Line Loop (2) = {1};" << std::endl
85  << "Plane Surface (3) = {2};" << std::endl
86  << "Characteristic Length { 1 } = " << prm.characteristic_length
87  << ";" << std::endl
88  << "Mesh.RecombineAll = 1;" << std::endl
89  << "Mesh.SubdivisionAlgorithm = 1;" << std::endl;
90  geofile.close();
91 
92  std::stringstream command;
93  command << DEAL_II_GMSH_EXECUTABLE_PATH << " -2 " << geo_file_name << " 1> "
94  << log_file_name << " 2> " << warnings_file_name;
95 
96  const auto ret_value = std::system(command.str().c_str());
97  AssertThrow(ret_value == 0,
98  ExcMessage("Gmsh failed to run. Check the " + log_file_name +
99  " file."));
100 
101  std::ifstream grid_file(msh_file_name);
102  Assert(grid_file.fail() == false, ExcIO());
103 
104  GridIn<2, spacedim> gridin;
105  gridin.attach_triangulation(tria);
106  gridin.read_msh(grid_file);
107 
108  if (base_name != prm.output_base_name)
109  {
110  // declaring the list without a type, i.e.,
111  //
112  // auto filenames = {{iges_file_name, geo_file_name, ...}})
113  //
114  // causes internal compiler errors with GCC's concepts implementation,
115  // so give it an explicit type:
116  const std::array<const std::string *, 5> filenames{
117  {&iges_file_name,
118  &geo_file_name,
119  &msh_file_name,
120  &log_file_name,
121  &warnings_file_name}};
122  for (const std::string *filename : filenames)
123  {
124  const auto ret_value = std::remove(filename->c_str());
125  AssertThrow(ret_value == 0,
126  ExcMessage("Failed to remove " + *filename));
127  }
128  const auto ret_value = std::remove(dir_template);
129  AssertThrow(ret_value == 0,
130  ExcMessage("Failed to remove " +
131  std::string(dir_template)));
132  }
133  }
134 # endif
135 
136  // explicit instantiations
137 # ifdef DEAL_II_WITH_OPENCASCADE
138 # include "utilities.inst"
139 # endif
140 } // namespace Gmsh
141 
143 
144 #endif
void add_parameters(ParameterHandler &prm)
Definition: utilities.cc:43
std::string output_base_name
Definition: utilities.h:78
AdditionalParameters(const double characteristic_length=1.0, const std::string &output_base_name="")
Definition: utilities.cc:33
void attach_triangulation(Triangulation< dim, spacedim > &tria)
Definition: grid_in.cc:150
void read_msh(std::istream &in)
Definition: grid_in.cc:1988
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)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:461
#define DEAL_II_GMSH_EXECUTABLE_PATH
Definition: config.h:300
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:462
#define Assert(cond, exc)
Definition: exceptions.h:1509
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1619
Definition: utilities.h:45
void create_triangulation_from_boundary_curve(const TopoDS_Edge &boundary, Triangulation< 2, spacedim > &tria, const AdditionalParameters &prm=AdditionalParameters())
Definition: utilities.cc:58
void write_IGES(const TopoDS_Shape &shape, const std::string &filename)
Definition: utilities.cc:268
const ::Triangulation< dim, spacedim > & tria