PetIGA / demo / BoundaryIntegral.c

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214``` ```/* This code solves the Laplace problem where the boundary conditions can be changed from Neumann to Dirichlet via the commandline. While its primary use is in regression tests for PetIGA, it also demonstrates how boundary integrals may be performed to enforce things like Neumann conditions. keywords: steady, scalar, linear, testing, dimension independent, boundary integrals */ #include "petiga.h" typedef struct { PetscInt dir; PetscInt side; } AppCtx; PETSC_STATIC_INLINE PetscReal DOT(PetscInt dim,const PetscReal a[],const PetscReal b[]) { PetscInt i; PetscReal s = 0.0; for (i=0; idim; PetscInt nen = p->nen; const PetscReal (*N1)[dim] = (typeof(N1)) p->shape[1]; PetscInt a,b; for (a=0; anen; const PetscReal *N0 = (typeof(N0)) p->shape[0]; PetscInt a; for (a=0; anen; PetscInt dim = p->dim; const PetscReal (*N2)[dim][dim] = (typeof(N2)) p->shape[2]; PetscInt a; for (a=0; anen; const PetscReal *N0 = (typeof(N0)) p->shape[0]; PetscInt a; for (a=0; anen; PetscInt dim = p->dim; const PetscReal (*N1)[dim] = (typeof(N1)) p->shape[1]; const PetscReal *normal = p->normal; PetscInt a; for (a=0; aside == 0) x = 1 - p->point[user->dir] + 1; else x = p->point[user->dir] + 1; PetscReal e = u - x; S[0] = e*e; return 0; } #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc, char *argv[]) { PetscErrorCode ierr; ierr = PetscInitialize(&argc,&argv,0,0);CHKERRQ(ierr); AppCtx user; user.dir = 0; user.side = 1; PetscBool print_error = PETSC_FALSE; PetscBool check_error = PETSC_FALSE; PetscBool draw = PETSC_FALSE; ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"","BoundaryIntegral Options","IGA");CHKERRQ(ierr); ierr = PetscOptionsInt ("-dir", "Neuman BC direction",__FILE__,user.dir, &user.dir, PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsInt ("-side","Neuman BC side", __FILE__,user.side,&user.side,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsBool("-print_error","Prints the L2 error of the solution",__FILE__,print_error,&print_error,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsBool("-check_error","Checks the L2 error of the solution",__FILE__,check_error,&check_error,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsBool("-draw","If dim <= 2, then draw the solution to the screen",__FILE__,draw,&draw,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsEnd();CHKERRQ(ierr); IGA iga; ierr = IGACreate(PETSC_COMM_WORLD,&iga);CHKERRQ(ierr); ierr = IGASetDof(iga,1);CHKERRQ(ierr); ierr = IGASetFromOptions(iga);CHKERRQ(ierr); if (iga->dim < 1) {ierr = IGASetDim(iga,2);CHKERRQ(ierr);} PetscInt dim; ierr = IGAGetDim(iga,&dim);CHKERRQ(ierr); PetscInt d = !user.side; PetscInt n = !!user.side; if (!iga->collocation) { IGABoundary bnd; ierr = IGAGetBoundary(iga,user.dir,d,&bnd);CHKERRQ(ierr); ierr = IGABoundarySetValue(bnd,0,1.0);CHKERRQ(ierr); ierr = IGAGetBoundary(iga,user.dir,n,&bnd);CHKERRQ(ierr); ierr = IGABoundarySetUserSystem(bnd,Neumann,PETSC_NULL);CHKERRQ(ierr); } else { IGABoundary bnd; PetscInt dir,side; for (dir=0; dircollocation) { ierr = IGASetUserSystem(iga,System,PETSC_NULL);CHKERRQ(ierr); } else { ierr = IGASetUserSystem(iga,SystemCollocation,PETSC_NULL);CHKERRQ(ierr); } ierr = IGAComputeSystem(iga,A,b);CHKERRQ(ierr); KSP ksp; ierr = IGACreateKSP(iga,&ksp);CHKERRQ(ierr); ierr = KSPSetOperators(ksp,A,A,SAME_NONZERO_PATTERN);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); PetscScalar error = 0; ierr = IGAFormScalar(iga,x,1,&error,Error,&user);CHKERRQ(ierr); error = PetscSqrtReal(PetscRealPart(error)); if (print_error) {ierr = PetscPrintf(PETSC_COMM_WORLD,"L2 error = %G\n",error);CHKERRQ(ierr);} if (check_error) {if (error>1e-4) SETERRQ1(PETSC_COMM_WORLD,1,"L2 error=%G\n",error);} if (draw&&dim<3) {ierr = VecView(x,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr);} ierr = KSPDestroy(&ksp);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = IGADestroy(&iga);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; } ```