Actual source code: gcreatev.c

  1: /*$Id: gcreatev.c,v 1.86 2001/04/10 19:34:51 bsmith Exp $*/

 3:  #include petscsys.h
 4:  #include petsc.h
 5:  #include petscis.h
 6:  #include petscvec.h


 9:  #include src/vec/vecimpl.h
 10: /*@C
 11:    VecGetType - Gets the vector type name (as a string) from the vector.

 13:    Not Collective

 15:    Input Parameter:
 16: .  vec - the vector

 18:    Output Parameter:
 19: .  type - the vector type name

 21:    Level: intermediate

 23: .seealso: VecSetType()
 24: @*/
 25: int VecGetType(Vec vec,VecType *type)
 26: {
 28:   *type = vec->type_name;
 29:   return(0);
 30: }

 32: /*
 33:    Contains the list of registered Vec routines
 34: */
 35: PetscFList      VecList = 0;
 36: PetscTruth VecRegisterAllCalled = PETSC_FALSE;
 37: 
 38: /*@C
 39:    VecRegisterDestroy - Frees the list of Vec methods that were
 40:    registered by VecRegisterDynamic().

 42:    Not Collective

 44:    Level: advanced

 46: .seealso: VecRegisterDynamic(), VecRegisterAll()
 47: @*/
 48: int VecRegisterDestroy(void)
 49: {

 53:   if (VecList) {
 54:     PetscFListDestroy(&VecList);
 55:     VecList = 0;
 56:   }
 57:   VecRegisterAllCalled = PETSC_FALSE;
 58:   return(0);
 59: }

 61: /*MC
 62:    VecRegisterDynamic - Adds a new vector component implementation

 64:    Synopsis:
 65:    VecRegisterDynamic(char *name_solver,char *path,char *name_create,
 66:                int (*routine_create)(MPI_Comm,int,int,Vec*))

 68:    Not Collective

 70:    Input Parameters:
 71: +  name_solver - name of a new user-defined vector object
 72: .  path - path (either absolute or relative) the library containing this vector object
 73: .  name_create - name of routine to create vector
 74: -  routine_create - routine to create vector

 76:    Notes:
 77:    VecRegisterDynamic() may be called multiple times to add several user-defined vectors

 79:    If dynamic libraries are used, then the fourth input argument (routine_create)
 80:    is ignored.

 82:    Sample usage:
 83: .vb
 84:    VecRegisterDynamic("my_solver","/home/username/my_lib/lib/libO/solaris/libmine",
 85:                "MyVectorCreate",MyVectorCreate);
 86: .ve

 88:    Then, your solver can be chosen with the procedural interface via
 89: .vb
 90:       VecCreate(MPI_Comm,int n,int N,Vec *);
 91:       VecSetType(Vec,"my_vector_name");
 92: .ve
 93:    or at runtime via the option
 94: .vb
 95:       -vec_type my_vector_name
 96: .ve

 98:    ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT}, or ${any environmental variable}
 99:   occuring in pathname will be replaced with appropriate values.

101:    Level: advanced

103:    Concepts: vector^adding new type

105: .seealso: VecRegisterAll(), VecRegisterDestroy()
106: M*/

108: int VecRegister(const char sname[],const char path[],const char name[],int (*function)(Vec))
109: {
110:   int  ierr;
111:   char fullname[256];

114:   PetscFListConcat(path,name,fullname);
115:   PetscFListAdd(&VecList,sname,fullname,(void (*)())function);
116:   return(0);
117: }

119: /*@C
120:     VecSetType - Builds a vector, for a particular vector implementation.

122:     Collective on Vec

124:     Input Parameters:
125: +   vec - the vector object
126: -   type_name - name of the vector type

128:     Options Database Key:
129: .  -vec_type <type> - Sets the vector type; use -help for a list of available types

131:     Notes:
132:     See "petsc/include/petscvec.h" for available vector types (for instance,
133:     VEC_SEQ, VEC_MPI, or VEC_SHARED).

135:      Use VecDuplicate() or VecDuplicateVecs() to form additional vectors
136:     of the same type as an existing vector.

138:     Level: intermediate

140: .seealso: VecCreate()
141: @*/
142: int VecSetType(Vec vec,VecType type_name)
143: {
144:   int        ierr,(*r)(Vec);
145:   PetscTruth match;


151:   PetscTypeCompare((PetscObject)vec,type_name,&match);
152:   if (match) return(0);

154:   /* Get the function pointers for the vector requested */
155:   if (!VecRegisterAllCalled) {VecRegisterAll(PETSC_NULL);}

157:    PetscFListFind(vec->comm,VecList,type_name,(void (**)(void)) &r);

159:   if (!r) SETERRQ1(1,"Unknown vector type given: %s",type_name);

161:   if (vec->ops->destroy) {
162:     (*vec->ops->destroy)(vec);
163:   }

165:   (*r)(vec);

167:   PetscObjectChangeTypeName((PetscObject)vec,type_name);
168:   return(0);
169: }

171: #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX)
172: #include "engine.h"   /* Matlab include file */
173: #include "mex.h"      /* Matlab include file */
174: EXTERN_C_BEGIN
175: int VecMatlabEnginePut_Default(PetscObject obj,void *engine)
176: {
177:   int     ierr,n;
178:   Vec     vec = (Vec)obj;
179:   Scalar  *array;
180:   mxArray *mat;

183:   VecGetArray(vec,&array);
184:   VecGetLocalSize(vec,&n);
185: #if !defined(PETSC_USE_COMPLEX)
186:   mat  = mxCreateDoubleMatrix(n,1,mxREAL);
187: #else
188:   mat  = mxCreateDoubleMatrix(n,1,mxCOMPLEX);
189: #endif
190:   PetscMemcpy(mxGetPr(mat),array,n*sizeof(Scalar));
191:   PetscObjectName(obj);
192:   mxSetName(mat,obj->name);
193:   engPutArray((Engine *)engine,mat);
194: 
195:   VecRestoreArray(vec,&array);
196:   return(0);
197: }
198: EXTERN_C_END

200: EXTERN_C_BEGIN
201: int VecMatlabEngineGet_Default(PetscObject obj,void *engine)
202: {
203:   int     ierr,n;
204:   Vec     vec = (Vec)obj;
205:   Scalar  *array;
206:   mxArray *mat;

209:   VecGetArray(vec,&array);
210:   VecGetLocalSize(vec,&n);
211:   mat  = engGetArray((Engine *)engine,obj->name);
212:   if (!mat) SETERRQ1(1,"Unable to get object %s from matlab",obj->name);
213:   PetscMemcpy(array,mxGetPr(mat),n*sizeof(Scalar));
214:   VecRestoreArray(vec,&array);
215:   return(0);
216: }
217: EXTERN_C_END
218: #endif