libMesh
Loading...
Searching...
No Matches
solution_components.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 mesh and solution file given, create a new solution file,
20// and copy all listed variables from the old solution to the new.
21
22#include "libmesh/libmesh.h"
23
24#include "libmesh/equation_systems.h"
25#include "libmesh/mesh.h"
26#include "libmesh/numeric_vector.h"
27#include "libmesh/id_types.h"
28#include "libmesh/elem.h"
29#include "libmesh/variable.h"
30
31unsigned char dim = 2; // This gets overridden by most mesh formats
32
33int main(int argc, char ** argv)
34{
35 using namespace libMesh;
36
37 LibMeshInit init(argc, argv);
38
39 Mesh mesh1(init.comm(), dim);
40 EquationSystems es1(mesh1);
41
42 libMesh::out << "Usage: " << argv[0]
43 << " mesh oldsolution newsolution system1 variable1 [sys2 var2...]" << std::endl;
44
45 // We should have one system name for each variable name, and those
46 // get preceded by an even number of arguments.
47 libmesh_assert (!(argc % 2));
48
49 // We should have at least one system/variable pair following the
50 // initial arguments
51 libmesh_assert_greater_equal (argc, 6);
52
53 mesh1.read(argv[1]);
54 libMesh::out << "Loaded mesh " << argv[1] << std::endl;
55 Mesh mesh2(mesh1);
56 EquationSystems es2(mesh2);
57
58 es1.read(argv[2],
59 EquationSystems::READ_HEADER |
60 EquationSystems::READ_DATA |
61 EquationSystems::READ_ADDITIONAL_DATA |
62 EquationSystems::READ_BASIC_ONLY);
63 libMesh::out << "Loaded solution " << argv[2] << std::endl;
64
65 std::vector<unsigned int> old_sys_num((argc-4)/2),
66 new_sys_num((argc-4)/2),
67 old_var_num((argc-4)/2),
68 new_var_num((argc-4)/2);
69
70 std::vector<const System *> old_system((argc-4)/2);
71 std::vector<System *> new_system((argc-4)/2);
72
73 for (int argi = 4; argi < argc; argi += 2)
74 {
75 const char * sysname = argv[argi];
76 const char * varname = argv[argi+1];
77
78 const unsigned int pairnum = (argi-4)/2;
79
80 libmesh_assert(es1.has_system(sysname));
81
82 const System & old_sys = es1.get_system(sysname);
83 old_system[pairnum] = &old_sys;
84 old_sys_num[pairnum] = old_sys.number();
85
86 libmesh_assert(old_sys.has_variable(varname));
87
88 old_var_num[pairnum] = old_sys.variable_number(varname);
89
90 const Variable & variable = old_sys.variable(old_var_num[pairnum]);
91
92 std::string systype = old_sys.system_type();
93
94 System & new_sys = es2.add_system(systype, sysname);
95 new_system[pairnum] = &new_sys;
96 new_sys_num[pairnum] = new_sys.number();
97
98 new_var_num[pairnum] =
99 new_sys.add_variable(varname, variable.type(),
100 &variable.active_subdomains());
101 }
102
103 es2.init();
104
105 // A future version of this app should copy variables for
106 // non-solution vectors too.
107
108 // Copy over any nodal degree of freedom coefficients
109 MeshBase::const_node_iterator new_nit = mesh2.local_nodes_begin();
110
111 for (const auto & old_node : mesh1.local_node_ptr_range())
112 {
113 const Node * new_node = *new_nit++;
114
115 // Mesh::operator= hopefully preserved elem/node orderings...
116 libmesh_assert (*old_node == *new_node);
117
118 for (int argi = 4; argi < argc; argi += 2)
119 {
120 const unsigned int pairnum = (argi-4)/2;
121
122 const System & old_sys = *old_system[pairnum];
123 System & new_sys = *new_system[pairnum];
124
125 const unsigned int n_comp =
126 old_node->n_comp(old_sys_num[pairnum],old_var_num[pairnum]);
127 libmesh_assert_equal_to(n_comp,
128 new_node->n_comp(new_sys_num[pairnum],new_var_num[pairnum]));
129
130 for (unsigned int i=0; i<n_comp; i++)
131 {
132 const dof_id_type
133 old_index = old_node->dof_number
134 (old_sys_num[pairnum], old_var_num[pairnum], i),
135 new_index = new_node->dof_number
136 (new_sys_num[pairnum], new_var_num[pairnum], i);
137 new_sys.solution->set(new_index,(*old_sys.solution)(old_index));
138 }
139 }
140 }
141
142
143 // Copy over any element degree of freedom coefficients
144 MeshBase::const_element_iterator new_eit = mesh2.active_local_elements_begin();
145
146 for (const auto & old_elem : mesh1.active_local_element_ptr_range())
147 {
148 const Elem * new_elem = *new_eit++;
149
150 // Mesh::operator= hopefully preserved elem/node orderings...
151 libmesh_assert (*old_elem == *new_elem);
152
153 for (int argi = 4; argi < argc; argi += 2)
154 {
155 const unsigned int pairnum = (argi-4)/2;
156
157 const System & old_sys = *old_system[pairnum];
158 System & new_sys = *new_system[pairnum];
159
160 const unsigned int n_comp =
161 old_elem->n_comp(old_sys_num[pairnum],old_var_num[pairnum]);
162 libmesh_assert_equal_to(n_comp,
163 new_elem->n_comp(new_sys_num[pairnum],new_var_num[pairnum]));
164
165 for (unsigned int i=0; i<n_comp; i++)
166 {
167 const dof_id_type
168 old_index = old_elem->dof_number
169 (old_sys_num[pairnum], old_var_num[pairnum], i),
170 new_index = new_elem->dof_number
171 (new_sys_num[pairnum], new_var_num[pairnum], i);
172 new_sys.solution->set(new_index,(*old_sys.solution)(old_index));
173 }
174 }
175 }
176
177 es2.write(argv[3], EquationSystems::WRITE_DATA);
178
179 return 0;
180}
unsigned int n_comp(const unsigned int s, const unsigned int var) const
Definition dof_object.h:978
dof_id_type dof_number(const unsigned int s, const unsigned int var, const unsigned int comp) const
This is the base class from which all geometric element types are derived.
Definition elem.h:96
This is the EquationSystems class.
void write(std::string_view name, const XdrMODE, const unsigned int write_flags=(WRITE_DATA), bool partition_agnostic=true) const
Write the systems to disk using the XDR data format.
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
virtual void init()
Initialize all the systems.
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
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
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
A Node is like a Point, but with more information.
Definition node.h:55
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
const Variable & variable(unsigned int var) const
Return a constant reference to Variable var.
Definition system.C:2704
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition system.C:1344
virtual std::string system_type() const
Definition system.h:510
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition system.h:1655
unsigned int variable_number(std::string_view var) const
Definition system.C:1398
bool has_variable(std::string_view var) const
Definition system.C:1393
unsigned int number() const
Definition system.h:2393
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.
This class defines the notion of a variable in the system.
Definition variable.h:51
const std::set< subdomain_id_type > & active_subdomains() const
Definition variable.h:181
const FEType & type() const
Definition variable.h:144
The libMesh namespace provides an interface to certain functionality in the library.
OStreamProxy out
uint8_t dof_id_type
Definition id_types.h:67
unsigned char dim
The definition of the const_element_iterator struct.
Definition mesh_base.h:2556
The definition of the const_node_iterator struct.
Definition mesh_base.h:2607
int main()