Reference documentation for deal.II version 9.0.0
utilities.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2018 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/gmsh/utilities.h>
17 #include <deal.II/opencascade/utilities.h>
18 
19 #include <deal.II/grid/grid_in.h>
20 #include <deal.II/grid/grid_out.h>
21 
22 #include <cstdio>
23 
24 #ifdef DEAL_II_WITH_GMSH
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 namespace Gmsh
29 {
30  AdditionalParameters::AdditionalParameters(const double characteristic_length,
31  const std::string &output_base_name):
32  characteristic_length(characteristic_length),
33  output_base_name(output_base_name)
34  {}
35 
36 
37 
39  {
40  prm.add_parameter("Characteristic length", characteristic_length);
41  prm.add_parameter("Intermediate file name base", output_base_name,
42  "Keep empty, if you want the program to generate "
43  "temporary files, and then remove them when they "
44  "are no longer used.");
45  }
46 
47 
48 
49 #ifdef DEAL_II_WITH_OPENCASCADE
50  template <int spacedim>
51  void
52  create_triangulation_from_boundary_curve(const TopoDS_Edge &boundary,
54  const AdditionalParameters &prm)
55  {
56  std::string base_name = prm.output_base_name;
57  char dir_template[] = "ctfbc-XXXXXX";
58  if (base_name == "")
59  {
60  const char *temp = mkdtemp(dir_template);
61  AssertThrow(temp != nullptr,
62  ExcMessage("Creating temporary directory failed!"));
63  base_name = temp;
64  base_name += "tmp";
65  }
66 
67  const std::string iges_file_name = base_name+".iges";
68  const std::string geo_file_name = base_name+".geo";
69  const std::string msh_file_name = base_name+".msh";
70  const std::string log_file_name = base_name+".log";
71  const std::string warnings_file_name = base_name+"_warn.log";
72 
73  ::OpenCASCADE::write_IGES(boundary, iges_file_name);
74 
75  ofstream geofile;
76  geofile.open(geo_file_name);
77  geofile << "Merge \"" << iges_file_name << "\";" << std::endl
78  << "Line Loop (2) = {1};" << std::endl
79  << "Plane Surface (3) = {2};" << std::endl
80  << "Characteristic Length { 1 } = " << prm.characteristic_length << ";" << std::endl
81  << "Mesh.RecombineAll = 1;" << std::endl
82  << "Mesh.SubdivisionAlgorithm = 1;" << std::endl;
83  geofile.close();
84 
85  std::stringstream command;
86  command << DEAL_II_GMSH_EXECUTABLE_PATH << " -2 " << geo_file_name
87  << " 1> " << log_file_name
88  << " 2> " << warnings_file_name;
89 
90  const auto ret_value = std::system(command.str().c_str());
91  AssertThrow(ret_value == 0,
92  ExcMessage("Gmsh failed to run. Check the "+log_file_name+" file."));
93 
94  std::ifstream grid_file(msh_file_name);
95  Assert(grid_file, ExcIO());
96 
97  GridIn<2,spacedim> gridin;
98  gridin.attach_triangulation(tria);
99  gridin.read_msh(grid_file);
100 
101  if (base_name != prm.output_base_name)
102  {
103  const std::array<const std::string *, 5> filenames
104  {{&iges_file_name, &geo_file_name, &msh_file_name, &log_file_name, &warnings_file_name}};
105  for (const std::string *filename : filenames)
106  {
107  const auto ret_value = std::remove(filename->c_str());
108  AssertThrow(ret_value == 0, ExcMessage("Failed to remove " + *filename));
109  }
110  const auto ret_value = std::remove(dir_template);
111  AssertThrow(ret_value == 0,
112  ExcMessage("Failed to remove "+std::string(dir_template)));
113  }
114  }
115 #endif
116 
117  // explicit instantiations
118 #ifdef DEAL_II_WITH_OPENCASCADE
119 #include "utilities.inst"
120 #endif
121 }
122 
123 DEAL_II_NAMESPACE_CLOSE
124 
125 #endif
void create_triangulation_from_boundary_curve(const TopoDS_Edge &boundary, Triangulation< 2, spacedim > &tria, const AdditionalParameters &prm=AdditionalParameters())
Definition: utilities.cc:52
static ::ExceptionBase & ExcIO()
void add_parameters(ParameterHandler &prm)
Definition: utilities.cc:38
#define AssertThrow(cond, exc)
Definition: exceptions.h:1221
std::string output_base_name
Definition: utilities.h:79
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1142
Definition: utilities.h:43
void read_msh(std::istream &in)
Definition: grid_in.cc:1232
void add_parameter(const std::string &entry, ParameterType &parameter, const std::string &documentation=std::string(), const Patterns::PatternBase &pattern= *Patterns::Tools::Convert< ParameterType >::to_pattern())
void attach_triangulation(Triangulation< dim, spacedim > &tria)
Definition: grid_in.cc:98
AdditionalParameters(const double characteristic_length=1.0, const std::string &output_base_name="")
Definition: utilities.cc:30