Actual source code: ex41.c
petsc-3.4.2 2013-07-02

      1: static char help[] = "Nest vector set subvector functionality.\n\n";

      3: /*T
      4:    Concepts: vectors^block operators
      5:    Concepts: vectors^setting values
      6:    Concepts: vectors^local access to
      7:    Processors: n
      8: T*/

     10: #include <petscvec.h>

     14: PetscErrorCode test_vec_ops(void)
     15: {
     16:   Vec            X,Y,a,b;
     17:   Vec            c,d,e,f,g,h;
     18:   PetscScalar    val;
     20:   PetscInt       tmp_ind[2];
     21:   Vec            tmp_buf[2];

     24:   PetscPrintf(PETSC_COMM_WORLD, "============== %s ==============\n",PETSC_FUNCTION_NAME);

     26:   /* create 4 slave vectors */
     27:   VecCreate(PETSC_COMM_WORLD, &c);
     28:   VecSetSizes(c, PETSC_DECIDE, 4);
     29:   VecSetType(c, VECMPI);
     30:   VecDuplicate(c, &d);
     31:   VecDuplicate(c, &e);
     32:   VecDuplicate(c, &f);

     34:   /* create two more slaves of different sizes */
     35:   VecCreate(PETSC_COMM_WORLD, &g);
     36:   VecSetSizes(g, PETSC_DECIDE, 6);
     37:   VecSetType(g, VECMPI);
     38:   VecCreate(PETSC_COMM_WORLD, &h);
     39:   VecSetSizes(h, PETSC_DECIDE, 8);
     40:   VecSetType(h, VECMPI);

     42:   /* set the 6 vectors to some numbers */
     43:   VecSet(c, 1.0);
     44:   VecSet(d, 2.0);
     45:   VecSet(e, 3.0);
     46:   VecSet(f, 4.0);
     47:   VecSet(g, 5.0);
     48:   VecSet(h, 6.0);

     50:   /* assemble a */
     51:   PetscPrintf(PETSC_COMM_WORLD, "a = [c d] \n");
     52:   tmp_buf[0] = c; tmp_buf[1] = d;

     54:   VecCreateNest(PETSC_COMM_WORLD,2,NULL,tmp_buf,&a);
     55:   VecView(a,PETSC_VIEWER_STDOUT_WORLD);
     56:   PetscPrintf(PETSC_COMM_WORLD, "a = [d c] \n");
     57:   VecNestSetSubVec(a, 1, c);
     58:   VecNestSetSubVec(a, 0, d);
     59:   VecAssemblyBegin(a);
     60:   VecAssemblyEnd(a);
     61:   VecView(a,PETSC_VIEWER_STDOUT_WORLD);

     63:   /* assemble b */
     64:   PetscPrintf(PETSC_COMM_WORLD, "b = [e f] \n");
     65:   tmp_buf[0] = e; tmp_buf[1] = f;

     67:   VecCreateNest(PETSC_COMM_WORLD,2,NULL,tmp_buf,&b);
     68:   VecView(b,PETSC_VIEWER_STDOUT_WORLD);
     69:   PetscPrintf(PETSC_COMM_WORLD, "b = [f e] \n");
     70:   VecNestSetSubVec(b, 1, e);
     71:   VecNestSetSubVec(b, 0, f);
     72:   VecAssemblyBegin(b);
     73:   VecAssemblyEnd(b);
     74:   VecView(b,PETSC_VIEWER_STDOUT_WORLD);

     76:   PetscPrintf(PETSC_COMM_WORLD, "X = [a b] \n");
     77:   tmp_buf[0] = a; tmp_buf[1] = b;

     79:   VecCreateNest(PETSC_COMM_WORLD,2,NULL,tmp_buf,&X);
     80:   VecView(X,PETSC_VIEWER_STDOUT_WORLD);
     81:   VecDot(X,X, &val);
     82:   PetscPrintf(PETSC_COMM_WORLD, "X.X = %f \n", val);

     84:   /* re-order components of X */
     85:   PetscPrintf(PETSC_COMM_WORLD, "X = [b a] \n");
     86:   VecNestSetSubVec(X,1,a);
     87:   VecNestSetSubVec(X,0,b);
     88:   VecAssemblyBegin(X);
     89:   VecAssemblyEnd(X);
     90:   VecView(X,PETSC_VIEWER_STDOUT_WORLD);
     91:   VecDot(X,X,&val);
     92:   PetscPrintf(PETSC_COMM_WORLD, "X.X = %f \n", val);

     94:   /* re-assemble X */
     95:   PetscPrintf(PETSC_COMM_WORLD, "X = [g h] \n");
     96:   VecNestSetSubVec(X,1,g);
     97:   VecNestSetSubVec(X,0,h);
     98:   VecAssemblyBegin(X);
     99:   VecAssemblyEnd(X);
    100:   VecView(X,PETSC_VIEWER_STDOUT_WORLD);
    101:   VecDot(X,X,&val);
    102:   PetscPrintf(PETSC_COMM_WORLD, "X.X = %f \n", val);

    104:   PetscPrintf(PETSC_COMM_WORLD, "Y = X \n");
    105:   VecDuplicate(X, &Y);
    106:   VecCopy(X,Y);
    107:   VecView(Y,PETSC_VIEWER_STDOUT_WORLD);
    108:   VecDot(Y,Y,&val);
    109:   PetscPrintf(PETSC_COMM_WORLD, "Y.Y = %f \n", val);

    111:   PetscPrintf(PETSC_COMM_WORLD, "Y = [a b] \n");
    112:   tmp_buf[0] = a; tmp_buf[1] = b;
    113:   tmp_ind[0] = 0; tmp_ind[1] = 1;

    115:   VecNestSetSubVecs(Y,2,tmp_ind,tmp_buf);
    116:   VecView(Y,PETSC_VIEWER_STDOUT_WORLD);

    118:   VecDestroy(&c);
    119:   VecDestroy(&d);
    120:   VecDestroy(&e);
    121:   VecDestroy(&f);
    122:   VecDestroy(&g);
    123:   VecDestroy(&h);
    124:   VecDestroy(&a);
    125:   VecDestroy(&b);
    126:   VecDestroy(&X);
    127:   VecDestroy(&Y);
    128:   return(0);
    129: }

    131: int main(int argc, char **args)
    132: {
    133:   PetscInitialize(&argc, &args,(char*)0, help);

    135:   test_vec_ops();

    137:   PetscFinalize();
    138:   return 0;
    139: }