Line data Source code
1 : // The libMesh Finite Element Library. 2 : // Copyright (C) 2002-2025 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 0 : MeshFunctionSolutionTransfer::MeshFunctionSolutionTransfer(const libMesh::Parallel::Communicator & comm_in) : 31 0 : SolutionTransfer(comm_in) 32 0 : {} 33 : 34 0 : MeshFunctionSolutionTransfer::~MeshFunctionSolutionTransfer() = default; 35 : 36 : void 37 0 : MeshFunctionSolutionTransfer::transfer(const Variable & from_var, 38 : const Variable & to_var) 39 : { 40 : // This only works when transferring to a Lagrange variable 41 0 : libmesh_assert(to_var.type().family == LAGRANGE); 42 : 43 0 : unsigned int to_var_num = to_var.number(); 44 : 45 0 : System * from_sys = from_var.system(); 46 0 : System * to_sys = to_var.system(); 47 : 48 : // Only works with a serialized mesh to transfer from! 49 0 : libmesh_assert(from_sys->get_mesh().is_serial()); 50 : 51 0 : unsigned int to_sys_num = to_sys->number(); 52 : 53 0 : EquationSystems & from_es = from_sys->get_equation_systems(); 54 : 55 : //Create a serialized version of the solution vector 56 0 : std::unique_ptr<NumericVector<Number>> serialized_solution = NumericVector<Number>::build(from_sys->get_mesh().comm()); 57 0 : serialized_solution->init(from_sys->n_dofs(), false, SERIAL); 58 : 59 : // Need to pull down a full copy of this vector on every processor 60 : // so we can get values in parallel 61 0 : from_sys->solution->localize(*serialized_solution); 62 : 63 0 : MeshFunction from_func(from_es, *serialized_solution, from_sys->get_dof_map(), to_var_num); 64 0 : from_func.init(); 65 : 66 : // Now loop over the nodes of the 'To' mesh setting values for each variable. 67 0 : for (const auto & node : to_sys->get_mesh().local_node_ptr_range()) 68 0 : to_sys->solution->set(node->dof_number(to_sys_num, to_var_num, 0), from_func(*node)); // 0 is for the value component 69 : 70 0 : to_sys->solution->close(); 71 0 : to_sys->update(); 72 0 : } 73 : 74 : } // namespace libMesh