# PetIGA / demo / Laplace.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``` ```/* This code solves the Laplace problem in one of the following ways: 1) On the parametric unit domain [0,1]^dim (default) To solve on the parametric domain, do not specify a geometry file. You may change the discretization by altering the dimension of the space (-iga_dim), the number of uniform elements in each direction (-iga_elements), the polynomial order (-iga_degree), and the continuity (-iga_continuity). 2) On a geometry If a geometry file is specified (-iga_geometry), the discretization will be what is read in from the geometry and is not editable from the command line. Note that the boundary conditions for this problem are such that the solution is always u(x)=1 (unit Dirichlet on the left side and free Neumann on the right). The error in the solution may be computed by using the -print_error command. */ #include "petiga.h" PETSC_STATIC_INLINE PetscReal DOT(PetscInt dim,const PetscReal a[],const PetscReal b[]) { PetscInt i; PetscReal s = 0.0; for (i=0; inen; PetscInt dim = p->dim; const PetscReal (*N1)[dim] = (typeof(N1)) p->shape[1]; PetscInt a,b; for (a=0; anen; PetscInt dim = p->dim; const PetscReal (*N2)[dim][dim] = (typeof(N2)) p->shape[2]; PetscInt a; for (a=0; adim < 1) {ierr = IGASetDim(iga,2);CHKERRQ(ierr);} ierr = IGASetUp(iga);CHKERRQ(ierr); // Set boundary conditions PetscInt dim,dir; ierr = IGAGetDim(iga,&dim);CHKERRQ(ierr); for (dir=0; dircollocation) { ierr = IGASetUserSystem(iga,SystemGalerkin,NULL);CHKERRQ(ierr); ierr = MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); ierr = MatSetOption(A,MAT_SPD,PETSC_TRUE);CHKERRQ(ierr); } else { ierr = IGASetUserSystem(iga,SystemCollocation,NULL);CHKERRQ(ierr); ierr = MatSetOption(A,MAT_SYMMETRIC,PETSC_FALSE);CHKERRQ(ierr); } ierr = IGAComputeSystem(iga,A,b);CHKERRQ(ierr); // Solve 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); // Various post-processing options PetscScalar error = 0; ierr = IGAFormScalar(iga,x,1,&error,Error,NULL);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);} if (save) {ierr = IGAWrite(iga,"LaplaceGeometry.dat");CHKERRQ(ierr);} if (save) {ierr = IGAWriteVec(iga,x,"LaplaceSolution.dat");CHKERRQ(ierr);} // Cleanup 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; } ```