Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 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 : 10 : #include "PolyLineMeshGenerator.h" 11 : 12 : #include "CastUniquePointer.h" 13 : #include "MooseMeshUtils.h" 14 : #include "MooseUtils.h" 15 : 16 : #include "libmesh/elem.h" 17 : #include "libmesh/int_range.h" 18 : #include "libmesh/unstructured_mesh.h" 19 : 20 : registerMooseObject("MooseApp", PolyLineMeshGenerator); 21 : 22 : InputParameters 23 15053 : PolyLineMeshGenerator::validParams() 24 : { 25 15053 : InputParameters params = MeshGenerator::validParams(); 26 : 27 15053 : params.addParam<std::vector<Point>>("points", "The points defining the polyline, in order"); 28 : 29 15053 : params.addParam<bool>("loop", false, "Whether edges should form a closed loop"); 30 : 31 15053 : params.addParam<BoundaryName>( 32 : "start_boundary", "start", "Boundary to assign to (non-looped) polyline start"); 33 : 34 15053 : params.addParam<BoundaryName>( 35 : "end_boundary", "end", "Boundary to assign to (non-looped) polyline end"); 36 : 37 45159 : params.addParam<unsigned int>( 38 30106 : "num_edges_between_points", 1, "How many Edge elements to build between each point pair"); 39 : 40 15053 : params.addClassDescription("Generates meshes from edges connecting a list of points."); 41 : 42 15053 : return params; 43 0 : } 44 : 45 394 : PolyLineMeshGenerator::PolyLineMeshGenerator(const InputParameters & parameters) 46 : : MeshGenerator(parameters), 47 394 : _points(getParam<std::vector<Point>>("points")), 48 394 : _loop(getParam<bool>("loop")), 49 394 : _start_boundary(getParam<BoundaryName>("start_boundary")), 50 394 : _end_boundary(getParam<BoundaryName>("end_boundary")), 51 788 : _num_edges_between_points(getParam<unsigned int>("num_edges_between_points")) 52 : { 53 394 : if (_points.size() < 2) 54 0 : paramError("points", "At least 2 points are needed to define a polyline"); 55 : 56 394 : if (_loop && _points.size() < 3) 57 0 : paramError("points", "At least 3 points are needed to define a polygon"); 58 394 : } 59 : 60 : std::unique_ptr<MeshBase> 61 381 : PolyLineMeshGenerator::generate() 62 : { 63 381 : auto uptr_mesh = buildMeshBaseObject(); 64 381 : MeshBase & mesh = *uptr_mesh; 65 : 66 381 : const auto n_points = _points.size(); 67 1909 : for (auto i : make_range(n_points)) 68 : { 69 1538 : Point p = _points[i]; 70 1538 : mesh.add_point(p, i * _num_edges_between_points); 71 1538 : if (_num_edges_between_points > 1) 72 : { 73 100 : if (!_loop && (i + 1) == n_points) 74 10 : break; 75 : 76 90 : const auto ip1 = (i + 1) % n_points; 77 90 : const Point pvec = (_points[ip1] - p) / _num_edges_between_points; 78 : 79 290 : for (auto j : make_range(1u, _num_edges_between_points)) 80 : { 81 200 : p += pvec; 82 200 : mesh.add_point(p, i * _num_edges_between_points + j); 83 : } 84 : } 85 : } 86 : 87 381 : const auto n_segments = _loop ? n_points : (n_points - 1); 88 381 : const auto n_elem = n_segments * _num_edges_between_points; 89 381 : const auto max_nodes = n_points * _num_edges_between_points; 90 2077 : for (auto i : make_range(n_elem)) 91 : { 92 1696 : const auto ip1 = (i + 1) % max_nodes; 93 1696 : auto elem = Elem::build(EDGE2); 94 1696 : elem->set_node(0, mesh.node_ptr(i)); 95 1696 : elem->set_node(1, mesh.node_ptr(ip1)); 96 1696 : elem->set_id() = i; 97 1696 : mesh.add_elem(std::move(elem)); 98 1696 : } 99 : 100 381 : if (!_loop) 101 : { 102 42 : BoundaryInfo & bi = mesh.get_boundary_info(); 103 126 : std::vector<BoundaryName> bdy_names{_start_boundary, _end_boundary}; 104 42 : std::vector<boundary_id_type> ids = MooseMeshUtils::getBoundaryIDs(mesh, bdy_names, true); 105 42 : bi.add_side(mesh.elem_ptr(0), 0, ids[0]); 106 42 : bi.add_side(mesh.elem_ptr(n_elem - 1), 1, ids[1]); 107 42 : } 108 : 109 381 : mesh.prepare_for_use(); 110 : 111 381 : return uptr_mesh; 112 42 : }