libMesh
Loading...
Searching...
No Matches
meshavg.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19// Open the solution files named on standard input, take the average
20// of their fields' values, and output to a new solution file.
21#include "libmesh/libmesh.h"
22#include "libmesh/equation_systems.h"
23#include "libmesh/mesh.h"
24#include "libmesh/namebased_io.h"
25#include "libmesh/numeric_vector.h"
26#include "libmesh/int_range.h"
27
28using namespace libMesh;
29
30unsigned char dim = 2; // This gets overridden by most mesh formats
31
32int main(int argc, char ** argv)
33{
34 LibMeshInit init(argc, argv);
35
36 Mesh mesh1(init.comm(), dim);
37 EquationSystems es1(mesh1);
38
39 libMesh::out << "Usage: " << argv[0]
40 << " outputsolution mesh firstsolution [secondsolution ...]" << std::endl;
41
42 mesh1.read(argv[2]);
43 libMesh::out << "Loaded mesh " << argv[2] << std::endl;
44
45 mesh1.print_info();
46
47 es1.read(argv[3],
51 libMesh::out << "Loaded first solution " << argv[3] << std::endl;
52
53 es1.print_info();
54
55 std::vector<std::string> sysnames;
56 std::vector<std::unique_ptr<NumericVector<libMesh::Number>>> summed_solutions;
57
58 for (unsigned int s = 0; s != es1.n_systems(); ++s)
59 {
60 sysnames.push_back(es1.get_system(s).name());
61 summed_solutions.emplace_back(es1.get_system(s).solution->clone());
62 }
63
64 for (int i=4; i < argc; ++i)
65 {
66 Mesh mesh2(init.comm(), dim);
67 EquationSystems es2(mesh2);
68 mesh2.read(argv[2]);
69 es2.read(argv[i],
73 libMesh::out << "Loaded next solution " << argv[i] << std::endl;
74
75 for (auto s : index_range(sysnames))
76 {
77 libmesh_error_msg_if(!es2.has_system(sysnames[s]),
78 "EquationSystems object does not have " << sysnames[s]);
79
80 (*summed_solutions[s]) += *es2.get_system(s).solution;
81 }
82 }
83
84 int n_solutions = argc - 3;
85
86 for (auto s : index_range(sysnames))
87 {
88 (*summed_solutions[s]) /= n_solutions;
89 es1.get_system(s).solution->swap(*summed_solutions[s]);
90 es1.get_system(s).solution->close();
91 }
92
93 NameBasedIO(mesh1).write_equation_systems (argv[1], es1);
94
95 return 0;
96}
This is the EquationSystems class.
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
unsigned int n_systems() const
void read(std::string_view name, const XdrMODE, const unsigned int read_flags=(READ_HEADER|READ_DATA), bool partition_agnostic=true)
Read & initialize the systems from disk using the XDR data format.
bool has_system(std::string_view name) const
const T_sys & get_system(std::string_view name) const
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
Definition mesh_base.C:1755
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
This class supports simple reads and writes in any libMesh-supported format, by dispatching to one of...
virtual void write_equation_systems(const std::string &filename, const EquationSystems &es, const std::set< std::string > *system_names=nullptr) override
This method implements writing a mesh with data to a specified file where the data is taken from the ...
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false, bool skip_detect_interior_parents=false) override
Reads the file specified by name.
unsigned char dim
Definition meshavg.C:30
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition int_range.h:153
OStreamProxy out
int main()