Debian Codegraph @

packages/panzer/adapters-stk/test/panzer_workset_builder/hdiv_basis.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Panzer: A partial differential equation assembly
4 // engine for strongly coupled complex multiphysics systems
5 //
6 // Copyright 2011 NTESS and the Panzer contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10
11 #include <Teuchos_ConfigDefs.hpp>
12 #include <Teuchos_UnitTestHarness.hpp>
13 #include <Teuchos_RCP.hpp>
14 #include <Teuchos_TimeMonitor.hpp>
15
16 using Teuchos::RCP;
17 using Teuchos::rcp;
18
19 #include "Teuchos_DefaultComm.hpp"
20 #include "Teuchos_GlobalMPISession.hpp"
21
22 #include "Panzer_STK_Version.hpp"
23 #include "PanzerAdaptersSTK_config.hpp"
24
25 #include "Panzer_WorksetContainer.hpp"
26 #include "Panzer_IntrepidBasisFactory.hpp"
27 #include "Panzer_DOFManager.hpp"
28
29 #include "Panzer_STK_Interface.hpp"
30 #include "Panzer_STK_CubeHexMeshFactory.hpp"
31 #include "Panzer_STKConnManager.hpp"
32 #include "Panzer_STK_WorksetFactory.hpp"
33
34 namespace panzer {
35
36 TEUCHOS_UNIT_TEST(hdiv_basis, check_dirac)
37 {
38 using Teuchos::RCP;
39 using Teuchos::rcp;
40
41 typedef Intrepid2::Basis<PHX::Device::execution_space,double,double> IntrepidBasis;
42
43 std::string element_block = "eblock-0_0_0";
44 int basis_order = 4;
45 int workset_size = 10;
46
47 RCP<Teuchos::ParameterList> pl = rcp(new Teuchos::ParameterList);
48 pl->set("X Elements",2);
49 pl->set("Y Elements",2);
50 pl->set("Z Elements",2);
51
52 panzer_stk::CubeHexMeshFactory factory;
53 factory.setParameterList(pl);
54 RCP<panzer_stk::STK_Interface> mesh = factory.buildMesh(MPI_COMM_WORLD);
55 RCP<const Teuchos::MpiComm<int> > tComm = Teuchos::rcp(new Teuchos::MpiComm<int>(MPI_COMM_WORLD));
56
57 // build DOF Manager (with a single HDiv basis)
58 /////////////////////////////////////////////////////////////
59
60 // build the connection manager
61 const RCP<panzer::ConnManager>
62 conn_manager = rcp(new panzer_stk::STKConnManager(mesh));
63
64 RCP<panzer::DOFManager> dof_manager
65 = rcp(new panzer::DOFManager(conn_manager,MPI_COMM_WORLD));
66
67 // build an intrepid basis and a related field pattern for seeding the DOFManager
68 RCP<IntrepidBasis> hdiv_intrepid_basis, hcurl_intrepid_basis;
69 {
70 hdiv_intrepid_basis
71 = panzer::createIntrepid2Basis<PHX::Device::execution_space,double,double>("HDiv",basis_order,
72 *mesh->getCellTopology(element_block));
73 hcurl_intrepid_basis
74 = panzer::createIntrepid2Basis<PHX::Device::execution_space,double,double>("HCurl",basis_order,
75 *mesh->getCellTopology(element_block));
76 RCP<panzer::Intrepid2FieldPattern> hdiv_field_pattern = rcp(new panzer::Intrepid2FieldPattern(hdiv_intrepid_basis));
77 RCP<panzer::Intrepid2FieldPattern> hcurl_field_pattern = rcp(new panzer::Intrepid2FieldPattern(hcurl_intrepid_basis));
78
79 dof_manager->addField(element_block, "B", hdiv_field_pattern);
80 dof_manager->addField(element_block, "E", hcurl_field_pattern);
81 }
82 dof_manager->buildGlobalUnknowns();
83
84 // build WorksetContainer
85 //////////////////////////////////////////////////////////////
86 BasisDescriptor hdiv_basis_desc(basis_order,"HDiv");
87 BasisDescriptor hcurl_basis_desc(basis_order,"HCurl");
88
89 out << "workset container setup [start]" << std::endl;
90
91 RCP<panzer_stk::WorksetFactory> wkstFactory
92 = rcp(new panzer_stk::WorksetFactory(mesh)); // build STK workset factory
93 RCP<panzer::WorksetContainer> wkstContainer // attach it to a workset container (uses lazy evaluation)
94 = rcp(new panzer::WorksetContainer);
95 wkstContainer->setFactory(wkstFactory);
96 {
97 WorksetNeeds needs;
98 needs.addBasis(hdiv_basis_desc);
99 needs.addBasis(hcurl_basis_desc);
100 needs.addPoint(hdiv_basis_desc.getPointDescriptor());
101 wkstContainer->setNeeds(element_block,needs);
102 }
103 wkstContainer->setGlobalIndexer(dof_manager);
104 wkstContainer->setWorksetSize(workset_size);
105
106 out << "workset container setup [complete]" << std::endl;
107
108 // Get worksets
109 ///////////////////////////////////////////////////////////////////////
110 out << "getting worksets [start]" << std::endl;
111
112 // this must use this descriptor!
113 panzer::WorksetDescriptor workset_descriptor(element_block, panzer::WorksetSizeType::ALL_ELEMENTS, true,true);
114 std::vector<Workset> & worksets = *wkstContainer->getWorksets(workset_descriptor);
115
116 out << "getting worksets [complete]" << std::endl;
117
118 // get BasisValues2
119 ///////////////////////////////////////////////////////////////////////
120
121 const PointValues2<double> & point_values = worksets[0].getPointValues(hdiv_basis_desc.getPointDescriptor());
122 const BasisValues2<double> & hdiv_basis_values = worksets[0].getBasisValues(hdiv_basis_desc,hdiv_basis_desc.getPointDescriptor());
123 const BasisValues2<double> & hcurl_basis_values = worksets[0].getBasisValues(hcurl_basis_desc,hdiv_basis_desc.getPointDescriptor());
124
125 auto hdiv_basis_vector = hdiv_basis_values.getVectorBasisValues(false);
126 auto hcurl_curl_basis = hcurl_basis_values.getCurlVectorBasis(false);
127
128 // check some sizing stuff
129 ///////////////////////////////////////////////////////////////////////
130
131 TEST_EQUALITY(Teuchos::as<int>(hdiv_basis_vector.extent(1)),hdiv_intrepid_basis->getCardinality());
132 TEST_EQUALITY(hdiv_basis_vector.extent(1),hdiv_basis_vector.extent(2));
133
134 TEST_EQUALITY(Teuchos::as<int>(hcurl_curl_basis.extent(1)),hcurl_intrepid_basis->getCardinality());
135 TEST_EQUALITY(hcurl_curl_basis.extent(2),hdiv_basis_vector.extent(2));
136
137 TEST_EQUALITY(Teuchos::as<int>(point_values.coords_ref.extent(0)),hdiv_intrepid_basis->getCardinality());
138 TEST_EQUALITY(point_values.coords_ref.extent(1),3u);
139
140 TEST_EQUALITY(Teuchos::as<int>(point_values.point_coords.extent(1)),hdiv_intrepid_basis->getCardinality());
141 TEST_EQUALITY(point_values.point_coords.extent(2),3u);
142
143 // print out basis values
144 ///////////////////////////////////////////////////////////////////////
145
146 auto hdiv_basis_vector_view = PHX::as_view(hdiv_basis_vector);
147 auto hdiv_basis_vector_h = Kokkos::create_mirror_view(hdiv_basis_vector_view);
148 Kokkos::deep_copy(hdiv_basis_vector_h, hdiv_basis_vector_view);
149
150 // print out phi_i(x_j).phi_j(x_j)
151 for(size_t c=0;c<hdiv_basis_vector.extent(0);c++) {
152 out << "cell " << c << std::endl;
153 for(size_t i=0;i<hdiv_basis_vector.extent(1);i++) {
154 // compute diagonal magnitude
155 double diagonal = 0.0;
156 for(size_t d=0;d<hdiv_basis_vector.extent(3);d++)
157 diagonal += hdiv_basis_vector_h(c,i,i,d)* hdiv_basis_vector_h(c,i,i,d);
158
159 out << " ";
160 for(size_t j=0;j<hdiv_basis_vector_h.extent(2);j++) {
161 double entry = 0.0;
162
163 // loop over dimension
164 for(size_t d=0;d<hdiv_basis_vector.extent(3);d++)
165 entry += hdiv_basis_vector_h(c,i,j,d)* hdiv_basis_vector_h(c,j,j,d);
166
167 out << std::fixed << std::setprecision(2) << std::setw(8) << entry / diagonal;
168
169 if(i==j) {
170 TEST_ASSERT(std::fabs(entry/diagonal)-1.0 <= 1.0e-15);
171 }
172 else {
173 TEST_ASSERT(std::fabs(entry/diagonal) <= 1.0e-15);
174 }
175 } // end j
176 out << std::endl;
177 } // end i
178 } // end c
179 }
180
181 TEUCHOS_UNIT_TEST(hdiv_basis, check_weighted_orientations)
182 {
183 using Teuchos::RCP;
184 using Teuchos::rcp;
185
186 typedef Intrepid2::Basis<PHX::Device::execution_space,double,double> IntrepidBasis;
187
188 std::string element_block = "eblock-0_0_0";
189 int basis_order = 1;
190 int workset_size = 2;
191
192 RCP<Teuchos::ParameterList> pl = rcp(new Teuchos::ParameterList);
193 pl->set("X Elements",2);
194 pl->set("Y Elements",1);
195 pl->set("Z Elements",1);
196
197 panzer_stk::CubeHexMeshFactory factory;
198 factory.setParameterList(pl);
199 RCP<panzer_stk::STK_Interface> mesh = factory.buildMesh(MPI_COMM_WORLD);
200 RCP<const Teuchos::MpiComm<int> > tComm = Teuchos::rcp(new Teuchos::MpiComm<int>(MPI_COMM_WORLD));
201
202 // build DOF Manager (with a single HDiv basis)
203 /////////////////////////////////////////////////////////////
204
205 // build the connection manager
206 const RCP<panzer::ConnManager>
207 conn_manager = rcp(new panzer_stk::STKConnManager(mesh));
208
209 RCP<panzer::DOFManager> dof_manager
210 = rcp(new panzer::DOFManager(conn_manager,MPI_COMM_WORLD));
211
212 // build an intrepid basis and a related field pattern for seeding the DOFManager
213 RCP<IntrepidBasis> hdiv_intrepid_basis, hcurl_intrepid_basis;
214 {
215 hdiv_intrepid_basis
216 = panzer::createIntrepid2Basis<PHX::Device::execution_space,double,double>("HDiv",basis_order,
217 *mesh->getCellTopology(element_block));
218 hcurl_intrepid_basis
219 = panzer::createIntrepid2Basis<PHX::Device::execution_space,double,double>("HCurl",basis_order,
220 *mesh->getCellTopology(element_block));
221 RCP<panzer::Intrepid2FieldPattern> hdiv_field_pattern = rcp(new panzer::Intrepid2FieldPattern(hdiv_intrepid_basis));
222 RCP<panzer::Intrepid2FieldPattern> hcurl_field_pattern = rcp(new panzer::Intrepid2FieldPattern(hcurl_intrepid_basis));
223
224 dof_manager->addField(element_block, "B", hdiv_field_pattern);
225 dof_manager->addField(element_block, "E", hcurl_field_pattern);
226 }
227 dof_manager->buildGlobalUnknowns();
228
229 // build WorksetContainer
230 //////////////////////////////////////////////////////////////
231 BasisDescriptor hdiv_basis_desc(basis_order,"HDiv");
232 BasisDescriptor hcurl_basis_desc(basis_order,"HCurl");
233 IntegrationDescriptor quad_desc(2,IntegrationDescriptor::VOLUME);
234
235 out << "workset container setup [start]" << std::endl;
236
237 RCP<panzer_stk::WorksetFactory> wkstFactory
238 = rcp(new panzer_stk::WorksetFactory(mesh)); // build STK workset factory
239 RCP<panzer::WorksetContainer> wkstContainer // attach it to a workset container (uses lazy evaluation)
240 = rcp(new panzer::WorksetContainer);
241 wkstContainer->setFactory(wkstFactory);
242 {
243 WorksetNeeds needs;
244 needs.bases.push_back(Teuchos::rcp(new panzer::PureBasis(hdiv_basis_desc,mesh->getCellTopology(element_block),workset_size)));
245 needs.bases.push_back(Teuchos::rcp(new panzer::PureBasis(hcurl_basis_desc,mesh->getCellTopology(element_block),workset_size)));
246 needs.rep_field_name.push_back("B");
247 needs.rep_field_name.push_back("E");
248 needs.int_rules.push_back(Teuchos::rcp(new panzer::IntegrationRule(quad_desc,mesh->getCellTopology(element_block),workset_size)));
249 needs.cellData = CellData(workset_size,mesh->getCellTopology(element_block));
250
251 wkstContainer->setNeeds(element_block,needs);
252 }
253 wkstContainer->setGlobalIndexer(dof_manager);
254 wkstContainer->setWorksetSize(workset_size);
255
256 out << "workset container setup [complete]" << std::endl;
257
258 // Get worksets
259 ///////////////////////////////////////////////////////////////////////
260 out << "getting worksets [start]" << std::endl;
261
262 // this must use this descriptor!
263 // panzer::WorksetDescriptor workset_descriptor(element_block, panzer::WorksetSizeType::ALL_ELEMENTS, true,true);
264 panzer::WorksetDescriptor workset_descriptor(element_block);
265 std::vector<Workset> & worksets = *wkstContainer->getWorksets(workset_descriptor);
266
267 out << "getting worksets [complete]" << std::endl;
268
269 // get BasisValues2
270 ///////////////////////////////////////////////////////////////////////
271 out << worksets[0].bases.size() << std::endl;
272
273 const BasisValues2<double> & hdiv_basis_values = *worksets[0].bases[0];
274 out << (*worksets[0].basis_names)[0]
275 << " " << (*worksets[0].basis_names)[1] << std::endl;
276
277 auto hdiv_weighted_basis_vector = hdiv_basis_values.weighted_basis_vector;
278 auto hdiv_weighted_basis_vector_view = PHX::as_view(hdiv_weighted_basis_vector);
279 auto hdiv_weighted_basis_vector_h = Kokkos::create_mirror_view(hdiv_weighted_basis_vector_view);
280 Kokkos::deep_copy(hdiv_weighted_basis_vector_h, hdiv_weighted_basis_vector_view);
281
282 auto hdiv_basis_vector = hdiv_basis_values.basis_vector;
283 auto hdiv_basis_vector_view = PHX::as_view(hdiv_basis_vector);
284 auto hdiv_basis_vector_h = Kokkos::create_mirror_view(hdiv_basis_vector_view);
285 Kokkos::deep_copy(hdiv_basis_vector_h, hdiv_basis_vector_view);
286
287 // check some sizing stuff
288 ///////////////////////////////////////////////////////////////////////
289
290 TEST_EQUALITY(worksets[0].num_cells,2);
291 TEST_EQUALITY(Teuchos::as<int>(hdiv_weighted_basis_vector.extent(0)),2);
292 TEST_EQUALITY(Teuchos::as<int>(hdiv_weighted_basis_vector.extent(1)),6);
293 TEST_EQUALITY(Teuchos::as<int>(hdiv_weighted_basis_vector.extent(2)),8);
294 TEST_EQUALITY(Teuchos::as<int>(hdiv_weighted_basis_vector.extent(3)),3);
295
296 // print out basis values
297 ///////////////////////////////////////////////////////////////////////
298
299 out << "BASIS VECTOR\n" << std::endl;
300 for(size_t c=0;c<2;c++) {
301 out << "cell " << c << " = ";
302 std::vector<panzer::GlobalOrdinal> gids;
303 dof_manager->getElementGIDs(c,gids);
304
305 out << " gids = ";
306 for(auto g : gids)
307 out << g << " ";
308 out << std::endl;
309
310 for(size_t b=0;b<hdiv_basis_vector.extent(1);b++) {
311 out << " basis " << b << " = ";
312 for(size_t q=0;q<hdiv_basis_vector.extent(2);q++) {
313 out << "[ ";
314 for(size_t d=0;d<hdiv_basis_vector.extent(3);d++) {
315 out << hdiv_basis_vector_h(c,b,q,d) << ", ";
316 } // end d
317 out << "], ";
318 } // end q
319 out << std::endl;
320 } // end b
321 out << std::endl;
322 } // end c
323
324 double sign_diff = hdiv_basis_vector_h(0,1,0,0)/std::fabs(hdiv_basis_vector_h(0,1,0,0))
325 - hdiv_basis_vector_h(1,3,0,0)/std::fabs(hdiv_basis_vector_h(1,3,0,0));
326 TEST_ASSERT(std::fabs(sign_diff) <= 1e-15);
327
328 out << "WEIGHTED BASIS VECTOR\n" << std::endl;
329 for(size_t c=0;c<2;c++) {
330 out << "cell " << c << " = ";
331 std::vector<panzer::GlobalOrdinal> gids;
332 dof_manager->getElementGIDs(c,gids);
333
334 out << " gids = ";
335 for(auto g : gids)
336 out << g << " ";
337 out << std::endl;
338
339 for(size_t b=0;b<hdiv_weighted_basis_vector.extent(1);b++) {
340 out << " basis " << b << " = ";
341 for(size_t q=0;q<hdiv_weighted_basis_vector.extent(2);q++) {
342 out << "[ ";
343 for(size_t d=0;d<hdiv_weighted_basis_vector.extent(3);d++) {
344 out << hdiv_weighted_basis_vector_h(c,b,q,d) << ", ";
345 } // end d
346 out << "], ";
347 } // end q
348 out << std::endl;
349 } // end b
350 out << std::endl;
351 } // end c
352 }
353 }