www.mooseframework.org
SphereSurfaceMeshGenerator.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
11 #include "CastUniquePointer.h"
12 
13 // libMesh includes
14 #include "libmesh/face_tri3.h"
15 #include "libmesh/mesh_modification.h"
16 #include "libmesh/mesh_refinement.h"
17 #include "libmesh/sphere.h"
18 #include "libmesh/unstructured_mesh.h"
19 #include "libmesh/replicated_mesh.h"
20 
22 
23 template <>
24 InputParameters
26 {
27  InputParameters params = validParams<MeshGenerator>();
28  params.addClassDescription(
29  "Generated sphere mesh - a two dimensional manifold embedded in three dimensional space");
30  params.addParam<Real>("radius", 1.0, "Sphere radius");
31  params.addParam<Point>("center", Point(0, 0, 0), "Center of the sphere");
32  params.addParam<unsigned int>(
33  "depth", 3, "Iteration steps in the triangle bisection construction");
34  return params;
35 }
36 
37 SphereSurfaceMeshGenerator::SphereSurfaceMeshGenerator(const InputParameters & parameters)
38  : MeshGenerator(parameters),
39  _radius(getParam<Real>("radius")),
40  _center(getParam<Point>("center")),
41  _depth(getParam<unsigned int>("depth"))
42 {
43 }
44 
45 std::unique_ptr<MeshBase>
47 {
48  // Have MOOSE construct the correct libMesh::Mesh object using Mesh block and CLI parameters.
49  auto mesh = _mesh->buildMeshBaseObject();
50  mesh->set_mesh_dimension(2);
51  mesh->set_spatial_dimension(3);
52 
53  const Sphere sphere(_center, _radius);
54 
55  // icosahedron points (using golden ratio rectangle construction)
56  const Real phi = (1.0 + std::sqrt(5.0)) / 2.0;
57  const Real X = std::sqrt(1.0 / (phi * phi + 1.0));
58  const Real Z = X * phi;
59  const Point vdata[12] = {{-X, 0.0, Z},
60  {X, 0.0, Z},
61  {-X, 0.0, -Z},
62  {X, 0.0, -Z},
63  {0.0, Z, X},
64  {0.0, Z, -X},
65  {0.0, -Z, X},
66  {0.0, -Z, -X},
67  {Z, X, 0.0},
68  {-Z, X, 0.0},
69  {Z, -X, 0.0},
70  {-Z, -X, 0.0}};
71  for (unsigned int i = 0; i < 12; ++i)
72  mesh->add_point(vdata[i] * _radius + _center, i);
73 
74  // icosahedron faces
75  const unsigned int tindices[20][3] = {{0, 4, 1}, {0, 9, 4}, {9, 5, 4}, {4, 5, 8}, {4, 8, 1},
76  {8, 10, 1}, {8, 3, 10}, {5, 3, 8}, {5, 2, 3}, {2, 7, 3},
77  {7, 10, 3}, {7, 6, 10}, {7, 11, 6}, {11, 0, 6}, {0, 1, 6},
78  {6, 1, 10}, {9, 0, 11}, {9, 11, 2}, {9, 2, 5}, {7, 2, 11}};
79  for (unsigned int i = 0; i < 20; ++i)
80  {
81  Elem * elem = mesh->add_elem(new Tri3);
82  elem->set_node(0) = mesh->node_ptr(tindices[i][0]);
83  elem->set_node(1) = mesh->node_ptr(tindices[i][1]);
84  elem->set_node(2) = mesh->node_ptr(tindices[i][2]);
85  }
86 
87  // we need to prepare distributed meshes before using refinement
88  if (!mesh->is_replicated())
89  mesh->prepare_for_use(/*skip_renumber =*/false);
90 
91  // Now we have the beginnings of a sphere.
92  // Add some more elements by doing uniform refinements and
93  // popping nodes to the boundary.
94  MeshRefinement mesh_refinement(*mesh);
95 
96  // Loop over the elements, refine, pop nodes to boundary.
97  for (unsigned int r = 0; r < _depth; ++r)
98  {
99  mesh_refinement.uniformly_refine(1);
100 
101  auto it = mesh->active_nodes_begin();
102  const auto end = mesh->active_nodes_end();
103 
104  for (; it != end; ++it)
105  {
106  Node & node = **it;
107  node = sphere.closest_point(node);
108  }
109  }
110 
111  // Flatten the AMR mesh to get rid of inactive elements
112  MeshTools::Modification::flatten(*mesh);
113 
114  return dynamic_pointer_cast<MeshBase>(mesh);
115 }
SphereSurfaceMeshGenerator::_radius
const Real _radius
sphere radius
Definition: SphereSurfaceMeshGenerator.h:33
validParams< SphereSurfaceMeshGenerator >
InputParameters validParams< SphereSurfaceMeshGenerator >()
Definition: SphereSurfaceMeshGenerator.C:25
SphereSurfaceMeshGenerator::generate
std::unique_ptr< MeshBase > generate() override
Definition: SphereSurfaceMeshGenerator.C:46
SphereSurfaceMeshGenerator::_depth
const unsigned int _depth
recursion levels for triangle subdivision
Definition: SphereSurfaceMeshGenerator.h:39
registerMooseObject
registerMooseObject("PhaseFieldApp", SphereSurfaceMeshGenerator)
SphereSurfaceMeshGenerator
Create a sphere surface mesh based on the recursive subdivision of the faces of a regular icosahedron...
Definition: SphereSurfaceMeshGenerator.h:24
SphereSurfaceMeshGenerator.h
SphereSurfaceMeshGenerator::_center
const Point _center
Definition: SphereSurfaceMeshGenerator.h:36
SphereSurfaceMeshGenerator::SphereSurfaceMeshGenerator
SphereSurfaceMeshGenerator(const InputParameters &parameters)
Definition: SphereSurfaceMeshGenerator.C:37