-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy patherror_vector.C
More file actions
318 lines (237 loc) · 7.84 KB
/
error_vector.C
File metadata and controls
318 lines (237 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// The libMesh Finite Element Library.
// Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// C++ includes
#include <limits>
// Local includes
#include "libmesh/dof_map.h"
#include "libmesh/elem.h"
#include "libmesh/enum_xdr_mode.h"
#include "libmesh/error_vector.h"
#include "libmesh/equation_systems.h"
#include "libmesh/explicit_system.h"
#include "libmesh/libmesh_logging.h"
#include "libmesh/mesh_base.h"
#include "libmesh/numeric_vector.h"
#include "libmesh/exodusII_io.h"
#include "libmesh/gmv_io.h"
#include "libmesh/nemesis_io.h"
#include "libmesh/tecplot_io.h"
#include "libmesh/xdr_io.h"
namespace libMesh
{
// ------------------------------------------------------------
// ErrorVector class member functions
ErrorVectorReal ErrorVector::minimum() const
{
LOG_SCOPE ("minimum()", "ErrorVector");
const dof_id_type n = cast_int<dof_id_type>(this->size());
ErrorVectorReal min = std::numeric_limits<ErrorVectorReal>::max();
for (dof_id_type i=0; i<n; i++)
{
if (this->is_active_elem(i))
min = std::min (min, (*this)[i]);
}
return min;
}
Real ErrorVector::mean() const
{
LOG_SCOPE ("mean()", "ErrorVector");
const dof_id_type n = cast_int<dof_id_type>(this->size());
Real the_mean = 0;
dof_id_type nnz = 0;
for (dof_id_type i=0; i<n; i++)
if (this->is_active_elem(i))
{
the_mean += ( static_cast<Real>((*this)[i]) - the_mean ) / (nnz + 1);
nnz++;
}
return the_mean;
}
Real ErrorVector::median()
{
const dof_id_type n = cast_int<dof_id_type>(this->size());
if (n == 0)
return 0.;
// Build a StatisticsVector<ErrorVectorReal> containing
// only our active entries and take its mean
StatisticsVector<ErrorVectorReal> sv;
sv.reserve (n);
for (dof_id_type i=0; i<n; i++)
if (this->is_active_elem(i))
sv.push_back((*this)[i]);
return sv.median();
}
Real ErrorVector::median() const
{
ErrorVector ev = (*this);
return ev.median();
}
Real ErrorVector::variance(const Real mean_in) const
{
const dof_id_type n = cast_int<dof_id_type>(this->size());
LOG_SCOPE ("variance()", "ErrorVector");
Real the_variance = 0;
dof_id_type nnz = 0;
for (dof_id_type i=0; i<n; i++)
if (this->is_active_elem(i))
{
const Real delta = ( static_cast<Real>((*this)[i]) - mean_in );
the_variance += (delta * delta - the_variance) / (nnz + 1);
nnz++;
}
return the_variance;
}
std::vector<dof_id_type> ErrorVector::cut_below(Real cut) const
{
LOG_SCOPE ("cut_below()", "ErrorVector");
const dof_id_type n = cast_int<dof_id_type>(this->size());
std::vector<dof_id_type> cut_indices;
cut_indices.reserve(n/2); // Arbitrary
for (dof_id_type i=0; i<n; i++)
if (this->is_active_elem(i))
{
if ((*this)[i] < cut)
{
cut_indices.push_back(i);
}
}
return cut_indices;
}
std::vector<dof_id_type> ErrorVector::cut_above(Real cut) const
{
LOG_SCOPE ("cut_above()", "ErrorVector");
const dof_id_type n = cast_int<dof_id_type>(this->size());
std::vector<dof_id_type> cut_indices;
cut_indices.reserve(n/2); // Arbitrary
for (dof_id_type i=0; i<n; i++)
if (this->is_active_elem(i))
{
if ((*this)[i] > cut)
{
cut_indices.push_back(i);
}
}
return cut_indices;
}
bool ErrorVector::is_active_elem (dof_id_type i) const
{
libmesh_assert_less (i, this->size());
if (_mesh)
{
return _mesh->elem_ptr(i)->active();
}
else
return ((*this)[i] != 0.);
}
void ErrorVector::plot_error(const std::string & filename,
const MeshBase & oldmesh,
const std::string & data_type) const
{
std::unique_ptr<MeshBase> meshptr = oldmesh.clone();
MeshBase & mesh = *meshptr;
// The all_first_order routine will prepare_for_use(), which would
// break our ordering if elements get changed.
mesh.allow_renumbering(false);
mesh.all_first_order();
#ifdef LIBMESH_ENABLE_AMR
// We don't want p elevation when plotting a single constant value
// per element
for (auto & elem : mesh.element_ptr_range())
{
elem->set_p_refinement_flag(Elem::DO_NOTHING);
elem->set_p_level(0);
}
#endif // LIBMESH_ENABLE_AMR
EquationSystems temp_es (mesh);
ExplicitSystem & error_system
= temp_es.add_system<ExplicitSystem> ("Error");
error_system.add_variable(data_type, CONSTANT, MONOMIAL);
temp_es.init();
const DofMap & error_dof_map = error_system.get_dof_map();
std::vector<dof_id_type> dof_indices;
for (const auto & elem : mesh.active_local_element_ptr_range())
{
error_dof_map.dof_indices(elem, dof_indices);
const dof_id_type elem_id = elem->id();
//0 for the monomial basis
const dof_id_type solution_index = dof_indices[0];
// libMesh::out << "elem_number=" << elem_number << std::endl;
libmesh_assert_less (elem_id, (*this).size());
// We may have zero error values in special circumstances
// libmesh_assert_greater ((*this)[elem_id], 0.);
error_system.solution->set(solution_index, (*this)[elem_id]);
}
error_system.solution->close();
error_system.update();
// We may have to renumber if the original numbering was not
// contiguous. Since this is just a temporary mesh, that's probably
// fine.
if (mesh.max_elem_id() != mesh.n_elem() ||
mesh.max_node_id() != mesh.n_nodes())
{
mesh.allow_renumbering(true);
mesh.renumber_nodes_and_elements();
}
if (Utility::contains(filename, ".gmv"))
{
GMVIO(mesh).write_discontinuous_gmv(filename,
temp_es, false);
}
else if (Utility::contains(filename, ".plt"))
{
TecplotIO (mesh).write_equation_systems
(filename, temp_es);
}
#if defined(LIBMESH_HAVE_EXODUS_API) && defined(LIBMESH_HAVE_NEMESIS_API)
else if (Utility::contains(filename, ".nem") ||
Utility::contains(filename, ".n"))
{
Nemesis_IO io(mesh);
io.write(filename);
io.write_element_data(temp_es);
}
#endif
#ifdef LIBMESH_HAVE_EXODUS_API
else if (Utility::contains(filename, ".exo") ||
Utility::contains(filename, ".e"))
{
ExodusII_IO io(mesh);
io.write(filename);
io.write_element_data(temp_es);
}
#endif
else if (Utility::contains(filename, ".xda"))
{
XdrIO(mesh).write("mesh-"+filename);
temp_es.write("soln-"+filename,WRITE,
EquationSystems::WRITE_DATA |
EquationSystems::WRITE_ADDITIONAL_DATA);
}
else if (Utility::contains(filename, ".xdr"))
{
XdrIO(mesh,true).write("mesh-"+filename);
temp_es.write("soln-"+filename,ENCODE,
EquationSystems::WRITE_DATA |
EquationSystems::WRITE_ADDITIONAL_DATA);
}
else
{
libmesh_here();
libMesh::err << "Warning: ErrorVector::plot_error currently only"
<< " supports .gmv, .plt, .xdr/.xda, and .exo/.e (if enabled) output;" << std::endl;
libMesh::err << "Could not recognize filename: " << filename
<< std::endl;
}
}
} // namespace libMesh