Skip to content

Commit

Permalink
Merge branch 'dim_ind_convert' into dim_ind_3d_main_library
Browse files Browse the repository at this point in the history
  • Loading branch information
scottaiton committed Jan 29, 2024
2 parents 5df7111 + 5c16e9d commit d6a427d
Show file tree
Hide file tree
Showing 5 changed files with 969 additions and 225 deletions.
160 changes: 153 additions & 7 deletions applications/clawpack/advection/2d/swirl/swirl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#include <fclaw2d_file.h>

#include "../all/advection_user.h"
#include <p4est_wrap.h> /* just temporary for testing */
#include <fclaw2d_convenience.h>

#define FCLAW_SWIRL_IO_DEMO 0
#define FCLAW_SWIRL_IO_DEMO 1

static
void create_domain(fclaw_global_t *glob)
Expand All @@ -53,12 +55,40 @@ void create_domain(fclaw_global_t *glob)
fclaw_domain_list_neighbors(domain, FCLAW_VERBOSITY_DEBUG);
}

#if FCLAW_SWIRL_IO_DEMO
static void
check_fclaw2d_file_error_code (int errcode, const char *str)
{
int reslen, retval;
char err_str[sc_MPI_MAX_ERROR_STRING];

if (errcode != FCLAW2D_FILE_ERR_SUCCESS)
{
/* In case of a not successful fclaw2d_file function call we always
* close the file if applicable and deallocate the file context.
*/
/* examine the error code */
retval = fclaw2d_file_error_string (errcode, err_str, &reslen);
/* check for error in the error string function */
SC_CHECK_ABORTF (!retval, "%s: error string function not successful",
str);
SC_ABORTF ("%s: %*.*s", str, reslen, reslen, err_str);
}
}
#endif

static
void run_program(fclaw_global_t* glob)
{
#if FCLAW_SWIRL_IO_DEMO
int errcode;
int i;
int errcode, retval;
fclaw2d_file_context_t *fc;
char read_user_string[FCLAW2D_FILE_USER_STRING_BYTES + 1];
sc_array_t block_arr, field_arr, read_arr, *current_arr;
int64_t test_int = 12;
char *data, *local_arr_data;
fclaw2d_domain_t *read_domain;
#endif

/* Initialize virtual table for ForestClaw */
Expand Down Expand Up @@ -89,13 +119,129 @@ void run_program(fclaw_global_t* glob)
* the workflow must be extended by providing buffers with the required
* data and the functions may be called at a more suitable place.
*/
/** WARNING: This is work in progress and currently not a valid example
* workflow.
*/
fc = fclaw2d_file_open_write ("swirl_io_test", "ForestClaw data file", 0,
/* create a file, which is open for further writing */
/* the passed domain is written to the file */
fc = fclaw2d_file_open_write ("swirl_io_test", "ForestClaw data file",
glob->domain->d2, &errcode);
check_fclaw2d_file_error_code (errcode, "file open write");

#if 0
retval = fclaw2d_file_write_partition ("swirl_io_test_partition",
"Test partition write",
glob->domain->d2, &errcode);
check_fclaw2d_file_error_code (errcode, "file write partition");
#endif

/* write a block to the file */
/* Initialize a sc_array with one element and the element size equals
* to the number of bytes of the block section. */
sc_array_init_data (&block_arr, &test_int, sizeof (int64_t), 1);
fc = fclaw2d_file_write_block (fc, "Test block", block_arr.elem_size,
&block_arr, &errcode);
check_fclaw2d_file_error_code (errcode, "file write block");

/* write an array associated to the domain to the file */
/* we write non-contiguous data to demonstrate how to assemble the array */
/* To this end, we initialize a sc_array with the number of local patches
* of the domain passed to \ref fclaw2d_file_open_write and sizeof (sc_array_t)
* as element size.
*/
sc_array_init_size (&field_arr, sizeof (sc_array_t),
glob->domain->local_num_patches);

for (i = 0; i < glob->domain->local_num_patches; ++i)
{
/* Each sc_array in field_array represents one data entity
* associated to a patch. That means we write the data
* associated to the i-th local patch below.
*/
current_arr = (sc_array_t *) sc_array_index (&field_arr, i);
/* To not allocate data but point to already allocated data
* use \ref sc_array_init_data.
*/
sc_array_init_size (current_arr, 3 * sizeof (char), 1);
data = (char *) sc_array_index (current_arr, 0);
data[0] = 'a';
data[1] = 'b';
data[2] = 'c';
}

fc = fclaw2d_file_write_array (fc, "Test array", 3 * sizeof (char),
&field_arr, &errcode);
check_fclaw2d_file_error_code (errcode, "file write array");
/* free the local array data */
for (i = 0; i < glob->domain->local_num_patches; ++i)
{
current_arr = (sc_array_t *) sc_array_index (&field_arr, i);
sc_array_reset (current_arr);
}
sc_array_reset (&field_arr);
retval = fclaw2d_file_close (fc, &errcode);
check_fclaw2d_file_error_code (errcode, "file close 1");
FCLAW_EXECUTE_ASSERT_FALSE (retval);

/* open the file for reading */
/* the domain stored in the file is read to read_domain */
fc = fclaw2d_file_open_read ("swirl_io_test", read_user_string,
glob->domain->mpicomm, NULL, &read_domain,
&errcode);
check_fclaw2d_file_error_code (errcode, "file open read");
fclaw_global_productionf ("Opened file with user string: %s\n",
read_user_string);

/* read a block from the file */
test_int = -1;
fc = fclaw2d_file_read_block (fc, read_user_string, sizeof (int64_t),
&block_arr, &errcode);
check_fclaw2d_file_error_code (errcode, "file read block");
fclaw_global_productionf ("Read block with user string: %s\n",
read_user_string);
FCLAW_ASSERT (test_int == 12);

/* read an array from the file */
/* For reading array data we need to pass a sc_array with element
* equals to sizeof (sc_array_t). The sc_array will be resized by
* \ref fclaw2d_file_read_array. Each entry of the output sc_array
* is an sc_array with one element and element size equals to the
* patch data size.
*/
sc_array_init (&read_arr, sizeof (sc_array_t));
fc = fclaw2d_file_read_array (fc, read_user_string, 3 * sizeof (char),
&read_arr, &errcode);
check_fclaw2d_file_error_code (errcode, "file write array");
fclaw_global_productionf ("Read array with user string: %s\n",
read_user_string);
/* check read array */
for (i = 0; i < read_domain->local_num_patches; ++i)
{
current_arr = (sc_array_t *) sc_array_index_int (&read_arr, i);
/* local_arr_data is the pointer to the actual data of the
* i-th array entry
*/
local_arr_data = (char *) sc_array_index (current_arr, 0);
/* check array entry */
FCLAW_ASSERT (local_arr_data[0] == 'a');
FCLAW_ASSERT (local_arr_data[1] == 'b');
FCLAW_ASSERT (local_arr_data[2] == 'c');
}
for (i = 0; i < read_domain->local_num_patches; ++i)
{
current_arr = (sc_array_t *) sc_array_index (&read_arr, i);
sc_array_reset (current_arr);
}
sc_array_reset (&read_arr);

/* sanity check of read domain */
FCLAW_ASSERT (p4est_checksum (((p4est_wrap_t *) read_domain->pp)->p4est)
==
p4est_checksum (((p4est_wrap_t *) glob->domain->d2->pp)->
p4est));

fclaw2d_domain_destroy (read_domain);

fclaw2d_file_close (fc, &errcode);
retval = fclaw2d_file_close (fc, &errcode);
check_fclaw2d_file_error_code (errcode, "file close 2");
FCLAW_EXECUTE_ASSERT_FALSE (retval);
#endif

fclaw_finalize(glob);
Expand Down
Loading

0 comments on commit d6a427d

Please sign in to comment.