コード例 #1
0
ファイル: ex16.c プロジェクト: 00liujj/petsc
int main(int argc,char **argv)
{
  PetscErrorCode ierr;
  PetscMPIInt    rank;
  char           buffer[256],*output,user[256];
  PetscBool      userhappy = PETSC_FALSE;

  PetscInitialize(&argc,&argv,(char*)0,help);
  ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

  ierr = PetscMatlabEngineGetOutput(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD),&output);CHKERRQ(ierr);

  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD),"MPI_Comm_rank");CHKERRQ(ierr);
  ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d]Processor rank is %s",rank,output);CHKERRQ(ierr);
  ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);CHKERRQ(ierr);
  ierr = PetscPrintf(PETSC_COMM_WORLD,">>");CHKERRQ(ierr);
  ierr = PetscSynchronizedFGets(PETSC_COMM_WORLD,stdin,256,user);CHKERRQ(ierr);
  ierr = PetscStrncmp(user,"exit",4,&userhappy);CHKERRQ(ierr);
  while (!userhappy) {
    ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD),user);CHKERRQ(ierr);
    ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d]The result is %s",rank,output);CHKERRQ(ierr);
    ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);CHKERRQ(ierr);
    ierr = PetscPrintf(PETSC_COMM_WORLD,">>");CHKERRQ(ierr);
    ierr = PetscSynchronizedFGets(PETSC_COMM_WORLD,stdin,256,user);CHKERRQ(ierr);
    ierr = PetscStrncmp(user,"exit",4,&userhappy);CHKERRQ(ierr);
  }
  ierr = PetscFinalize();
  return 0;
}
コード例 #2
0
ファイル: aijmatlab.c プロジェクト: petsc/petsc
PetscErrorCode MatSolve_Matlab(Mat A,Vec b,Vec x)
{
    PetscErrorCode ierr;
    const char     *_A,*_b,*_x;

    PetscFunctionBegin;
    /* make sure objects have names; use default if not */
    ierr = PetscObjectName((PetscObject)b);
    CHKERRQ(ierr);
    ierr = PetscObjectName((PetscObject)x);
    CHKERRQ(ierr);

    ierr = PetscObjectGetName((PetscObject)A,&_A);
    CHKERRQ(ierr);
    ierr = PetscObjectGetName((PetscObject)b,&_b);
    CHKERRQ(ierr);
    ierr = PetscObjectGetName((PetscObject)x,&_x);
    CHKERRQ(ierr);
    ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)b);
    CHKERRQ(ierr);
    ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = u%s\\(l%s\\(p%s*%s));",_x,_A,_A,_A,_b);
    CHKERRQ(ierr);
    ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = 0;",_b);
    CHKERRQ(ierr);
    /* ierr = PetscMatlabEnginePrintOutput(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),stdout);CHKERRQ(ierr);  */
    ierr = PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)x);
    CHKERRQ(ierr);
    PetscFunctionReturn(0);
}
コード例 #3
0
int main(int argc,char **argv)
{
  PetscErrorCode ierr;
  PetscMPIInt    rank;
  PetscInt       n = 5;
  char           *output;
  Vec            x;

  PetscInitialize(&argc,&argv,(char *)0,0);

  ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr);
  ierr = VecSetSizes(x,PETSC_DECIDE,n);CHKERRQ(ierr);
  ierr = VecSetFromOptions(x);CHKERRQ(ierr);

  ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
  ierr = PetscMatlabEngineGetOutput(PETSC_MATLAB_ENGINE_WORLD,&output);
  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_WORLD,"MPI_Comm_rank");
  ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d]Processor rank is %s",rank,output);CHKERRQ(ierr);
  ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD);CHKERRQ(ierr);

  ierr = PetscObjectSetName((PetscObject)x,"x");CHKERRQ(ierr);
  ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_WORLD,(PetscObject)x);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_WORLD,"x = x + MPI_Comm_rank;\n");
  ierr = PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_WORLD,(PetscObject)x);CHKERRQ(ierr);

  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_WORLD,"whos\n");
  ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d]The result is %s",rank,output);CHKERRQ(ierr);
  ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD);CHKERRQ(ierr);

  ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
  ierr = VecDestroy(x);CHKERRQ(ierr);
  ierr = PetscFinalize();CHKERRQ(ierr);
  return 0;
}
コード例 #4
0
ファイル: aijmatlab.c プロジェクト: petsc/petsc
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);
}
コード例 #5
0
ファイル: aijmatlab.c プロジェクト: firedrakeproject/petsc
PetscErrorCode MatDestroy_matlab(Mat A)
{
  PetscErrorCode ierr;
  const char     *_A;

  PetscFunctionBegin;
  ierr = PetscObjectGetName((PetscObject)A,&_A);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"delete %s l_%s u_%s;",_A,_A,_A);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
コード例 #6
0
PetscErrorCode PFApply_Matlab(void *value,PetscInt n,PetscScalar *in,PetscScalar *out)
{
  PF_Matlab      *matlab = (PF_Matlab*)value;
  PetscErrorCode ierr;

  PetscFunctionBegin;
  if (!value) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to set string for Matlab function, via -pf_matlab string");
  ierr = PetscMatlabEnginePutArray(matlab->mengine,matlab->dimin,n,in,"x");CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(matlab->mengine,matlab->string);CHKERRQ(ierr);
  ierr = PetscMatlabEngineGetArray(matlab->mengine,matlab->dimout,n,out,"f");CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
コード例 #7
0
static PetscErrorCode EvaluateJacobian(Tao tao, Vec X, Mat J, Mat JPre, void *ptr)
{
  AppCtx         *user = (AppCtx *)ptr;
  PetscErrorCode  ierr;

  PetscFunctionBegin;
  ierr = PetscObjectSetName((PetscObject)X,"X");CHKERRQ(ierr);
  ierr = PetscMatlabEnginePut(user->mengine,(PetscObject)X);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(user->mengine,"J = jac(X);");CHKERRQ(ierr);
  ierr = PetscObjectSetName((PetscObject)J,"J");CHKERRQ(ierr);
  ierr = PetscMatlabEngineGet(user->mengine,(PetscObject)J);CHKERRQ(ierr);
  ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
  ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
コード例 #8
0
int main(int argc, char **argv)
{
  AppCtx         user;
  PetscErrorCode ierr;
  PetscScalar    tmp;
  int            i;

  ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
  ierr = PetscMatlabEngineCreate(PETSC_COMM_SELF,NULL,&user.mengine);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(user.mengine,"TestingInitialize");CHKERRQ(ierr);

  for (i = 1; i <= 53; ++i) {
    ierr = PetscPrintf(PETSC_COMM_SELF,"%d\n",i);
    ierr = PetscMatlabEngineEvaluate(user.mengine,"np = %d; ProblemInitialize",i);CHKERRQ(ierr);
    ierr = PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"n");CHKERRQ(ierr);
    user.n = (int)tmp;
    ierr = PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"m");CHKERRQ(ierr);
    user.m = (int)tmp;
    ierr = PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"nfmax");CHKERRQ(ierr);
    user.nfmax = (int)tmp;
    ierr = PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"npmax");CHKERRQ(ierr);
    user.npmax = (int)tmp;
    ierr = PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"delta");CHKERRQ(ierr);
    user.delta = (double)tmp;

    /* Ignore return code for now -- do not stop testing on inf or nan errors */
    ierr = TaoPounders(&user);

    ierr = PetscMatlabEngineEvaluate(user.mengine,"ProblemFinalize");CHKERRQ(ierr);
  }

  ierr = PetscMatlabEngineEvaluate(user.mengine,"TestingFinalize");CHKERRQ(ierr);
  ierr = PetscMatlabEngineDestroy(&user.mengine);CHKERRQ(ierr);
  ierr = PetscFinalize();
  return ierr;
}
コード例 #9
0
ファイル: ex5.c プロジェクト: pombredanne/petsc
PetscErrorCode FormFunctionMatlab(SNES snes,Vec X,Vec F,void *ptr)
{
  AppCtx         *user = (AppCtx*)ptr;
  PetscErrorCode ierr;
  PetscInt       Mx,My;
  PetscReal      lambda,hx,hy;
  Vec            localX,localF;
  MPI_Comm       comm;
  DM             da;

  PetscFunctionBeginUser;
  ierr = SNESGetDM(snes,&da);CHKERRQ(ierr);
  ierr = DMGetLocalVector(da,&localX);CHKERRQ(ierr);
  ierr = DMGetLocalVector(da,&localF);CHKERRQ(ierr);
  ierr = PetscObjectSetName((PetscObject)localX,"localX");CHKERRQ(ierr);
  ierr = PetscObjectSetName((PetscObject)localF,"localF");CHKERRQ(ierr);
  ierr = DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);CHKERRQ(ierr);

  lambda = user->param;
  hx     = 1.0/(PetscReal)(Mx-1);
  hy     = 1.0/(PetscReal)(My-1);

  ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
  /*
     Scatter ghost points to local vector,using the 2-step process
        DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
     By placing code between these two statements, computations can be
     done while messages are in transition.
  */
  ierr = DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX);CHKERRQ(ierr);
  ierr = DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX);CHKERRQ(ierr);
  ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(comm),(PetscObject)localX);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(comm),"localF=ex5m(localX,%18.16e,%18.16e,%18.16e)",hx,hy,lambda);CHKERRQ(ierr);
  ierr = PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_(comm),(PetscObject)localF);CHKERRQ(ierr);

  /*
     Insert values into global vector
  */
  ierr = DMLocalToGlobalBegin(da,localF,INSERT_VALUES,F);CHKERRQ(ierr);
  ierr = DMLocalToGlobalEnd(da,localF,INSERT_VALUES,F);CHKERRQ(ierr);
  ierr = DMRestoreLocalVector(da,&localX);CHKERRQ(ierr);
  ierr = DMRestoreLocalVector(da,&localF);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
コード例 #10
0
ファイル: pounders_util.c プロジェクト: 00liujj/petsc
/*
  Given:
    mfqP->delta
    mfqP->Xhist (nHist-array of n-Vec)
    mfqP->Fhist (nHist-array of m-Vec)
    mfqP->minindex

  Computes:
    mfqP->Mdir (array of n-Vec)
    mfqP->nmodelpoints
    mfqP->valid
    mfqP->model_indices (array of integers)
    if (!checkonly):
       mfqP->Gdel (m-array of n-Vec)
       mfqp->Hdel (m-array of nxn Mat)
*/
PetscErrorCode TaoPounders_formquad(TAO_POUNDERS *mfqP,PetscBool checkonly)
{
  PetscErrorCode ierr;
  PetscScalar    *h,tempx[1];
  PetscInt       i,j,*ind;
  const char     *machine="localhost";
  char           name[12];
  Vec            Mind_real;

#ifdef PETSC_HAVE_MATLAB_ENGINE
  if (!mfqP->me) {
    ierr = PetscMatlabEngineCreate(PETSC_COMM_SELF,machine,&mfqP->me);CHKERRQ(ierr);
  }
#endif
  ierr = PetscMatlabEngineEvaluate(me,"Xhist = zeros(%d,%d);",mfqP->nHist,mfqP->n);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(me,"Fhist = zeros(%d,%d);",mfqP->nHist,mfqP->n);CHKERRQ(ierr);
  for (i=0;i<mfqP->nHist;i++) {
    snprintf(name,12,"x%010d",(int)i);
    ierr = PetscObjectSetName((PetscObject)(mfqP->Xhist[i]),name);CHKERRQ(ierr);
    ierr = PetscMatlabEnginePut(me,mfqP->Xhist[i]);CHKERRQ(ierr);
    ierr = PetscMatlabEngineEvaluate(me,"Xhist(%d+1,:) = %s;",i,name);CHKERRQ(ierr);
    snprintf(name,12,"f%010d",(int)i);
    ierr = PetscObjectSetName((PetscObject)(mfqP->Fhist[i]),name);CHKERRQ(ierr);
    ierr = PetscMatlabEnginePut(me,mfqP->Fhist[i]);CHKERRQ(ierr);
    ierr = PetscMatlabEngineEvaluate(me,"Fhist(%d,:) = %s;",i,name);CHKERRQ(ierr);
  }
  ierr = PetscMatlabEngineEvaluate(me,"delta=%f;",mfqP->delta);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(me,"xkin=%d;",mfqP->xkin+1);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(me,"npmax=%d;",mfqP->npmax);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(me,"Pars = [%f,%f,%f,%f];",mfqP->par1,mfqP->par2,mfqP->par3,mfqP->par4);CHKERRQ(ierr);
  ierr = PetscMatlabEngineEvaluate(me,"vf=%d;",checkonly?1:0);CHKERRQ(ierr);

  ierr = PetscMatlabEngineEvaluate(me,"[Mdir,np,valid,G,H,Mind] = formquad(X,F,delta,xkin,npmax,Pars,vf);");CHKERRQ(ierr);

  /* Get Mdir */
  ierr = PetscObjectSetName((PetscObject)(mfqP->Mdir),"Mdir");CHKERRQ(ierr);
  ierr = PetscMatlabEngineGet(me,mfqP->Mdir);CHKERRQ(ierr);

  /* Get np */
  ierr = PetscMatlabEngineGetArray(me,1,1,tempx,"np");CHKERRQ(ierr);
  mfqP->nmodelpoints = floor(tempx[0]+0.5);

  /* Get valid */
  ierr = PetscMatlabEngineGetArray(me,1,1,tempx,"valid");CHKERRQ(ierr);
  mfqP->valid = floor(tempx[0]+0.5);

  /* Get Mind */
  ierr = VecCreate(PETSC_COMM_SELF,&Mind_real);
  ierr = PetscObjectSetName((PetscObject)(mfqP->Mind_real),"Mind");CHKERRQ(ierr);
  ierr = PetscMatlabEngineGet(me,mfqP->Mind_real);CHKERRQ(ierr);
  ierr = VecGetArray(mfqP->Mind_real,&v);CHKERRQ(ierr);
  for (i=0;i<mfqP->n;i++) {
    mfqP->model_indices[i] = floor(v[i]+0.5);
  }
  ierr = VecRestoreArray(mfqP->Mind_real,&v);CHKERRQ(ierr);
  ierr = VecDestroy(&Mind_real);CHKERRQ(ierr);

  if (!checkonly) {
    /* Get Gdel */
    for (i=0;i<mfqP->m;i++) {
      snprintf(name,12,"g%010d",(int)i);
      ierr = PetscObjectSetName((PetscObject)(mfqP->Gdel[i]),name);CHKERRQ(ierr);
      ierr = PetscMatlabEngineEvaluate(me,"%s = G(%d+1,:);",name,i);CHKERRQ(ierr);
      ierr = PetscMatlabEngineGet(me,mfqP->Gdel[i]);CHKERRQ(ierr);
    }
    /* Get Hdel */
    for (i=0;i<mfqP->m;i++) {
      snprintf(name,12,"h%010d",(int)i);
      ierr = PetscMatlabeEngineEvaluate(me,"%s = H(:,:,%d+1);",name,i);CHKERRQ(ierr);
      ierr = PetscMatlabEngineGetArray(me,);
    }
  }

  PetscFunctionReturn(0);
}