xref: /petsc/src/sys/tutorials/ex20.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
1c4762a1bSJed Brown 
2c4762a1bSJed Brown static char help[] = "Demonstrates PetscOptionsPush()/PetscOptionsPop().\n\n";
3c4762a1bSJed Brown 
4c4762a1bSJed Brown #include <petscsys.h>
5c4762a1bSJed Brown #include <petscoptions.h>
6*d71ae5a4SJacob Faibussowitsch int main(int argc, char **argv)
7*d71ae5a4SJacob Faibussowitsch {
8c4762a1bSJed Brown   PetscOptions opt1, opt2;
9c4762a1bSJed Brown   PetscInt     int1, int2;
10c4762a1bSJed Brown   PetscBool    flg1, flg2, flga, match;
11c4762a1bSJed Brown   char         str[16];
12c4762a1bSJed Brown 
13327415f7SBarry Smith   PetscFunctionBeginUser;
149566063dSJacob Faibussowitsch   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
15c4762a1bSJed Brown 
169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsCreate(&opt1));
179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInsertString(opt1, "-testa a"));
189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPush(opt1));
199566063dSJacob Faibussowitsch   PetscCall(PetscOptionsSetValue(NULL, "-test1", "1"));
209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test1", &int1, &flg1));
21e00437b9SBarry Smith   PetscCheck(flg1 && int1 == 1, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unable to locate option test1 or it has the wrong value");
229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, NULL, "-testa", str, sizeof(str), &flga));
239566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(str, "a", &match));
24e00437b9SBarry Smith   PetscCheck(flga && match, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unable to locate option testa or it has the wrong value");
259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsCreate(&opt2));
269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPush(opt2));
279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsSetValue(NULL, "-test2", "2"));
289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test2", &int2, &flg2));
29e00437b9SBarry Smith   PetscCheck(flg2 && int2 == 2, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unable to locate option test2 or it has the wrong value");
309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test1", &int1, &flg1));
3128b400f6SJacob Faibussowitsch   PetscCheck(!flg1, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Able to access test1 from a different options database");
32c4762a1bSJed Brown 
339566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPop());
349566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPop());
359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsDestroy(&opt2));
369566063dSJacob Faibussowitsch   PetscCall(PetscOptionsDestroy(&opt1));
379566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
38b122ec5aSJacob Faibussowitsch   return 0;
39c4762a1bSJed Brown }
40c4762a1bSJed Brown 
41c4762a1bSJed Brown /*TEST
42c4762a1bSJed Brown 
43c4762a1bSJed Brown    test:
44c4762a1bSJed Brown 
45c4762a1bSJed Brown TEST*/
46