Sierra Toolkit  Version of the Day
BoxFixture.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 
10 #include <stk_mesh/fixtures/BoxFixture.hpp>
11 
12 #include <stk_util/environment/ReportHandler.hpp>
13 
14 #include <stk_mesh/base/Types.hpp>
15 #include <stk_mesh/base/MetaData.hpp>
16 #include <stk_mesh/base/BulkData.hpp>
17 #include <stk_mesh/base/GetEntities.hpp>
18 #include <stk_mesh/base/Field.hpp>
19 #include <stk_mesh/base/FieldData.hpp>
20 #include <stk_mesh/base/Comm.hpp>
21 #include <stk_mesh/base/GetBuckets.hpp>
22 
23 #include <stdexcept>
24 
25 namespace stk_classic {
26 namespace mesh {
27 namespace fixtures {
28 
29 BoxFixture::BoxFixture( stk_classic::ParallelMachine pm ,
30  unsigned block_size,
31  const std::vector<std::string>& entity_names )
32  : m_fem_meta ( spatial_dimension, entity_names ),
33  m_bulk_data ( fem::FEMMetaData::get_meta_data(m_fem_meta) , pm , block_size ),
34  m_comm_rank( stk_classic::parallel_machine_rank( pm ) ),
35  m_comm_size( stk_classic::parallel_machine_size( pm ) ),
36  m_previous_state ( stk_classic::mesh::BulkData::MODIFIABLE )
37 {}
38 
39 Entity& BoxFixture::get_new_entity ( EntityRank rank , EntityId parallel_dependent_id )
40 {
41  return m_bulk_data.declare_entity ( rank , parallel_dependent_id*m_comm_size + m_comm_rank + 1 , std::vector<Part *> () );
42 }
43 
44 void BoxFixture::generate_boxes( const BOX root_box,
45  BOX local_box )
46 {
47  const unsigned p_rank = m_bulk_data.parallel_rank();
48  const unsigned p_size = m_bulk_data.parallel_size();
49  const unsigned ngx = root_box[0][1] - root_box[0][0] ;
50  const unsigned ngy = root_box[1][1] - root_box[1][0] ;
51 
52  BOX * const p_box = new BOX[ p_size ];
53 
54  box_partition( 0 , p_size , 2 , root_box , & p_box[0] );
55 
56  local_box[0][0] = p_box[ p_rank ][0][0] ;
57  local_box[0][1] = p_box[ p_rank ][0][1] ;
58  local_box[1][0] = p_box[ p_rank ][1][0] ;
59  local_box[1][1] = p_box[ p_rank ][1][1] ;
60  local_box[2][0] = p_box[ p_rank ][2][0] ;
61  local_box[2][1] = p_box[ p_rank ][2][1] ;
62 
63  // Create elements:
64 
65  std::vector<unsigned> local_count ;
66 
67  const stk_classic::mesh::PartVector no_parts ;
68 
69  for ( int k = local_box[2][0] ; k < local_box[2][1] ; ++k ) {
70  for ( int j = local_box[1][0] ; j < local_box[1][1] ; ++j ) {
71  for ( int i = local_box[0][0] ; i < local_box[0][1] ; ++i ) {
72  const EntityId n0= 1 + (i+0) + (j+0) * (ngx+1) + (k+0) * (ngx+1) * (ngy+1);
73  const EntityId n1= 1 + (i+1) + (j+0) * (ngx+1) + (k+0) * (ngx+1) * (ngy+1);
74  const EntityId n2= 1 + (i+1) + (j+1) * (ngx+1) + (k+0) * (ngx+1) * (ngy+1);
75  const EntityId n3= 1 + (i+0) + (j+1) * (ngx+1) + (k+0) * (ngx+1) * (ngy+1);
76  const EntityId n4= 1 + (i+0) + (j+0) * (ngx+1) + (k+1) * (ngx+1) * (ngy+1);
77  const EntityId n5= 1 + (i+1) + (j+0) * (ngx+1) + (k+1) * (ngx+1) * (ngy+1);
78  const EntityId n6= 1 + (i+1) + (j+1) * (ngx+1) + (k+1) * (ngx+1) * (ngy+1);
79  const EntityId n7= 1 + (i+0) + (j+1) * (ngx+1) + (k+1) * (ngx+1) * (ngy+1);
80 
81  const EntityId elem_id = 1 + i + j * ngx + k * ngx * ngy;
82 
83  Entity & node0 = m_bulk_data.declare_entity( 0 , n0 , no_parts );
84  Entity & node1 = m_bulk_data.declare_entity( 0 , n1 , no_parts );
85  Entity & node2 = m_bulk_data.declare_entity( 0 , n2 , no_parts );
86  Entity & node3 = m_bulk_data.declare_entity( 0 , n3 , no_parts );
87  Entity & node4 = m_bulk_data.declare_entity( 0 , n4 , no_parts );
88  Entity & node5 = m_bulk_data.declare_entity( 0 , n5 , no_parts );
89  Entity & node6 = m_bulk_data.declare_entity( 0 , n6 , no_parts );
90  Entity & node7 = m_bulk_data.declare_entity( 0 , n7 , no_parts );
91  Entity & elem = m_bulk_data.declare_entity( 3 , elem_id , no_parts );
92 
93  m_bulk_data.declare_relation( elem , node0 , 0 );
94  m_bulk_data.declare_relation( elem , node1 , 1 );
95  m_bulk_data.declare_relation( elem , node2 , 2 );
96  m_bulk_data.declare_relation( elem , node3 , 3 );
97  m_bulk_data.declare_relation( elem , node4 , 4 );
98  m_bulk_data.declare_relation( elem , node5 , 5 );
99  m_bulk_data.declare_relation( elem , node6 , 6 );
100  m_bulk_data.declare_relation( elem , node7 , 7 );
101  }
102  }
103  }
104 
105  delete[] p_box ;
106 }
107 
108 void BoxFixture::box_partition( int ip , int up , int axis ,
109  const BOX box ,
110  BOX p_box[] )
111 {
112  const int np = up - ip ;
113  if ( 1 == np ) {
114  p_box[ip][0][0] = box[0][0] ; p_box[ip][0][1] = box[0][1] ;
115  p_box[ip][1][0] = box[1][0] ; p_box[ip][1][1] = box[1][1] ;
116  p_box[ip][2][0] = box[2][0] ; p_box[ip][2][1] = box[2][1] ;
117  }
118  else {
119  const int n = box[ axis ][1] - box[ axis ][0] ;
120  const int np_low = np / 2 ; /* Rounded down */
121  const int np_upp = np - np_low ;
122 
123  const int n_upp = (int) (((double) n) * ( ((double)np_upp) / ((double)np)));
124  const int n_low = n - n_upp ;
125  const int next_axis = ( axis + 2 ) % 3 ;
126 
127  if ( np_low ) { /* P = [ip,ip+np_low) */
128  BOX dbox ;
129  dbox[0][0] = box[0][0] ; dbox[0][1] = box[0][1] ;
130  dbox[1][0] = box[1][0] ; dbox[1][1] = box[1][1] ;
131  dbox[2][0] = box[2][0] ; dbox[2][1] = box[2][1] ;
132 
133  dbox[ axis ][1] = dbox[ axis ][0] + n_low ;
134 
135  box_partition( ip, ip + np_low, next_axis,
136  (const int (*)[2]) dbox, p_box );
137  }
138 
139  if ( np_upp ) { /* P = [ip+np_low,ip+np_low+np_upp) */
140  BOX dbox ;
141  dbox[0][0] = box[0][0] ; dbox[0][1] = box[0][1] ;
142  dbox[1][0] = box[1][0] ; dbox[1][1] = box[1][1] ;
143  dbox[2][0] = box[2][0] ; dbox[2][1] = box[2][1] ;
144 
145  ip += np_low ;
146  dbox[ axis ][0] += n_low ;
147  dbox[ axis ][1] = dbox[ axis ][0] + n_upp ;
148 
149  box_partition( ip, ip + np_upp, next_axis,
150  (const int (*)[2]) dbox, p_box );
151  }
152  }
153 }
154 
155 }
156 }
157 }
unsigned parallel_machine_rank(ParallelMachine parallel_machine)
Member function parallel_machine_rank ...
Definition: Parallel.cpp:29
unsigned parallel_machine_size(ParallelMachine parallel_machine)
Member function parallel_machine_size ...
Definition: Parallel.cpp:18
A fundamental unit within the discretization of a problem domain, including but not limited to nodes...
Definition: Entity.hpp:120
Sierra Toolkit.
MPI_Comm ParallelMachine
Definition: Parallel.hpp:32
std::vector< Part *> PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
Definition: Types.hpp:31