xref: /petsc/src/sys/tutorials/ex20.c (revision 609caa7c8c030312b00807b4f015fd827bb80932)
1c4762a1bSJed Brown static char help[] = "Demonstrates PetscOptionsPush()/PetscOptionsPop().\n\n";
2c4762a1bSJed Brown 
3c4762a1bSJed Brown #include <petscsys.h>
4c4762a1bSJed Brown #include <petscoptions.h>
main(int argc,char ** argv)5d71ae5a4SJacob Faibussowitsch int main(int argc, char **argv)
6d71ae5a4SJacob Faibussowitsch {
7c4762a1bSJed Brown   PetscOptions opt1, opt2;
8c4762a1bSJed Brown   PetscInt     int1, int2;
9c4762a1bSJed Brown   PetscBool    flg1, flg2, flga, match;
10c4762a1bSJed Brown   char         str[16];
11c4762a1bSJed Brown 
12327415f7SBarry Smith   PetscFunctionBeginUser;
13c8025a54SPierre Jolivet   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
14c4762a1bSJed Brown 
159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsCreate(&opt1));
169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInsertString(opt1, "-testa a"));
179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPush(opt1));
189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsSetValue(NULL, "-test1", "1"));
199566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test1", &int1, &flg1));
20e00437b9SBarry Smith   PetscCheck(flg1 && int1 == 1, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unable to locate option test1 or it has the wrong value");
219566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString(NULL, NULL, "-testa", str, sizeof(str), &flga));
229566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(str, "a", &match));
23e00437b9SBarry Smith   PetscCheck(flga && match, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unable to locate option testa or it has the wrong value");
249566063dSJacob Faibussowitsch   PetscCall(PetscOptionsCreate(&opt2));
259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPush(opt2));
269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsSetValue(NULL, "-test2", "2"));
279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test2", &int2, &flg2));
28e00437b9SBarry Smith   PetscCheck(flg2 && int2 == 2, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unable to locate option test2 or it has the wrong value");
299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, NULL, "-test1", &int1, &flg1));
3028b400f6SJacob Faibussowitsch   PetscCheck(!flg1, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Able to access test1 from a different options database");
31c4762a1bSJed Brown 
329566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPop());
339566063dSJacob Faibussowitsch   PetscCall(PetscOptionsPop());
349566063dSJacob Faibussowitsch   PetscCall(PetscOptionsDestroy(&opt2));
359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsDestroy(&opt1));
369566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
37b122ec5aSJacob Faibussowitsch   return 0;
38c4762a1bSJed Brown }
39c4762a1bSJed Brown 
40c4762a1bSJed Brown /*TEST
41c4762a1bSJed Brown 
42c4762a1bSJed Brown    test:
43*3886731fSPierre Jolivet      output_file: output/empty.out
44c4762a1bSJed Brown 
45c4762a1bSJed Brown TEST*/
46