xref: /petsc/src/snes/interface/dlregissnes.c (revision a29dfd43bb0c77e2653d3bfa2c953f902720a6d2)
1 #include <petsc/private/snesimpl.h>
2 #include <petsc/private/linesearchimpl.h>
3 #include <petsc/private/dmadaptorimpl.h>
4 
5 static PetscBool SNESPackageInitialized = PETSC_FALSE;
6 
7 /*@C
8   SNESFinalizePackage - This function destroys everything in the PETSc interface to the `SNES` package. It is
9   called from `PetscFinalize()`.
10 
11   Level: developer
12 
13 .seealso: [](ch_snes), `SNES`, `PetscFinalize()`
14 @*/
15 PetscErrorCode SNESFinalizePackage(void)
16 {
17   PetscFunctionBegin;
18   PetscCall(PetscFunctionListDestroy(&SNESList));
19   PetscCall(PetscFunctionListDestroy(&SNESLineSearchList));
20   SNESPackageInitialized          = PETSC_FALSE;
21   SNESRegisterAllCalled           = PETSC_FALSE;
22   SNESLineSearchRegisterAllCalled = PETSC_FALSE;
23   PetscFunctionReturn(PETSC_SUCCESS);
24 }
25 
26 /*@C
27   SNESInitializePackage - This function initializes everything in the `SNES` package. It is called
28   from PetscDLLibraryRegister_petscsnes() when using dynamic libraries, and on the first call to `SNESCreate()`
29   when using shared or static libraries.
30 
31   Level: developer
32 
33 .seealso: [](ch_snes), `SNES`, `PetscInitialize()`
34 @*/
35 PetscErrorCode SNESInitializePackage(void)
36 {
37   char      logList[256];
38   PetscBool opt, pkg, cls;
39 
40   PetscFunctionBegin;
41   if (SNESPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
42   SNESPackageInitialized = PETSC_TRUE;
43   /* Initialize subpackages */
44   PetscCall(SNESMSInitializePackage());
45   /* Register Classes */
46   PetscCall(PetscClassIdRegister("SNES", &SNES_CLASSID));
47   PetscCall(PetscClassIdRegister("DMSNES", &DMSNES_CLASSID));
48   PetscCall(PetscClassIdRegister("SNESLineSearch", &SNESLINESEARCH_CLASSID));
49   PetscCall(PetscClassIdRegister("DM Adaptor", &DMADAPTOR_CLASSID));
50   /* Register Constructors */
51   PetscCall(SNESRegisterAll());
52   PetscCall(SNESLineSearchRegisterAll());
53   PetscCall(DMAdaptorRegisterAll());
54   PetscCall(PetscRegisterFinalize(DMAdaptorRegisterDestroy));
55   PetscCall(DMAdaptorMonitorRegisterAll());
56   PetscCall(PetscRegisterFinalize(DMAdaptorMonitorRegisterDestroy));
57   /* Register Events */
58   PetscCall(PetscLogEventRegister("SNESSolve", SNES_CLASSID, &SNES_Solve));
59   PetscCall(PetscLogEventRegister("SNESSetUp", SNES_CLASSID, &SNES_SetUp));
60   PetscCall(PetscLogEventRegister("SNESFunctionEval", SNES_CLASSID, &SNES_FunctionEval));
61   PetscCall(PetscLogEventRegister("SNESObjectiveEval", SNES_CLASSID, &SNES_ObjectiveEval));
62   PetscCall(PetscLogEventRegister("SNESNGSEval", SNES_CLASSID, &SNES_NGSEval));
63   PetscCall(PetscLogEventRegister("SNESNGSFuncEval", SNES_CLASSID, &SNES_NGSFuncEval));
64   PetscCall(PetscLogEventRegister("SNESJacobianEval", SNES_CLASSID, &SNES_JacobianEval));
65   PetscCall(PetscLogEventRegister("SNESNPCSolve", SNES_CLASSID, &SNES_NPCSolve));
66   PetscCall(PetscLogEventRegister("SNESLineSearch", SNESLINESEARCH_CLASSID, &SNESLINESEARCH_Apply));
67   /* Process Info */
68   {
69     PetscClassId classids[3];
70 
71     classids[0] = SNES_CLASSID;
72     classids[1] = DMSNES_CLASSID;
73     classids[2] = SNESLINESEARCH_CLASSID;
74     PetscCall(PetscInfoProcessClass("snes", 1, classids));
75     PetscCall(PetscInfoProcessClass("dm", 1, &classids[1]));
76     PetscCall(PetscInfoProcessClass("sneslinesearch", 1, &classids[2]));
77   }
78   /* Process summary exclusions */
79   PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
80   if (opt) {
81     PetscCall(PetscStrInList("snes", logList, ',', &pkg));
82     if (pkg) PetscCall(PetscLogEventExcludeClass(SNES_CLASSID));
83     PetscCall(PetscStrInList("dm", logList, ',', &cls));
84     if (pkg || cls) PetscCall(PetscLogEventExcludeClass(DMSNES_CLASSID));
85     PetscCall(PetscStrInList("sneslinesearch", logList, ',', &cls));
86     if (pkg || cls) PetscCall(PetscLogEventExcludeClass(SNESLINESEARCH_CLASSID));
87   }
88   /* Register package finalizer */
89   PetscCall(PetscRegisterFinalize(SNESFinalizePackage));
90   PetscFunctionReturn(PETSC_SUCCESS);
91 }
92 
93 #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
94 /*
95   PetscDLLibraryRegister - This function is called when the dynamic library it is in is opened.
96 
97   This registers all of the SNES methods that are in the basic PETSc libpetscsnes library.
98 
99  */
100 PETSC_EXTERN PetscErrorCode PetscDLLibraryRegister_petscsnes(void)
101 {
102   PetscFunctionBegin;
103   PetscCall(SNESInitializePackage());
104   PetscFunctionReturn(PETSC_SUCCESS);
105 }
106 
107 #endif /* PETSC_HAVE_DYNAMIC_LIBRARIES */
108