Debian Codegraph @

packages/stk/stk_doc_tests/stk_mesh/howToIterateEntities.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include <gtest/gtest.h> // for TEST
36 #include <stddef.h> // for size_t
37 #include <set> // for set
38 #include <stk_io/FillMesh.hpp>
39 #include <stk_mesh/base/MetaData.hpp> // for MetaData, etc
40 #include <stk_mesh/base/BulkData.hpp> // for BulkData
41 #include <stk_mesh/base/MeshBuilder.hpp>
42 #include <stk_mesh/base/Selector.hpp> // for Selector
43 #include <stk_mesh/base/ForEachEntity.hpp>
44 #include <stk_topology/topology.hpp> // for topology, etc
45 #include <string> // for string
46 #include "stkMeshTestUtils.hpp"
47 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH
48 #include "stk_mesh/base/Bucket.hpp" // for Bucket
49 #include "stk_mesh/base/Entity.hpp" // for Entity
50 #include "stk_mesh/base/Field.hpp" // for Field
51 #include "stk_mesh/base/FieldBase.hpp" // for field_data
52 #include "stk_mesh/base/Types.hpp" // for EntityId, EntityVector, etc
53 namespace stk { namespace mesh { class Part; } }
54
55 namespace
56 {
57 //-BEGIN
58 TEST(StkMeshHowTo, iterateSidesetNodes_BucketLoop_ContiguousFieldDataWithinBucket)
59 {
60 MPI_Comm comm = MPI_COMM_WORLD;
61 if (stk::parallel_machine_size(comm) != 1) { GTEST_SKIP(); }
62
63 std::unique_ptr<stk::mesh::BulkData> stkMesh = stk::mesh::MeshBuilder(comm)
64 .set_spatial_dimension(3)
65 .create();
66 stk::mesh::MetaData &stkMeshMeta = stkMesh->mesh_meta_data();
67 stk::mesh::Field<double> &temperatureField = stkMeshMeta.declare_field<double>(stk::topology::NODE_RANK, "temperature");
68 stk::mesh::put_field_on_entire_mesh(temperatureField);
69
70 // syntax creates faces for the surface on the positive 'x-side' of the 2x2x2 cube,
71 // this part is given the name 'surface_1' when it is created.
72 const std::string generatedMeshSpecification = "generated:2x2x2|sideset:X";
73 stk::io::fill_mesh(generatedMeshSpecification, *stkMesh);
74
75 stk::mesh::Part &boundaryConditionPart = *stkMeshMeta.get_part("surface_1");
76 stk::mesh::Selector boundaryNodesSelector(boundaryConditionPart);
77
78 const stk::mesh::BucketVector &boundaryNodeBuckets = stkMesh->get_buckets(stk::topology::NODE_RANK, boundaryNodesSelector);
79
80 constexpr double prescribedTemperatureValue = 2.0;
81 for (size_t bucketIndex = 0; bucketIndex < boundaryNodeBuckets.size(); ++bucketIndex) {
82 const stk::mesh::Bucket &nodeBucket = *boundaryNodeBuckets[bucketIndex];
83 double *temperatureValues = stk::mesh::field_data(temperatureField, nodeBucket);
84
85 for (size_t nodeIndex = 0; nodeIndex < nodeBucket.size(); ++nodeIndex) {
86 temperatureValues[nodeIndex] = prescribedTemperatureValue;
87 }
88 }
89
90 testUtils::testTemperatureFieldSetCorrectly(temperatureField, boundaryNodesSelector, prescribedTemperatureValue);
91 }
92
93 TEST(StkMeshHowTo, iterateSidesetNodes_ForEachEntity_FieldDataAccess)
94 {
95 MPI_Comm comm = MPI_COMM_WORLD;
96 if (stk::parallel_machine_size(comm) != 1) { GTEST_SKIP(); }
97
98 std::unique_ptr<stk::mesh::BulkData> stkMesh = stk::mesh::MeshBuilder(comm)
99 .set_spatial_dimension(3)
100 .create();
101 stk::mesh::MetaData &stkMeshMeta = stkMesh->mesh_meta_data();
102 stk::mesh::Field<double> &temperatureField = stkMeshMeta.declare_field<double>(stk::topology::NODE_RANK, "temperature");
103 stk::mesh::put_field_on_entire_mesh(temperatureField);
104
105 // syntax creates faces for the surface on the positive 'x-side' of the 2x2x2 cube,
106 // this part is given the name 'surface_1' when it is created.
107 const std::string generatedMeshSpecification = "generated:2x2x2|sideset:X";
108 stk::io::fill_mesh(generatedMeshSpecification, *stkMesh);
109
110 stk::mesh::Part &boundaryConditionPart = *stkMeshMeta.get_part("surface_1");
111 stk::mesh::Selector boundaryNodesSelector(boundaryConditionPart);
112
113 constexpr double prescribedTemperatureValue = 2.0;
114
115 stk::mesh::for_each_entity_run(*stkMesh, stk::topology::NODE_RANK, boundaryNodesSelector,
116 [&](const stk::mesh::BulkData& /*bulk*/, stk::mesh::Entity node) {
117 double *temperatureValues = stk::mesh::field_data(temperatureField, node);
118 *temperatureValues = prescribedTemperatureValue;
119 });
120
121 testUtils::testTemperatureFieldSetCorrectly(temperatureField, boundaryNodesSelector, prescribedTemperatureValue);
122 }
123 //-END
124 }