deal.II version GIT relicensing-2289-g1e5549a87a 2024-12-21 21:30: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
29
30namespace Gmsh
31{
33 const double characteristic_length,
34 const std::string &output_base_name)
35 : characteristic_length(characteristic_length)
36 , output_base_name(output_base_name)
37 {}
38
39
40
41 void
43 {
44 prm.add_parameter("Characteristic length", characteristic_length);
45 prm.add_parameter("Intermediate file name base",
47 "Keep empty, if you want the program to generate "
48 "temporary files, and then remove them when they "
49 "are no longer used.");
50 }
51
52
53
54# ifdef DEAL_II_WITH_OPENCASCADE
55 template <int spacedim>
56 void
57 create_triangulation_from_boundary_curve(const TopoDS_Edge &boundary,
59 const AdditionalParameters &prm)
60 {
61 std::string base_name = prm.output_base_name;
62
63 // If necessary, create a temp directory to put files into. The
64 // following variable will hold the name of the tmp dir; it is
65 // initialized with a template, and 'mkdtemp' then overwrites it
66 // with the name of the directory it creates.
67 char tmp_dir_name[] = "ctfbc-XXXXXX";
68 if (prm.output_base_name.empty())
69 {
70 const char *temp = mkdtemp(tmp_dir_name);
71 AssertThrow(temp != nullptr,
72 ExcMessage("Creating temporary directory failed!"));
73 base_name = temp;
74 base_name += "tmp";
75 }
76
77 const std::string iges_file_name = base_name + ".iges";
78 const std::string geo_file_name = base_name + ".geo";
79 const std::string msh_file_name = base_name + ".msh";
80 const std::string log_file_name = base_name + ".log";
81 const std::string warnings_file_name = base_name + "_warn.log";
82
83 ::OpenCASCADE::write_IGES(boundary, iges_file_name);
84
85 std::ofstream geofile;
86 geofile.open(geo_file_name);
87 geofile << "Merge \"" << iges_file_name << "\";" << std::endl
88 << "Line Loop (2) = {1};" << std::endl
89 << "Plane Surface (3) = {2};" << std::endl
90 << "Characteristic Length { 1 } = " << prm.characteristic_length
91 << ";" << std::endl
92 << "Mesh.RecombineAll = 1;" << std::endl
93 << "Mesh.SubdivisionAlgorithm = 1;" << std::endl;
94 geofile.close();
95
96 std::stringstream command;
97 command << DEAL_II_GMSH_EXECUTABLE_PATH << " -2 " << geo_file_name << " 1> "
98 << log_file_name << " 2> " << warnings_file_name;
99
100 const auto ret_value = std::system(command.str().c_str());
101 AssertThrow(ret_value == 0,
102 ExcMessage("Gmsh failed to run. Check the " + log_file_name +
103 " file."));
104
105 std::ifstream grid_file(msh_file_name);
106 Assert(grid_file.fail() == false, ExcIO());
107
108 GridIn<2, spacedim> gridin;
109 gridin.attach_triangulation(tria);
110 gridin.read_msh(grid_file);
111
112 // Clean up files if a tmp directory was used:
113 if (prm.output_base_name.empty())
114 {
115 // declaring the list without a type, i.e.,
116 //
117 // auto filenames = {{iges_file_name, geo_file_name, ...}})
118 //
119 // causes internal compiler errors with GCC's concepts implementation,
120 // so give it an explicit type:
121 const std::array<const std::string *, 5> filenames{
122 {&iges_file_name,
123 &geo_file_name,
124 &msh_file_name,
125 &log_file_name,
126 &warnings_file_name}};
127 for (const std::string *filename : filenames)
128 {
129 const auto ret_value = std::remove(filename->c_str());
130 AssertThrow(ret_value == 0,
131 ExcMessage("Failed to remove " + *filename));
132 }
133
134 // Finally also remove the tmp directory:
135 const auto ret_value = std::remove(tmp_dir_name);
136 AssertThrow(ret_value == 0,
137 ExcMessage("Failed to remove " +
138 std::string(tmp_dir_name)));
139 }
140 }
141# endif
142
143 // explicit instantiations
144# ifdef DEAL_II_WITH_OPENCASCADE
145# include "utilities.inst"
146# endif
147} // namespace Gmsh
148
150
151#endif
void add_parameters(ParameterHandler &prm)
Definition utilities.cc:42
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:32
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:498
#define DEAL_II_GMSH_EXECUTABLE_PATH
Definition config.h:354
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
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:57
void write_IGES(const TopoDS_Shape &shape, const std::string &filename)
Definition utilities.cc:270