/*@C PetscDrawAppendTitle - Appends to the title of a PetscDraw context. Not collective (any processor or all can call this) Input Parameters: + draw - the graphics context - title - the title Note: A copy of the string is made, so you may destroy the title string after calling this routine. Level: advanced .seealso: PetscDrawSetTitle(), PetscDrawGetTitle() @*/ PetscErrorCode PetscDrawAppendTitle(PetscDraw draw,const char title[]) { PetscErrorCode ierr; size_t len1,len2,len; char *newtitle; PetscFunctionBegin; PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); if (!title) PetscFunctionReturn(0); if (draw->title) { ierr = PetscStrlen(title,&len1);CHKERRQ(ierr); ierr = PetscStrlen(draw->title,&len2);CHKERRQ(ierr); len = len1 + len2; ierr = PetscMalloc1((len + 1),&newtitle);CHKERRQ(ierr); ierr = PetscStrcpy(newtitle,draw->title);CHKERRQ(ierr); ierr = PetscStrcat(newtitle,title);CHKERRQ(ierr); ierr = PetscFree(draw->title);CHKERRQ(ierr); draw->title = newtitle; } else { ierr = PetscStrallocpy(title,&draw->title);CHKERRQ(ierr); } if (draw->ops->settitle) { ierr = (*draw->ops->settitle)(draw,draw->title);CHKERRQ(ierr); } PetscFunctionReturn(0); }
static PetscErrorCode PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char chrs[]) { PetscErrorCode ierr; int xx,yy; size_t len; PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; char *substr; PetscToken token; PetscFunctionBegin; xx = XTRANS(draw,XiWin,x); yy = YTRANS(draw,XiWin,y); PetscDrawXiSetColor(XiWin,c); ierr = PetscTokenCreate(chrs,'\n',&token);CHKERRQ(ierr); ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr); ierr = PetscStrlen(substr,&len);CHKERRQ(ierr); XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len); ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr); while (substr) { yy += 4*XiWin->font->font_descent; ierr = PetscStrlen(substr,&len);CHKERRQ(ierr); XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len); ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr); } ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C PetscGetHostName - Returns the name of the host. This attempts to return the entire Internet name. It may not return the same name as MPI_Get_processor_name(). Not Collective Input Parameter: . nlen - length of name Output Parameter: . name - contains host name. Must be long enough to hold the name This is the fully qualified name, including the domain. Level: developer Concepts: machine name Concepts: host name Fortran Version: In Fortran this routine has the format $ character*(64) name $ call PetscGetHostName(name,ierr) .seealso: PetscGetUserName(),PetscGetArchType() @*/ PetscErrorCode PetscGetHostName(char name[],size_t nlen) { char *domain; PetscErrorCode ierr; #if defined(PETSC_HAVE_UNAME) && !defined(PETSC_HAVE_GETCOMPUTERNAME) struct utsname utname; #endif PetscFunctionBegin; #if defined(PETSC_HAVE_GETCOMPUTERNAME) { size_t nnlen = nlen; GetComputerName((LPTSTR)name,(LPDWORD)(&nnlen)); } #elif defined(PETSC_HAVE_UNAME) uname(&utname); ierr = PetscStrncpy(name,utname.nodename,nlen); CHKERRQ(ierr); #elif defined(PETSC_HAVE_GETHOSTNAME) gethostname(name,nlen); #elif defined(PETSC_HAVE_SYSINFO_3ARG) sysinfo(SI_HOSTNAME,name,nlen); #endif /* if there was not enough room then system call will not null terminate name */ name[nlen-1] = 0; /* See if this name includes the domain */ ierr = PetscStrchr(name,'.',&domain); CHKERRQ(ierr); if (!domain) { size_t l,ll; ierr = PetscStrlen(name,&l); CHKERRQ(ierr); if (l == nlen-1) PetscFunctionReturn(0); name[l++] = '.'; name[l] = 0; #if defined(PETSC_HAVE_SYSINFO_3ARG) sysinfo(SI_SRPC_DOMAIN,name+l,nlen-l); #elif defined(PETSC_HAVE_GETDOMAINNAME) if (getdomainname(name+l,nlen - l)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"getdomainname()"); #endif /* check if domain name is not a dnsdomainname and nuke it */ ierr = PetscStrlen(name,&ll); CHKERRQ(ierr); if (ll > 4) { const char *suffixes[] = {".edu",".com",".net",".org",".mil",0}; PetscInt index; ierr = PetscStrendswithwhich(name,suffixes,&index); CHKERRQ(ierr); if (!suffixes[index]) { ierr = PetscInfo1(0,"Rejecting domainname, likely is NIS %s\n",name); CHKERRQ(ierr); name[l-1] = 0; } } } PetscFunctionReturn(0); }
PetscErrorCode MatLUFactorNumeric_Matlab(Mat F,Mat A,const MatFactorInfo *info) { PetscErrorCode ierr; size_t len; char *_A,*name; PetscReal dtcol = info->dtcol; PetscFunctionBegin; if (F->factortype == MAT_FACTOR_ILU || info->dt > 0) { /* the ILU form is not currently registered */ if (info->dtcol == PETSC_DEFAULT) dtcol = .01; F->ops->solve = MatSolve_Matlab; F->factortype = MAT_FACTOR_LU; ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)A); CHKERRQ(ierr); _A = ((PetscObject)A)->name; ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"info_%s = struct('droptol',%g,'thresh',%g);",_A,info->dt,dtcol); CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"[l_%s,u_%s,p_%s] = luinc(%s',info_%s);",_A,_A,_A,_A,_A); CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = 0;",_A); CHKERRQ(ierr); ierr = PetscStrlen(_A,&len); CHKERRQ(ierr); ierr = PetscMalloc1(len+2,&name); CHKERRQ(ierr); sprintf(name,"_%s",_A); ierr = PetscObjectSetName((PetscObject)F,name); CHKERRQ(ierr); ierr = PetscFree(name); CHKERRQ(ierr); } else { ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)A); CHKERRQ(ierr); _A = ((PetscObject)A)->name; ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"[l_%s,u_%s,p_%s] = lu(%s',%g);",_A,_A,_A,_A,dtcol); CHKERRQ(ierr); ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = 0;",_A); CHKERRQ(ierr); ierr = PetscStrlen(_A,&len); CHKERRQ(ierr); ierr = PetscMalloc1(len+2,&name); CHKERRQ(ierr); sprintf(name,"_%s",_A); ierr = PetscObjectSetName((PetscObject)F,name); CHKERRQ(ierr); ierr = PetscFree(name); CHKERRQ(ierr); F->ops->solve = MatSolve_Matlab; } PetscFunctionReturn(0); }
PetscErrorCode PetscPathJoin(const char dname[],const char fname[],size_t n,char fullname[]) { PetscErrorCode ierr; size_t l1,l2; PetscFunctionBegin; ierr = PetscStrlen(dname,&l1);CHKERRQ(ierr); ierr = PetscStrlen(fname,&l2);CHKERRQ(ierr); if ((l1+l2+2)>n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Path length is greater than buffer size"); ierr = PetscStrcpy(fullname,dname);CHKERRQ(ierr); ierr = PetscStrcat(fullname,"/");CHKERRQ(ierr); ierr = PetscStrcat(fullname,fname);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C PetscDrawSetSave - Saves images produced in a PetscDraw into a file Collective on PetscDraw Input Parameter: + draw - the graphics context . filename - name of the file, if .ext then uses name of draw object plus .ext using .ext to determine the image type - movieext - if not NULL, produces a movie of all the images Options Database Command: + -draw_save <filename> - filename could be name.ext or .ext (where .ext determines the type of graphics file to save, for example .png) . -draw_save_movie <.ext> - saves a movie to filename.ext . -draw_save_final_image [optional filename] - saves the final image displayed in a window - -draw_save_single_file - saves each new image in the same file, normally each new image is saved in a new file with filename/filename_%d.ext Level: intermediate Concepts: X windows^graphics Notes: You should call this BEFORE creating your image and calling PetscDrawSave(). The supported image types are .png, .gif, .jpg, and .ppm (PETSc chooses the default in that order). Support for .png images requires configure --with-libpng. Support for .gif images requires configure --with-giflib. Support for .jpg images requires configure --with-libjpeg. Support for .ppm images is built-in. The PPM format has no compression (640x480 pixels ~ 900 KiB). The ffmpeg utility must be in your path to make the movie. .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSetSaveFinalImage() @*/ PetscErrorCode PetscDrawSetSave(PetscDraw draw,const char filename[],const char movieext[]) { const char *savename = NULL; const char *imageext = NULL; char buf[PETSC_MAX_PATH_LEN]; PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); if (filename) PetscValidCharPointer(filename,2); if (movieext) PetscValidCharPointer(movieext,2); /* determine save filename and image extension */ if (filename && filename[0]) { ierr = PetscStrchr(filename,'.',(char **)&imageext);CHKERRQ(ierr); if (!imageext) savename = filename; else if (imageext != filename) { size_t l1 = 0,l2 = 0; ierr = PetscStrlen(filename,&l1);CHKERRQ(ierr); ierr = PetscStrlen(imageext,&l2);CHKERRQ(ierr); ierr = PetscStrncpy(buf,filename,l1-l2+1);CHKERRQ(ierr); savename = buf; } } if (!savename) {ierr = PetscObjectGetName((PetscObject)draw,&savename);CHKERRQ(ierr);} ierr = PetscDrawImageCheckFormat(&imageext);CHKERRQ(ierr); if (movieext) {ierr = PetscDrawMovieCheckFormat(&movieext);CHKERRQ(ierr);} if (movieext) draw->savesinglefile = PETSC_FALSE; /* otherwise we cannot generage movies */ if (draw->savesinglefile) { ierr = PetscInfo2(NULL,"Will save image to file %s%s\n",savename,imageext);CHKERRQ(ierr); } else { ierr = PetscInfo3(NULL,"Will save images to file %s/%s_%%d%s\n",savename,savename,imageext);CHKERRQ(ierr); } if (movieext) { ierr = PetscInfo2(NULL,"Will save movie to file %s%s\n",savename,movieext);CHKERRQ(ierr); } draw->savefilecount = 0; ierr = PetscFree(draw->savefilename);CHKERRQ(ierr); ierr = PetscFree(draw->saveimageext);CHKERRQ(ierr); ierr = PetscFree(draw->savemovieext);CHKERRQ(ierr); ierr = PetscStrallocpy(savename,&draw->savefilename);CHKERRQ(ierr); ierr = PetscStrallocpy(imageext,&draw->saveimageext);CHKERRQ(ierr); ierr = PetscStrallocpy(movieext,&draw->savemovieext);CHKERRQ(ierr); PetscFunctionReturn(0); }
/* Removes trailing zeros */ PetscErrorCode PetscStripTrailingZeros(char *buf) { PetscErrorCode ierr; char *found; size_t i,n,m = PETSC_MAX_INT; PetscFunctionBegin; /* if there is an e in string DO NOT strip trailing zeros */ ierr = PetscStrchr(buf,'e',&found);CHKERRQ(ierr); if (found) PetscFunctionReturn(0); ierr = PetscStrlen(buf,&n);CHKERRQ(ierr); /* locate decimal point */ for (i=0; i<n; i++) { if (buf[i] == '.') {m = i; break;} } /* if not decimal point then no zeros to remove */ if (m == PETSC_MAX_INT) PetscFunctionReturn(0); /* start at right end of string removing 0s */ for (i=n-1; i>m; i++) { if (buf[i] != '0') PetscFunctionReturn(0); buf[i] = 0; } PetscFunctionReturn(0); }
PetscErrorCode PetscViewerFileSetName_MPIIO(PetscViewer viewer,const char name[]) { PetscMPIInt rank; PetscErrorCode ierr; size_t len; PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; char *gz; PetscBool found; PetscFileMode type = vbinary->btype; PetscFunctionBegin; if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()"); ierr = PetscViewerFileClose_MPIIO(viewer);CHKERRQ(ierr); ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_info",&vbinary->skipinfo,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_options",&vbinary->skipoptions,NULL);CHKERRQ(ierr); ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr); vbinary->storecompressed = PETSC_FALSE; /* only first processor opens file if writeable */ if (type == FILE_MODE_READ) { MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_RDONLY,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr); } else if (type == FILE_MODE_WRITE) { MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_WRONLY | MPI_MODE_CREATE,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr); } /* try to open info file: all processors open this file if read only Below is identical code to the code for Binary above, should be put in seperate routine */ if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) { char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN]; ierr = PetscStrcpy(infoname,name);CHKERRQ(ierr); /* remove .gz if it ends library name */ ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr); if (gz) { ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); if (len == 3) *gz = 0; } ierr = PetscStrcat(infoname,".info");CHKERRQ(ierr); ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr); if (type == FILE_MODE_READ) { ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),infoname,PETSC_FALSE);CHKERRQ(ierr); } else { vbinary->fdes_info = fopen(infoname,"w"); if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname); } } #if defined(PETSC_USE_LOG) PetscLogObjectState((PetscObject)viewer,"File: %s",name); #endif PetscFunctionReturn(0); }
static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *ctx, DM *dm) { PetscInt dim = ctx->dim; PetscBool cellSimplex = ctx->cellSimplex; const char *filename = ctx->filename; const PetscInt cells[3] = {1, 1, 1}; size_t len; PetscMPIInt rank, size; PetscErrorCode ierr; PetscFunctionBegin; ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); ierr = PetscStrlen(filename, &len);CHKERRQ(ierr); if (len) {ierr = DMPlexCreateFromFile(comm, filename, PETSC_TRUE, dm);CHKERRQ(ierr);} else {ierr = DMPlexCreateBoxMesh(comm, dim, cellSimplex, cells, NULL, NULL, NULL, PETSC_TRUE, dm);CHKERRQ(ierr);} { DM distributedMesh = NULL; PetscPartitioner part; ierr = DMPlexGetPartitioner(*dm, &part);CHKERRQ(ierr); ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr); /* Distribute mesh over processes */ ierr = DMPlexDistribute(*dm, 0, NULL, &distributedMesh);CHKERRQ(ierr); if (distributedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = distributedMesh; } } ierr = PetscObjectSetName((PetscObject) *dm, "Mesh");CHKERRQ(ierr); ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr); PetscFunctionReturn(0); }
/* Removes the plus in something like 1.1e+2 or 1.1e+02 */ PetscErrorCode PetscStripZerosPlus(char *buf) { PetscErrorCode ierr; size_t i,j,n; PetscFunctionBegin; ierr = PetscStrlen(buf,&n);CHKERRQ(ierr); if (n<5) PetscFunctionReturn(0); for (i=1; i<n-2; i++) { if (buf[i] == '+') { if (buf[i+1] == '0') { for (j=i+1; j<n; j++) buf[j-1] = buf[j+1]; PetscFunctionReturn(0); } else { for (j=i+1; j<n+1; j++) buf[j-1] = buf[j]; PetscFunctionReturn(0); } } else if (buf[i] == '-') { if (buf[i+1] == '0') { for (j=i+1; j<n; j++) buf[j] = buf[j+1]; PetscFunctionReturn(0); } } } PetscFunctionReturn(0); }
/*@C PetscVFPrintf - All PETSc standard out and error messages are sent through this function; so, in theory, this can can be replaced with something that does not simply write to a file. To use, write your own function for example, $PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp) ${ $ PetscErrorCode ierr; $ $ PetscFunctionBegin; $ if (fd != stdout && fd != stderr) { handle regular files $ ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERR(ierr); $ } else { $ char buff[BIG]; $ size_t length; $ ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr); $ now send buff to whatever stream or whatever you want $ } $ PetscFunctionReturn(0); $} then before the call to PetscInitialize() do the assignment $ PetscVFPrintf = mypetscvfprintf; Notes: For error messages this may be called by any process, for regular standard out it is called only by process 0 of a given communicator Developer Notes: this could be called by an error handler, if that happens then a recursion of the error handler may occur and a crash Level: developer .seealso: PetscVSNPrintf(), PetscErrorPrintf() @*/ PetscErrorCode PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp) { char *newformat; char formatbuf[8*1024]; size_t oldLength; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr); if (oldLength < 8*1024) { newformat = formatbuf; oldLength = 8*1024-1; } else { oldLength = PETSC_MAX_LENGTH_FORMAT(oldLength); ierr = PetscMalloc(oldLength * sizeof(char), &newformat);CHKERRQ(ierr); } ierr = PetscFormatConvert(format,newformat,oldLength);CHKERRQ(ierr); #if defined(PETSC_HAVE_VFPRINTF_CHAR) vfprintf(fd,newformat,(char*)Argp); #else vfprintf(fd,newformat,Argp); #endif fflush(fd); if (oldLength >= 8*1024) { ierr = PetscFree(newformat);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode PetscViewerFileSetName_VTK(PetscViewer viewer,const char name[]) { PetscViewer_VTK *vtk = (PetscViewer_VTK*)viewer->data; PetscErrorCode ierr; PetscBool isvtk,isvts,isvtu,isvtr; size_t len; PetscFunctionBegin; ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); ierr = PetscFree(vtk->filename);CHKERRQ(ierr); ierr = PetscStrlen(name,&len);CHKERRQ(ierr); ierr = PetscStrcasecmp(name+len-4,".vtk",&isvtk);CHKERRQ(ierr); ierr = PetscStrcasecmp(name+len-4,".vts",&isvts);CHKERRQ(ierr); ierr = PetscStrcasecmp(name+len-4,".vtu",&isvtu);CHKERRQ(ierr); ierr = PetscStrcasecmp(name+len-4,".vtr",&isvtr);CHKERRQ(ierr); if (isvtk) { if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);} if (viewer->format != PETSC_VIEWER_ASCII_VTK) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_INCOMP,"Cannot use file '%s' with format %s, should have '.vtk' extension",name,PetscViewerFormats[viewer->format]); } else if (isvts) { if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_VTK_VTS);CHKERRQ(ierr);} if (viewer->format != PETSC_VIEWER_VTK_VTS) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_INCOMP,"Cannot use file '%s' with format %s, should have '.vts' extension",name,PetscViewerFormats[viewer->format]); } else if (isvtu) { if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_VTK_VTU);CHKERRQ(ierr);} if (viewer->format != PETSC_VIEWER_VTK_VTU) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_INCOMP,"Cannot use file '%s' with format %s, should have '.vtu' extension",name,PetscViewerFormats[viewer->format]); } else if (isvtr) { if (viewer->format == PETSC_VIEWER_DEFAULT) {ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_VTK_VTR);CHKERRQ(ierr);} if (viewer->format != PETSC_VIEWER_VTK_VTR) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_INCOMP,"Cannot use file '%s' with format %s, should have '.vtr' extension",name,PetscViewerFormats[viewer->format]); } else SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_UNKNOWN_TYPE,"File '%s' has unrecognized extension",name); ierr = PetscStrallocpy(name,&vtk->filename);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) { const char *filename = user->filename; PetscInt dim = user->dim; size_t len; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscStrlen(filename, &len);CHKERRQ(ierr); if (!len) {ierr = DMPlexCreateBoxMesh(comm, dim, PETSC_TRUE, dm);CHKERRQ(ierr);} else {ierr = DMPlexCreateFromFile(comm, filename, PETSC_TRUE, dm);CHKERRQ(ierr);} ierr = DMViewFromOptions(*dm, NULL, "-orig_dm_view");CHKERRQ(ierr); { DM distributedMesh = NULL; ierr = DMPlexDistribute(*dm, 0, NULL, &distributedMesh);CHKERRQ(ierr); if (distributedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = distributedMesh; ierr = DMViewFromOptions(*dm, NULL, "-dist_dm_view");CHKERRQ(ierr); } } ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); if (user->uninterpolate) { DM udm = NULL; ierr = DMPlexUninterpolate(*dm, &udm);CHKERRQ(ierr); ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = udm; ierr = DMViewFromOptions(*dm, NULL, "-un_dm_view");CHKERRQ(ierr); } ierr = PetscObjectSetName((PetscObject) *dm, "Test Mesh");CHKERRQ(ierr); ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode PetscDrawSave_SAWs(PetscDraw draw) { PetscImageList image; char body[4096]; size_t len = 0; PetscErrorCode ierr; PetscFunctionBegin; if (!draw->savefilename) PetscFunctionReturn(0); ierr = PetscImageListAdd(draw->savefilename,draw->saveimageext,draw->savefilecount-1);CHKERRQ(ierr); image = SAWs_images; while (image) { const char *name = image->filename; const char *ext = image->ext; if (draw->savesinglefile) { ierr = PetscSNPrintf(body+len,4086-len,"<img src=\"%s%s\" alt=\"None\">",name,ext);CHKERRQ(ierr); } else { ierr = PetscSNPrintf(body+len,4086-len,"<img src=\"%s/%s_%d%s\" alt=\"None\">",name,name,image->count,ext);CHKERRQ(ierr); } ierr = PetscStrlen(body,&len);CHKERRQ(ierr); image = image->next; } ierr = PetscStrcat(body,"<br>\n");CHKERRQ(ierr); if (draw->savefilecount > 0) PetscStackCallSAWs(SAWs_Pop_Body,("index.html",1)); PetscStackCallSAWs(SAWs_Push_Body,("index.html",1,body)); PetscFunctionReturn(0); }
PetscErrorCode PetscLs(MPI_Comm comm,const char libname[],char found[],size_t tlen,PetscBool *flg) { PetscErrorCode ierr; size_t len; char *f,program[PETSC_MAX_PATH_LEN]; FILE *fp; PetscFunctionBegin; ierr = PetscStrcpy(program,"ls ");CHKERRQ(ierr); ierr = PetscStrcat(program,libname);CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(comm,PETSC_NULL,program,"r",&fp);CHKERRQ(ierr); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif f = fgets(found,tlen,fp); if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE; while (f) { ierr = PetscStrlen(found,&len);CHKERRQ(ierr); f = fgets(found+len,tlen-len,fp); } if (*flg) {ierr = PetscInfo2(0,"ls on %s gives \n%s\n",libname,found);CHKERRQ(ierr);} #if defined(PETSC_HAVE_POPEN) ierr = PetscPClose(comm,fp);CHKERRQ(ierr); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif PetscFunctionReturn(0); }
/*@C PetscViewerStringSPrintf - Prints information to a PetscViewer string. Logically Collective on PetscViewer (Hmmm, each processor maintains a separate string) Input Parameters: + v - a string PetscViewer, formed by PetscViewerStringOpen() - format - the format of the input Level: developer Fortran Note: This routine is not supported in Fortran. Concepts: printing^to string .seealso: PetscViewerStringOpen(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING @*/ PetscErrorCode PetscViewerStringSPrintf(PetscViewer viewer,const char format[],...) { va_list Argp; size_t fullLength; size_t shift,cshift; PetscErrorCode ierr; PetscBool isstring; char tmp[4096]; PetscViewer_String *vstr = (PetscViewer_String*)viewer->data; PetscFunctionBegin; PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); PetscValidCharPointer(format,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); if (!isstring) PetscFunctionReturn(0); if (!vstr->string) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using"); va_start(Argp,format); ierr = PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);CHKERRQ(ierr); va_end(Argp); ierr = PetscStrlen(tmp,&shift);CHKERRQ(ierr); cshift = shift+1; if (cshift >= vstr->maxlen - vstr->curlen - 1) cshift = vstr->maxlen - vstr->curlen - 1; ierr = PetscStrncpy(vstr->head,tmp,cshift);CHKERRQ(ierr); vstr->head += shift; vstr->curlen += shift; PetscFunctionReturn(0); }
static PetscErrorCode PCBDDCScalingSetUp_Deluxe_Private(PC pc) { PC_BDDC *pcbddc=(PC_BDDC*)pc->data; PCBDDCDeluxeScaling deluxe_ctx=pcbddc->deluxe_ctx; PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs; PetscErrorCode ierr; PetscFunctionBegin; if (!sub_schurs->n_subs) { PetscFunctionReturn(0); } /* Create work vectors for sequential part of deluxe */ ierr = MatCreateVecs(sub_schurs->S_Ej_all,&deluxe_ctx->seq_work1,&deluxe_ctx->seq_work2);CHKERRQ(ierr); /* Compute deluxe sequential scatter */ if (sub_schurs->reuse_mumps && !sub_schurs->is_dir) { PCBDDCReuseMumps reuse_mumps = sub_schurs->reuse_mumps; ierr = PetscObjectReference((PetscObject)reuse_mumps->correction_scatter_B);CHKERRQ(ierr); deluxe_ctx->seq_scctx = reuse_mumps->correction_scatter_B; } else { ierr = VecScatterCreate(pcbddc->work_scaling,sub_schurs->is_Ej_all,deluxe_ctx->seq_work1,NULL,&deluxe_ctx->seq_scctx);CHKERRQ(ierr); } /* Create Mat object for deluxe scaling */ ierr = PetscObjectReference((PetscObject)sub_schurs->S_Ej_all);CHKERRQ(ierr); deluxe_ctx->seq_mat = sub_schurs->S_Ej_all; if (sub_schurs->sum_S_Ej_all) { /* if this matrix is present, then we need to create the KSP object to invert it */ PC pc_temp; MatSolverPackage solver=NULL; char ksp_prefix[256]; size_t len; ierr = KSPCreate(PETSC_COMM_SELF,&deluxe_ctx->seq_ksp);CHKERRQ(ierr); ierr = KSPSetOperators(deluxe_ctx->seq_ksp,sub_schurs->sum_S_Ej_all,sub_schurs->sum_S_Ej_all);CHKERRQ(ierr); ierr = KSPSetType(deluxe_ctx->seq_ksp,KSPPREONLY);CHKERRQ(ierr); ierr = KSPGetPC(deluxe_ctx->seq_ksp,&pc_temp);CHKERRQ(ierr); ierr = PCSetType(pc_temp,PCLU);CHKERRQ(ierr); ierr = KSPGetPC(pcbddc->ksp_D,&pc_temp);CHKERRQ(ierr); ierr = PCFactorGetMatSolverPackage(pc_temp,(const MatSolverPackage*)&solver);CHKERRQ(ierr); if (solver) { PC new_pc; PCType type; ierr = PCGetType(pc_temp,&type);CHKERRQ(ierr); ierr = KSPGetPC(deluxe_ctx->seq_ksp,&new_pc);CHKERRQ(ierr); ierr = PCSetType(new_pc,type);CHKERRQ(ierr); ierr = PCFactorSetMatSolverPackage(new_pc,solver);CHKERRQ(ierr); } ierr = PetscStrlen(((PetscObject)(pcbddc->ksp_D))->prefix,&len);CHKERRQ(ierr); len -= 10; /* remove "dirichlet_" */ ierr = PetscStrncpy(ksp_prefix,((PetscObject)(pcbddc->ksp_D))->prefix,len+1);CHKERRQ(ierr); ierr = PetscStrcat(ksp_prefix,"deluxe_");CHKERRQ(ierr); ierr = KSPSetOptionsPrefix(deluxe_ctx->seq_ksp,ksp_prefix);CHKERRQ(ierr); ierr = KSPSetFromOptions(deluxe_ctx->seq_ksp);CHKERRQ(ierr); ierr = KSPSetUp(deluxe_ctx->seq_ksp);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/* These are not extern C because they are passed into non-extern C user level functions */ static PetscErrorCode ourerrorhandler(MPI_Comm comm,int line,const char *fun,const char *file,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx) { PetscErrorCode ierr = 0; size_t len1,len2,len3; int l1,l2,l3; PetscStrlen(fun,&len1); l1 = (int)len1; PetscStrlen(file,&len2);l2 = (int)len2; PetscStrlen(mess,&len3);l3 = (int)len3; #if defined(PETSC_HAVE_FORTRAN_MIXED_STR_ARG) (*f2)(&comm,&line,fun,l1,file,l2,&n,&p,mess,l3,ctx,&ierr); #else (*f2)(&comm,&line,fun,file,&n,&p,mess,ctx,&ierr,l1,l2,l3); #endif return ierr; }
static PetscErrorCode PetscHTTPBuildRequest(const char type[],const char url[],const char header[],const char ctype[],const char body[],char **outrequest) { char *request=0; char contentlength[40],contenttype[80],*path,*host; size_t request_len,headlen,bodylen,contentlen,pathlen,hostlen,typelen,contenttypelen = 0; PetscErrorCode ierr; PetscBool flg; PetscFunctionBegin; ierr = PetscStrallocpy(url,&host);CHKERRQ(ierr); ierr = PetscStrchr(host,'/',&path);CHKERRQ(ierr); if (!path) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"url must contain / it is %s",url); *path = 0; ierr = PetscStrlen(host,&hostlen);CHKERRQ(ierr); ierr = PetscStrchr(url,'/',&path);CHKERRQ(ierr); ierr = PetscStrlen(path,&pathlen);CHKERRQ(ierr); if (header) { ierr = PetscStrendswith(header,"\r\n",&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"header must end with \\r\\n"); } ierr = PetscStrlen(type,&typelen);CHKERRQ(ierr); if (ctype) { ierr = PetscSNPrintf(contenttype,80,"Content-Type: %s\r\n",ctype);CHKERRQ(ierr); ierr = PetscStrlen(contenttype,&contenttypelen);CHKERRQ(ierr); } ierr = PetscStrlen(header,&headlen);CHKERRQ(ierr); ierr = PetscStrlen(body,&bodylen);CHKERRQ(ierr); ierr = PetscSNPrintf(contentlength,40,"Content-Length: %d\r\n\r\n",(int)bodylen);CHKERRQ(ierr); ierr = PetscStrlen(contentlength,&contentlen);CHKERRQ(ierr); /* Now construct our HTTP request */ request_len = typelen + 1 + pathlen + hostlen + 100 + headlen + contenttypelen + contentlen + bodylen + 1; ierr = PetscMalloc1(request_len,&request);CHKERRQ(ierr); ierr = PetscStrcpy(request,type);CHKERRQ(ierr); ierr = PetscStrcat(request," ");CHKERRQ(ierr); ierr = PetscStrcat(request,path);CHKERRQ(ierr); ierr = PetscStrcat(request," HTTP/1.1\r\nHost: ");CHKERRQ(ierr); ierr = PetscStrcat(request,host);CHKERRQ(ierr); ierr = PetscFree(host);CHKERRQ(ierr); ierr = PetscStrcat(request,"\r\nUser-Agent:PETScClient\r\n");CHKERRQ(ierr); ierr = PetscStrcat(request,header);CHKERRQ(ierr); if (ctype) { ierr = PetscStrcat(request,contenttype);CHKERRQ(ierr); } ierr = PetscStrcat(request,contentlength);CHKERRQ(ierr); ierr = PetscStrcat(request,body);CHKERRQ(ierr); ierr = PetscStrlen(request,&request_len);CHKERRQ(ierr); ierr = PetscInfo1(NULL,"HTTPS request follows: \n%s\n",request);CHKERRQ(ierr); *outrequest = request; PetscFunctionReturn(0); }
/*@C PetscPOpen - Runs a program on processor zero and sends either its input or output to a file. Logically Collective on MPI_Comm, but only process 0 runs the command Input Parameters: + comm - MPI communicator, only processor zero runs the program . machine - machine to run command on or NULL, or string with 0 in first location . program - name of program to run - mode - either r or w Output Parameter: . fp - the file pointer where program input or output may be read or NULL if don't care Level: intermediate Notes: Use PetscPClose() to close the file pointer when you are finished with it Does not work under Windows If machine is not provided will use the value set with PetsPOpenSetMachine() if that was provided, otherwise will use the machine running node zero of the communicator The program string may contain ${DISPLAY}, ${HOMEDIRECTORY} or ${WORKINGDIRECTORY}; these will be replaced with relevent values. .seealso: PetscFOpen(), PetscFClose(), PetscPClose(), PetscPOpenSetMachine() @*/ PetscErrorCode PetscPOpen(MPI_Comm comm,const char machine[],const char program[],const char mode[],FILE **fp) { PetscErrorCode ierr; PetscMPIInt rank; size_t i,len,cnt; char commandt[PETSC_MAX_PATH_LEN],command[PETSC_MAX_PATH_LEN]; FILE *fd; PetscFunctionBegin; /* all processors have to do the string manipulation because PetscStrreplace() is a collective operation */ if (PetscPOpenMachine[0] || (machine && machine[0])) { ierr = PetscStrcpy(command,"ssh ");CHKERRQ(ierr); if (PetscPOpenMachine[0]) { ierr = PetscStrcat(command,PetscPOpenMachine);CHKERRQ(ierr); } else { ierr = PetscStrcat(command,machine);CHKERRQ(ierr); } ierr = PetscStrcat(command," \" export DISPLAY=${DISPLAY}; ");CHKERRQ(ierr); /* Copy program into command but protect the " with a \ in front of it */ ierr = PetscStrlen(command,&cnt);CHKERRQ(ierr); ierr = PetscStrlen(program,&len);CHKERRQ(ierr); for (i=0; i<len; i++) { if (program[i] == '\"') command[cnt++] = '\\'; command[cnt++] = program[i]; } command[cnt] = 0; ierr = PetscStrcat(command,"\"");CHKERRQ(ierr); } else { ierr = PetscStrcpy(command,program);CHKERRQ(ierr); } ierr = PetscStrreplace(comm,command,commandt,1024);CHKERRQ(ierr); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscInfo1(0,"Running command :%s\n",commandt);CHKERRQ(ierr); if (!(fd = popen(commandt,mode))) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot run command %s",commandt); if (fp) *fp = fd; } PetscFunctionReturn(0); }
/*@C PetscBoxUpload - Loads a file to the Box Drive This routine has not yet been written; it is just copied from Google Drive Not collective, only the first process in the MPI_Comm uploads the file Input Parameters: + comm - MPI communicator . access_token - obtained with PetscBoxRefresh(), pass NULL to have PETSc generate one - filename - file to upload; if you upload multiple times it will have different names each time on Box Drive Options Database: . -box_refresh_token XXX Usage Patterns: With PETSc option -box_refresh_token XXX given PetscBoxUpload(comm,NULL,filename); will upload file with no user interaction Without PETSc option -box_refresh_token XXX given PetscBoxUpload(comm,NULL,filename); for first use will prompt user to authorize access to Box Drive with their processor With PETSc option -box_refresh_token XXX given PetscBoxRefresh(comm,NULL,access_token,sizeof(access_token)); PetscBoxUpload(comm,access_token,filename); With refresh token entered in some way by the user PetscBoxRefresh(comm,refresh_token,access_token,sizeof(access_token)); PetscBoxUpload(comm,access_token,filename); PetscBoxAuthorize(comm,access_token,refresh_token,sizeof(access_token)); PetscBoxUpload(comm,access_token,filename); Level: intermediate .seealso: PetscURLShorten(), PetscBoxAuthorize(), PetscBoxRefresh() @*/ PetscErrorCode PetscBoxUpload(MPI_Comm comm,const char access_token[],const char filename[]) { SSL_CTX *ctx; SSL *ssl; int sock; PetscErrorCode ierr; char head[1024],buff[8*1024],*body,*title; PetscMPIInt rank; struct stat sb; size_t len,blen,rd; FILE *fd; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscStrcpy(head,"Authorization: Bearer ");CHKERRQ(ierr); ierr = PetscStrcat(head,access_token);CHKERRQ(ierr); ierr = PetscStrcat(head,"\r\n");CHKERRQ(ierr); ierr = PetscStrcat(head,"uploadType: multipart\r\n");CHKERRQ(ierr); ierr = stat(filename,&sb); if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to stat file: %s",filename); len = 1024 + sb.st_size; ierr = PetscMalloc1(len,&body);CHKERRQ(ierr); ierr = PetscStrcpy(body,"--foo_bar_baz\r\n" "Content-Type: application/json\r\n\r\n" "{");CHKERRQ(ierr); ierr = PetscPushJSONValue(body,"title",filename,len);CHKERRQ(ierr); ierr = PetscStrcat(body,",");CHKERRQ(ierr); ierr = PetscPushJSONValue(body,"mimeType","text.html",len);CHKERRQ(ierr); ierr = PetscStrcat(body,",");CHKERRQ(ierr); ierr = PetscPushJSONValue(body,"description","a file",len);CHKERRQ(ierr); ierr = PetscStrcat(body, "}\r\n\r\n" "--foo_bar_baz\r\n" "Content-Type: text/html\r\n\r\n");CHKERRQ(ierr); ierr = PetscStrlen(body,&blen);CHKERRQ(ierr); fd = fopen (filename, "r"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open file: %s",filename); rd = fread (body+blen, sizeof (unsigned char), sb.st_size, fd); if (rd != (size_t)sb.st_size) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to read entire file: %s %d %d",filename,(int)rd,(int)sb.st_size); fclose(fd); body[blen + rd] = 0; ierr = PetscStrcat(body,"\r\n\r\n" "--foo_bar_baz\r\n");CHKERRQ(ierr); ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); ierr = PetscHTTPSConnect("www.boxapis.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); ierr = PetscHTTPSRequest("POST","www.boxapis.com/upload/drive/v2/files/",head,"multipart/related; boundary=\"foo_bar_baz\"",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); ierr = PetscFree(body);CHKERRQ(ierr); ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); close(sock); ierr = PetscStrstr(buff,"\"title\"",&title);CHKERRQ(ierr); if (!title) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Upload of file %s failed",filename); } PetscFunctionReturn(0); }
/*@C PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc Not collective, only the first process in MPI_Comm does anything Input Parameters: + comm - the MPI communicator - tokensize - size of the token arrays Output Parameters: + access_token - can be used with PetscGoogleDriveUpload() for this one session - refresh_token - can be used for ever to obtain new access_tokens with PetscGoogleDriveRefresh(), guard this like a password it gives access to your Google Drive Notes: This call requires stdout and stdin access from process 0 on the MPI communicator You can run src/sys/webclient/examples/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to PETSc programs with -google_refresh_token XXX Level: intermediate .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscURLShorten() @*/ PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize) { SSL_CTX *ctx; SSL *ssl; int sock; PetscErrorCode ierr; char buff[8*1024],*ptr,body[1024]; PetscMPIInt rank; size_t len; PetscBool found; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output"); ierr = PetscPrintf(comm,"Cut and paste the following into your browser:\n\n" "https://accounts.google.com/o/oauth2/auth?" "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&" "redirect_uri=urn:ietf:wg:oauth:2.0:oob&" "response_type=code&" "client_id=" PETSC_GOOGLE_CLIENT_ID "\n\n");CHKERRQ(ierr); ierr = PetscPrintf(comm,"Paste the result here:");CHKERRQ(ierr); ptr = fgets(buff, 1024, stdin); if (!ptr) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno); ierr = PetscStrlen(buff,&len);CHKERRQ(ierr); buff[len-1] = 0; /* remove carriage return at end of line */ ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); ierr = PetscStrcpy(body,"code=");CHKERRQ(ierr); ierr = PetscStrcat(body,buff);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_id=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); ierr = PetscStrcat(body,"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&");CHKERRQ(ierr); ierr = PetscStrcat(body,"grant_type=authorization_code");CHKERRQ(ierr); ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); close(sock); ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token"); ierr = PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return refresh_token"); ierr = PetscPrintf(comm,"Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n");CHKERRQ(ierr); ierr = PetscPrintf(comm,"programs with the option -google_refresh_token %s\n",refresh_token);CHKERRQ(ierr); ierr = PetscPrintf(comm,"to access Google Drive automatically\n");CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C PetscDrawBoxedString - Draws a string with a box around it Not Collective Input Parameters: + draw - the drawing context . sxl - the coordinates of center of the box . syl - the coordinates of top line of box . sc - the color of the text . bc - the color of the bounding box - text - the text to draw Output Parameter: . w,h - width and height of resulting box (optional) Level: beginner Concepts: drawing^string Concepts: string^drawing .seealso: PetscDrawStringVertical(), PetscDrawBoxedStringSize() @*/ PetscErrorCode PetscDrawBoxedString(PetscDraw draw,PetscReal sxl,PetscReal syl,int sc,int bc,const char text[],PetscReal *w,PetscReal *h) { PetscErrorCode ierr ; PetscBool isnull; PetscReal top,left,right,bottom,tw,th; size_t len,mlen = 0; char **array; int cnt,i; PetscFunctionBegin; PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); PetscValidCharPointer(text,5); ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); if (draw->ops->boxedstring) { ierr = (*draw->ops->boxedstring)(draw,sxl,syl,sc,bc,text,w,h);CHKERRQ(ierr); PetscFunctionReturn(0); } ierr = PetscStrToArray(text,'\n',&cnt,&array);CHKERRQ(ierr); for (i=0; i<cnt; i++) { ierr = PetscStrlen(array[i],&len);CHKERRQ(ierr); mlen = PetscMax(mlen,len); } ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr); top = syl; left = sxl - .5*(mlen + 2)*tw; right = sxl + .5*(mlen + 2)*tw; bottom = syl - (1.0 + cnt)*th; if (w) *w = right - left; if (h) *h = top - bottom; /* compute new bounding box */ draw->boundbox_xl = PetscMin(draw->boundbox_xl,left); draw->boundbox_xr = PetscMax(draw->boundbox_xr,right); draw->boundbox_yl = PetscMin(draw->boundbox_yl,bottom); draw->boundbox_yr = PetscMax(draw->boundbox_yr,top); /* top, left, bottom, right lines */ ierr = PetscDrawLine(draw,left,top,right,top,bc);CHKERRQ(ierr); ierr = PetscDrawLine(draw,left,bottom,left,top,bc);CHKERRQ(ierr); ierr = PetscDrawLine(draw,right,bottom,right,top,bc);CHKERRQ(ierr); ierr = PetscDrawLine(draw,left,bottom,right,bottom,bc);CHKERRQ(ierr); for (i=0; i<cnt; i++) { ierr = PetscDrawString(draw,left + tw,top - (1.5 + i)*th,sc,array[i]);CHKERRQ(ierr); } ierr = PetscStrToArrayDestroy(cnt,array);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C PetscGetFileFromPath - Finds a file from a name and a path string. A default can be provided. Not Collective Input Parameters: + path - A string containing "directory:directory:..." (without the quotes, of course). As a special case, if the name is a single FILE, that file is used. . defname - default name . name - file name to use with the directories from env - mode - file mode desired (usually r for readable, w for writable, or e for executable) Output Parameter: . fname - qualified file name Level: developer Developer Notes: Wrongly returns 1 as an error code sometimes. Maybe should have additional flag argument indicating if it found it. Most arguments likely should be const. Concepts: files^finding in path Concepts: path^searching for file @*/ PetscErrorCode PetscGetFileFromPath(char *path,char *defname,char *name,char *fname,char mode) { char *p,*cdir,trial[PETSC_MAX_PATH_LEN],*senv,*env; size_t ln; PetscErrorCode ierr; PetscBool flg; PetscFunctionBegin; /* Setup default */ ierr = PetscGetFullPath(defname,fname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); if (path) { /* Check to see if the path is a valid regular FILE */ ierr = PetscTestFile(path,mode,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscStrcpy(fname,path);CHKERRQ(ierr); PetscFunctionReturn(1); } /* Make a local copy of path and mangle it */ ierr = PetscStrallocpy(path,&senv);CHKERRQ(ierr); env = senv; while (env) { /* Find next directory in env */ cdir = env; ierr = PetscStrchr(env,PETSC_PATH_SEPARATOR,&p);CHKERRQ(ierr); if (p) { *p = 0; env = p + 1; } else env = 0; /* Form trial file name */ ierr = PetscStrcpy(trial,cdir);CHKERRQ(ierr); ierr = PetscStrlen(trial,&ln);CHKERRQ(ierr); if (trial[ln-1] != '/') trial[ln++] = '/'; ierr = PetscStrcpy(trial + ln,name);CHKERRQ(ierr); ierr = PetscTestFile(path,mode,&flg);CHKERRQ(ierr); if (flg) { /* need PetscGetFullPath rather then copy in case path has . in it */ ierr = PetscGetFullPath(trial,fname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); ierr = PetscFree(senv);CHKERRQ(ierr); PetscFunctionReturn(1); } } ierr = PetscFree(senv);CHKERRQ(ierr); } ierr = PetscTestFile(path,mode,&flg);CHKERRQ(ierr); if (flg) PetscFunctionReturn(1); PetscFunctionReturn(0); }
EXTERN_C_END /* These are not extern C because they are passed into non-extern C user level functions */ static PetscErrorCode ourerrorhandler(int line,const char *fun,const char *file,const char *dir,int n,int p,const char *mess,void *ctx) { PetscErrorCode ierr = 0; size_t len1,len2,len3,len4; int l1,l2,l3,l4; PetscStrlen(fun,&len1); l1 = (int)len1; PetscStrlen(file,&len2);l2 = (int)len2; PetscStrlen(dir,&len3);l3 = (int)len3; PetscStrlen(mess,&len4);l4 = (int)len4; #if defined(PETSC_HAVE_FORTRAN_MIXED_STR_ARG) (*f2)(&line,fun,l1,file,l2,dir,l3,&n,&p,mess,l4,ctx,&ierr); #else (*f2)(&line,fun,file,dir,&n,&p,mess,ctx,&ierr,l1,l2,l3,l4); #endif return ierr; }
/*@ PetscDrawLGDraw - Redraws a line graph. Not Collective,but ignored by all processors except processor 0 in PetscDrawLG Input Parameter: . lg - the line graph context Level: intermediate .seealso: PetscDrawSPDraw(), PetscDrawLGSPDraw() @*/ PetscErrorCode PetscDrawLGDraw(PetscDrawLG lg) { PetscReal xmin=lg->xmin,xmax=lg->xmax,ymin=lg->ymin,ymax=lg->ymax; PetscErrorCode ierr; int i,j,dim = lg->dim,nopts = lg->nopts,rank,cl; PetscDraw draw = lg->win; PetscFunctionBegin; if (lg && ((PetscObject)lg)->classid == PETSC_DRAW_CLASSID) PetscFunctionReturn(0); PetscValidHeaderSpecific(lg,PETSC_DRAWLG_CLASSID,1); ierr = PetscDrawClear(draw);CHKERRQ(ierr); ierr = PetscDrawAxisSetLimits(lg->axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr); ierr = PetscDrawAxisDraw(lg->axis);CHKERRQ(ierr); ierr = MPI_Comm_rank(((PetscObject)lg)->comm,&rank);CHKERRQ(ierr); if (!rank) { for (i=0; i<dim; i++) { for (j=1; j<nopts; j++) { if (lg->colors) cl = lg->colors[i]; else cl = PETSC_DRAW_BLACK+i; ierr = PetscDrawLine(draw,lg->x[(j-1)*dim+i],lg->y[(j-1)*dim+i],lg->x[j*dim+i],lg->y[j*dim+i],cl);CHKERRQ(ierr); if (lg->use_dots) { ierr = PetscDrawString(draw,lg->x[j*dim+i],lg->y[j*dim+i],PETSC_DRAW_RED,"x");CHKERRQ(ierr); } } } } if (lg->legend) { PetscReal xl,yl,xr,yr,tw,th; size_t len,mlen = 0; int cl; ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); ierr = PetscDrawStringGetSize(draw,&tw,&th);CHKERRQ(ierr); for (i=0; i<dim; i++) { ierr = PetscStrlen(lg->legend[i],&len);CHKERRQ(ierr); mlen = PetscMax(mlen,len); } ierr = PetscDrawLine(draw,xr - (mlen + 8)*tw,yr - 3*th,xr - 2*tw,yr - 3*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xr - (mlen + 8)*tw,yr - 3*th,xr - (mlen + 8)*tw,yr - (4+lg->dim)*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); for (i=0; i<dim; i++) { cl = (lg->colors ? lg->colors[i] : i + 1); ierr = PetscDrawLine(draw,xr - (mlen + 6.7)*tw,yr - (4 + i)*th,xr - (mlen + 3.2)*tw,yr - (4 + i)*th,cl);CHKERRQ(ierr); ierr = PetscDrawString(draw,xr - (mlen + 3)*tw,yr - (4.5 + i)*th,PETSC_DRAW_BLACK,lg->legend[i]);CHKERRQ(ierr); } ierr = PetscDrawLine(draw,xr - 2*tw,yr - 3*th,xr - 2*tw,yr - (4+lg->dim)*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); ierr = PetscDrawLine(draw,xr - (mlen + 8)*tw,yr - (4+lg->dim)*th,xr - 2*tw,yr - (4+lg->dim)*th,PETSC_DRAW_BLACK);CHKERRQ(ierr); } ierr = PetscDrawFlush(lg->win);CHKERRQ(ierr); ierr = PetscDrawPause(lg->win);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C PetscVSNPrintf - The PETSc version of vsnprintf(). Converts a PETSc format string into a standard C format string and then puts all the function arguments into a string using the format statement. Input Parameters: + str - location to put result . len - the amount of space in str + format - the PETSc format string - fullLength - the amount of space in str actually used. Developer Notes: this function may be called from an error handler, if an error occurs when it is called by the error handler than likely a recursion will occur and possible crash. Level: developer @*/ PetscErrorCode PetscVSNPrintf(char *str,size_t len,const char *format,size_t *fullLength,va_list Argp) { char *newformat; char formatbuf[8*1024]; size_t oldLength,length; int fullLengthInt; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr); if (oldLength < 8*1024) { newformat = formatbuf; oldLength = 8*1024-1; } else { oldLength = PETSC_MAX_LENGTH_FORMAT(oldLength); ierr = PetscMalloc(oldLength * sizeof(char), &newformat);CHKERRQ(ierr); } PetscFormatConvert(format,newformat,oldLength); ierr = PetscStrlen(newformat, &length);CHKERRQ(ierr); #if 0 if (length > len) { newformat[len] = '\0'; } #endif #if defined(PETSC_HAVE_VSNPRINTF_CHAR) fullLengthInt = vsnprintf(str,len,newformat,(char *)Argp); #elif defined(PETSC_HAVE_VSNPRINTF) fullLengthInt = vsnprintf(str,len,newformat,Argp); #elif defined(PETSC_HAVE__VSNPRINTF) fullLengthInt = _vsnprintf(str,len,newformat,Argp); #else #error "vsnprintf not found" #endif if (fullLengthInt < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"vsnprintf() failed"); if (fullLength) *fullLength = (size_t)fullLengthInt; if (oldLength >= 8*1024) { ierr = PetscFree(newformat);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C PetscTextBelt - Sends an SMS to an American/Canadian phone number Not collective, only the first process in MPI_Comm does anything Input Parameters: + comm - the MPI communicator . number - the 10 digit telephone number - message - the message Output Parameter: . flg - PETSC_TRUE if the text was sent Level: intermediate Notes: TextBelt is run for testing purposes only, please do not use this feature often Developer Notes: I do not know how to make the buff[] long enough to receive the "success" string but short enough that the code does not hang waiting for part of the message to arrive that does not exist, hence the success flg may be improperly set to false even though the message was delivered. .seealso: PetscOpenSocket(), PetscHTTPRequest() @*/ PetscErrorCode PetscTextBelt(MPI_Comm comm,const char number[],const char message[],PetscBool *flg) { PetscErrorCode ierr; size_t nlen,mlen,blen; PetscMPIInt rank; PetscFunctionBegin; ierr = PetscStrlen(number,&nlen);CHKERRQ(ierr); if (nlen != 10) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Number %s is not ten digits",number); ierr = PetscStrlen(message,&mlen);CHKERRQ(ierr); if (mlen > 100) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Message %s is too long",message); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { int sock; char buff[474],*body; PetscInt i; ierr = PetscMalloc1(mlen+nlen+100,&body);CHKERRQ(ierr); ierr = PetscStrcpy(body,"number=");CHKERRQ(ierr); ierr = PetscStrcat(body,number);CHKERRQ(ierr); ierr = PetscStrcat(body,"&");CHKERRQ(ierr); ierr = PetscStrcat(body,"message=");CHKERRQ(ierr); ierr = PetscStrcat(body,message);CHKERRQ(ierr); ierr = PetscStrlen(body,&blen);CHKERRQ(ierr); for (i=0; i<(int)blen; i++) { if (body[i] == ' ') body[i] = '+'; } ierr = PetscOpenSocket("textbelt.com",80,&sock);CHKERRQ(ierr); ierr = PetscHTTPRequest("POST","textbelt.com/text",NULL,"application/x-www-form-urlencoded",body,sock,buff,sizeof(buff));CHKERRQ(ierr); close(sock); ierr = PetscFree(body);CHKERRQ(ierr); if (flg) { char *found; ierr = PetscStrstr(buff,"\"success\":tr",&found);CHKERRQ(ierr); *flg = found ? PETSC_TRUE : PETSC_FALSE; } } PetscFunctionReturn(0); }
/* PetscObjectAppendOptionsPrefix - Sets the prefix used for searching for all options of PetscObjectType in the database. Input Parameters: . obj - any PETSc object, for example a Vec, Mat or KSP. . prefix - the prefix string to prepend to option requests of the object. Notes: A hyphen (-) must NOT be given at the beginning of the prefix name. The first character of all runtime options is AUTOMATICALLY the hyphen. Concepts: prefix^setting */ PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject obj,const char prefix[]) { char *buf = obj->prefix; PetscErrorCode ierr; size_t len1,len2; PetscFunctionBegin; PetscValidHeader(obj,1); if (!prefix) PetscFunctionReturn(0); if (!buf) { ierr = PetscObjectSetOptionsPrefix(obj,prefix);CHKERRQ(ierr); PetscFunctionReturn(0); } if (prefix[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen"); ierr = PetscStrlen(prefix,&len1);CHKERRQ(ierr); ierr = PetscStrlen(buf,&len2);CHKERRQ(ierr); ierr = PetscMalloc1(1+len1+len2,&obj->prefix);CHKERRQ(ierr); ierr = PetscStrcpy(obj->prefix,buf);CHKERRQ(ierr); ierr = PetscStrcat(obj->prefix,prefix);CHKERRQ(ierr); ierr = PetscFree(buf);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode PetscFixSlashN(const char *in, char **out) { PetscErrorCode ierr; PetscInt i; size_t len; PetscFunctionBegin; ierr = PetscStrallocpy(in,out);CHKERRQ(ierr); ierr = PetscStrlen(*out,&len);CHKERRQ(ierr); for (i=0; i<(int)len-1; i++) { if ((*out)[i] == '\\' && (*out)[i+1] == 'n') {(*out)[i] = ' '; (*out)[i+1] = '\n';} } PetscFunctionReturn(0); }