Lisandro Dalcin avatar Lisandro Dalcin committed e6ab833

Support for option -snes_fd_color in petsc-3.3

Comments (0)

Files changed (3)

 	-@${MPIEXEC} -n 8 ./Bratu ${OPTS} -iga_dim 2 -steady true -iga_collocation
 runex6d_9:
 	-@${MPIEXEC} -n 9 ./Bratu ${OPTS} -iga_dim 2 -steady true -iga_collocation
+runex6e_1:
+	-@${MPIEXEC} -n 1 ./Bratu ${OPTS} -iga_dim 2 -iga_degree 1 -snes_fd_color
+runex6e_4:
+	-@${MPIEXEC} -n 4 ./Bratu ${OPTS} -iga_dim 2 -iga_degree 1 -snes_fd_color
 runex7a_1:
 	-@${MPIEXEC} -n 1 ./Neumann ${OPTS} -iga_dim 1
 runex7a_4:
 Bratu.PETSc \
 runex6a_1 runex6a_2 runex6b_1 runex6b_4 runex6c_1 runex6c_4 \
 runex6d_1 runex6d_2 runex6d_4 runex6d_8 runex6d_9 \
+runex6e_1 runex6e_4 \
 Bratu.rm
 CahnHilliard2D := CahnHilliard2D.PETSc runex4_1 runex4_4 CahnHilliard2D.rm
 PatternFormation := PatternFormation.PETSc runex5a_1 runex5a_4 runex5b_1 runex5b_4 PatternFormation.rm
 EXTERN_C_END
 
 EXTERN_C_BEGIN
-extern PetscErrorCode SNESSetFromOptions_FDColoring(SNES);
+extern PetscErrorCode SNESSetFromOptions_FDColor(SNES);
 EXTERN_C_END
 
 static PetscBool IGAPackageInitialized = PETSC_FALSE;
   ierr = PetscLogEventRegister("IGAFormJacobian",IGA_CLASSID,&IGA_FormJacobian);CHKERRQ(ierr);
   ierr = PetscLogEventRegister("IGAFormIFunction",IGA_CLASSID,&IGA_FormIFunction);CHKERRQ(ierr);
   ierr = PetscLogEventRegister("IGAFormIJacobian",IGA_CLASSID,&IGA_FormIJacobian);CHKERRQ(ierr);
-  /* Additional option handlers */
-  ierr = SNESAddOptionsChecker(SNESSetFromOptions_FDColoring);CHKERRQ(ierr);
+#if PETSC_VERSION_LE(3,3,0)
+  /* Additional SNES option handler to support -snes_fd_color */
+  ierr = SNESAddOptionsChecker(SNESSetFromOptions_FDColor);CHKERRQ(ierr);
+#endif
   /* Register finalization routine */
   ierr = PetscRegisterFinalize(IGAFinalizePackage);CHKERRQ(ierr);
   PetscFunctionReturn(0);

src/snesfdcolor.c

 #include <petsc-private/petscimpl.h>
 
 #if PETSC_VERSION_LE(3,3,0)
-#define SNESComputeJacobianDefaultColor SNESDefaultComputeJacobianColor
-#endif
-
-PETSC_EXTERN PetscErrorCode SNESSetUpFDColoring(SNES);
-
-#undef __FUNCT__
-#define __FUNCT__ "SNESSetUpFDColoring"
-PetscErrorCode SNESSetUpFDColoring(SNES snes)
+#undef  __FUNCT__
+#define __FUNCT__ "SNESComputeJacobianDefaultColor"
+PETSC_EXTERN PetscErrorCode SNESComputeJacobianDefaultColor(SNES snes,Vec x,Mat *J,Mat *B,MatStructure *flag,void *ctx)
 {
-  const char*    prefix = NULL;
   Vec            f = NULL;
   PetscErrorCode (*fun)(SNES,Vec,Vec,void*) = NULL;
   void*          funP = NULL;
-  Mat            A = NULL, B = NULL;
-  PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*) = NULL;
-  void*          jacP = NULL;
   ISColoring     iscoloring = NULL;
-  MatFDColoring  fdcoloring = NULL;
+  MatFDColoring  color = NULL;
   PetscErrorCode ierr;
   PetscFunctionBegin;
-  PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
-
-  ierr = SNESGetOptionsPrefix(snes,&prefix);CHKERRQ(ierr);
   ierr = SNESGetFunction(snes,&f,&fun,&funP);CHKERRQ(ierr);
-  ierr = SNESGetJacobian(snes,&A,&B,&jac,&jacP);CHKERRQ(ierr);
-  if (!fun) {
-    SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
-    PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
+  ierr = PetscObjectQuery((PetscObject)*B,"SNESMatFDColoring",(PetscObject*)&color);CHKERRQ(ierr);
+  if (!color) {
+    ierr = MatGetColoring(*B,MATCOLORINGSL,&iscoloring);CHKERRQ(ierr);
+    ierr = MatFDColoringCreate(*B,iscoloring,&color);CHKERRQ(ierr);
+    ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
+    ierr = MatFDColoringSetFunction(color,(PetscErrorCode(*)(void))fun,(void*)funP);CHKERRQ(ierr);
+    ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
+    ierr = PetscObjectCompose((PetscObject)*B,"SNESMatFDColoring",(PetscObject)color);CHKERRQ(ierr);
+    ierr = PetscObjectDereference((PetscObject)color);CHKERRQ(ierr);
   }
-  if (!A && !B) {
-    SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetJacobian() must be called first");
-    PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
-  }
-  ierr = PetscObjectQuery((PetscObject)snes,"fdcoloring",(PetscObject*)&fdcoloring);CHKERRQ(ierr);
-  if (fdcoloring && fdcoloring == (MatFDColoring)jacP) PetscFunctionReturn(0);
-
-  ierr = MatGetColoring((B?B:A),MATCOLORINGSL,&iscoloring);CHKERRQ(ierr);
-  ierr = MatFDColoringCreate((B?B:A),iscoloring,&fdcoloring);CHKERRQ(ierr);
-  ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
-  ierr = MatFDColoringSetFunction(fdcoloring,(PetscErrorCode (*)(void))fun,funP);
-  ierr = PetscObjectSetOptionsPrefix((PetscObject)fdcoloring,prefix);CHKERRQ(ierr);
-  ierr = MatFDColoringSetFromOptions(fdcoloring);CHKERRQ(ierr);
-  ierr = PetscObjectCompose((PetscObject)snes,"fdcoloring",(PetscObject)fdcoloring);CHKERRQ(ierr);
-  ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobianDefaultColor,fdcoloring);CHKERRQ(ierr);
-  ierr = MatFDColoringDestroy(&fdcoloring);CHKERRQ(ierr);
-
+  ierr = SNESDefaultComputeJacobianColor(snes,x,J,B,flag,(void*)color);CHKERRQ(ierr);
   PetscFunctionReturn(0);
 }
+#endif
 
 EXTERN_C_BEGIN
 #undef __FUNCT__
-#define __FUNCT__ "SNESSetFromOptions_FDColoring"
-PetscErrorCode SNESSetFromOptions_FDColoring(SNES snes)
+#define __FUNCT__ "SNESSetFromOptions_FDColor"
+PetscErrorCode SNESSetFromOptions_FDColor(SNES snes)
 {
-  static PetscBool fdc = PETSC_FALSE;
-  PetscBool        opt;
-  PetscErrorCode   ierr;
+  PetscBool      flg = PETSC_FALSE;
+  void           *ctx;
+  PetscErrorCode ierr;
   PetscFunctionBegin;
-  ierr = PetscOptionsBool("-snes_fd_color","Use colored finite differences to compute Jacobian","SNESSetUpFDColoring",fdc,&fdc,NULL);CHKERRQ(ierr);
+  ierr = PetscOptionsBool("-snes_fd_color","Use finite differences with coloring to compute Jacobian","SNESComputeJacobianDefaultColor",flg,&flg,NULL);CHKERRQ(ierr);
   if (PetscOptionsPublishCount != 1) PetscFunctionReturn(0);
-  opt = fdc; fdc = PETSC_FALSE;
-  if (opt) {ierr = SNESSetUpFDColoring(snes);CHKERRQ(ierr);}
+  if (flg) {
+    ierr = SNESGetFunction(snes,NULL,NULL,&ctx);CHKERRQ(ierr);
+    ierr = SNESSetJacobian(snes,NULL,NULL,SNESComputeJacobianDefaultColor,NULL);CHKERRQ(ierr);
+    ierr = PetscInfo(snes,"Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
+  }
   PetscFunctionReturn(0);
 }
 EXTERN_C_END
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.