Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
utilities.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2020-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/exceptions.h>
5#include <core/revision.h>
6#include <core/utilities.h>
7#include <core/vector.h>
8
9#include <deal.II/base/mpi.h>
10#include <deal.II/base/revision.h>
11
12#include <deal.II/numerics/vector_tools.h>
13
14#include <boost/algorithm/string/replace.hpp>
15
16#include <filesystem>
17#include <iostream>
18#include <regex>
19#include <system_error>
20#include <type_traits>
21
22template <typename T>
23TableHandler
25 const std::vector<T> &independent_values,
26 const std::string &independent_column_name,
27 const std::vector<std::vector<double>> &dependent_vector,
28 const std::vector<std::string> &dependent_column_names,
29 const unsigned int display_precision,
30 const bool display_scientific_notation)
31{
32 AssertDimension(independent_values.size(), dependent_vector.size());
33
34 TableHandler table;
35
36 for (unsigned int i = 0; i < independent_values.size(); ++i)
37 {
38 table.add_value(independent_column_name, independent_values[i]);
39 for (unsigned int d = 0; d < dependent_column_names.size(); ++d)
40 table.add_value(dependent_column_names[d], dependent_vector[i][d]);
41 }
42
43 table.set_precision(independent_column_name, display_precision);
44 for (const auto &name : dependent_column_names)
45 table.set_precision(name, display_precision);
46
47 if (display_scientific_notation)
48 {
49 table.set_scientific(independent_column_name, true);
50 for (const auto &name : dependent_column_names)
51 table.set_scientific(name, true);
52 }
53
54 return table;
55}
56
57template <int dim, typename T>
58TableHandler
60 const std::vector<T> &independent_vector,
61 const std::string &independent_column_name,
62 const std::vector<Tensor<1, dim>> &dependent_vector,
63 const std::vector<std::string> &dependent_column_names,
64 const unsigned int display_precision,
65 const bool display_scientific_notation)
66{
67 AssertDimension(independent_vector.size(), dependent_vector.size());
68 AssertDimension(dependent_column_names.size(), dim);
69
70 TableHandler table;
71
72 for (unsigned int i = 0; i < dependent_vector.size(); ++i)
73 {
74 table.add_value(independent_column_name, independent_vector[i]);
75 for (int d = 0; d < dim; ++d)
76 table.add_value(dependent_column_names[d], dependent_vector[i][d]);
77 }
78
79 for (int d = 0; d < dim; ++d)
80 table.set_precision(dependent_column_names[d], display_precision);
81
82 if constexpr (!std::is_integral_v<T>)
83 table.set_precision(independent_column_name, display_precision);
84
85 if (display_scientific_notation)
86 {
87 if constexpr (!std::is_integral_v<T>)
88 table.set_scientific(independent_column_name, true);
89 for (int d = 0; d < dim; ++d)
90 table.set_scientific(dependent_column_names[d], true);
91 }
92
93 return table;
94}
95
96template <int dim, typename T>
97TableHandler
99 const std::vector<T> &independent_vector,
100 const std::string &independent_column_name,
101 const std::vector<std::vector<Tensor<1, dim>>> &dependent_vectors,
102 const std::vector<std::string> &dependent_column_names,
103 const unsigned int display_precision,
104 const bool display_scientific_notation)
105{
106 AssertDimension(dependent_column_names.size(),
107 dependent_vectors.size() * dim);
108
109 TableHandler table;
110 unsigned int vect_index = 0;
111
112 for (unsigned int i = 0; i < dependent_vectors[0].size(); ++i)
113 table.add_value(independent_column_name, independent_vector[i]);
114
115 for (auto &vect : dependent_vectors)
116 {
117 AssertDimension(independent_vector.size(), vect.size());
118 for (unsigned int i = 0; i < vect.size(); ++i)
119 {
120 for (int d = 0; d < dim; ++d)
121 {
122 table.add_value(dependent_column_names[d + vect_index],
123 vect[i][d]);
124 table.set_precision(dependent_column_names[d + vect_index],
125 display_precision);
126 if (display_scientific_notation)
127 table.set_scientific(dependent_column_names[d + vect_index],
128 true);
129 }
130 }
131 vect_index += dim;
132 }
133
134 if constexpr (!std::is_integral_v<T>)
135 {
136 table.set_precision(independent_column_name, display_precision);
137 if (display_scientific_notation)
138 table.set_scientific(independent_column_name, true);
139 }
140
141 return table;
142}
143
144
145template <int dim>
146TableHandler
148 const std::vector<Tensor<1, dim>> &independent_vector,
149 const std::vector<std::string> &independent_column_names,
150 const std::vector<Tensor<1, dim>> &dependent_vector,
151 const std::vector<std::string> &dependent_column_names,
152 const unsigned int display_precision,
153 const bool display_scientific_notation)
154{
155 AssertDimension(independent_vector.size(), dependent_vector.size());
156 AssertDimension(independent_column_names.size(), dim);
157 AssertDimension(dependent_column_names.size(), dim);
158
159 TableHandler table;
160
161 for (unsigned int i = 0; i < dependent_vector.size(); ++i)
162 {
163 for (int d = 0; d < dim; ++d)
164 {
165 table.add_value(independent_column_names[d],
166 independent_vector[i][d]);
167 table.add_value(dependent_column_names[d], dependent_vector[i][d]);
168 }
169 }
170
171 for (int d = 0; d < dim; ++d)
172 {
173 table.set_precision(independent_column_names[d], display_precision);
174 table.set_precision(dependent_column_names[d], display_precision);
175 }
176
177 if (display_scientific_notation)
178 for (int d = 0; d < dim; ++d)
179 {
180 table.set_scientific(independent_column_names[d], true);
181 table.set_scientific(dependent_column_names[d], true);
182 }
183
184 return table;
185}
186
187template <int dim>
188TableHandler
190 const std::vector<Tensor<1, dim>> &independent_vector,
191 const std::vector<std::string> &independent_column_names,
192 const std::vector<double> &dependent_vector,
193 const std::string &dependent_column_name,
194 const unsigned int display_precision,
195 const bool display_scientific_notation)
196{
197 AssertDimension(independent_vector.size(), dependent_vector.size());
198 AssertDimension(independent_column_names.size(), dim);
199
200
201 TableHandler table;
202
203 for (unsigned int i = 0; i < dependent_vector.size(); ++i)
204 {
205 table.add_value(dependent_column_name, dependent_vector[i]);
206 for (int d = 0; d < dim; ++d)
207 table.add_value(independent_column_names[d], independent_vector[i][d]);
208 }
209
210 table.set_precision(dependent_column_name, display_precision);
211 for (int d = 0; d < dim; ++d)
212 table.set_precision(independent_column_names[d], display_precision);
213
214 if (display_scientific_notation)
215 {
216 table.set_scientific(dependent_column_name, true);
217 for (int d = 0; d < dim; ++d)
218 table.set_scientific(independent_column_names[d], true);
219 }
220
221 return table;
222}
223
224void
225check_file_exists(const std::string &file_name,
226 const std::string &file_description)
227{
228 AssertThrow(!file_name.empty(), EmptyFileName(file_description));
229
230 // The error code overload of std::filesystem::exists is used so that a
231 // problem with the file system is reported through the exceptions below
232 // rather than through a std::filesystem::filesystem_error that the user
233 // cannot act upon.
234 std::error_code error_code;
235 const std::filesystem::path path(file_name);
236
237 AssertThrow(std::filesystem::exists(path, error_code),
238 FileDoesNotExist(file_name, file_description));
239
240 // A directory can be opened as a stream on Linux, and it is only the read
241 // that fails afterwards, so a directory given where a file is expected is
242 // rejected explicitly.
243 AssertThrow(!std::filesystem::is_directory(path, error_code),
244 FileIsNotReadable(file_name, file_description));
245
246 // The remaining case is a file whose permissions prevent Lethe from reading
247 // it, which opening the file detects.
248 const std::ifstream file(file_name);
249 AssertThrow(file.good(), FileIsNotReadable(file_name, file_description));
250}
251
252void
253fill_table_from_file(TableHandler &table,
254 const std::string &file_name,
255 const std::string &delimiter)
256{
257 table.clear();
258 check_file_exists(file_name, "tabulated data file");
259 std::ifstream myfile(file_name);
260
261 std::string line;
262 std::vector<std::string> vector_of_column_names;
263 std::vector<double> line_of_data;
264 unsigned int line_count = 0;
265
266 while (std::getline(myfile, line))
267 {
268 // read the line and clean the resulting vector
269 std::vector<std::string> list_of_words_base =
270 Utilities::split_string_list(line, delimiter);
271 std::vector<std::string> list_of_words_clean;
272 for (const auto &word : list_of_words_base)
273 {
274 if (word != "")
275 {
276 list_of_words_clean.emplace_back(word);
277 }
278 }
279 // If it's the first line, we only initialize the variable names.
280 if (line_count != 0)
281 {
282 line_of_data = Utilities::string_to_double(list_of_words_clean);
283 for (unsigned int i = 0; i < line_of_data.size(); ++i)
284 {
285 table.add_value(vector_of_column_names[i], line_of_data[i]);
286 }
287 }
288 else
289 {
290 // the line contains words we assume these are the column
291 vector_of_column_names = list_of_words_clean;
292 }
293 ++line_count;
294 }
295 myfile.close();
296}
297
298void
299fill_vectors_from_file(std::map<std::string, std::vector<double>> &map,
300 const std::string &file,
301 const std::string &delimiter)
302{
303 // fill a pair, first being a vector of vector name and the second being the
304 // vector of vector associated with the vector name.
305 check_file_exists(file, "tabulated data file");
306 std::ifstream myfile(file);
307
308 std::string line;
309 std::vector<std::string> column_names;
310 std::vector<double> line_of_data;
311 unsigned int line_count = 0;
312
313 while (std::getline(myfile, line))
314 {
315 // read the line and clean the resulting vector.
316 std::vector<std::string> list_of_words_base =
317 Utilities::split_string_list(line, delimiter);
318 std::vector<std::string> list_of_words_clean;
319 for (const auto &word : list_of_words_base)
320 {
321 if (word != "")
322 {
323 list_of_words_clean.emplace_back(word);
324 }
325 }
326 // check if the line is contained words or numbers.
327 if (line_count != 0)
328 {
329 line_of_data = Utilities::string_to_double(list_of_words_clean);
330 for (unsigned int i = 0; i < line_of_data.size(); ++i)
331 {
332 map[column_names[i]].emplace_back(line_of_data[i]);
333 }
334 }
335 else
336 {
337 // the line contains words, we assume these are the columns
338 // names.
339 column_names = list_of_words_clean;
340 for (unsigned int i = 0; i < list_of_words_clean.size(); ++i)
341 {
342 std::vector<double> base_vector;
343 map[column_names[i]] = base_vector;
344 }
345 }
346 ++line_count;
347 }
348 myfile.close();
349}
350
351void
353 std::map<std::string, std::vector<std::string>> &map,
354 const std::string &file,
355 const std::string &delimiter)
356{
357 check_file_exists(file, "tabulated data file");
358 std::ifstream myfile(file);
359
360 std::string line;
361 std::vector<std::string> column_names;
362 std::vector<std::string> line_of_data;
363 unsigned int line_count = 0;
364
365 while (std::getline(myfile, line))
366 {
367 // read the line and clean the resulting vector.
368 std::vector<std::string> list_of_words_base =
369 Utilities::split_string_list(line, delimiter);
370 std::vector<std::string> list_of_words_clean;
371 for (const auto &word : list_of_words_base)
372 {
373 if (word != "")
374 list_of_words_clean.emplace_back(word);
375 }
376 // Check if it is the first line. If it is we assume it is the
377 // column name.
378 if (line_count != 0)
379 {
380 line_of_data = list_of_words_clean;
381 for (unsigned int i = 0; i < line_of_data.size(); ++i)
382 {
383 map[column_names[i]].emplace_back(line_of_data[i]);
384 }
385 }
386 else
387 {
388 column_names = list_of_words_clean;
389 for (unsigned int i = 0; i < list_of_words_clean.size(); ++i)
390 {
391 std::vector<std::string> base_vector;
392 map[column_names[i]] = base_vector;
393 }
394 }
395 ++line_count;
396 }
397 myfile.close();
398}
399
400void
401create_output_folder(const std::string &dirname)
402{
403 std::filesystem::create_directories(dirname);
404}
405
406void
407delete_output_folder(const std::string &dirname)
408{
409 std::filesystem::remove_all(dirname);
410}
411
412template TableHandler
414 const std::vector<double> &independent_values,
415 const std::string &independent_column_name,
416 const std::vector<std::vector<double>> &dependent_vector,
417 const std::vector<std::string> &dependent_column_names,
418 const unsigned int display_precision,
419 const bool display_scientific_notation);
420
421template TableHandler
423 const std::vector<unsigned int> &independent_values,
424 const std::string &independent_column_name,
425 const std::vector<std::vector<double>> &dependent_vector,
426 const std::vector<std::string> &dependent_column_names,
427 const unsigned int display_precision,
428 const bool display_scientific_notation);
429
430template TableHandler
432 const std::vector<int> &independent_values,
433 const std::string &independent_column_name,
434 const std::vector<std::vector<double>> &dependent_vector,
435 const std::vector<std::string> &dependent_column_names,
436 const unsigned int display_precision,
437 const bool display_scientific_notation);
438
439template TableHandler
441 const std::vector<double> &independent_values,
442 const std::string &independent_column_name,
443 const std::vector<Tensor<1, 2>> &dependent_vector,
444 const std::vector<std::string> &dependent_column_names,
445 const unsigned int display_precision,
446 const bool display_scientific_notation);
447
448template TableHandler
450 const std::vector<double> &independent_values,
451 const std::string &independent_column_name,
452 const std::vector<Tensor<1, 3>> &dependent_vector,
453 const std::vector<std::string> &dependent_column_names,
454 const unsigned int display_precision,
455 const bool display_scientific_notation);
456
457template TableHandler
459 const std::vector<double> &independent_values,
460 const std::string &independent_column_name,
461 const std::vector<std::vector<Tensor<1, 2>>> &dependent_vector,
462 const std::vector<std::string> &dependent_column_names,
463 const unsigned int display_precision,
464 const bool display_scientific_notation);
465
466template TableHandler
468 const std::vector<double> &independent_values,
469 const std::string &independent_column_name,
470 const std::vector<std::vector<Tensor<1, 3>>> &dependent_vector,
471 const std::vector<std::string> &dependent_column_names,
472 const unsigned int display_precision,
473 const bool display_scientific_notation);
474
475template TableHandler
477 const std::vector<unsigned int> &independent_values,
478 const std::string &independent_column_name,
479 const std::vector<Tensor<1, 2>> &dependent_vector,
480 const std::vector<std::string> &dependent_column_names,
481 const unsigned int display_precision,
482 const bool display_scientific_notation);
483
484template TableHandler
486 const std::vector<unsigned int> &independent_values,
487 const std::string &independent_column_name,
488 const std::vector<Tensor<1, 3>> &dependent_vector,
489 const std::vector<std::string> &dependent_column_names,
490 const unsigned int display_precision,
491 const bool display_scientific_notation);
492
493template TableHandler
495 const std::vector<unsigned int> &independent_values,
496 const std::string &independent_column_name,
497 const std::vector<std::vector<Tensor<1, 2>>> &dependent_vector,
498 const std::vector<std::string> &dependent_column_names,
499 const unsigned int display_precision,
500 const bool display_scientific_notation);
501
502template TableHandler
504 const std::vector<unsigned int> &independent_values,
505 const std::string &independent_column_name,
506 const std::vector<std::vector<Tensor<1, 3>>> &dependent_vector,
507 const std::vector<std::string> &dependent_column_names,
508 const unsigned int display_precision,
509 const bool display_scientific_notation);
510
511template TableHandler
513 const std::vector<int> &independent_values,
514 const std::string &independent_column_name,
515 const std::vector<Tensor<1, 2>> &dependent_vector,
516 const std::vector<std::string> &dependent_column_names,
517 const unsigned int display_precision,
518 const bool display_scientific_notation);
519
520template TableHandler
522 const std::vector<int> &independent_values,
523 const std::string &independent_column_name,
524 const std::vector<Tensor<1, 3>> &dependent_vector,
525 const std::vector<std::string> &dependent_column_names,
526 const unsigned int display_precision,
527 const bool display_scientific_notation);
528
529template TableHandler
531 const std::vector<int> &independent_values,
532 const std::string &independent_column_name,
533 const std::vector<std::vector<Tensor<1, 2>>> &dependent_vector,
534 const std::vector<std::string> &dependent_column_names,
535 const unsigned int display_precision,
536 const bool display_scientific_notation);
537
538template TableHandler
540 const std::vector<int> &independent_values,
541 const std::string &independent_column_name,
542 const std::vector<std::vector<Tensor<1, 3>>> &dependent_vector,
543 const std::vector<std::string> &dependent_column_names,
544 const unsigned int display_precision,
545 const bool display_scientific_notation);
546
547template TableHandler
549 const std::vector<Tensor<1, 2>> &independent_values,
550 const std::vector<std::string> &independent_column_names,
551 const std::vector<Tensor<1, 2>> &dependent_vector,
552 const std::vector<std::string> &dependent_column_names,
553 const unsigned int display_precision,
554 const bool display_scientific_notation);
555
556template TableHandler
558 const std::vector<Tensor<1, 3>> &independent_values,
559 const std::vector<std::string> &independent_column_names,
560 const std::vector<Tensor<1, 3>> &dependent_vector,
561 const std::vector<std::string> &dependent_column_names,
562 const unsigned int display_precision,
563 const bool display_scientific_notation);
564
565template TableHandler
567 const std::vector<Tensor<1, 2>> &independent_vector,
568 const std::vector<std::string> &independent_column_names,
569 const std::vector<double> &dependent_values,
570 const std::string &dependent_column_name,
571 const unsigned int display_precision,
572 const bool display_scientific_notation);
573
574template TableHandler
576 const std::vector<Tensor<1, 3>> &independent_vector,
577 const std::vector<std::string> &independent_column_names,
578 const std::vector<double> &dependent_values,
579 const std::string &dependent_column_name,
580 const unsigned int display_precision,
581 const bool display_scientific_notation);
582
583
584std::string
585get_last_value_of_parameter(const std::string &file_name,
586 const std::string &parameter_name)
587{
588 check_file_exists(file_name, "parameter file given to the application");
589
590 std::string return_value;
591 std::ifstream x_file(file_name);
592
593 while (x_file)
594 {
595 // Get one line and then match a regex to it that matches the parameter
596 // we are looking for. Before we do that, strip spaces from the front
597 // and back of the line:
598 std::string line;
599 std::getline(x_file, line);
600
601 while ((line.size() > 0) && (line[0] == ' ' || line[0] == '\t'))
602 line.erase(0, 1);
603 while ((line.size() > 0) &&
604 (line[line.size() - 1] == ' ' || line[line.size() - 1] == '\t'))
605 line.erase(line.size() - 1, std::string::npos);
606
607 std::match_results<std::string::const_iterator> matches;
608 const std::string regex =
609 "set[ \t]+" + parameter_name + "[ \t]*=[ \t]*(.*)";
610 if (std::regex_match(line, matches, std::regex(regex)))
611 {
612 // Since the line as a whole matched, the 'matches' variable needs
613 // to contain two entries: [0] denotes the whole string, and [1] the
614 // one that was matched by the '(.*)' expression.
615 Assert(matches.size() == 2, dealii::ExcInternalError());
616 return_value = std::string(matches[1].first, matches[1].second);
617 }
618 }
619
620 return return_value;
621}
622
623
624int
625get_max_value_of_parameter(const std::string &file_name,
626 const std::string &parameter_name)
627{
628 check_file_exists(file_name, "parameter file given to the application");
629
630 std::string return_string;
631 int return_value = -100000;
632
633 std::ifstream x_file(file_name);
634
635 while (x_file)
636 {
637 // Get one line and then match a regex to it that matches the parameter
638 // we are looking for. Before we do that, strip spaces from the front
639 // and back of the line:
640 std::string line;
641 std::getline(x_file, line);
642
643 while ((line.size() > 0) && (line[0] == ' ' || line[0] == '\t'))
644 line.erase(0, 1);
645 while ((line.size() > 0) &&
646 (line[line.size() - 1] == ' ' || line[line.size() - 1] == '\t'))
647 line.erase(line.size() - 1, std::string::npos);
648
649 std::match_results<std::string::const_iterator> matches;
650 const std::string regex =
651 "set[ \t]+" + parameter_name + "[ \t]*=[ \t]*(.*)";
652 if (std::regex_match(line, matches, std::regex(regex)))
653 {
654 // Since the line as a whole matched, the 'matches' variable needs
655 // to contain two entries: [0] denotes the whole string, and [1] the
656 // one that was matched by the '(.*)' expression.
657 Assert(matches.size() == 2, dealii::ExcInternalError());
658 return_string = std::string(matches[1].first, matches[1].second);
659 return_value =
660 std::max(return_value, Utilities::string_to_int(return_string));
661 }
662 }
663
664 return return_value;
665}
666
667
668
669unsigned int
670get_dimension(const std::string &file_name)
671{
672 const std::string dimension =
673 get_last_value_of_parameter(file_name, "dimension");
674
675 if (dimension.size() > 0)
676 {
677 // Extracted from ASPECT
678 // A common problem is that people have .prm files that were generated
679 // on Windows, but then run this on Linux where the line endings are
680 // different. This is pernicious because it means that the conversion
681 // of a string such as "2\r" to an integer fails, but if we print
682 // this string, it comes out completely garbled because it contains
683 // a carriage-return without a newline -- so the error message looks
684 // like this:
685 //
686 // >. While reading the dimension from the input file, Lethe found
687 // a string that can not be converted to an integer: <2
688 //
689 // Note how the end of the error message overwrites the beginning
690 // of the line.
691 //
692 // To avoid this kind of error, specifically test up front that the
693 // text in question does not contain '\r' characters. If we are on
694 // linux, then this kind of character would means that the line endings
695 // are wrong. On the other hand, if we are on windows, then the
696 // getline command we have used in finding 'dimension' would have
697 // filtered it out. So its presence points to a problem.
698
699 AssertThrow(
700 dimension.find('\r') == std::string::npos,
701 dealii::ExcMessage(
702 "It appears that your input file uses Windows-style "
703 "line endings ('\\r\\n') but you are running on a system where "
704 "the C++ run time environment expects input files to have "
705 "Unix-style line endings ('\\n'). You need to convert your "
706 "input file to use the correct line endings before running "
707 "Lethe with it."));
708 try
709 {
710 return dealii::Utilities::string_to_int(dimension);
711 }
712 catch (...)
713 {
714 AssertThrow(false,
715 dealii::ExcMessage(
716 "While reading the dimension from the input file, "
717 "Lethe found a string that can not be converted to "
718 "an integer: <" +
719 dimension + ">."));
720 return 0; // we should never get here.
721 }
722 }
723 else
724 {
725 AssertThrow(
726 false,
727 dealii::ExcMessage(
728 "While reading the dimension from the input file, "
729 "Lethe found a value that is neither 2 or 3. Since August 2023, "
730 "Lethe requires that the user explicitly specify the dimension of the problem within the parameter file. This can be achieved by adding set dimension = 2 or set dimension = 3 within the parameter file"));
731 }
732}
733
734
735int
736get_max_subsection_size(const std::string &file_name,
737 const bool require_subsection_size)
738{
739 int max_number_of_boundary_conditions =
740 get_max_value_of_parameter(file_name, "number");
741
742 AssertThrow(
743 !require_subsection_size || max_number_of_boundary_conditions >= 0,
744 dealii::ExcMessage(
745 "Your parameter file does not contain any indication for the number of boundary conditions for any physics supported by Lethe. Since November 2023, Lethe requires that a \"boundary conditions\" subsection is present with at least \"number=0\" "));
746
747 return std::max(max_number_of_boundary_conditions, 0);
748}
749
750/**
751 * @brief Concatenate words which are passed from the command line to a main function
752 * into a single string in which the words are separated by space.
753 *
754 * @param[in] argc number of arguments in C style.
755 * @param[in] argv arguments themselves in C style.
756 */
757std::string
758concatenate_strings(const int argc, char **argv)
759{
760 std::string result = std::string(argv[0]);
761
762 for (int i = 1; i < argc; ++i)
763 result = result + " " + std::string(argv[i]);
764
765 return result;
766}
767
768/**
769 * @brief Print the information of the deal.II and Lethe branches
770 * used to run an application in the "git describe" format.
771 *
772 * @param[in] pcout Parallel output stream
773 */
774void
775print_version_info(const ConditionalOStream &pcout)
776{
777 (void)pcout;
778
779 // Copy the tags to be able to delete the first v
780 std::string lethe_tag = LETHE_GIT_FANCY_TAG;
781 std::string dealii_tag = DEAL_II_GIT_FANCY_TAG;
782
783 // Print tags using the right format
784 pcout << "lethe/" << lethe_tag.erase(0, 1) << " deal.II/"
785 << dealii_tag.erase(0, 1) << std::endl;
786 pcout << std::endl;
787}
788
789/**
790 * @brief Parse the arguments given to the application
791 *
792 * @param[in] argc Number of arguments
793 * @param[in] argv Array of strings representing the arguments
794 * @return Tuple containing a map with the command line flags
795 * and a boolean set to 1 for the specific flag; and a vector of
796 * strings with the rest of the arguments that are not flags; in Lethe,
797 * this corresponds to the parameter file given to the application.
798 */
799std::tuple<std::map<std::string, bool>, std::vector<std::string>>
800parse_args(int argc, char **argv)
801{
802 int argi = 1; // skip program name
803 std::map<std::string, bool> options{};
804 for (; argi < argc; ++argi)
805 {
806 std::string arg{argv[argi]};
807 auto n{arg.length()};
808 if (n == 0 || arg[0] != '-' || n == 1)
809 { // empty arg or single hyphen
810 break;
811 }
812 if (arg[1] != '-')
813 { // short option
814 for (decltype(n) i = 1; i < n; ++i)
815 {
816 options[std::string{'-'} + std::string{arg[i]}] = true;
817 }
818 }
819 else
820 { // long option
821 if (n == 2)
822 { // done options on "--" and skip it
823 argi++;
824 break;
825 }
826 options[arg] = true;
827 }
828 }
829 std::vector<std::string> args{};
830 for (; argi < argc; ++argi)
831 {
832 std::string arg{argv[argi]};
833 args.push_back(arg);
834 }
835 return {options, args};
836}
837
838void
839warn_deprecated_parameter_value(const std::string &parameter_name,
840 const std::string &deprecated_value,
841 const std::string &new_value)
842{
843 // Parameters are parsed by every MPI process, but the warning should only be
844 // printed once. Parameters may also be parsed without MPI being initialized
845 // (e.g. in unit tests).
846 const bool is_first_process =
847 !Utilities::MPI::job_supports_mpi() ||
848 Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0;
849 const ConditionalOStream pcout(std::cout, is_first_process);
850
851 pcout << "Warning: the value \"" << deprecated_value
852 << "\" of the parameter \"" << parameter_name
853 << "\" is deprecated and will be removed. Use \"" << new_value
854 << "\" instead." << std::endl;
855}
856
857/**
858 * @brief Print the parameters given by the parameter file of the application
859 *
860 * @param[in] pcout Parallel output stream
861 * @param[in] prm Object containing the parameters parsed from the parameter
862 * file
863 */
864void
865print_parameters_to_output_file(const ConditionalOStream &pcout,
866 const ParameterHandler &prm,
867 const std::string &file_name)
868{
869 const std::string print_parameters =
870 get_last_value_of_parameter(file_name, "print parameters");
871
872 if (print_parameters == "only changed")
873 {
874 prm.print_parameters(pcout.get_stream(),
875 ParameterHandler::OutputStyle::PRM |
876 ParameterHandler::OutputStyle::Short |
877 ParameterHandler::KeepDeclarationOrder |
878 ParameterHandler::KeepOnlyChanged);
879 pcout << std::endl << std::endl;
880 }
881 else if (print_parameters == "all")
882 {
883 prm.print_parameters(pcout.get_stream(),
884 ParameterHandler::OutputStyle::PRM |
885 ParameterHandler::OutputStyle::Short |
886 ParameterHandler::KeepDeclarationOrder);
887 pcout << std::endl << std::endl;
888 }
889}
890
891void
893{
894 boost::replace_all(s, "\\n", "\n");
895 boost::replace_all(s, "\\t", "\t");
896 boost::replace_all(s, "\\r", "\r");
897 boost::replace_all(s, "\\b", "\b");
898 boost::replace_all(s, "\\v", "\v");
899}
900
901void
902print_comment_to_output_file(const ConditionalOStream &pcout,
903 ParameterHandler &prm)
904{
905 std::string comment_string = prm.get("comment message");
906 interpret_escape_sequences(comment_string);
907
908 if (!comment_string.empty())
909 {
910 pcout << "Comment message: " << std::endl;
911 pcout << comment_string << std::endl;
912 pcout << std::endl << std::endl;
913 }
914}
915
916void
917delete_vtu_and_pvd_files(const std::string &output_path)
918{
919 for (auto const &filename : std::filesystem::directory_iterator{output_path})
920 {
921 if (filename.path().extension() == ".vtu" ||
922 filename.path().extension() == ".pvd" ||
923 filename.path().extension() == ".pvtu")
924 std::filesystem::remove(filename.path());
925 }
926}
927
928template <int dim, typename VectorType>
929void
931 const Mapping<dim> &mapping,
932 const DoFHandler<dim> &dof_handler,
933 const VectorType &solution_field,
934 const std::vector<Point<dim>> &evaluation_points,
935 Utilities::MPI::RemotePointEvaluation<dim> &remote_point_evaluator,
936 std::vector<double> &evaluated_scalar_values,
937 const unsigned int first_selected_component)
938{
939 // Check if solution vector is relevant when running on multiple processes
940 if (Utilities::MPI::n_mpi_processes(dof_handler.get_mpi_communicator()) > 1)
941 AssertThrow(solution_field.has_ghost_elements(),
942 GhostElementsRequired("evaluate_values_at_points"));
943
944 // Reinitialize remote point evaluator if needed
945 if (!remote_point_evaluator.is_ready())
946 remote_point_evaluator.reinit(evaluation_points,
947 dof_handler.get_triangulation(),
948 mapping);
949
950 // Evaluate scalar values at points using RemotePointEvaluation if they are
951 // found within the domain
952 if (points_are_in_domain<dim>(evaluation_points, remote_point_evaluator))
953 evaluated_scalar_values = VectorTools::point_values<1>(
954 mapping,
955 dof_handler,
956 solution_field,
957 evaluation_points,
958 remote_point_evaluator,
959 VectorTools::EvaluationFlags::EvaluationFlags::avg,
960 first_selected_component);
961}
962
963template void
965 const Mapping<2> &mapping,
966 const DoFHandler<2> &dof_handler,
967 const GlobalVectorType &solution_field,
968 const std::vector<Point<2>> &evaluation_points,
969 Utilities::MPI::RemotePointEvaluation<2> &remote_point_evaluator,
970 std::vector<double> &evaluated_scalar_values,
971 const unsigned int first_selected_component);
972
973template void
975 const Mapping<3> &mapping,
976 const DoFHandler<3> &dof_handler,
977 const GlobalVectorType &solution_field,
978 const std::vector<Point<3>> &evaluation_points,
979 Utilities::MPI::RemotePointEvaluation<3> &remote_point_evaluator,
980 std::vector<double> &evaluated_scalar_values,
981 const unsigned int first_selected_component);
982
983template void
985 const Mapping<2> &mapping,
986 const DoFHandler<2> &dof_handler,
987 const GlobalBlockVectorType &solution_field,
988 const std::vector<Point<2>> &evaluation_points,
989 Utilities::MPI::RemotePointEvaluation<2> &remote_point_evaluator,
990 std::vector<double> &evaluated_scalar_values,
991 const unsigned int first_selected_component);
992
993template void
995 const Mapping<3> &mapping,
996 const DoFHandler<3> &dof_handler,
997 const GlobalBlockVectorType &solution_field,
998 const std::vector<Point<3>> &evaluation_points,
999 Utilities::MPI::RemotePointEvaluation<3> &remote_point_evaluator,
1000 std::vector<double> &evaluated_scalar_values,
1001 const unsigned int first_selected_component);
1002
1003#ifndef LETHE_USE_LDV
1004template void
1006 const Mapping<2> &mapping,
1007 const DoFHandler<2> &dof_handler,
1008 const LinearAlgebra::distributed::Vector<double> &solution_field,
1009 const std::vector<Point<2>> &evaluation_points,
1010 Utilities::MPI::RemotePointEvaluation<2> &remote_point_evaluator,
1011 std::vector<double> &evaluated_scalar_values,
1012 const unsigned int first_selected_component);
1013
1014template void
1016 const Mapping<3> &mapping,
1017 const DoFHandler<3> &dof_handler,
1018 const LinearAlgebra::distributed::Vector<double> &solution_field,
1019 const std::vector<Point<3>> &evaluation_points,
1020 Utilities::MPI::RemotePointEvaluation<3> &remote_point_evaluator,
1021 std::vector<double> &evaluated_scalar_values,
1022 const unsigned int first_selected_component);
1023#endif
1024
1025template <int n_component, int dim, typename VectorType>
1026void
1028 const Mapping<dim> &mapping,
1029 const DoFHandler<dim> &dof_handler,
1030 const VectorType &solution_field,
1031 const std::vector<Point<dim>> &evaluation_points,
1032 Utilities::MPI::RemotePointEvaluation<dim> &remote_point_evaluator,
1033 std::vector<Tensor<1, dim, double>> &evaluated_vector_values,
1034 const unsigned int first_selected_component)
1035{
1036 // Check if solution vector is relevant when running on multiple processes
1037 if (Utilities::MPI::n_mpi_processes(dof_handler.get_mpi_communicator()) > 1)
1038 AssertThrow(solution_field.has_ghost_elements(),
1039 GhostElementsRequired("evaluate_values_at_points"));
1040
1041 // Reinitialize remote point evaluator if needed
1042 if (!remote_point_evaluator.is_ready())
1043 remote_point_evaluator.reinit(evaluation_points,
1044 dof_handler.get_triangulation(),
1045 mapping);
1046
1047 // Evaluate scalar values at points using RemotePointEvaluation
1048 if (points_are_in_domain<dim>(evaluation_points, remote_point_evaluator))
1049 evaluated_vector_values = VectorTools::point_values<n_component>(
1050 mapping,
1051 dof_handler,
1052 solution_field,
1053 evaluation_points,
1054 remote_point_evaluator,
1055 VectorTools::EvaluationFlags::EvaluationFlags::avg,
1056 first_selected_component);
1057}
1058
1059template void
1061 const Mapping<2> &mapping,
1062 const DoFHandler<2> &dof_handler,
1063 const GlobalVectorType &solution_field,
1064 const std::vector<Point<2>> &evaluation_points,
1065 Utilities::MPI::RemotePointEvaluation<2> &remote_point_evaluator,
1066 std::vector<Tensor<1, 2, double>> &evaluated_vector_values,
1067 const unsigned int first_selected_component);
1068
1069template void
1071 const Mapping<3> &mapping,
1072 const DoFHandler<3> &dof_handler,
1073 const GlobalVectorType &solution_field,
1074 const std::vector<Point<3>> &evaluation_points,
1075 Utilities::MPI::RemotePointEvaluation<3> &remote_point_evaluator,
1076 std::vector<Tensor<1, 3, double>> &evaluated_vector_values,
1077 const unsigned int first_selected_component);
1078
1079template void
1081 const Mapping<2> &mapping,
1082 const DoFHandler<2> &dof_handler,
1083 const GlobalBlockVectorType &solution_field,
1084 const std::vector<Point<2>> &evaluation_points,
1085 Utilities::MPI::RemotePointEvaluation<2> &remote_point_evaluator,
1086 std::vector<Tensor<1, 2, double>> &evaluated_vector_values,
1087 const unsigned int first_selected_component);
1088
1089template void
1091 const Mapping<3> &mapping,
1092 const DoFHandler<3> &dof_handler,
1093 const GlobalBlockVectorType &solution_field,
1094 const std::vector<Point<3>> &evaluation_points,
1095 Utilities::MPI::RemotePointEvaluation<3> &remote_point_evaluator,
1096 std::vector<Tensor<1, 3, double>> &evaluated_vector_values,
1097 const unsigned int first_selected_component);
1098
1099#ifndef LETHE_USE_LDV
1100template void
1102 const Mapping<2> &mapping,
1103 const DoFHandler<2> &dof_handler,
1104 const LinearAlgebra::distributed::Vector<double> &solution_field,
1105 const std::vector<Point<2>> &evaluation_points,
1106 Utilities::MPI::RemotePointEvaluation<2> &remote_point_evaluator,
1107 std::vector<Tensor<1, 2, double>> &evaluated_vector_values,
1108 const unsigned int first_selected_component);
1109
1110template void
1112 const Mapping<3> &mapping,
1113 const DoFHandler<3> &dof_handler,
1114 const LinearAlgebra::distributed::Vector<double> &solution_field,
1115 const std::vector<Point<3>> &evaluation_points,
1116 Utilities::MPI::RemotePointEvaluation<3> &remote_point_evaluator,
1117 std::vector<Tensor<1, 3, double>> &evaluated_vector_values,
1118 const unsigned int first_selected_component);
1119#endif
void evaluate_values_at_points(const Mapping< dim > &mapping, const DoFHandler< dim > &dof_handler, const VectorType &solution_field, const std::vector< Point< dim > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< dim > &remote_point_evaluator, std::vector< double > &evaluated_scalar_values, const unsigned int first_selected_component=0)
Evaluates the values of a scalar field at remote points of the domain from the mapping,...
Definition utilities.cc:930
bool points_are_in_domain(const std::vector< Point< dim > > &evaluation_points, const Utilities::MPI::RemotePointEvaluation< dim, dim > &remote_point_evaluator)
Checks that the evaluation points are within the simulated domain and returns true if all points are ...
Definition utilities.h:1527
Common exception declarations shared across multiple solvers in Lethe.
GlobalVectorType VectorType
TableHandler make_table_scalars_vectors(const std::vector< T > &independent_values, const std::string &independent_column_name, const std::vector< std::vector< double > > &dependent_vector, const std::vector< std::string > &dependent_column_names, const unsigned int display_precision, const bool display_scientific_notation)
Generate a table from a vector of scalars (independent variable) and a vector of vectors of scalars (...
Definition utilities.cc:24
template void evaluate_values_at_points< 3, GlobalVectorType >(const Mapping< 3 > &mapping, const DoFHandler< 3 > &dof_handler, const GlobalVectorType &solution_field, const std::vector< Point< 3 > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< 3 > &remote_point_evaluator, std::vector< double > &evaluated_scalar_values, const unsigned int first_selected_component)
void check_file_exists(const std::string &file_name, const std::string &file_description)
Verify that a file that Lethe must read exists and can be opened, and raise an error stated in the us...
Definition utilities.cc:225
TableHandler make_table_scalars_tensors(const std::vector< T > &independent_vector, const std::string &independent_column_name, const std::vector< Tensor< 1, dim > > &dependent_vector, const std::vector< std::string > &dependent_column_names, const unsigned int display_precision, const bool display_scientific_notation)
Generate a table from a vector of scalars (independent variable) and a vector of Tensor<1,...
Definition utilities.cc:59
template void evaluate_values_at_points< 3, 3, GlobalVectorType >(const Mapping< 3 > &mapping, const DoFHandler< 3 > &dof_handler, const GlobalVectorType &solution_field, const std::vector< Point< 3 > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< 3 > &remote_point_evaluator, std::vector< Tensor< 1, 3, double > > &evaluated_vector_values, const unsigned int first_selected_component)
unsigned int get_dimension(const std::string &file_name)
Extract the dimension in which to run Lethe from the contents of the parameter file....
Definition utilities.cc:670
std::string get_last_value_of_parameter(const std::string &file_name, const std::string &parameter_name)
Retrieves the value of a specific parameter from an input file. Returns an empty string if the parame...
Definition utilities.cc:585
std::tuple< std::map< std::string, bool >, std::vector< std::string > > parse_args(int argc, char **argv)
Parse the arguments given to the application.
Definition utilities.cc:800
TableHandler make_table_tensors_scalars(const std::vector< Tensor< 1, dim > > &independent_vector, const std::vector< std::string > &independent_column_names, const std::vector< double > &dependent_vector, const std::string &dependent_column_name, const unsigned int display_precision, const bool display_scientific_notation)
Generate a table from a vector of Tensor<1,dim> (independent variables) and a vector of scalars (depe...
Definition utilities.cc:189
template void evaluate_values_at_points< 2, 2, GlobalVectorType >(const Mapping< 2 > &mapping, const DoFHandler< 2 > &dof_handler, const GlobalVectorType &solution_field, const std::vector< Point< 2 > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< 2 > &remote_point_evaluator, std::vector< Tensor< 1, 2, double > > &evaluated_vector_values, const unsigned int first_selected_component)
void fill_table_from_file(TableHandler &table, const std::string &file_name, const std::string &delimiter)
Reads a file generated by a deal.II TableHandler and fills a TableHandler with its data.
Definition utilities.cc:253
void create_output_folder(const std::string &dirname)
Creates the simulation output folder.
Definition utilities.cc:401
void print_parameters_to_output_file(const ConditionalOStream &pcout, const ParameterHandler &prm, const std::string &file_name)
Print the parameters given by the parameter file of the application.
Definition utilities.cc:865
void delete_output_folder(const std::string &dirname)
Delete the output folder and its content.
Definition utilities.cc:407
void delete_vtu_and_pvd_files(const std::string &output_path)
Delete vtu and pvd files.
Definition utilities.cc:917
void fill_string_vectors_from_file(std::map< std::string, std::vector< std::string > > &map, const std::string &file, const std::string &delimiter)
Function that read a file that was build from a dealii table and create a map with the key being the ...
Definition utilities.cc:352
void evaluate_values_at_points(const Mapping< dim > &mapping, const DoFHandler< dim > &dof_handler, const VectorType &solution_field, const std::vector< Point< dim > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< dim > &remote_point_evaluator, std::vector< double > &evaluated_scalar_values, const unsigned int first_selected_component)
Evaluates the values of a scalar field at remote points of the domain from the mapping,...
Definition utilities.cc:930
int get_max_subsection_size(const std::string &file_name, const bool require_subsection_size)
Extract the maximum value between the number of boundary conditions and the number of manifolds from ...
Definition utilities.cc:736
void print_version_info(const ConditionalOStream &pcout)
Print the information of the deal.II and Lethe branches used to run an application in the "git descri...
Definition utilities.cc:775
void fill_vectors_from_file(std::map< std::string, std::vector< double > > &map, const std::string &file, const std::string &delimiter)
function that read a file that was build from a dealii table and fill 2 vectors. The first vector con...
Definition utilities.cc:299
int get_max_value_of_parameter(const std::string &file_name, const std::string &parameter_name)
Definition utilities.cc:625
std::string concatenate_strings(const int argc, char **argv)
Concatenate words which are passed from the command line to a main function into a single string in w...
Definition utilities.cc:758
template void evaluate_values_at_points< 3, GlobalBlockVectorType >(const Mapping< 3 > &mapping, const DoFHandler< 3 > &dof_handler, const GlobalBlockVectorType &solution_field, const std::vector< Point< 3 > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< 3 > &remote_point_evaluator, std::vector< double > &evaluated_scalar_values, const unsigned int first_selected_component)
TableHandler make_table_tensors_tensors(const std::vector< Tensor< 1, dim > > &independent_vector, const std::vector< std::string > &independent_column_names, const std::vector< Tensor< 1, dim > > &dependent_vector, const std::vector< std::string > &dependent_column_names, const unsigned int display_precision, const bool display_scientific_notation)
Generate a table from a vector of Tensor<1,dim> (independent variables) and a vector of Tensor<1,...
Definition utilities.cc:147
template void evaluate_values_at_points< 3, 3, GlobalBlockVectorType >(const Mapping< 3 > &mapping, const DoFHandler< 3 > &dof_handler, const GlobalBlockVectorType &solution_field, const std::vector< Point< 3 > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< 3 > &remote_point_evaluator, std::vector< Tensor< 1, 3, double > > &evaluated_vector_values, const unsigned int first_selected_component)
template void evaluate_values_at_points< 2, 2, GlobalBlockVectorType >(const Mapping< 2 > &mapping, const DoFHandler< 2 > &dof_handler, const GlobalBlockVectorType &solution_field, const std::vector< Point< 2 > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< 2 > &remote_point_evaluator, std::vector< Tensor< 1, 2, double > > &evaluated_vector_values, const unsigned int first_selected_component)
void warn_deprecated_parameter_value(const std::string &parameter_name, const std::string &deprecated_value, const std::string &new_value)
Print a warning stating that a deprecated value was given to a parameter, along with the value which ...
Definition utilities.cc:839
void interpret_escape_sequences(std::string &s)
Identify escape sequences listed below and apply them to the string.
Definition utilities.cc:892
template void evaluate_values_at_points< 2, GlobalBlockVectorType >(const Mapping< 2 > &mapping, const DoFHandler< 2 > &dof_handler, const GlobalBlockVectorType &solution_field, const std::vector< Point< 2 > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< 2 > &remote_point_evaluator, std::vector< double > &evaluated_scalar_values, const unsigned int first_selected_component)
template void evaluate_values_at_points< 2, GlobalVectorType >(const Mapping< 2 > &mapping, const DoFHandler< 2 > &dof_handler, const GlobalVectorType &solution_field, const std::vector< Point< 2 > > &evaluation_points, Utilities::MPI::RemotePointEvaluation< 2 > &remote_point_evaluator, std::vector< double > &evaluated_scalar_values, const unsigned int first_selected_component)
void print_comment_to_output_file(const ConditionalOStream &pcout, ParameterHandler &prm)
Print a user comment at the beginning of the simulation on the console output.
Definition utilities.cc:902
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17
dealii::TrilinosWrappers::MPI::BlockVector GlobalBlockVectorType
Definition vector.h:18