-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtstBovWriter.hpp
356 lines (308 loc) · 14.8 KB
/
tstBovWriter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/****************************************************************************
* Copyright (c) 2018-2023 by the Cabana authors *
* All rights reserved. *
* *
* This file is part of the Cabana library. Cabana is distributed under a *
* BSD 3-clause license. For the licensing terms see the LICENSE file in *
* the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
#include <Cabana_Grid_Array.hpp>
#include <Cabana_Grid_BovWriter.hpp>
#include <Cabana_Grid_GlobalGrid.hpp>
#include <Cabana_Grid_GlobalMesh.hpp>
#include <Cabana_Grid_Halo.hpp>
#include <Cabana_Grid_IndexSpace.hpp>
#include <Cabana_Grid_Partitioner.hpp>
#include <Cabana_Grid_Types.hpp>
#include <Kokkos_Core.hpp>
#include <gtest/gtest.h>
#include <mpi.h>
#include <array>
#include <cmath>
#include <fstream>
#include <memory>
using namespace Cabana::Grid;
namespace Test
{
//---------------------------------------------------------------------------//
void writeTest3d()
{
// Create the global mesh.
DimBlockPartitioner<3> partitioner;
double cell_size = 0.23;
std::array<int, 3> global_num_cell = { 22, 19, 21 };
std::array<double, 3> global_low_corner = { 1.2, 3.3, -2.8 };
std::array<double, 3> global_high_corner = {
global_low_corner[0] + cell_size * global_num_cell[0],
global_low_corner[1] + cell_size * global_num_cell[1],
global_low_corner[2] + cell_size * global_num_cell[2] };
std::array<bool, 3> is_dim_periodic = { true, true, true };
auto global_mesh = createUniformGlobalMesh(
global_low_corner, global_high_corner, global_num_cell );
// Create the global grid.
auto global_grid = createGlobalGrid( MPI_COMM_WORLD, global_mesh,
is_dim_periodic, partitioner );
// Device-accessible mesh data.
Kokkos::Array<int, 3> num_cell_dev = {
global_num_cell[0], global_num_cell[1], global_num_cell[2] };
// Get the global ijk offsets.
auto off_i = global_grid->globalOffset( Dim::I );
auto off_j = global_grid->globalOffset( Dim::J );
auto off_k = global_grid->globalOffset( Dim::K );
// Field data values.
double pi2 = 8.0 * atan( 1.0 );
{
// Create a scalar cell field and fill it with data.
auto cell_layout = createArrayLayout( global_grid, 0, 1, Cell() );
auto cell_field =
createArray<double, TEST_MEMSPACE>( "cell_field_3d", cell_layout );
auto cell_data = cell_field->view();
Kokkos::parallel_for(
"fill_cell_field",
createExecutionPolicy(
cell_layout->localGrid()->indexSpace( Own(), Cell(), Local() ),
TEST_EXECSPACE() ),
KOKKOS_LAMBDA( const int i, const int j, const int k ) {
double xarg = double( off_i + i ) / num_cell_dev[0];
double yarg = double( off_j + j ) / num_cell_dev[1];
double zarg = double( off_k + k ) / num_cell_dev[2];
cell_data( i, j, k, 0 ) =
1.0 + fabs( Kokkos::cos( pi2 * xarg ) *
Kokkos::cos( pi2 * yarg ) *
Kokkos::cos( pi2 * zarg ) );
} );
// Create a vector node field and fill it with data.
auto node_layout = createArrayLayout( global_grid, 0, 3, Node() );
auto node_field =
createArray<double, TEST_MEMSPACE>( "node_field_3d", node_layout );
auto node_data = node_field->view();
Kokkos::parallel_for(
"fill_node_field",
createExecutionPolicy(
node_layout->localGrid()->indexSpace( Own(), Node(), Local() ),
TEST_EXECSPACE() ),
KOKKOS_LAMBDA( const int i, const int j, const int k ) {
double xarg = double( off_i + i ) / num_cell_dev[0];
double yarg = double( off_j + j ) / num_cell_dev[1];
double zarg = double( off_k + k ) / num_cell_dev[2];
node_data( i, j, k, Dim::I ) =
1.0 + fabs( Kokkos::cos( pi2 * xarg ) );
node_data( i, j, k, Dim::J ) =
1.0 + fabs( Kokkos::cos( pi2 * yarg ) );
node_data( i, j, k, Dim::K ) =
1.0 + fabs( Kokkos::cos( pi2 * zarg ) );
} );
// Gather the node data.
auto node_halo = createHalo( NodeHaloPattern<3>(), 0, *node_field );
node_halo->gather( TEST_EXECSPACE(), *node_field );
// Write the fields to a file.
Experimental::BovWriter::writeTimeStep( "grid_cell_field_3d", 302, 3.43,
*cell_field );
Experimental::BovWriter::writeTimeStep( "grid_node_field_3d", 1972,
12.457, *node_field );
}
// Read the data back in on rank 0 and make sure it is OK.
int rank;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
if ( 0 == rank )
{
// Open the cell file.
std::fstream cell_data_file;
cell_data_file.open( "grid_cell_field_3d_000302.dat",
std::fstream::in | std::fstream::binary );
// The cell file data is ordered KJI
double cell_value;
int cell_id = 0;
for ( int k = 0; k < global_grid->globalNumEntity( Cell(), Dim::K );
++k )
for ( int j = 0; j < global_grid->globalNumEntity( Cell(), Dim::J );
++j )
for ( int i = 0;
i < global_grid->globalNumEntity( Cell(), Dim::I ); ++i )
{
double xarg = double( i ) / global_num_cell[0];
double yarg = double( j ) / global_num_cell[1];
double zarg = double( k ) / global_num_cell[2];
cell_data_file.seekg( cell_id * sizeof( double ) );
cell_data_file.read( (char*)&cell_value, sizeof( double ) );
EXPECT_FLOAT_EQ( cell_value,
1.0 + fabs( Kokkos::cos( pi2 * xarg ) *
Kokkos::cos( pi2 * yarg ) *
Kokkos::cos( pi2 * zarg ) ) );
++cell_id;
}
// Close the cell file.
cell_data_file.close();
// Open the node file.
std::fstream node_data_file;
node_data_file.open( "grid_node_field_3d_001972.dat",
std::fstream::in | std::fstream::binary );
// The node file data is ordered KJI
double node_value;
int node_id = 0;
for ( int k = 0; k < global_grid->globalNumEntity( Cell(), Dim::K ) + 1;
++k )
for ( int j = 0;
j < global_grid->globalNumEntity( Cell(), Dim::J ) + 1; ++j )
for ( int i = 0;
i < global_grid->globalNumEntity( Cell(), Dim::I ) + 1;
++i )
{
double xarg = double( i ) / global_num_cell[0];
double yarg = double( j ) / global_num_cell[1];
double zarg = double( k ) / global_num_cell[2];
node_data_file.seekg( node_id * sizeof( double ) );
node_data_file.read( (char*)&node_value, sizeof( double ) );
EXPECT_FLOAT_EQ( node_value,
1.0 + fabs( Kokkos::cos( pi2 * xarg ) ) );
++node_id;
node_data_file.seekg( node_id * sizeof( double ) );
node_data_file.read( (char*)&node_value, sizeof( double ) );
EXPECT_FLOAT_EQ( node_value,
1.0 + fabs( Kokkos::cos( pi2 * yarg ) ) );
++node_id;
node_data_file.seekg( node_id * sizeof( double ) );
node_data_file.read( (char*)&node_value, sizeof( double ) );
EXPECT_FLOAT_EQ( node_value,
1.0 + fabs( Kokkos::cos( pi2 * zarg ) ) );
++node_id;
}
// Close the node file.
node_data_file.close();
}
}
//---------------------------------------------------------------------------//
void writeTest2d()
{
// Create the global mesh.
DimBlockPartitioner<2> partitioner;
double cell_size = 0.23;
std::array<int, 2> global_num_cell = { 22, 19 };
std::array<double, 2> global_low_corner = { 1.2, 3.3 };
std::array<double, 2> global_high_corner = {
global_low_corner[0] + cell_size * global_num_cell[0],
global_low_corner[1] + cell_size * global_num_cell[1] };
std::array<bool, 2> is_dim_periodic = { true, true };
auto global_mesh = createUniformGlobalMesh(
global_low_corner, global_high_corner, global_num_cell );
// Create the global grid.
auto global_grid = createGlobalGrid( MPI_COMM_WORLD, global_mesh,
is_dim_periodic, partitioner );
// Device-accessible mesh data.
Kokkos::Array<int, 2> num_cell_dev = { global_num_cell[0],
global_num_cell[1] };
// Get the global ijk offsets.
auto off_i = global_grid->globalOffset( Dim::I );
auto off_j = global_grid->globalOffset( Dim::J );
// Field data values.
double pi2 = 8.0 * atan( 1.0 );
{
// Create a scalar cell field and fill it with data.
auto cell_layout = createArrayLayout( global_grid, 0, 1, Cell() );
auto cell_field =
createArray<double, TEST_MEMSPACE>( "cell_field_2d", cell_layout );
auto cell_data = cell_field->view();
Kokkos::parallel_for(
"fill_cell_field",
createExecutionPolicy(
cell_layout->localGrid()->indexSpace( Own(), Cell(), Local() ),
TEST_EXECSPACE() ),
KOKKOS_LAMBDA( const int i, const int j ) {
double xarg = double( off_i + i ) / num_cell_dev[0];
double yarg = double( off_j + j ) / num_cell_dev[1];
cell_data( i, j, 0 ) = 1.0 + fabs( Kokkos::cos( pi2 * xarg ) *
Kokkos::cos( pi2 * yarg ) );
} );
// Create a vector node field and fill it with data.
auto node_layout = createArrayLayout( global_grid, 0, 2, Node() );
auto node_field =
createArray<double, TEST_MEMSPACE>( "node_field_2d", node_layout );
auto node_data = node_field->view();
Kokkos::parallel_for(
"fill_node_field",
createExecutionPolicy(
node_layout->localGrid()->indexSpace( Own(), Node(), Local() ),
TEST_EXECSPACE() ),
KOKKOS_LAMBDA( const int i, const int j ) {
double xarg = double( off_i + i ) / num_cell_dev[0];
double yarg = double( off_j + j ) / num_cell_dev[1];
node_data( i, j, Dim::I ) =
1.0 + fabs( Kokkos::cos( pi2 * xarg ) );
node_data( i, j, Dim::J ) =
1.0 + fabs( Kokkos::cos( pi2 * yarg ) );
} );
// Gather the node data.
auto node_halo = createHalo( NodeHaloPattern<2>(), 0, *node_field );
node_halo->gather( TEST_EXECSPACE(), *node_field );
// Write the fields to a file.
Experimental::BovWriter::writeTimeStep( "grid_cell_field_2d", 302, 3.43,
*cell_field );
Experimental::BovWriter::writeTimeStep( "grid_node_field_2d", 1972,
12.457, *node_field );
}
// Read the data back in on rank 0 and make sure it is OK.
int rank;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
if ( 0 == rank )
{
// Open the cell file.
std::fstream cell_data_file;
cell_data_file.open( "grid_cell_field_2d_000302.dat",
std::fstream::in | std::fstream::binary );
// The cell file data is ordered KJI
double cell_value;
int cell_id = 0;
for ( int j = 0; j < global_grid->globalNumEntity( Cell(), Dim::J );
++j )
for ( int i = 0; i < global_grid->globalNumEntity( Cell(), Dim::I );
++i )
{
double xarg = double( i ) / global_num_cell[0];
double yarg = double( j ) / global_num_cell[1];
cell_data_file.seekg( cell_id * sizeof( double ) );
cell_data_file.read( (char*)&cell_value, sizeof( double ) );
EXPECT_FLOAT_EQ( cell_value,
1.0 + fabs( Kokkos::cos( pi2 * xarg ) *
Kokkos::cos( pi2 * yarg ) ) );
++cell_id;
}
// Close the cell file.
cell_data_file.close();
// Open the node file.
std::fstream node_data_file;
node_data_file.open( "grid_node_field_2d_001972.dat",
std::fstream::in | std::fstream::binary );
// The node file data is ordered KJI
double node_value;
int node_id = 0;
for ( int j = 0; j < global_grid->globalNumEntity( Cell(), Dim::J ) + 1;
++j )
for ( int i = 0;
i < global_grid->globalNumEntity( Cell(), Dim::I ) + 1; ++i )
{
double xarg = double( i ) / global_num_cell[0];
double yarg = double( j ) / global_num_cell[1];
node_data_file.seekg( node_id * sizeof( double ) );
node_data_file.read( (char*)&node_value, sizeof( double ) );
EXPECT_FLOAT_EQ( node_value,
1.0 + fabs( Kokkos::cos( pi2 * xarg ) ) );
++node_id;
node_data_file.seekg( node_id * sizeof( double ) );
node_data_file.read( (char*)&node_value, sizeof( double ) );
EXPECT_FLOAT_EQ( node_value,
1.0 + fabs( Kokkos::cos( pi2 * yarg ) ) );
++node_id;
}
// Close the node file.
node_data_file.close();
}
}
//---------------------------------------------------------------------------//
// RUN TESTS
//---------------------------------------------------------------------------//
TEST( Bov, Write3d ) { writeTest3d(); }
TEST( Bov, Write2d ) { writeTest2d(); }
//---------------------------------------------------------------------------//
} // end namespace Test