Date: 2021-02-01T14:41:39+00:00

Actual source code: test1.c
slepc-3.14.2 2021-02-01
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2020, Universitat Politecnica de Valencia, Spain

      6:    This file is part of SLEPc.
      7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
      8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      9: */

     11: static char help[] = "Test VecComp.\n\n";

     13: #include <slepcsys.h>

     15: int main(int argc,char **argv)
     16: {
     17:   Vec            v,w,x,y,vc,wc,xc,yc,vparent,vchild[2],vecs[2];
     18:   const Vec      *varray;
     19:   PetscMPIInt    size,rank;
     20:   PetscInt       i,n,k,Nx[2];
     21:   PetscReal      norm,normc,norm12[2],norm12c[2],vmax,vmin;
     22:   PetscScalar    dot[2],dotc[2];

     25:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     26:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     27:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     28:   if (size > 2) SETERRQ(PETSC_COMM_WORLD,1,"This test needs one or two processes");
     29:   PetscPrintf(PETSC_COMM_WORLD,"VecComp test\n");

     31:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     32:      Create standard vectors
     33:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     35:   VecCreate(PETSC_COMM_WORLD,&v);
     36:   VecSetSizes(v,8/size,8);
     37:   VecSetFromOptions(v);

     39:   if (!rank) {
     40:     VecSetValue(v,0,2.0,INSERT_VALUES);
     41:     VecSetValue(v,1,-1.0,INSERT_VALUES);
     42:     VecSetValue(v,2,3.0,INSERT_VALUES);
     43:     VecSetValue(v,3,3.5,INSERT_VALUES);
     44:   }
     45:   if ((!rank && size==1) || (rank && size==2)) {
     46:     VecSetValue(v,4,1.2,INSERT_VALUES);
     47:     VecSetValue(v,5,1.8,INSERT_VALUES);
     48:     VecSetValue(v,6,-2.2,INSERT_VALUES);
     49:     VecSetValue(v,7,2.0,INSERT_VALUES);
     50:   }
     51:   VecAssemblyBegin(v);
     52:   VecAssemblyEnd(v);
     53:   VecDuplicate(v,&w);
     54:   VecSet(w,1.0);
     55:   VecDuplicate(v,&x);
     56:   VecDuplicate(v,&y);
     57:   if (!rank) {
     58:     VecSetValue(y,0,1.0,INSERT_VALUES);
     59:   }
     60:   VecAssemblyBegin(y);
     61:   VecAssemblyEnd(y);

     63:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     64:      Create veccomp vectors
     65:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     67:   VecCreate(PETSC_COMM_WORLD,&vparent);
     68:   VecSetSizes(vparent,4/size,4);
     69:   VecSetFromOptions(vparent);

     71:   /* create a veccomp vector with two subvectors */
     72:   VecDuplicate(vparent,&vchild[0]);
     73:   VecDuplicate(vparent,&vchild[1]);
     74:   if (!rank) {
     75:     VecSetValue(vchild[0],0,2.0,INSERT_VALUES);
     76:     VecSetValue(vchild[0],1,-1.0,INSERT_VALUES);
     77:     VecSetValue(vchild[1],0,1.2,INSERT_VALUES);
     78:     VecSetValue(vchild[1],1,1.8,INSERT_VALUES);
     79:   }
     80:   if ((!rank && size==1) || (rank && size==2)) {
     81:     VecSetValue(vchild[0],2,3.0,INSERT_VALUES);
     82:     VecSetValue(vchild[0],3,3.5,INSERT_VALUES);
     83:     VecSetValue(vchild[1],2,-2.2,INSERT_VALUES);
     84:     VecSetValue(vchild[1],3,2.0,INSERT_VALUES);
     85:   }
     86:   VecAssemblyBegin(vchild[0]);
     87:   VecAssemblyBegin(vchild[1]);
     88:   VecAssemblyEnd(vchild[0]);
     89:   VecAssemblyEnd(vchild[1]);
     90:   VecCreateCompWithVecs(vchild,2,vparent,&vc);
     91:   VecDestroy(&vchild[0]);
     92:   VecDestroy(&vchild[1]);
     93:   VecView(vc,NULL);

     95:   VecGetSize(vc,&k);
     96:   if (k!=8) SETERRQ(PETSC_COMM_WORLD,1,"Vector global length should be 8");

     98:   /* create an empty veccomp vector with two subvectors */
     99:   Nx[0] = 4;
    100:   Nx[1] = 4;
    101:   VecCreateComp(PETSC_COMM_WORLD,Nx,2,VECSTANDARD,vparent,&wc);
    102:   VecCompGetSubVecs(wc,&n,&varray);
    103:   if (n!=2) SETERRQ(PETSC_COMM_WORLD,1,"n should be 2");
    104:   for (i=0;i<2;i++) {
    105:     VecSet(varray[i],1.0);
    106:   }

    108:   VecGetSize(wc,&k);
    109:   if (k!=8) SETERRQ(PETSC_COMM_WORLD,1,"Vector global length should be 8");

    111:   /* duplicate a veccomp */
    112:   VecDuplicate(vc,&xc);

    114:   /* create a veccomp via VecSetType */
    115:   VecCreate(PETSC_COMM_WORLD,&yc);
    116:   VecSetType(yc,VECCOMP);
    117:   VecSetSizes(yc,8/size,8);
    118:   VecCompSetSubVecs(yc,2,NULL);

    120:   VecCompGetSubVecs(yc,&n,&varray);
    121:   if (n!=2) SETERRQ(PETSC_COMM_WORLD,1,"n should be 2");
    122:   if (!rank) {
    123:     VecSetValue(varray[0],0,1.0,INSERT_VALUES);
    124:   }
    125:   VecAssemblyBegin(varray[0]);
    126:   VecAssemblyEnd(varray[0]);

    128:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    129:      Operate with vectors
    130:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    132:   VecCopy(w,x);
    133:   VecAXPBY(x,1.0,-2.0,v);
    134:   VecNorm(x,NORM_2,&norm);
    135:   VecCopy(wc,xc);
    136:   VecAXPBY(xc,1.0,-2.0,vc);
    137:   VecNorm(xc,NORM_2,&normc);
    138:   if (PetscAbsReal(norm-normc)>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Norms are different");

    140:   VecCopy(w,x);
    141:   VecWAXPY(x,-2.0,w,v);
    142:   VecNorm(x,NORM_2,&norm);
    143:   VecCopy(wc,xc);
    144:   VecWAXPY(xc,-2.0,wc,vc);
    145:   VecNorm(xc,NORM_2,&normc);
    146:   if (PetscAbsReal(norm-normc)>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Norms are different");

    148:   VecAXPBYPCZ(y,3.0,-1.0,1.0,w,v);
    149:   VecNorm(y,NORM_2,&norm);
    150:   VecAXPBYPCZ(yc,3.0,-1.0,1.0,wc,vc);
    151:   VecNorm(yc,NORM_2,&normc);
    152:   if (PetscAbsReal(norm-normc)>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Norms are different");

    154:   VecMax(xc,NULL,&vmax);
    155:   VecMin(xc,NULL,&vmin);
    156:   PetscPrintf(PETSC_COMM_WORLD,"xc has max value %g min value %g\n",(double)vmax,(double)vmin);

    158:   VecMaxPointwiseDivide(wc,xc,&vmax);
    159:   PetscPrintf(PETSC_COMM_WORLD,"wc/xc has max value %g\n",(double)vmax);

    161:   VecDot(x,y,&dot[0]);
    162:   VecDot(xc,yc,&dotc[0]);
    163:   if (PetscAbsScalar(dot[0]-dotc[0])>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Dots are different");
    164:   VecTDot(x,y,&dot[0]);
    165:   VecTDot(xc,yc,&dotc[0]);
    166:   if (PetscAbsScalar(dot[0]-dotc[0])>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Dots are different");

    168:   vecs[0] = w; vecs[1] = y;
    169:   VecMDot(x,2,vecs,dot);
    170:   vecs[0] = wc; vecs[1] = yc;
    171:   VecMDot(xc,2,vecs,dotc);
    172:   if (PetscAbsScalar(dot[0]-dotc[0])>10*PETSC_MACHINE_EPSILON || PetscAbsScalar(dot[1]-dotc[1])>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Dots are different");
    173:   vecs[0] = w; vecs[1] = y;
    174:   VecMTDot(x,2,vecs,dot);
    175:   vecs[0] = wc; vecs[1] = yc;
    176:   VecMTDot(xc,2,vecs,dotc);
    177:   if (PetscAbsScalar(dot[0]-dotc[0])>10*PETSC_MACHINE_EPSILON || PetscAbsScalar(dot[1]-dotc[1])>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Dots are different");

    179:   VecDotNorm2(x,y,&dot[0],&norm);
    180:   VecDotNorm2(xc,yc,&dotc[0],&normc);
    181:   if (PetscAbsScalar(dot[0]-dotc[0])>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Dots are different");
    182:   if (PetscAbsReal(norm-normc)>100*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Norms are different");

    184:   VecAbs(w);
    185:   VecAbs(wc);
    186:   VecConjugate(x);
    187:   VecConjugate(xc);
    188:   VecShift(y,0.5);
    189:   VecShift(yc,0.5);
    190:   VecReciprocal(y);
    191:   VecReciprocal(yc);
    192:   VecExp(y);
    193:   VecExp(yc);
    194:   VecLog(y);
    195:   VecLog(yc);
    196:   VecNorm(y,NORM_1,&norm);
    197:   VecNorm(yc,NORM_1,&normc);
    198:   if (PetscAbsReal(norm-normc)>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Norms are different");

    200:   VecPointwiseMult(w,x,y);
    201:   VecPointwiseMult(wc,xc,yc);
    202:   VecNorm(w,NORM_INFINITY,&norm);
    203:   VecNorm(wc,NORM_INFINITY,&normc);
    204:   if (PetscAbsReal(norm-normc)>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Norms are different");

    206:   VecPointwiseMax(w,x,y);
    207:   VecPointwiseMax(wc,xc,yc);
    208:   VecNorm(w,NORM_INFINITY,&norm);
    209:   VecNorm(wc,NORM_INFINITY,&normc);
    210:   if (PetscAbsReal(norm-normc)>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Norms are different");

    212:   VecSwap(x,y);
    213:   VecSwap(xc,yc);
    214:   VecPointwiseDivide(w,x,y);
    215:   VecPointwiseDivide(wc,xc,yc);
    216:   VecScale(w,0.3);
    217:   VecScale(wc,0.3);
    218:   VecSqrtAbs(w);
    219:   VecSqrtAbs(wc);
    220:   VecNorm(w,NORM_1_AND_2,norm12);
    221:   VecNorm(wc,NORM_1_AND_2,norm12c);
    222:   if (PetscAbsReal(norm12[0]-norm12c[0])>10*PETSC_MACHINE_EPSILON || PetscAbsReal(norm12[1]-norm12c[1])>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Norms are different");

    224:   VecPointwiseMin(w,x,y);
    225:   VecPointwiseMin(wc,xc,yc);
    226:   VecPointwiseMaxAbs(x,y,w);
    227:   VecPointwiseMaxAbs(xc,yc,wc);
    228:   VecNorm(x,NORM_INFINITY,&norm);
    229:   VecNorm(xc,NORM_INFINITY,&normc);
    230:   if (PetscAbsReal(norm-normc)>10*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_WORLD,1,"Norms are different");

    232:   VecSetRandom(wc,NULL);

    234:   VecDestroy(&v);
    235:   VecDestroy(&w);
    236:   VecDestroy(&x);
    237:   VecDestroy(&y);
    238:   VecDestroy(&vparent);
    239:   VecDestroy(&vc);
    240:   VecDestroy(&wc);
    241:   VecDestroy(&xc);
    242:   VecDestroy(&yc);
    243:   SlepcFinalize();
    244:   return ierr;
    245: }

    247: /*TEST

    249:    test:
    250:       suffix: 1

    252:    test:
    253:       suffix: 2
    254:       nsize: 2

    256: TEST*/