BarryFSmith avatar BarryFSmith committed 8e4b2d1

Sys: add PetscDataTypeFromString() and test code

Comments (0)

Files changed (4)

include/petscsys.h

 PETSC_EXTERN PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType,MPI_Datatype*);
 PETSC_EXTERN PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype,PetscDataType*);
 PETSC_EXTERN PetscErrorCode PetscDataTypeGetSize(PetscDataType,size_t*);
+PETSC_EXTERN PetscErrorCode PetscDataTypeFromString(const char*,PetscDataType*,PetscBool*);
 
 /*
     Basic memory and string operations. These are usually simple wrappers

src/sys/examples/tutorials/ex11.c

+
+static char help[] = "Demonstrates PetscDataTypeFromString().\n\n";
+
+/*T
+   Concepts: introduction to PETSc;
+   Concepts: printing^in parallel
+   Processors: n
+T*/
+
+#include <petscsys.h>
+int main(int argc,char **argv)
+{
+  PetscErrorCode ierr;
+  PetscDataType  dtype;
+  PetscBool      found;
+
+  /*
+    Every PETSc routine should begin with the PetscInitialize() routine.
+    argc, argv - These command line arguments are taken to extract the options
+                 supplied to PETSc and options supplied to MPI.
+    help       - When PETSc executable is invoked with the option -help,
+                 it prints the various options that can be applied at
+                 runtime.  The user can use the "help" variable place
+                 additional help messages in this printout.
+  */
+  ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr);
+
+  ierr = PetscDataTypeFromString("Scalar",&dtype,&found);CHKERRQ(ierr);
+  if (!found) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Did not find scalar datatype");
+  if (dtype != PETSC_SCALAR) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Found wrong datatype for scalar");
+
+  ierr = PetscDataTypeFromString("INT",&dtype,&found);CHKERRQ(ierr);
+  if (!found) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Did not find int datatype");
+  if (dtype != PETSC_INT) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Found wrong datatype for int");
+
+  ierr = PetscDataTypeFromString("real",&dtype,&found);CHKERRQ(ierr);
+  if (!found) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Did not find real datatype");
+  if (dtype != PETSC_REAL) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Found wrong datatype for real");
+
+  ierr = PetscDataTypeFromString("abogusdatatype",&dtype,&found);CHKERRQ(ierr);
+  if (found) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Found a bogus datatype");
+
+  ierr = PetscFinalize();
+  return 0;
+}

src/sys/examples/tutorials/makefile

 CPPFLAGS        =
 FPPFLAGS        =
 LOCDIR          = src/sys/examples/tutorials/
-EXAMPLESC       = ex1.c ex2.c ex3.c ex4.c ex5.c ex6.c ex7.c ex9.c
+EXAMPLESC       = ex1.c ex2.c ex3.c ex4.c ex5.c ex6.c ex7.c ex9.c ex11.c
 EXAMPLESF       = ex3f.F ex4f.F ex4f90.F90 ex5f90.F90 ex8f90.F90 ex10f90.F90
 MANSEC          = Sys
 CLEANFILES      = binaryoutput binaryoutput.info
 ex10f90: ex10f90.o   chkopts
 	-${FLINKER} -o ex10f90 ex10f90.o  ${PETSC_SYS_LIB}
 	${RM} -f ex10f90.o
+
+ex11: ex11.o   chkopts
+	-${CLINKER} -o ex11 ex11.o  ${PETSC_SYS_LIB}
+	${RM} -f ex11.o
 #
 # ------------------------------------------------------------------------
 #
 	   else echo ${PWD} ; echo "Possible problem with with ex10f90_2, diffs above \n========================================="; fi; \
 	   ${RM} -f ex10f90_2.tmp
 
+runex11:
+	-@${MPIEXEC} -n 1 ./ex11
+
 TESTEXAMPLES_C		      = ex1.PETSc runex1 ex1.rm ex2.PETSc runex2 ex2.rm \
-                            ex3.PETSc runex3 ex3.rm ex4.PETSc runex4 ex4.rm ex6.PETSc runex6 ex6.rm
+                            ex3.PETSc runex3 ex3.rm ex4.PETSc runex4 ex4.rm ex6.PETSc runex6 ex6.rm ex11.PETSc runex11 ex11.rm
 TESTEXAMPLES_C_NOCOMPLEX  = ex5.PETSc runex5 ex5.rm
 TESTEXAMPLES_C_X_MPIUNI   = ex1.PETSc runex1 ex1.rm ex2.PETSc runex2 ex2.rm \
                             ex3.PETSc runex3 ex3.rm

src/sys/objects/ptype.c

   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown PETSc datatype");
   PetscFunctionReturn(0);
 }
+
+#undef __FUNCT__
+#define __FUNCT__ "PetscDataTypeFromString"
+/*@
+     PetscDataTypeFromString - Gets the enum value of a PETSc datatype represented as a string
+
+   Not collective
+
+    Input Parameter:
+.     name - the PETSc datatype name (for example, DOUBLE or real or Scalar)
+
+    Output Parameter:
++    ptype - the enum value
+-    found - the string matches one of the data types
+
+    Level: advanced
+
+.seealso: PetscDataType, PetscDataTypeToMPIDataType(), PetscDataTypeGetSize()
+@*/
+PetscErrorCode  PetscDataTypeFromString(const char*name, PetscDataType *ptype,PetscBool *found)
+{
+  PetscErrorCode ierr;
+
+  PetscFunctionBegin;
+  ierr = PetscEnumFind(PetscDataTypes,name,(PetscEnum*)ptype,found);CHKERRQ(ierr);
+  if (!*found) {
+    char formatted[7];
+
+    ierr = PetscStrncpy(formatted,name,7);CHKERRQ(ierr);
+    ierr = PetscStrtolower(formatted);CHKERRQ(ierr);
+    ierr = PetscStrcmp(formatted,"scalar",found);CHKERRQ(ierr);
+    if (*found) {
+      *ptype = PETSC_SCALAR;
+    } else {
+      ierr = PetscStrcmp(formatted,"real",found);CHKERRQ(ierr);
+      if (*found) {
+        *ptype = PETSC_REAL;
+      }
+    }
+  }
+  PetscFunctionReturn(0);
+}
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.