System Preprocessors
u3.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include "syspro.h"
00003 #include "sysprotransform.h"
00004 #include "string.h"
00005 
00006 /*
00007   Test for a single preprocessor, which adds 1 to the problem number
00008  */
00009 
00010 /* the problem solving function: just a simple copy */
00011 #undef __FUNCT__
00012 #define __FUNCT__ "solvebycopy"
00013 static PetscErrorCode solvebycopy
00014 (NumericalProblem problem,void *dum,NumericalSolution *rsol)
00015 {
00016   NumericalSolution sol; PetscErrorCode ierr;
00017   PetscFunctionBegin;
00018   ierr = PetscMalloc(sizeof(int),(int**)&sol); CHKERRQ(ierr);
00019   SysProTraceMessage("Solving problem %d\n",*(int*)problem);
00020   *(int*)sol = *(int*)problem;
00021   *rsol = sol;
00022   PetscFunctionReturn(0);
00023 }
00024 
00025 /* deallocate a problem object */
00026 #undef __FUNCT__
00027 #define __FUNCT__ "delprob"
00028 static PetscErrorCode delprob(NumericalProblem p)
00029 {
00030   PetscErrorCode ierr;
00031   PetscFunctionBegin;
00032   ierr = PetscFree(p); CHKERRQ(ierr);
00033   PetscFunctionReturn(0);
00034 }
00035 
00036 /* allocate a solution object */
00037 #undef __FUNCT__
00038 #define __FUNCT__ "makesol"
00039 static PetscErrorCode makesol(NumericalProblem p,NumericalSolution *rs)
00040 {
00041   NumericalSolution s; PetscErrorCode ierr;
00042   PetscFunctionBegin;
00043   ierr = PetscMalloc(sizeof(int),(int**)&s); CHKERRQ(ierr);
00044   *rs = s;
00045   PetscFunctionReturn(0);
00046 }
00047 
00048 /* deallocate a solution object */
00049 #undef __FUNCT__
00050 #define __FUNCT__ "delsol"
00051 static PetscErrorCode delsol(NumericalSolution s)
00052 {
00053   PetscErrorCode ierr;
00054   PetscFunctionBegin;
00055   ierr = PetscFree(s); CHKERRQ(ierr);
00056   PetscFunctionReturn(0);
00057 }
00058 
00059 /* preprocessor handling: we add 1 to the problem value */
00060 #undef __FUNCT__
00061 #define __FUNCT__ "adder"
00062 static PetscErrorCode adder
00063 (const char *choice,int optionvalue,PetscBool overwrite,
00064  NumericalProblem oldproblem,NumericalProblem *rnew,
00065  void *ctx,void **lctx,PetscBool *success)
00066 {
00067   NumericalProblem newproblem; PetscErrorCode ierr;
00068   PetscFunctionBegin;
00069   if (strcmp(choice,"add1")) SETERRQ1(PETSC_COMM_SELF,1,"Unknown choice <%s>",choice);
00070 
00071   ierr = PetscMalloc(sizeof(int),(int**)&newproblem); CHKERRQ(ierr);
00072   *(int*)newproblem = *(int*)oldproblem+1;
00073   SysProTraceMessage("new problem: %d\n",*(int*)newproblem);
00074   *rnew = newproblem;
00075 
00076   *success = PETSC_TRUE;
00077   PetscFunctionReturn(0);
00078 }
00079 
00080 /* the unpreprocess function subtracts one */
00081 #undef __FUNCT__
00082 #define __FUNCT__ "unadder"
00083 static PetscErrorCode unadder
00084 (const char *choice,PetscBool overwrite,void *ctx,void *lctx,
00085  NumericalProblem pproblem,NumericalProblem oproblem,
00086  NumericalSolution psol,NumericalSolution osol)
00087 {
00088   PetscFunctionBegin;
00089   if (strcmp(choice,"add1")) SETERRQ1(PETSC_COMM_SELF,1,"Unknown choice <%s>",choice);
00090   *(int*)osol = *(int*)psol-1;
00091   PetscFunctionReturn(0);
00092 }
00093 
00094 /* we declare our one and only preprocessor */
00095 #undef __FUNCT__
00096 #define __FUNCT__ "declareadders"
00097 static PetscErrorCode declareadders()
00098 {
00099   PetscErrorCode ierr;
00100   PetscFunctionBegin;
00101   ierr = NewTransformObject("adder","add1",NULL); CHKERRQ(ierr);
00102   PetscFunctionReturn(0);
00103 }
00104 
00105 #undef __FUNCT__
00106 #define __FUNCT__ "main"
00107 int main(int argc,char **argv) {
00108   NumericalProblem problem; NumericalSolution solution;
00109   PetscErrorCode ierr;
00110   PetscInitialize(&argc,&argv,0,0);
00111   ierr = SysProInitialize(); CHKERRQ(ierr);
00112   ierr = SysProDeclareFunctions
00113     (NULL,NULL,NULL,solvebycopy,delprob,
00114      makesol,NULL,delsol,NULL,NULL,NULL); CHKERRQ(ierr);
00115   ierr = SysProDeclareTraceFunction(&SysProDefaultTrace); CHKERRQ(ierr);
00116 
00117   /* set up a simple problem */
00118   ierr = PetscMalloc(sizeof(int),(int**)&problem); CHKERRQ(ierr);
00119   *(int*)problem = 5;
00120 
00121   /* declare a preprocessor */
00122   ierr = DeclarePreprocessor
00123     ("adder",&declareadders,NULL,NULL,NULL,NULL,NULL,
00124      &adder,&unadder); CHKERRQ(ierr);
00125 
00126   /* solve this problem */
00127   ierr = PreprocessedProblemSolving(problem,&solution); CHKERRQ(ierr);
00128 
00129   /* check for correctness */
00130   {
00131     int s = *(int*)solution;
00132     if (s!=5)
00133       SETERRQ1(PETSC_COMM_SELF,1,"Computed wrong solution %d",s);
00134   }
00135 
00136   ierr = PetscFree(problem); CHKERRQ(ierr);
00137   ierr = PetscFree(solution); CHKERRQ(ierr);
00138   ierr = SysProFinalize(); CHKERRQ(ierr);
00139   PetscFinalize();
00140   return 0;
00141 }