Skip to content

Commit

Permalink
fluids: enable reading checkpoint files as i32 or i64 (CEED#1520)
Browse files Browse the repository at this point in the history
* fluids: enable reading checkpoint files as i32 or i64

Depends on https://gitlab.com/petsc/petsc/-/merge_requests/7377

* Apply suggestions from code review

Co-authored-by: James Wright <[email protected]>

---------

Co-authored-by: James Wright <[email protected]>
  • Loading branch information
jedbrown and jrwrigh authored Mar 18, 2024
1 parent ade1077 commit 7ceb642
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions examples/fluids/src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,42 @@ PetscErrorCode DMPlexInsertBoundaryValues_FromICs(DM dm, PetscBool insert_essent
PetscFunctionReturn(PETSC_SUCCESS);
}

static PetscErrorCode BinaryReadIntoInt(PetscViewer viewer, PetscInt *out, PetscDataType file_type) {
PetscFunctionBeginUser;
if (file_type == PETSC_INT32) {
PetscInt32 val;
PetscCall(PetscViewerBinaryRead(viewer, &val, 1, NULL, PETSC_INT32));
*out = val;
} else if (file_type == PETSC_INT64) {
PetscInt64 val;
PetscCall(PetscViewerBinaryRead(viewer, &val, 1, NULL, PETSC_INT64));
*out = val;
} else {
PetscCall(PetscViewerBinaryRead(viewer, out, 1, NULL, PETSC_INT));
}
PetscFunctionReturn(PETSC_SUCCESS);
}

// @brief Load vector from binary file, possibly with embedded solution time and step number
PetscErrorCode LoadFluidsBinaryVec(MPI_Comm comm, PetscViewer viewer, Vec Q, PetscReal *time, PetscInt *step_number) {
PetscInt file_step_number;
PetscInt32 token;
PetscReal file_time;
PetscInt file_step_number;
PetscInt32 token;
PetscReal file_time;
PetscDataType file_type = PETSC_INT32;

PetscFunctionBeginUser;
PetscCall(PetscViewerBinaryRead(viewer, &token, 1, NULL, PETSC_INT32));
if (token == FLUIDS_FILE_TOKEN_32 || token == FLUIDS_FILE_TOKEN_64 ||
token == FLUIDS_FILE_TOKEN) { // New style format; we're reading a file with step number and time in the header
PetscCall(PetscViewerBinaryRead(viewer, &file_step_number, 1, NULL, PETSC_INT));
if (token == FLUIDS_FILE_TOKEN_32) file_type = PETSC_INT32;
else if (token == FLUIDS_FILE_TOKEN_64) file_type = PETSC_INT64;
PetscCall(BinaryReadIntoInt(viewer, &file_step_number, file_type));
PetscCall(PetscViewerBinaryRead(viewer, &file_time, 1, NULL, PETSC_REAL));
if (time) *time = file_time;
if (step_number) *step_number = file_step_number;
} else if (token == VEC_FILE_CLASSID) { // Legacy format of just the vector, encoded as [VEC_FILE_CLASSID, length, ]
PetscInt length, N;
PetscCall(PetscViewerBinaryRead(viewer, &length, 1, NULL, PETSC_INT));
PetscCall(BinaryReadIntoInt(viewer, &length, file_type));
PetscCall(VecGetSize(Q, &N));
PetscCheck(length == N, comm, PETSC_ERR_ARG_INCOMP, "File Vec has length %" PetscInt_FMT " but DM has global Vec size %" PetscInt_FMT, length, N);
PetscCall(PetscViewerBinarySetSkipHeader(viewer, PETSC_TRUE));
Expand Down

0 comments on commit 7ceb642

Please sign in to comment.