Reference documentation for deal.II version 9.3.3
\(\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
17
20
22
23#include <cstdio>
24
25#ifdef DEAL_II_WITH_GMSH
26
28
29namespace 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
void add_parameters(ParameterHandler &prm)
Definition: utilities.cc:41
std::string output_base_name
Definition: utilities.h:76
AdditionalParameters(const double characteristic_length=1.0, const std::string &output_base_name="")
Definition: utilities.cc:31
void attach_triangulation(Triangulation< dim, spacedim > &tria)
Definition: grid_in.cc:119
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:402
#define DEAL_II_GMSH_EXECUTABLE_PATH
Definition: config.h:276
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:403
static ::ExceptionBase & ExcIO()
#define Assert(cond, exc)
Definition: exceptions.h:1465
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1575
void read_msh(std::istream &in)
Definition: grid_in.cc:1427
Definition: utilities.h:43
void create_triangulation_from_boundary_curve(const TopoDS_Edge &boundary, Triangulation< 2, spacedim > &tria, const AdditionalParameters &prm=AdditionalParameters())
Definition: utilities.cc:56
void write_IGES(const TopoDS_Shape &shape, const std::string &filename)
Definition: utilities.cc:267