ex55.c (be7a8307fd44d09b1b5ae337caf3795a085b8258) ex55.c (ed5e4e85792117e4bf875eb7efb50a9b8564ac26)
1static char help[] = "Load and save the mesh and fields to HDF5 and ExodusII\n\n";
2
3#include <petscdmplex.h>
4#include <petscviewerhdf5.h>
5#include <petscsf.h>
6
7typedef struct {
8 PetscBool compare; /* Compare the meshes using DMPlexEqual() */
9 PetscBool compare_labels; /* Compare labels in the meshes using DMCompareLabels() */
10 PetscBool distribute; /* Distribute the mesh */
11 PetscBool interpolate; /* Generate intermediate mesh elements */
12 char filename[PETSC_MAX_PATH_LEN]; /* Mesh filename */
1static char help[] = "Load and save the mesh and fields to HDF5 and ExodusII\n\n";
2
3#include <petscdmplex.h>
4#include <petscviewerhdf5.h>
5#include <petscsf.h>
6
7typedef struct {
8 PetscBool compare; /* Compare the meshes using DMPlexEqual() */
9 PetscBool compare_labels; /* Compare labels in the meshes using DMCompareLabels() */
10 PetscBool distribute; /* Distribute the mesh */
11 PetscBool interpolate; /* Generate intermediate mesh elements */
12 char filename[PETSC_MAX_PATH_LEN]; /* Mesh filename */
13 char meshname[PETSC_MAX_PATH_LEN]; /* Mesh name */
13 PetscViewerFormat format; /* Format to write and read */
14 PetscBool second_write_read; /* Write and read for the 2nd time */
15 PetscBool use_low_level_functions; /* Use low level functions for viewing and loading */
16} AppCtx;
17
18static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
19{
20 PetscErrorCode ierr;
21
22 PetscFunctionBeginUser;
23 options->compare = PETSC_FALSE;
24 options->compare_labels = PETSC_FALSE;
25 options->distribute = PETSC_TRUE;
26 options->interpolate = PETSC_FALSE;
27 options->filename[0] = '\0';
14 PetscViewerFormat format; /* Format to write and read */
15 PetscBool second_write_read; /* Write and read for the 2nd time */
16 PetscBool use_low_level_functions; /* Use low level functions for viewing and loading */
17} AppCtx;
18
19static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
20{
21 PetscErrorCode ierr;
22
23 PetscFunctionBeginUser;
24 options->compare = PETSC_FALSE;
25 options->compare_labels = PETSC_FALSE;
26 options->distribute = PETSC_TRUE;
27 options->interpolate = PETSC_FALSE;
28 options->filename[0] = '\0';
29 options->meshname[0] = '\0';
28 options->format = PETSC_VIEWER_DEFAULT;
29 options->second_write_read = PETSC_FALSE;
30 options->use_low_level_functions = PETSC_FALSE;
31
32 ierr = PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMPLEX");CHKERRQ(ierr);
33 ierr = PetscOptionsBool("-compare", "Compare the meshes using DMPlexEqual()", "ex55.c", options->compare, &options->compare, NULL);CHKERRQ(ierr);
34 ierr = PetscOptionsBool("-compare_labels", "Compare labels in the meshes using DMCompareLabels()", "ex55.c", options->compare_labels, &options->compare_labels, NULL);CHKERRQ(ierr);
35 ierr = PetscOptionsBool("-distribute", "Distribute the mesh", "ex55.c", options->distribute, &options->distribute, NULL);CHKERRQ(ierr);
36 ierr = PetscOptionsBool("-interpolate", "Generate intermediate mesh elements", "ex55.c", options->interpolate, &options->interpolate, NULL);CHKERRQ(ierr);
37 ierr = PetscOptionsString("-filename", "The mesh file", "ex55.c", options->filename, options->filename, sizeof(options->filename), NULL);CHKERRQ(ierr);
30 options->format = PETSC_VIEWER_DEFAULT;
31 options->second_write_read = PETSC_FALSE;
32 options->use_low_level_functions = PETSC_FALSE;
33
34 ierr = PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMPLEX");CHKERRQ(ierr);
35 ierr = PetscOptionsBool("-compare", "Compare the meshes using DMPlexEqual()", "ex55.c", options->compare, &options->compare, NULL);CHKERRQ(ierr);
36 ierr = PetscOptionsBool("-compare_labels", "Compare labels in the meshes using DMCompareLabels()", "ex55.c", options->compare_labels, &options->compare_labels, NULL);CHKERRQ(ierr);
37 ierr = PetscOptionsBool("-distribute", "Distribute the mesh", "ex55.c", options->distribute, &options->distribute, NULL);CHKERRQ(ierr);
38 ierr = PetscOptionsBool("-interpolate", "Generate intermediate mesh elements", "ex55.c", options->interpolate, &options->interpolate, NULL);CHKERRQ(ierr);
39 ierr = PetscOptionsString("-filename", "The mesh file", "ex55.c", options->filename, options->filename, sizeof(options->filename), NULL);CHKERRQ(ierr);
40 ierr = PetscOptionsString("-meshname", "The mesh file", "ex55.c", options->meshname, options->meshname, sizeof(options->meshname), NULL);CHKERRQ(ierr);
38 ierr = PetscOptionsEnum("-format", "Format to write and read", "ex55.c", PetscViewerFormats, (PetscEnum)options->format, (PetscEnum*)&options->format, NULL);CHKERRQ(ierr);
39 ierr = PetscOptionsBool("-second_write_read", "Write and read for the 2nd time", "ex55.c", options->second_write_read, &options->second_write_read, NULL);CHKERRQ(ierr);
40 ierr = PetscOptionsBool("-use_low_level_functions", "Use low level functions for viewing and loading", "ex55.c", options->use_low_level_functions, &options->use_low_level_functions, NULL);CHKERRQ(ierr);
41 ierr = PetscOptionsEnd();CHKERRQ(ierr);
42 PetscFunctionReturn(0);
43};
44
45static PetscErrorCode DMPlexWriteAndReadHDF5(DM dm, const char filename[], const char prefix[], AppCtx user, DM *dm_new)
46{
47 DM dmnew;
41 ierr = PetscOptionsEnum("-format", "Format to write and read", "ex55.c", PetscViewerFormats, (PetscEnum)options->format, (PetscEnum*)&options->format, NULL);CHKERRQ(ierr);
42 ierr = PetscOptionsBool("-second_write_read", "Write and read for the 2nd time", "ex55.c", options->second_write_read, &options->second_write_read, NULL);CHKERRQ(ierr);
43 ierr = PetscOptionsBool("-use_low_level_functions", "Use low level functions for viewing and loading", "ex55.c", options->use_low_level_functions, &options->use_low_level_functions, NULL);CHKERRQ(ierr);
44 ierr = PetscOptionsEnd();CHKERRQ(ierr);
45 PetscFunctionReturn(0);
46};
47
48static PetscErrorCode DMPlexWriteAndReadHDF5(DM dm, const char filename[], const char prefix[], AppCtx user, DM *dm_new)
49{
50 DM dmnew;
48 const char exampleDMPlexName[] = "DMPlex Object";
51 const char savedName[] = "Mesh";
52 const char loadedName[] = "Mesh_new";
49 PetscViewer v;
50 PetscErrorCode ierr;
51
52 PetscFunctionBeginUser;
53 ierr = PetscViewerHDF5Open(PetscObjectComm((PetscObject) dm), filename, FILE_MODE_WRITE, &v);CHKERRQ(ierr);
54 ierr = PetscViewerPushFormat(v, user.format);CHKERRQ(ierr);
53 PetscViewer v;
54 PetscErrorCode ierr;
55
56 PetscFunctionBeginUser;
57 ierr = PetscViewerHDF5Open(PetscObjectComm((PetscObject) dm), filename, FILE_MODE_WRITE, &v);CHKERRQ(ierr);
58 ierr = PetscViewerPushFormat(v, user.format);CHKERRQ(ierr);
55 ierr = PetscObjectSetName((PetscObject) dm, exampleDMPlexName);CHKERRQ(ierr);
59 ierr = PetscObjectSetName((PetscObject) dm, savedName);CHKERRQ(ierr);
56 if (user.use_low_level_functions) {
57 ierr = DMPlexTopologyView(dm, v);CHKERRQ(ierr);
58 ierr = DMPlexCoordinatesView(dm, v);CHKERRQ(ierr);
59 ierr = DMPlexLabelsView(dm, v);CHKERRQ(ierr);
60 } else {
61 ierr = DMView(dm, v);CHKERRQ(ierr);
62 }
63
64 ierr = PetscViewerFileSetMode(v, FILE_MODE_READ);CHKERRQ(ierr);
65 ierr = DMCreate(PETSC_COMM_WORLD, &dmnew);CHKERRQ(ierr);
66 ierr = DMSetType(dmnew, DMPLEX);CHKERRQ(ierr);
60 if (user.use_low_level_functions) {
61 ierr = DMPlexTopologyView(dm, v);CHKERRQ(ierr);
62 ierr = DMPlexCoordinatesView(dm, v);CHKERRQ(ierr);
63 ierr = DMPlexLabelsView(dm, v);CHKERRQ(ierr);
64 } else {
65 ierr = DMView(dm, v);CHKERRQ(ierr);
66 }
67
68 ierr = PetscViewerFileSetMode(v, FILE_MODE_READ);CHKERRQ(ierr);
69 ierr = DMCreate(PETSC_COMM_WORLD, &dmnew);CHKERRQ(ierr);
70 ierr = DMSetType(dmnew, DMPLEX);CHKERRQ(ierr);
67 ierr = PetscObjectSetName((PetscObject) dmnew, exampleDMPlexName);CHKERRQ(ierr);
71 ierr = PetscObjectSetName((PetscObject) dmnew, savedName);CHKERRQ(ierr);
68 ierr = DMSetOptionsPrefix(dmnew, prefix);CHKERRQ(ierr);
69 if (user.use_low_level_functions) {
70 PetscSF sfXC;
71
72 ierr = DMPlexTopologyLoad(dmnew, v, &sfXC);CHKERRQ(ierr);
73 ierr = DMPlexCoordinatesLoad(dmnew, v, sfXC);CHKERRQ(ierr);
74 ierr = PetscSFDestroy(&sfXC);CHKERRQ(ierr);
75 ierr = DMPlexLabelsLoad(dmnew, v);CHKERRQ(ierr);
76 } else {
77 ierr = DMLoad(dmnew, v);CHKERRQ(ierr);
78 }
72 ierr = DMSetOptionsPrefix(dmnew, prefix);CHKERRQ(ierr);
73 if (user.use_low_level_functions) {
74 PetscSF sfXC;
75
76 ierr = DMPlexTopologyLoad(dmnew, v, &sfXC);CHKERRQ(ierr);
77 ierr = DMPlexCoordinatesLoad(dmnew, v, sfXC);CHKERRQ(ierr);
78 ierr = PetscSFDestroy(&sfXC);CHKERRQ(ierr);
79 ierr = DMPlexLabelsLoad(dmnew, v);CHKERRQ(ierr);
80 } else {
81 ierr = DMLoad(dmnew, v);CHKERRQ(ierr);
82 }
79 ierr = PetscObjectSetName((PetscObject)dmnew,"Mesh_new");CHKERRQ(ierr);
83 ierr = PetscObjectSetName((PetscObject)dmnew,loadedName);CHKERRQ(ierr);
80
81 ierr = PetscViewerPopFormat(v);CHKERRQ(ierr);
82 ierr = PetscViewerDestroy(&v);CHKERRQ(ierr);
83 *dm_new = dmnew;
84 PetscFunctionReturn(0);
85}
86
87int main(int argc, char **argv)
88{
89 DM dm, dmnew;
90 PetscPartitioner part;
91 AppCtx user;
92 PetscBool flg;
93 PetscErrorCode ierr;
94
95 ierr = PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr;
96 ierr = ProcessOptions(PETSC_COMM_WORLD, &user);CHKERRQ(ierr);
84
85 ierr = PetscViewerPopFormat(v);CHKERRQ(ierr);
86 ierr = PetscViewerDestroy(&v);CHKERRQ(ierr);
87 *dm_new = dmnew;
88 PetscFunctionReturn(0);
89}
90
91int main(int argc, char **argv)
92{
93 DM dm, dmnew;
94 PetscPartitioner part;
95 AppCtx user;
96 PetscBool flg;
97 PetscErrorCode ierr;
98
99 ierr = PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr;
100 ierr = ProcessOptions(PETSC_COMM_WORLD, &user);CHKERRQ(ierr);
97 ierr = DMPlexCreateFromFile(PETSC_COMM_WORLD, user.filename, "ex55_plex", user.interpolate, &dm);CHKERRQ(ierr);
101 ierr = DMPlexCreateFromFile(PETSC_COMM_WORLD, user.filename, user.meshname, user.interpolate, &dm);CHKERRQ(ierr);
98 ierr = DMSetOptionsPrefix(dm,"orig_");CHKERRQ(ierr);
99 ierr = DMViewFromOptions(dm, NULL, "-dm_view");CHKERRQ(ierr);
100
101 if (user.distribute) {
102 DM dmdist;
103
104 ierr = DMPlexGetPartitioner(dm, &part);CHKERRQ(ierr);
105 ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr);

--- 183 unchanged lines hidden ---
102 ierr = DMSetOptionsPrefix(dm,"orig_");CHKERRQ(ierr);
103 ierr = DMViewFromOptions(dm, NULL, "-dm_view");CHKERRQ(ierr);
104
105 if (user.distribute) {
106 DM dmdist;
107
108 ierr = DMPlexGetPartitioner(dm, &part);CHKERRQ(ierr);
109 ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr);

--- 183 unchanged lines hidden ---