Actual source code: cstring.c

  1: /*$Id: cstring.c,v 1.13 2001/03/23 23:25:51 balay Exp $*/
 2:  #include src/pf/pfimpl.h

  4: /*
  5:         Ths PF generates a function on the fly and loads it into the running 
  6:    program.
  7: */

  9: int PFView_String(void *value,PetscViewer viewer)
 10: {
 11:   int        ierr;
 12:   PetscTruth isascii;

 15:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
 16:   if (isascii) {
 17:     PetscViewerASCIIPrintf(viewer,"String = %sn",(char*)value);
 18:   }
 19:   return(0);
 20: }

 22: int PFDestroy_String(void *value)
 23: {
 24:   int       ierr;

 27:   PetscStrfree((char*)value);
 28:   return(0);
 29: }

 31: int PFStringCreateFunction(PF pf,char *string,void **f)
 32: {
 33: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
 34:   int        ierr;
 35:   char       task[1024],tmp[256],lib[256],username[64];
 36:   FILE       *fd;
 37:   PetscTruth tmpshared,wdshared,keeptmpfiles = PETSC_FALSE;
 38:   MPI_Comm   comm;
 39: #endif

 42: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
 43:   PetscStrfree((char*)pf->data);
 44:   PetscStrallocpy(string,(char**)&pf->data);

 46:   /* create the new C function and compile it */
 47:   PetscSharedTmp(pf->comm,&tmpshared);
 48:   PetscSharedWorkingDirectory(pf->comm,&wdshared);
 49:   if (tmpshared) {  /* do it in /tmp since everyone has one */
 50:     PetscGetTmp(pf->comm,tmp,256);
 51:     comm = pf->comm;
 52:   } else if (!wdshared) {  /* each one does in private /tmp */
 53:     PetscGetTmp(pf->comm,tmp,256);
 54:     comm = PETSC_COMM_SELF;
 55:   } else { /* do it in current directory */
 56:     PetscStrcpy(tmp,".");
 57:     comm = pf->comm;
 58:   }
 59:   PetscOptionsHasName(pf->prefix,"-pf_string_keep_files",&keeptmpfiles);
 60:   if (keeptmpfiles) {
 61:     sprintf(task,"cd %s ; mkdir ${USERNAME} ; cd ${USERNAME} ; \cp -f ${PETSC_DIR}/src/pf/impls/string/makefile ./petscmakefile ; make BOPT=${BOPT} MIN=%d NOUT=%d -f petscmakefile petscdlib STRINGFUNCTION="%s" ; syncn",tmp,pf->dimin,pf->dimout,string);
 62:   } else {
 63:     sprintf(task,"cd %s ; mkdir ${USERNAME} ;cd ${USERNAME} ; \cp -f ${PETSC_DIR}/src/pf/impls/string/makefile ./petscmakefile ; make BOPT=${BOPT} MIN=%d NOUT=%d -f petscmakefile petscdlib STRINGFUNCTION="%s" ; \rm -f petscmakefile petscdlib.c libpetscdlib.a ;  syncn",tmp,pf->dimin,pf->dimout,string);
 64:   }
 65:   PetscPOpen(comm,PETSC_NULL,task,"r",&fd);
 66:   PetscPClose(comm,fd);

 68:   MPI_Barrier(comm);

 70:   /* load the apply function from the dynamic library */
 71:   PetscGetUserName(username,64);
 72:   sprintf(lib,"%s/%s/libpetscdlib",tmp,username);
 73:   PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);
 74: #endif
 75:   return(0);
 76: }

 78: int PFSetFromOptions_String(PF pf)
 79: {
 80:   int        ierr;
 81:   PetscTruth flag;
 82:   char       value[256];
 83:   int        (*f)(void *,int,Scalar*,Scalar*) = 0;

 86:   PetscOptionsHead("String function options");
 87:     PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,256,&flag);
 88:     if (flag) {
 89:       PFStringCreateFunction(pf,value,(void**)&f);
 90:       pf->ops->apply = f;
 91:     }
 92:   PetscOptionsTail();
 93:   return(0);
 94: }


 97: EXTERN_C_BEGIN
 98: int PFCreate_String(PF pf,void *value)
 99: {
100:   int        ierr;
101:   int        (*f)(void *,int,Scalar*,Scalar*) = 0;

104: 
105:   if (value) {
106:     PFStringCreateFunction(pf,(char*)value,(void**)&f);
107:   }
108:   ierr   = PFSet(pf,f,PETSC_NULL,PFView_String,PFDestroy_String,PETSC_NULL);

110:   pf->ops->setfromoptions = PFSetFromOptions_String;
111:   return(0);
112: }
113: EXTERN_C_END