libMesh
miscellaneous_ex8.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 // <h1>Miscellaneous Example 8 - Meshfree Interpolation Utilities.</h1>
21 // \author Benjamin S. Kirk
22 // \date 2012
23 //
24 // LibMesh provides some utilities for pointcloud-type interpolation, as
25 // demonstrated in this example.
26 
27 // Example include files
28 #include "libmesh/libmesh.h"
29 #include "libmesh/meshfree_interpolation.h"
30 #include "libmesh/radial_basis_interpolation.h"
31 #include "libmesh/mesh.h"
32 #include "libmesh/equation_systems.h"
33 #include "libmesh/numeric_vector.h"
34 #include "libmesh/tecplot_io.h"
35 #include "libmesh/threads.h"
36 #include "libmesh/node.h"
38 
39 // C++ includes
40 #include <cstdlib>
41 
42 
43 // Bring in everything from the libMesh namespace
44 using namespace libMesh;
45 
46 
47 void create_random_point_cloud (const unsigned int Npts,
48  std::vector<Point> & pts,
49  const Real max_range = 10)
50 {
51  libMesh::out << "Generating "<< Npts << " point cloud...";
52  pts.resize(Npts);
53 
54  for (size_t i=0;i<Npts;i++)
55  {
56  pts[i](0) = max_range * (std::rand() % 1000) / Real(1000);
57  pts[i](1) = max_range * (std::rand() % 1000) / Real(1000);
58  pts[i](2) = max_range * (std::rand() % 1000) / Real(1000);
59  }
60  libMesh::out << "done\n";
61 }
62 
63 
64 
66 {
67  const Real
68  x = p(0),
69  y = p(1),
70  z = p(2);
71 
72  return (x*x*x +
73  y*y*y*y +
74  z*z*z*z*z);
75 }
76 
77 
78 
80 {
81  const Real
82  x = p(0),
83  y = p(1),
84  z = p(2);
85 
86  return (x*x +
87  y*y +
88  z*z*z);
89 }
90 
92  const Parameters &,
93  const std::string &,
94  const std::string &)
95 {
96  return exact_solution_v(p);
97 }
98 
99 // We now define the function which provides the
100 // initialization routines for the "Convection-Diffusion"
101 // system. This handles things like setting initial
102 // conditions and boundary conditions.
104  const std::string & system_name)
105 {
106  // Get a reference to the Convection-Diffusion system object.
107  System & system =
108  es.get_system<System>(system_name);
109 
110  system.project_solution(exact_value, nullptr, es.parameters);
111 }
112 
113 
114 
115 
116 int main(int argc, char ** argv)
117 {
118  // Skip this example if we do not meet certain requirements
119  libmesh_example_requires(3 <= LIBMESH_DIM, "3D support");
120 #ifndef LIBMESH_HAVE_EIGEN
121  libmesh_example_requires(false, "--enable-eigen");
122 #endif
123 #ifndef LIBMESH_HAVE_ZLIB_H
124  libmesh_example_requires(false, "--enable-zlib");
125 #endif
126  // Nanoflann is giving me trouble with extended Reals
127 #ifdef LIBMESH_DEFAULT_TRIPLE_PRECISION
128  libmesh_example_requires(false, "--disable-triple-precision");
129 #endif
130 #ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION
131  libmesh_example_requires(false, "--disable-quadruple-precision");
132 #endif
133 
134  // Initialize libMesh.
135  LibMeshInit init (argc, argv);
136  {
137  // Demonstration case 1
138  {
139  std::vector<Point> tgt_pts;
140  std::vector<Number> tgt_data_idi, tgt_data_rbi;
141  std::vector<std::string> field_vars;
142 
143  field_vars.push_back("u");
144  field_vars.push_back("v");
145 
147  /* n_interp_pts = */ 8,
148  /* power = */ 2);
149 
150  RadialBasisInterpolation<3> rbi (init.comm());
151 
152  idi.set_field_variables (field_vars);
153  rbi.set_field_variables (field_vars);
154 
156  idi.get_source_points());
157 
158 
159  // Explicitly set the data values we will interpolate from
160  {
161  const std::vector<Point> & src_pts (idi.get_source_points());
162  std::vector<Number> & src_vals (idi.get_source_vals());
163 
164  src_vals.clear(); src_vals.reserve(2*src_pts.size());
165 
166  for (std::vector<Point>::const_iterator pt_it=src_pts.begin();
167  pt_it != src_pts.end(); ++pt_it)
168  {
169  src_vals.push_back (exact_solution_u (*pt_it));
170  src_vals.push_back (exact_solution_v (*pt_it));
171  }
172  }
173 
174  // give rbi the same info as idi
175  rbi.get_source_points() = idi.get_source_points();
176  rbi.get_source_vals() = idi.get_source_vals();
177 
178  idi.prepare_for_use();
179  rbi.prepare_for_use();
180 
181  libMesh::out << idi;
182 
183  // Interpolate to some other random points, and evaluate the result
184  {
186  tgt_pts);
187 
188  //tgt_pts = rbi.get_source_points();
189 
190  idi.interpolate_field_data (field_vars,
191  tgt_pts,
192  tgt_data_idi);
193 
194  rbi.interpolate_field_data (field_vars,
195  tgt_pts,
196  tgt_data_rbi);
197 
198  std::vector<Number>::const_iterator
199  v_idi = tgt_data_idi.begin(),
200  v_rbi = tgt_data_rbi.begin();
201 
202  for (std::vector<Point>::const_iterator p_it=tgt_pts.begin();
203  p_it!=tgt_pts.end(); ++p_it)
204  {
205  libMesh::out << "\nAt target point " << *p_it
206  << "\n u_interp_idi=" << *v_idi
207  << ", u_interp_rbi=" << *v_rbi
208  << ", u_exact=" << exact_solution_u(*p_it);
209  ++v_idi;
210  ++v_rbi;
211  libMesh::out << "\n v_interp_idi=" << *v_idi
212  << ", v_interp_rbi=" << *v_rbi
213  << ", v_exact=" << exact_solution_v(*p_it)
214  << std::endl;
215  ++v_idi;
216  ++v_rbi;
217  }
218  }
219  }
220 
221 
222  // Demonstration case 2
223  {
224  Mesh mesh_a(init.comm()), mesh_b(init.comm());
225 
226  mesh_a.read("struct.ucd.gz");
227  mesh_b.read("unstruct.ucd.gz");
228 
229  // Create equation systems objects.
231  es_a(mesh_a), es_b(mesh_b);
232 
233  System & sys_a = es_a.add_system<System>("src_system");
234  System & sys_b = es_b.add_system<System>("dest_system");
235 
236  sys_a.add_variable ("Cp", FIRST);
237  sys_b.add_variable ("Cp", FIRST);
238 
240  es_a.init();
241 
242  // Write out the initial conditions.
243  TecplotIO(mesh_a).write_equation_systems ("src.dat",
244  es_a);
245 
247  /* n_interp_pts = */ 4,
248  /* power = */ 2);
249  RadialBasisInterpolation<3> rbi (init.comm());
250 
251  std::vector<Point> & src_pts (idi.get_source_points());
252  std::vector<Number> & src_vals (idi.get_source_vals());
253  std::vector<std::string> field_vars;
254  field_vars.push_back("Cp");
255  idi.set_field_variables(field_vars);
256 
257  // We now will loop over every node in the source mesh
258  // and add it to a source point list, along with the solution
259  for (const auto & node : mesh_a.local_node_ptr_range())
260  {
261  src_pts.push_back(*node);
262  src_vals.push_back(sys_a.current_solution(node->dof_number(0, 0, 0)));
263  }
264 
265  rbi.set_field_variables(field_vars);
266  rbi.get_source_points() = idi.get_source_points();
267  rbi.get_source_vals() = idi.get_source_vals();
268 
269  // We have only set local values - prepare for use by gathering remote data
270  idi.prepare_for_use();
271  rbi.prepare_for_use();
272 
273  // Create a MeshlessInterpolationFunction that uses our InverseDistanceInterpolation
274  // object. Since each MeshlessInterpolationFunction shares the same InverseDistanceInterpolation
275  // object in a threaded environment we must also provide a locking mechanism.
276  {
277  Threads::spin_mutex mutex;
278  MeshlessInterpolationFunction mif(idi, mutex);
279 
280  // project the solution onto system b
281  es_b.init();
282  sys_b.project_solution (&mif);
283 
284  // Write the result
285  TecplotIO(mesh_b).write_equation_systems ("dest_idi.dat",
286  es_b);
287  }
288 
289  // Create a MeshlessInterpolationFunction that uses our RadialBasisInterpolation
290  // object. Since each MeshlessInterpolationFunction shares the same RadialBasisInterpolation
291  // object in a threaded environment we must also provide a locking mechanism.
292  {
293  Threads::spin_mutex mutex;
294  MeshlessInterpolationFunction mif(rbi, mutex);
295 
296  // project the solution onto system b
297  sys_b.project_solution (&mif);
298 
299  // Write the result
300  TecplotIO(mesh_b).write_equation_systems ("dest_rbi.dat",
301  es_b);
302  }
303  }
304  }
305  return 0;
306 }
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::Number
Real Number
Definition: libmesh_common.h:195
libMesh::Mesh
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
libMesh::EquationSystems::add_system
virtual System & add_system(const std::string &system_type, const std::string &name)
Add the system of type system_type named name to the systems array.
Definition: equation_systems.C:345
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::EquationSystems::get_system
const T_sys & get_system(const std::string &name) const
Definition: equation_systems.h:757
exact_solution_u
Real exact_solution_u(const Point &p)
Definition: miscellaneous_ex8.C:65
libMesh::System::current_solution
Number current_solution(const dof_id_type global_dof_number) const
Definition: system.C:194
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::System::add_variable
unsigned int add_variable(const std::string &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:1069
exact_value
Number exact_value(const Point &p, const Parameters &, const std::string &, const std::string &)
Definition: miscellaneous_ex8.C:91
libMesh::EquationSystems::init
virtual void init()
Initialize all the systems.
Definition: equation_systems.C:96
libMesh::Point
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
libMesh::UnstructuredMesh::read
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false) override
Reads the file specified by name.
Definition: unstructured_mesh.C:620
main
int main(int argc, char **argv)
Definition: miscellaneous_ex8.C:116
create_random_point_cloud
void create_random_point_cloud(const unsigned int Npts, std::vector< Point > &pts, const Real max_range=10)
Definition: miscellaneous_ex8.C:47
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
meshless_interpolation_function.h
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::MeshOutput::write_equation_systems
virtual void write_equation_systems(const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
This method implements writing a mesh with data to a specified file where the data is taken from the ...
Definition: mesh_output.C:31
libMesh::InverseDistanceInterpolation
Inverse distance interpolation.
Definition: meshfree_interpolation.h:179
libMesh::TecplotIO
This class implements writing meshes in the Tecplot format.
Definition: tecplot_io.h:43
libMesh::System::attach_init_function
void attach_init_function(void fptr(EquationSystems &es, const std::string &name))
Register a user function to use in initializing the system.
Definition: system.C:1720
exact_solution_v
Real exact_solution_v(const Point &p)
Definition: miscellaneous_ex8.C:79
libMesh::MeshlessInterpolationFunction
Definition: meshless_interpolation_function.h:44
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::MeshfreeInterpolation::set_field_variables
void set_field_variables(const std::vector< std::string > &names)
Defines the field variable(s) we are responsible for, and importantly their assumed ordering.
Definition: meshfree_interpolation.h:110
libMesh::System::project_solution
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr) const
Projects arbitrary functions onto the current solution.
Definition: system_projection.C:950
libMesh::RadialBasisInterpolation
Radial Basis Function interpolation.
Definition: radial_basis_interpolation.h:43
init_sys
void init_sys(EquationSystems &es, const std::string &system_name)
Definition: miscellaneous_ex8.C:103
libMesh::out
OStreamProxy out
libMesh::FIRST
Definition: enum_order.h:42
libMesh::Threads::spin_mutex
Spin mutex.
Definition: threads_none.h:127
libMesh::Parameters
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:59
libMesh::EquationSystems::parameters
Parameters parameters
Data structure holding arbitrary parameters.
Definition: equation_systems.h:557