Date: 2026-03-01T05:24:37+00:00

Actual source code: plexexodusii.c

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

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

      8:   Logically Collective

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

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

     16:   Level: intermediate

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

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

     31:   Collective

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

     37:   Output Parameter:

     39:   Level: beginner

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

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

     54:   Collective

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

     60:   Output Parameter:

     62:   Level: beginner

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

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

     77:   Collective

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

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

     92:   Level: beginner

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

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

    111:   Collective

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

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

    121:   Level: beginner

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

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