Actual source code: ex8.c
1: static char help[] = "Tests CGNS viewers.\n\n";
3: #include <petscsys.h>
4: #include <petscviewer.h>
5: #include <petscdm.h>
6: #include <petscfe.h>
8: static PetscErrorCode TestOpen(PetscFileMode mode, PetscViewer *viewer)
9: {
10: PetscFunctionBeginUser;
11: PetscCall(PetscViewerCGNSOpen(PETSC_COMM_WORLD, "cgns.cgns", mode, viewer));
12: PetscCall(PetscViewerSetUp(*viewer));
13: PetscFunctionReturn(PETSC_SUCCESS);
14: }
16: static PetscErrorCode GetLongDescription(PetscInt *len, char **desc)
17: {
18: const PetscInt desc_len = 4096, desc_dup_len = 32;
19: const char desc_dup[] = "this is a far longer description";
21: PetscFunctionBeginUser;
22: *len = desc_len;
23: PetscCall(PetscCalloc1(desc_len + 1, desc));
24: for (PetscInt i = 0; i < desc_len; i += desc_dup_len) {
25: PetscCall(PetscMemcpy(&(*desc)[i], desc_dup, desc_dup_len));
26: }
27: (*desc)[desc_len] = '\0';
28: PetscFunctionReturn(PETSC_SUCCESS);
29: }
31: static PetscErrorCode TestWriteDescriptors(PetscViewer viewer)
32: {
33: char *desc;
34: PetscInt desc_len;
36: PetscFunctionBeginUser;
37: PetscCall(PetscViewerCGNSSetDescriptor(viewer, "Help", "This will be overwritten"));
39: PetscCall(GetLongDescription(&desc_len, &desc));
40: PetscCall(PetscViewerCGNSSetDescriptor(viewer, "Long Description", desc));
41: PetscCall(PetscFree(desc));
43: PetscCall(PetscViewerCGNSSetDescriptor(viewer, "Resize", "This causes a resize operation"));
45: PetscCall(PetscViewerCGNSSetDescriptor(viewer, "Help", help));
46: PetscFunctionReturn(PETSC_SUCCESS);
47: }
49: static PetscErrorCode TestReadDescriptors(PetscViewer viewer)
50: {
51: char **descriptors, **names, *expected_desc;
52: PetscInt num_descriptors, expected_desc_len;
53: PetscBool is_same;
55: PetscFunctionBeginUser;
56: PetscCall(PetscViewerCGNSGetDescriptors(viewer, &num_descriptors, &names, &descriptors));
57: PetscCheck(num_descriptors == 3, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Expected 3 descriptors, got %" PetscInt_FMT, num_descriptors);
59: PetscCall(PetscStrcmp(names[0], "Help", &is_same));
60: PetscCheck(is_same, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Wrong name for descriptor 0, expected 'Help' but got '%s'", names[0] ? names[0] : "(null)");
62: PetscCall(PetscStrcmp(names[1], "Long Description", &is_same));
63: PetscCheck(is_same, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Wrong name for descriptor 1, expected 'Long Description' but got '%s'", names[1] ? names[1] : "(null)");
65: PetscCall(PetscStrcmp(names[2], "Resize", &is_same));
66: PetscCheck(is_same, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Wrong name for descriptor 2, expected 'Resize' but got '%s'", names[2] ? names[2] : "(null)");
68: PetscCall(PetscStrcmp(descriptors[0], help, &is_same));
69: PetscCheck(is_same, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Wrong value for descriptor 0, expected '%s' but got '%s'", help, descriptors[0] ? descriptors[0] : "(null)");
71: PetscCall(GetLongDescription(&expected_desc_len, &expected_desc));
72: PetscCall(PetscStrcmp(descriptors[1], expected_desc, &is_same));
73: PetscCheck(is_same, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Wrong value for descriptor 1");
74: PetscCall(PetscFree(expected_desc));
76: PetscCall(PetscStrcmp(descriptors[2], "This causes a resize operation", &is_same));
77: PetscCheck(is_same, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Wrong value for descriptor 2, expected 'This causes a resize operation' but got '%s'", descriptors[2] ? descriptors[2] : "(null)");
79: PetscCall(PetscViewerCGNSRestoreDescriptors(viewer, &num_descriptors, &names, &descriptors));
80: PetscFunctionReturn(PETSC_SUCCESS);
81: }
83: static PetscErrorCode TestReadDescriptorsEmpty(PetscViewer viewer)
84: {
85: char **descriptors, **names;
86: PetscInt num_descriptors;
88: PetscFunctionBeginUser;
89: PetscCall(PetscViewerCGNSGetDescriptors(viewer, &num_descriptors, &names, &descriptors));
90: PetscCheck(num_descriptors == 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Descriptors should be empty, found %" PetscInt_FMT, num_descriptors);
91: PetscCall(PetscViewerCGNSRestoreDescriptors(viewer, &num_descriptors, &names, &descriptors));
92: PetscFunctionReturn(PETSC_SUCCESS);
93: }
95: int main(int argc, char **args)
96: {
97: PetscViewer viewer;
98: DM dm;
99: Vec v;
100: PetscInt dim;
101: PetscFE fe;
103: PetscFunctionBeginUser;
104: PetscCall(PetscInitialize(&argc, &args, NULL, help));
105: PetscCall(DMCreate(PETSC_COMM_WORLD, &dm));
106: PetscCall(DMSetType(dm, DMPLEX));
107: PetscCall(DMSetFromOptions(dm));
108: PetscCall(DMGetCoordinateDim(dm, &dim));
109: PetscCall(PetscFECreateDefault(PETSC_COMM_WORLD, dim, 1, PETSC_FALSE, NULL, 2, &fe));
110: PetscCall(DMAddField(dm, NULL, (PetscObject)fe));
111: PetscCall(DMCreateDS(dm));
113: PetscCall(DMGetGlobalVector(dm, &v));
114: PetscCall(VecZeroEntries(v));
116: PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
117: PetscCall(VecView(v, viewer));
118: PetscCall(TestWriteDescriptors(viewer));
119: PetscCall(PetscViewerDestroy(&viewer));
121: PetscCall(TestOpen(FILE_MODE_READ, &viewer));
122: PetscCall(TestReadDescriptors(viewer));
123: PetscCall(PetscViewerDestroy(&viewer));
125: PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
126: PetscCall(TestWriteDescriptors(viewer));
127: PetscCall(VecView(v, viewer));
128: PetscCall(PetscViewerDestroy(&viewer));
130: PetscCall(TestOpen(FILE_MODE_READ, &viewer));
131: PetscCall(TestReadDescriptors(viewer));
132: PetscCall(PetscViewerDestroy(&viewer));
134: PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
135: PetscCall(TestWriteDescriptors(viewer));
136: PetscCall(PetscViewerDestroy(&viewer));
138: PetscCall(TestOpen(FILE_MODE_READ, &viewer));
139: PetscCall(TestReadDescriptorsEmpty(viewer));
140: PetscCall(PetscViewerDestroy(&viewer));
142: PetscCall(DMRestoreGlobalVector(dm, &v));
143: PetscCall(DMDestroy(&dm));
144: PetscCall(PetscFEDestroy(&fe));
145: PetscCall(PetscFinalize());
146: return 0;
147: }
149: /*TEST
150: build:
151: requires: cgns
152: test:
153: nsize: 1 2
154: output_file: output/empty.out
155: args: -dm_plex_box_faces 3,3,3 -dm_plex_dim 3
156: TEST*/