Rheolef  7.1
an efficient C++ finite element environment
adapt_bamg.cc
Go to the documentation of this file.
1 #include "rheolef/adapt.h"
22 #include "rheolef/form.h"
23 #include "rheolef/field_component.h"
24 #include "rheolef/rheostream.h"
25 #include "rheolef/field_expr.h"
26 
27 namespace rheolef {
28 
29 // extern in adapt.cc
30 template<class T, class M>
31 field_basic<T,M>
33  const field_basic<T,M>& uh0,
34  const adapt_option& opts);
35 
36 template<class T, class M>
37 field_basic<T,M>
38 proj (const field_basic<T,M>& uh, const std::string& approx = "P1");
39 
40 // -------------------------------------------
41 // bamgcad handler
42 // -------------------------------------------
43 // catch Geometry "NAME.bamgcad" and return "NAME"
44 std::string bamgcad_catch_name (idiststream& bamg_in)
45 {
46  using namespace std;
47  if (!bamg_in) return string();
48  check_macro (dis_scatch(bamg_in, "Geometry"), "mark \"Geometry\" not founded in bamg file");
49  string arg;
50  bamg_in >> arg;
51  ostringstream out;
52  for (size_t i = 0; i < arg.length(); ++i) {
53  if (arg[i] != '"') out << arg[i];
54  }
55  string file_bamgcad = out.str();
56  return file_bamgcad;
57 }
58 // -----------------------------------------
59 // adapt
60 // -----------------------------------------
61 template<class T, class M>
62 geo_basic<T,M>
64  const field_basic<T,M>& uh,
65  const adapt_option& opts)
66 {
67  using namespace std;
68  typedef typename geo_basic<T,M>::size_type size_type;
69  bool do_verbose = iorheo::getverbose(clog);
70  bool do_clean = iorheo::getclean(clog);
71  string clean_files;
72  string command;
73  // -----------------------------------------
74  // 1a) sortie du maillage : nom-<i>.geo
75  // -----------------------------------------
76  const geo_basic<T,M>& omega = uh.get_geo();
77  size_type i = omega.serial_number();
78  string i_name = omega.name();
79  string geo_name = i_name + ".geo";
81  if (! dis_file_exists(geo_name) &&
82  ! dis_file_exists(geo_name + ".gz")) {
83  out.open (geo_name);
84  out << omega;
85  check_macro (out.good(), "adapt: file \"" << geo_name << "\"creation failed");
86  out.close();
87  clean_files += " " + geo_name + " " + geo_name + ".gz";
88  }
89  // ----------------------------------------
90  // 1b) conversion geo : nom-<i>.bamg
91  // ----------------------------------------
92  string bamg_name = i_name + ".bamg";
93  if (i != 0) {
94  clean_files += " " + bamg_name;
95  }
96  // -----------------------------------------
97  // 2a) sortie du critere : nom-crit-<i>.field
98  // -----------------------------------------
99  string crit_name = i_name + "-crit.field";
100  clean_files += " " + crit_name + ".gz";
101  out.open (crit_name, "field");
102  out << proj (uh);
103  check_macro (out.good(), "adapt: file \"" << crit_name << "\"creation failed");
104  out.close();
105  // --------------------------------------------
106  // 2b) conversion field : nom-crit-<i>.bb
107  // --------------------------------------------
108  string bb_name = i_name + "-crit.bb";
109  clean_files += " " + bb_name;
110  string bindir = string(_RHEOLEF_RHEOLEF_LIBDIR) + "/rheolef";
111  command = "zcat < " + crit_name + ".gz | " + bindir + "/field2bb > " + bb_name;
112  if (do_verbose) derr << "! " << command << endl;
113  check_macro (dis_system (command) == 0, "adapt: unix command failed");
114  // ----------------------------------------
115  // 3) run bamg in adapt mode:
116  // => nom-<i+1>.bamg
117  // ----------------------------------------
118  idiststream bamg_in (bamg_name);
119  string bamgcad_file = bamgcad_catch_name (bamg_in);
120  if (bamgcad_file.length() == 0) bamgcad_file = omega.familyname() + ".bamgcad";
121  check_macro (dis_file_exists(bamgcad_file), "adapt: missing \""<<bamgcad_file<<"\" file");
122  size_type k = uh.get_space().degree();
123  if (! uh.get_space().get_basis().is_continuous()) k++; // uh is a grad of a solution
124  Float error = opts.err;
125  if (k > 1) {
126  // when k > 1, decreses the interpolation error level for bamg, as computed when k=1
127  error = pow (error, 2./(k+1));
128  }
129  string options =
130  "-coef " + ftos(opts.hcoef)
131  + " -err " + ftos(error)
132  + " -errg " + ftos(opts.errg)
133  + " -hmin " + ftos(opts.hmin)
134  + " -hmax " + ftos(opts.hmax)
135  + " -ratio " + ftos(opts.ratio)
136  + " -anisomax " + ftos(opts.anisomax)
137  + " -nbv " + itos(opts.n_vertices_max)
138  + " -NbJacobi " + itos(opts.n_smooth_metric)
139  + " -CutOff " + ftos(opts.cutoff);
140  if (opts.splitpbedge) {
141  options += " -splitpbedge ";
142  }
143  if (opts.thetaquad != numeric_limits<Float>::max()) {
144  options += " -thetaquad " + ftos(opts.thetaquad);
145  }
146  options += " " + opts.additional;
147  char buffer [100];
148  sprintf (buffer, "%.3d", int(i+1)); // see geo::name() in geo.cc
149  string i1_name = omega.familyname() + "-" + buffer;
150  command = "bamg "
151  + options
152  + " -b " + bamg_name
153  + " -Mbb " + bb_name
154  + " -o " + i1_name + ".bamg 1>&2";
155  if (do_verbose) derr << "! " << command << endl;
156  check_macro (dis_system (command) == 0, "adapt: command failed");
157  // ----------------------------------------
158  // 4) conversion : nom-crit-<i>.bamg
159  // ----------------------------------------
160  string dmn_name = delete_any_suffix(bamgcad_file) + ".dmn";
161  check_macro (dis_file_exists(dmn_name), "adapt: missing \""<<dmn_name<<"\" file");
162  command = "cat " + i1_name + ".bamg " + dmn_name
163  + " | bamg2geo -" + omega.coordinate_system_name()
164  + " | gzip -9 > " + i1_name + ".geo.gz";
165  if (do_verbose) derr << "! " << command << endl;
166  check_macro (dis_system (command) == 0, "adapt: command failed");
167  // ----------------------------------------
168  // 5) chargement nom-<i+1>.geo
169  // ----------------------------------------
170  idiststream in (i1_name, "geo");
171  geo_basic<T,M> new_omega;
172  in >> new_omega;
173  new_omega.set_name (omega.familyname());
174  new_omega.set_serial_number (i+1);
175  clean_files += " " + i1_name + ".geo.gz";
176  // ----------------------------------------
177  // 6) clean
178  // ----------------------------------------
179  if (do_clean) {
180  command = "rm -f" + clean_files;
181  if (do_verbose) derr << "! " << command << endl;
182  check_macro (dis_system (command) == 0, "adapt: command failed");
183  }
184  return new_omega;
185 }
186 // ----------------------------------------------------------------------------
187 // instanciation in library
188 // ----------------------------------------------------------------------------
189 #define _RHEOLEF_instanciation(T,M) \
190 template geo_basic<T,M> adapt_bamg (const field_basic<T,M>&, const adapt_option&);
191 
192 _RHEOLEF_instanciation(Float,sequential)
193 #ifdef _RHEOLEF_HAVE_MPI
195 #endif // _RHEOLEF_HAVE_MPI
196 
197 } // namespace rheolef
field::size_type size_type
Definition: branch.cc:425
see the Float page for the full documentation
const space_type & get_space() const
Definition: field.h:300
const geo_type & get_geo() const
Definition: field.h:301
generic mesh with rerefence counting
Definition: geo.h:1089
odiststream: see the diststream page for the full documentation
Definition: diststream.h:126
size_t size_type
Definition: basis_get.cc:76
distributed
Definition: asr.cc:228
odiststream derr(cerr)
see the diststream page for the full documentation
Definition: diststream.h:436
#define _RHEOLEF_RHEOLEF_LIBDIR
Definition: config.h:237
string command
Definition: mkgeo_ball.sh:136
This file is part of Rheolef.
string delete_any_suffix(const string &name)
delete_any_suffix: see the rheostream page for the full documentation
Definition: rheostream.cc:239
field_basic< T, M > proj(const field_basic< T, M > &uh, const std::string &approx="P1")
Definition: adapt.cc:39
int dis_system(const std::string &command, const communicator &comm)
Definition: diststream.cc:213
geo_basic< T, M > adapt_bamg(const field_basic< T, M > &uh, const adapt_option &opts)
Definition: adapt_bamg.cc:63
std::string itos(std::string::size_type i)
itos: see the rheostream page for the full documentation
bool dis_scatch(idiststream &ips, const communicator &comm, std::string ch)
distributed version of scatch(istream&,string)
Definition: diststream.cc:43
bool dis_file_exists(const std::string &filename, const communicator &comm)
Definition: diststream.cc:232
std::string bamgcad_catch_name(idiststream &bamg_in)
Definition: adapt_bamg.cc:44
string ftos(const Float &x)
itof: see the rheostream page for the full documentation
Definition: rheostream.cc:55
space_mult_list< T, M > pow(const space_basic< T, M > &X, size_t n)
Definition: space_mult.h:120
_RHEOLEF_instanciation(Float, sequential, std::allocator< Float >) _RHEOLEF_instanciation(Float
field_basic< T, M > hessian_criterion(const field_basic< T, M > &uh0, const adapt_option &opts)
Definition: adapt.cc:104
adapt_option: see the adapt page for the full documentation
Definition: adapt.h:147
size_type n_smooth_metric
Definition: adapt.h:159
size_type n_vertices_max
Definition: adapt.h:158
std::string additional
Definition: adapt.h:164