deal.II version GIT relicensing-2947-g2b65dfb0bb 2025-03-26 20:50:00+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\}}\)
Loading...
Searching...
No Matches
utilities.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2018 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
16
18
21
23
24#include <cstdio>
25
26#ifdef DEAL_II_WITH_GMSH
27
28# ifdef DEAL_II_WITH_OPENCASCADE
29# include <TopoDS_Edge.hxx>
30# endif
31
33
34namespace Gmsh
35{
37 const double characteristic_length,
38 const std::string &output_base_name)
39 : characteristic_length(characteristic_length)
40 , output_base_name(output_base_name)
41 {}
42
43
44
45 void
47 {
48 prm.add_parameter("Characteristic length", characteristic_length);
49 prm.add_parameter("Intermediate file name base",
51 "Keep empty, if you want the program to generate "
52 "temporary files, and then remove them when they "
53 "are no longer used.");
54 }
55
56
57
58# ifdef DEAL_II_WITH_OPENCASCADE
59 template <int spacedim>
60 void
61 create_triangulation_from_boundary_curve(const TopoDS_Edge &boundary,
63 const AdditionalParameters &prm)
64 {
65 std::string base_name = prm.output_base_name;
66
67 // If necessary, create a temp directory to put files into. The
68 // following variable will hold the name of the tmp dir; it is
69 // initialized with a template, and 'mkdtemp' then overwrites it
70 // with the name of the directory it creates.
71 char tmp_dir_name[] = "ctfbc-XXXXXX";
72 if (prm.output_base_name.empty())
73 {
74 const char *temp = mkdtemp(tmp_dir_name);
75 AssertThrow(temp != nullptr,
76 ExcMessage("Creating temporary directory failed!"));
77 base_name = temp;
78 base_name += "tmp";
79 }
80
81 const std::string iges_file_name = base_name + ".iges";
82 const std::string geo_file_name = base_name + ".geo";
83 const std::string msh_file_name = base_name + ".msh";
84 const std::string log_file_name = base_name + ".log";
85 const std::string warnings_file_name = base_name + "_warn.log";
86
87 ::OpenCASCADE::write_IGES(boundary, iges_file_name);
88
89 std::ofstream geofile;
90 geofile.open(geo_file_name);
91 geofile << "Merge \"" << iges_file_name << "\";" << std::endl
92 << "Line Loop (2) = {1};" << std::endl
93 << "Plane Surface (3) = {2};" << std::endl
94 << "Characteristic Length { 1 } = " << prm.characteristic_length
95 << ";" << std::endl
96 << "Mesh.RecombineAll = 1;" << std::endl
97 << "Mesh.SubdivisionAlgorithm = 1;" << std::endl;
98 geofile.close();
99
100 std::stringstream command;
101 command << DEAL_II_GMSH_EXECUTABLE_PATH << " -2 " << geo_file_name << " 1> "
102 << log_file_name << " 2> " << warnings_file_name;
103
104 const auto ret_value = std::system(command.str().c_str());
105 AssertThrow(ret_value == 0,
106 ExcMessage("Gmsh failed to run. Check the " + log_file_name +
107 " file."));
108
109 std::ifstream grid_file(msh_file_name);
110 Assert(grid_file.fail() == false, ExcIO());
111
112 GridIn<2, spacedim> gridin;
113 gridin.attach_triangulation(tria);
114 gridin.read_msh(grid_file);
115
116 // Clean up files if a tmp directory was used:
117 if (prm.output_base_name.empty())
118 {
119 // declaring the list without a type, i.e.,
120 //
121 // auto filenames = {{iges_file_name, geo_file_name, ...}})
122 //
123 // causes internal compiler errors with GCC's concepts implementation,
124 // so give it an explicit type:
125 const std::array<const std::string *, 5> filenames{
126 {&iges_file_name,
127 &geo_file_name,
128 &msh_file_name,
129 &log_file_name,
130 &warnings_file_name}};
131 for (const std::string *filename : filenames)
132 {
133 const auto ret_value = std::remove(filename->c_str());
134 AssertThrow(ret_value == 0,
135 ExcMessage("Failed to remove " + *filename));
136 }
137
138 // Finally also remove the tmp directory:
139 const auto ret_value = std::remove(tmp_dir_name);
140 AssertThrow(ret_value == 0,
141 ExcMessage("Failed to remove " +
142 std::string(tmp_dir_name)));
143 }
144 }
145# endif
146
147 // explicit instantiations
148# ifdef DEAL_II_WITH_OPENCASCADE
149// We don't build the utilities.inst file if deal.II isn't configured
150// with GMSH, but doxygen doesn't know that and tries to find that
151// file anyway for parsing -- which then of course it fails on. So
152// exclude the following from doxygen consideration.
153# ifndef DOXYGEN
154# include "gmsh/utilities.inst"
155# endif
156# endif
157} // namespace Gmsh
158
160
161#endif
void add_parameters(ParameterHandler &prm)
Definition utilities.cc:46
std::string output_base_name
Definition utilities.h:80
AdditionalParameters(const double characteristic_length=1.0, const std::string &output_base_name="")
Definition utilities.cc:36
void attach_triangulation(Triangulation< dim, spacedim > &tria)
Definition grid_in.cc:153
void read_msh(std::istream &in)
Definition grid_in.cc:2083
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:40
#define DEAL_II_GMSH_EXECUTABLE_PATH
Definition config.h:429
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:41
static ::ExceptionBase & ExcIO()
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
void create_triangulation_from_boundary_curve(const TopoDS_Edge &boundary, Triangulation< 2, spacedim > &tria, const AdditionalParameters &prm=AdditionalParameters())
Definition utilities.cc:61
void write_IGES(const TopoDS_Shape &shape, const std::string &filename)
Definition utilities.cc:272