Date: 2026-03-31T05:46:07+00:00

Actual source code: plexexodusii.c

      1: #include <petsc/private/dmpleximpl.h>
      2: #include <petsc/private/viewerimpl.h>

      4: /*@
      5:   PetscViewerExodusIIGetId - Get the file id of the `PETSCVIEWEREXODUSII` file

      7:   Logically Collective

      9:   Input Parameter:
     10: . viewer - the `PetscViewer`

     12:   Output Parameter:
     13: . exoid - The ExodusII file id

     15:   Level: intermediate

     17: .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerFileSetMode()`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerBinaryOpen()`
     18: @*/
     19: PetscErrorCode PetscViewerExodusIIGetId(PetscViewer viewer, int *exoid)
     20: {
     21:   PetscFunctionBegin;
     23:   PetscTryMethod(viewer, "PetscViewerGetId_C", (PetscViewer, int *), (viewer, exoid));
     24:   PetscFunctionReturn(PETSC_SUCCESS);
     25: }

     27: /*@
     28:   PetscViewerExodusIISetOrder - Set the elements order in the ExodusII file.

     30:   Collective

     32:   Input Parameters:
     33: + viewer - the `PETSCVIEWEREXODUSII` viewer
     34: - order  - elements order

     36:   Output Parameter:

     38:   Level: beginner

     40: .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerExodusIIGetId()`, `PetscViewerExodusIIGetOrder()`
     41: @*/
     42: PetscErrorCode PetscViewerExodusIISetOrder(PetscViewer viewer, PetscInt order)
     43: {
     44:   PetscFunctionBegin;
     46:   PetscTryMethod(viewer, "PetscViewerSetOrder_C", (PetscViewer, PetscInt), (viewer, order));
     47:   PetscFunctionReturn(PETSC_SUCCESS);
     48: }

     50: /*@
     51:   PetscViewerExodusIIGetOrder - Get the elements order in the ExodusII file.

     53:   Collective

     55:   Input Parameters:
     56: + viewer - the `PETSCVIEWEREXODUSII` viewer
     57: - order  - elements order

     59:   Output Parameter:

     61:   Level: beginner

     63: .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerExodusIIGetId()`, `PetscViewerExodusIISetOrder()`
     64: @*/
     65: PetscErrorCode PetscViewerExodusIIGetOrder(PetscViewer viewer, PetscInt *order)
     66: {
     67:   PetscFunctionBegin;
     69:   PetscTryMethod(viewer, "PetscViewerGetOrder_C", (PetscViewer, PetscInt *), (viewer, order));
     70:   PetscFunctionReturn(PETSC_SUCCESS);
     71: }

     73: /*@
     74:   PetscViewerExodusIIOpen - Opens a file for ExodusII input/output.

     76:   Collective

     78:   Input Parameters:
     79: + comm - MPI communicator
     80: . name - name of file
     81: - type - type of file
     82: .vb
     83:     FILE_MODE_WRITE - create new file for binary output
     84:     FILE_MODE_READ - open existing file for binary input
     85:     FILE_MODE_APPEND - open existing file for binary output
     86: .ve

     88:   Output Parameter:
     89: . exo - `PETSCVIEWEREXODUSII` `PetscViewer` for Exodus II input/output to use with the specified file

     91:   Level: beginner

     93: .seealso: `PETSCVIEWEREXODUSII`, `PetscViewer`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`,
     94:           `DMLoad()`, `PetscFileMode`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
     95: @*/
     96: PetscErrorCode PetscViewerExodusIIOpen(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *exo)
     97: {
     98:   PetscFunctionBegin;
     99:   PetscCall(PetscViewerCreate(comm, exo));
    100:   PetscCall(PetscViewerSetType(*exo, PETSCVIEWEREXODUSII));
    101:   PetscCall(PetscViewerFileSetMode(*exo, type));
    102:   PetscCall(PetscViewerFileSetName(*exo, name));
    103:   PetscCall(PetscViewerSetFromOptions(*exo));
    104:   PetscFunctionReturn(PETSC_SUCCESS);
    105: }

    107: /*@
    108:   DMPlexCreateExodusFromFile - Create a `DMPLEX` mesh from an ExodusII file.

    110:   Collective

    112:   Input Parameters:
    113: + comm        - The MPI communicator
    114: . filename    - The name of the ExodusII file
    115: - interpolate - Create faces and edges in the mesh

    117:   Output Parameter:
    118: . dm - The `DM` object representing the mesh

    120:   Level: beginner

    122: .seealso: [](ch_unstructured), `DM`, `PETSCVIEWEREXODUSII`, `DMPLEX`, `DMCreate()`, `DMPlexCreateExodus()`
    123: @*/
    124: PetscErrorCode DMPlexCreateExodusFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
    125: {
    126:   PetscFunctionBegin;
    127: #if defined(PETSC_HAVE_EXODUSII)
    128:   PetscMPIInt      rank;
    129:   PetscExodusIIInt CPU_word_size = sizeof(PetscReal), IO_word_size = 0, exoid = -1;
    130:   float            version;

    132:   PetscAssertPointer(filename, 2);
    133:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
    134:   if (rank == 0) {
    135:     exoid = ex_open(filename, EX_READ, &CPU_word_size, &IO_word_size, &version);
    136:     PetscCheck(exoid > 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "ex_open(\"%s\",...) did not return a valid file ID", filename);
    137:   }
    138:   PetscCall(DMPlexCreateExodus(comm, exoid, interpolate, dm));
    139:   if (rank == 0) PetscCallExternal(ex_close, exoid);
    140:   PetscFunctionReturn(PETSC_SUCCESS);
    141: #else
    142:   SETERRQ(comm, PETSC_ERR_SUP, "Loading meshes requires EXODUSII support. Reconfigure using --with-exodusii-dir or -download-exodusii");
    143: #endif
    144: }