Actual source code: ex8.c
1: static char help[] = "Tests CGNS viewers.\n\n";
3: #include <petscdm.h>
4: #include <petscfe.h>
6: static PetscErrorCode TestOpen(PetscFileMode mode, PetscViewer *viewer)
7: {
8: PetscFunctionBeginUser;
9: PetscCall(PetscViewerCGNSOpen(PETSC_COMM_WORLD, "cgns.cgns", mode, viewer));
10: PetscCall(PetscViewerSetUp(*viewer));
11: PetscFunctionReturn(PETSC_SUCCESS);
12: }
14: static PetscErrorCode GetLongDescription(PetscInt *len, char **desc)
15: {
16: const PetscInt desc_len = 4096, desc_dup_len = 32;
17: const char desc_dup[] = "this is a far longer description";
19: PetscFunctionBeginUser;
20: *len = desc_len;
21: PetscCall(PetscCalloc1(desc_len + 1, desc));
22: for (PetscInt i = 0; i < desc_len; i += desc_dup_len) {
23: PetscCall(PetscMemcpy(&(*desc)[i], desc_dup, desc_dup_len));
24: }
25: (*desc)[desc_len] = '\0';
26: PetscFunctionReturn(PETSC_SUCCESS);
27: }
29: static PetscErrorCode TestWriteDescriptors(PetscViewer viewer)
30: {
31: char *desc;
32: PetscInt desc_len;
34: PetscFunctionBeginUser;
35: PetscCall(PetscViewerCGNSSetDescriptor(viewer, "Help", "This will be overwritten"));
37: PetscCall(GetLongDescription(&desc_len, &desc));
38: PetscCall(PetscViewerCGNSSetDescriptor(viewer, "Long Description", desc));
39: PetscCall(PetscFree(desc));
41: PetscCall(PetscViewerCGNSSetDescriptor(viewer, "Resize", "This causes a resize operation"));
43: PetscCall(PetscViewerCGNSSetDescriptor(viewer, "Help", help));
44: PetscFunctionReturn(PETSC_SUCCESS);
45: }
47: static PetscErrorCode TestReadDescriptors(PetscViewer viewer)
48: {
49: char **descriptors, **names, *expected_desc;
50: PetscInt num_descriptors, expected_desc_len;
51: PetscBool is_same;
53: PetscFunctionBeginUser;
54: PetscCall(PetscViewerCGNSGetDescriptors(viewer, &num_descriptors, &names, &descriptors));
55: PetscCheck(num_descriptors == 3, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Expected 3 descriptors, got %" PetscInt_FMT, num_descriptors);
57: PetscCall(PetscStrcmp(names[0], "Help", &is_same));
58: 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)");
60: PetscCall(PetscStrcmp(names[1], "Long Description", &is_same));
61: 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)");
63: PetscCall(PetscStrcmp(names[2], "Resize", &is_same));
64: 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)");
66: PetscCall(PetscStrcmp(descriptors[0], help, &is_same));
67: 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)");
69: PetscCall(GetLongDescription(&expected_desc_len, &expected_desc));
70: PetscCall(PetscStrcmp(descriptors[1], expected_desc, &is_same));
71: PetscCheck(is_same, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Wrong value for descriptor 1");
72: PetscCall(PetscFree(expected_desc));
74: PetscCall(PetscStrcmp(descriptors[2], "This causes a resize operation", &is_same));
75: 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)");
77: PetscCall(PetscViewerCGNSRestoreDescriptors(viewer, &num_descriptors, &names, &descriptors));
78: PetscFunctionReturn(PETSC_SUCCESS);
79: }
81: static PetscErrorCode TestReadDescriptorsEmpty(PetscViewer viewer)
82: {
83: char **descriptors, **names;
84: PetscInt num_descriptors;
86: PetscFunctionBeginUser;
87: PetscCall(PetscViewerCGNSGetDescriptors(viewer, &num_descriptors, &names, &descriptors));
88: PetscCheck(num_descriptors == 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Descriptors should be empty, found %" PetscInt_FMT, num_descriptors);
89: PetscCall(PetscViewerCGNSRestoreDescriptors(viewer, &num_descriptors, &names, &descriptors));
90: PetscFunctionReturn(PETSC_SUCCESS);
91: }
93: int main(int argc, char **args)
94: {
95: PetscViewer viewer;
96: DM dm;
97: Vec v;
98: PetscInt dim;
99: PetscFE fe;
101: PetscFunctionBeginUser;
102: PetscCall(PetscInitialize(&argc, &args, NULL, help));
103: PetscCall(DMCreate(PETSC_COMM_WORLD, &dm));
104: PetscCall(DMSetType(dm, DMPLEX));
105: PetscCall(DMSetFromOptions(dm));
106: PetscCall(DMGetCoordinateDim(dm, &dim));
107: PetscCall(PetscFECreateDefault(PETSC_COMM_WORLD, dim, 1, PETSC_FALSE, NULL, 2, &fe));
108: PetscCall(DMAddField(dm, NULL, (PetscObject)fe));
109: PetscCall(DMCreateDS(dm));
111: PetscCall(DMGetGlobalVector(dm, &v));
112: PetscCall(VecZeroEntries(v));
114: PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
115: PetscCall(VecView(v, viewer));
116: PetscCall(TestWriteDescriptors(viewer));
117: PetscCall(PetscViewerDestroy(&viewer));
119: PetscCall(TestOpen(FILE_MODE_READ, &viewer));
120: PetscCall(TestReadDescriptors(viewer));
121: PetscCall(PetscViewerDestroy(&viewer));
123: PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
124: PetscCall(TestWriteDescriptors(viewer));
125: PetscCall(VecView(v, viewer));
126: PetscCall(PetscViewerDestroy(&viewer));
128: PetscCall(TestOpen(FILE_MODE_READ, &viewer));
129: PetscCall(TestReadDescriptors(viewer));
130: PetscCall(PetscViewerDestroy(&viewer));
132: PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
133: PetscCall(TestWriteDescriptors(viewer));
134: PetscCall(PetscViewerDestroy(&viewer));
136: PetscCall(TestOpen(FILE_MODE_READ, &viewer));
137: PetscCall(TestReadDescriptorsEmpty(viewer));
138: PetscCall(PetscViewerDestroy(&viewer));
140: PetscCall(DMRestoreGlobalVector(dm, &v));
141: PetscCall(DMDestroy(&dm));
142: PetscCall(PetscFEDestroy(&fe));
143: PetscCall(PetscFinalize());
144: return 0;
145: }
147: /*TEST
148: build:
149: requires: cgns
150: test:
151: nsize: 1 2
152: output_file: output/empty.out
153: args: -dm_plex_box_faces 3,3,3 -dm_plex_dim 3
154: TEST*/