libMesh
meshfunction_solution_transfer.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 
20 #include "libmesh/meshfunction_solution_transfer.h"
21 
22 #include "libmesh/system.h"
23 #include "libmesh/numeric_vector.h"
24 #include "libmesh/mesh_function.h"
25 #include "libmesh/node.h"
26 
27 namespace libMesh
28 {
29 
30 MeshFunctionSolutionTransfer::MeshFunctionSolutionTransfer(const libMesh::Parallel::Communicator & comm_in) :
31  SolutionTransfer(comm_in)
32 {}
33 
35 {}
36 
37 void
39  const Variable & to_var)
40 {
41  // This only works when transferring to a Lagrange variable
42  libmesh_assert(to_var.type().family == LAGRANGE);
43 
44  unsigned int to_var_num = to_var.number();
45 
46  System * from_sys = from_var.system();
47  System * to_sys = to_var.system();
48 
49  // Only works with a serialized mesh to transfer from!
50  libmesh_assert(from_sys->get_mesh().is_serial());
51 
52  unsigned int to_sys_num = to_sys->number();
53 
54  EquationSystems & from_es = from_sys->get_equation_systems();
55 
56  //Create a serialized version of the solution vector
57  std::unique_ptr<NumericVector<Number>> serialized_solution = NumericVector<Number>::build(from_sys->get_mesh().comm());
58  serialized_solution->init(from_sys->n_dofs(), false, SERIAL);
59 
60  // Need to pull down a full copy of this vector on every processor
61  // so we can get values in parallel
62  from_sys->solution->localize(*serialized_solution);
63 
64  MeshFunction from_func(from_es, *serialized_solution, from_sys->get_dof_map(), to_var_num);
65  from_func.init();
66 
67  // Now loop over the nodes of the 'To' mesh setting values for each variable.
68  for (const auto & node : to_sys->get_mesh().local_node_ptr_range())
69  to_sys->solution->set(node->dof_number(to_sys_num, to_var_num, 0), from_func(*node)); // 0 is for the value component
70 
71  to_sys->solution->close();
72  to_sys->update();
73 }
74 
75 } // namespace libMesh
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::System::get_equation_systems
const EquationSystems & get_equation_systems() const
Definition: system.h:720
libMesh::Variable::system
System * system() const
Definition: variable.h:92
libMesh::FEType::family
FEFamily family
The type of finite element.
Definition: fe_type.h:203
libMesh::MeshBase::is_serial
virtual bool is_serial() const
Definition: mesh_base.h:159
libMesh::MeshFunction::init
virtual void init() override
Override the FunctionBase::init() member function by calling our own and specifying the Trees::NODES ...
Definition: mesh_function.h:110
libMesh::Variable::number
unsigned int number() const
Definition: variable.h:106
libMesh::SolutionTransfer
Base class for objects that allow transferring variable values between different systems with differe...
Definition: solution_transfer.h:40
libMesh::MeshFunction
This class provides function-like objects for data distributed over a mesh.
Definition: mesh_function.h:53
libMesh::SERIAL
Definition: enum_parallel_type.h:35
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::ParallelObject::comm
const Parallel::Communicator & comm() const
Definition: parallel_object.h:94
libMesh::System::number
unsigned int number() const
Definition: system.h:2075
libMesh::Variable::type
const FEType & type() const
Definition: variable.h:119
libMesh::MeshFunctionSolutionTransfer::~MeshFunctionSolutionTransfer
virtual ~MeshFunctionSolutionTransfer()
Definition: meshfunction_solution_transfer.C:34
libMesh::NumericVector::build
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
Definition: numeric_vector.C:49
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::System::get_mesh
const MeshBase & get_mesh() const
Definition: system.h:2083
libMesh::MeshFunctionSolutionTransfer::transfer
virtual void transfer(const Variable &from_var, const Variable &to_var) override
Transfer the values of a variable to another.
Definition: meshfunction_solution_transfer.C:38
libMesh::MeshBase::local_node_ptr_range
virtual SimpleRange< node_iterator > local_node_ptr_range()=0
libMesh::Variable
This class defines the notion of a variable in the system.
Definition: variable.h:49
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::System::solution
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1539
libMesh::System::get_dof_map
const DofMap & get_dof_map() const
Definition: system.h:2099
libMesh::System::n_dofs
dof_id_type n_dofs() const
Definition: system.C:150
libMesh::LAGRANGE
Definition: enum_fe_family.h:36
libMesh::MeshFunctionSolutionTransfer::MeshFunctionSolutionTransfer
MeshFunctionSolutionTransfer(const libMesh::Parallel::Communicator &comm)
Definition: meshfunction_solution_transfer.C:30
libMesh::System::update
virtual void update()
Update the local values to reflect the solution on neighboring processors.
Definition: system.C:408