1 #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2 #include <petsclandau.h> /*I "petsclandau.h" I*/ 3 #include <petscts.h> 4 #include <petscdmforest.h> 5 6 /* Landau collision operator */ 7 #define PETSC_THREAD_SYNC 8 #include "land_tensors.h" 9 10 /* vector padding not supported */ 11 #define LANDAU_VL 1 12 13 static PetscErrorCode LandauGPUMapsDestroy(void *ptr) 14 { 15 P4estVertexMaps *maps = (P4estVertexMaps *)ptr; 16 PetscErrorCode ierr; 17 PetscFunctionBegin; 18 if (maps->deviceType != LANDAU_CPU) { 19 #if defined(PETSC_HAVE_KOKKOS_KERNELS) 20 if (maps->deviceType == LANDAU_KOKKOS) { 21 ierr = LandauKokkosDestroyMatMaps(maps);CHKERRQ(ierr); // imples Kokkos does 22 } // else could be CUDA 23 #elif defined(PETSC_HAVE_CUDA) 24 if (maps->deviceType == LANDAU_CUDA) { 25 ierr = LandauCUDADestroyMatMaps(maps);CHKERRQ(ierr); 26 } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "maps->deviceType %D ?????",maps->deviceType); 27 #endif 28 } 29 ierr = PetscFree(maps->c_maps);CHKERRQ(ierr); 30 ierr = PetscFree(maps->gIdx);CHKERRQ(ierr); 31 ierr = PetscFree(maps);CHKERRQ(ierr); 32 PetscFunctionReturn(0); 33 } 34 35 /* ------------------------------------------------------------------- */ 36 /* 37 LandauFormJacobian_Internal - Evaluates Jacobian matrix. 38 39 Input Parameters: 40 . globX - input vector 41 . actx - optional user-defined context 42 . dim - dimension 43 44 Output Parameters: 45 . J0acP - Jacobian matrix filled, not created 46 */ 47 static PetscErrorCode LandauFormJacobian_Internal(Vec a_X, Mat JacP, const PetscInt dim, PetscReal shift, void *a_ctx) 48 { 49 LandauCtx *ctx = (LandauCtx*)a_ctx; 50 PetscErrorCode ierr; 51 PetscInt cStart, cEnd, elemMatSize; 52 PetscDS prob; 53 PetscSection section,globsection; 54 PetscInt numCells,totDim,ej,Nq,*Nbf,*Ncf,Nb,Ncx,Nf,d,f,fieldA,qj,N; 55 PetscQuadrature quad; 56 const PetscReal *quadWeights; 57 PetscTabulation *Tf; // used for CPU and print info 58 PetscReal Eq_m[LANDAU_MAX_SPECIES], m_0=ctx->m_0; /* normalize mass -- not needed! */ 59 PetscScalar *IPf=NULL; 60 const PetscScalar *xdata=NULL; 61 PetscLogDouble flops; 62 PetscContainer container; 63 P4estVertexMaps *maps=NULL; 64 65 PetscFunctionBegin; 66 PetscValidHeaderSpecific(a_X,VEC_CLASSID,1); 67 PetscValidHeaderSpecific(JacP,MAT_CLASSID,2); 68 PetscValidPointer(ctx,5); 69 /* check for matrix container for GPU assembly */ 70 ierr = PetscLogEventBegin(ctx->events[10],0,0,0,0);CHKERRQ(ierr); 71 ierr = PetscObjectQuery((PetscObject) JacP, "assembly_maps", (PetscObject *) &container);CHKERRQ(ierr); 72 if (container /* && ctx->deviceType != LANDAU_CPU */) { 73 if (!ctx->gpu_assembly) SETERRQ(ctx->comm,PETSC_ERR_ARG_WRONG,"GPU matrix container but no GPU assembly"); 74 ierr = PetscContainerGetPointer(container, (void **) &maps);CHKERRQ(ierr); 75 if (!maps) SETERRQ(ctx->comm,PETSC_ERR_ARG_WRONG,"empty GPU matrix container"); 76 } 77 if (ctx->plex == NULL) { 78 ierr = DMConvert(ctx->dmv, DMPLEX, &ctx->plex);CHKERRQ(ierr); 79 } 80 ierr = DMPlexGetHeightStratum(ctx->plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 81 ierr = DMGetLocalSection(ctx->plex, §ion);CHKERRQ(ierr); 82 ierr = DMGetGlobalSection(ctx->plex, &globsection);CHKERRQ(ierr); 83 ierr = DMGetDS(ctx->plex, &prob);CHKERRQ(ierr); 84 ierr = PetscDSGetTabulation(prob, &Tf);CHKERRQ(ierr); // Bf, &Df 85 ierr = PetscDSGetDimensions(prob, &Nbf);CHKERRQ(ierr); Nb = Nbf[0]; /* number of vertices*S */ 86 ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 87 if (Nf!=ctx->num_species) SETERRQ1(ctx->comm, PETSC_ERR_PLIB, "Nf %D != S",Nf); 88 ierr = PetscDSGetComponents(prob, &Ncf);CHKERRQ(ierr); 89 Ncx = Ncf[0]; 90 if (Ncx!=1) SETERRQ1(ctx->comm, PETSC_ERR_PLIB, "Nc %D != 1",Ncx); 91 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 92 numCells = cEnd - cStart; 93 ierr = PetscFEGetQuadrature(ctx->fe[0], &quad);CHKERRQ(ierr); 94 ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, &quadWeights);CHKERRQ(ierr); 95 if (Nb!=Nq) SETERRQ4(ctx->comm, PETSC_ERR_PLIB, "Nb!=Nq %D %D over integration or simplices? Tf[0]->Nb=%D dim=%D",Nb,Nq,Tf[0]->Nb,dim); 96 if (Nq >LANDAU_MAX_NQ) SETERRQ2(ctx->comm,PETSC_ERR_ARG_WRONG,"Order too high. Nq = %D > LANDAU_MAX_NQ (%D)",Nq,LANDAU_MAX_NQ); 97 if (LANDAU_DIM != dim) SETERRQ2(ctx->comm, PETSC_ERR_PLIB, "dim %D != LANDAU_DIM %d",dim,LANDAU_DIM); 98 if (!ctx->SData_d) { 99 ierr = PetscNew(&ctx->SData_d);CHKERRQ(ierr); 100 } 101 elemMatSize = totDim*totDim; // used for CPU and print info 102 ierr = PetscLogEventEnd(ctx->events[10],0,0,0,0);CHKERRQ(ierr); 103 ierr = VecGetSize(a_X,&N);CHKERRQ(ierr); 104 if (!ctx->init) { /* create static point data, Jacobian called first */ 105 PetscReal *invJ,*ww,*xx,*yy,*zz=NULL,*mass_w,*invJ_a; 106 const PetscInt nip = Nq*numCells; 107 108 ierr = PetscLogEventBegin(ctx->events[7],0,0,0,0);CHKERRQ(ierr); 109 ctx->init = PETSC_TRUE; 110 ierr = PetscInfo(ctx->plex, "Initialize static data\n");CHKERRQ(ierr); 111 /* collect f data, first time is for Jacobian, but make mass now */ 112 if (ctx->verbose > 1 || ctx->verbose > 0) { 113 ierr = PetscPrintf(ctx->comm,"[%D]%s: %D IPs, %D cells, totDim=%D, Nb=%D, Nq=%D, elemMatSize=%D, dim=%D, Tab: Nb=%D Nf=%D Np=%D cdim=%D N=%D shift=%g\n", 114 0,"FormLandau",Nq*numCells,numCells, totDim, Nb, Nq, elemMatSize, dim, Tf[0]->Nb, Nf, Tf[0]->Np, Tf[0]->cdim, N, shift);CHKERRQ(ierr); 115 } 116 ierr = PetscMalloc5(nip,&mass_w,nip,&ww,nip,&xx,nip,&yy,nip*dim*dim,&invJ_a);CHKERRQ(ierr); 117 if (dim==3) { 118 ierr = PetscMalloc1(nip,&zz);CHKERRQ(ierr); 119 } 120 for (ej = 0 ; ej < numCells; ++ej) { 121 PetscReal vj[LANDAU_MAX_NQ*LANDAU_DIM],detJj[LANDAU_MAX_NQ], Jdummy[LANDAU_MAX_NQ*LANDAU_DIM*LANDAU_DIM]; 122 invJ = invJ_a ? invJ_a + ej * Nq*dim*dim : NULL; 123 ierr = DMPlexComputeCellGeometryFEM(ctx->plex, cStart+ej, quad, vj, Jdummy, invJ, detJj);CHKERRQ(ierr); 124 /* create dynamic point data */ 125 for (qj = 0; qj < Nq; ++qj) { 126 PetscInt gidx = (ej*Nq + qj); 127 mass_w[gidx] = detJj[qj] * quadWeights[qj]; 128 if (dim==2) mass_w[gidx] *= 2.*PETSC_PI*vj[qj * dim + 0]; /* cylindrical coordinate, w/o 2pi */ 129 xx[gidx] = vj[qj * dim + 0]; /* coordinate */ 130 yy[gidx] = vj[qj * dim + 1]; 131 if (dim==3) zz[gidx] = vj[qj * dim + 2]; 132 ww[gidx] = detJj[qj] * quadWeights[qj]; 133 if (dim==2) ww[gidx] *= xx[gidx]; /* cylindrical coordinate, w/o 2pi */ 134 } /* q */ 135 } /* ej */ 136 /* cache static data */ 137 if (ctx->deviceType == LANDAU_CUDA || ctx->deviceType == LANDAU_KOKKOS) { 138 #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_KOKKOS) 139 PetscReal invMass[LANDAU_MAX_SPECIES],nu_alpha[LANDAU_MAX_SPECIES], nu_beta[LANDAU_MAX_SPECIES]; 140 for (fieldA=0;fieldA<Nf;fieldA++) { 141 invMass[fieldA] = m_0/ctx->masses[fieldA]; 142 nu_alpha[fieldA] = PetscSqr(ctx->charges[fieldA]/m_0)*m_0/ctx->masses[fieldA]; 143 nu_beta[fieldA] = PetscSqr(ctx->charges[fieldA]/ctx->epsilon0)*ctx->lnLam / (8*PETSC_PI) * ctx->t_0*ctx->n_0/PetscPowReal(ctx->v_0,3); 144 } 145 if (ctx->deviceType == LANDAU_CUDA) { 146 #if defined(PETSC_HAVE_CUDA) 147 ierr = LandauCUDAStaticDataSet(ctx->plex,Nq,nu_alpha,nu_beta,invMass,invJ_a,mass_w,xx,yy,zz,ww,ctx->SData_d);CHKERRQ(ierr); 148 #else 149 SETERRQ1(ctx->comm,PETSC_ERR_ARG_WRONG,"-landau_device_type %s not built","cuda"); 150 #endif 151 } else if (ctx->deviceType == LANDAU_KOKKOS) { 152 #if defined(PETSC_HAVE_KOKKOS) 153 ierr = LandauKokkosStaticDataSet(ctx->plex,Nq,nu_alpha,nu_beta,invMass,invJ_a,mass_w,xx,yy,zz,ww,ctx->SData_d);CHKERRQ(ierr); 154 #else 155 SETERRQ1(ctx->comm,PETSC_ERR_ARG_WRONG,"-landau_device_type %s not built","kokkos"); 156 #endif 157 } 158 #endif 159 /* free */ 160 ierr = PetscFree5(mass_w,ww,xx,yy,invJ_a);CHKERRQ(ierr); 161 if (dim==3) { 162 ierr = PetscFree(zz);CHKERRQ(ierr); 163 } 164 } else { /* CPU version, just copy in, only use part */ 165 ctx->SData_d->w = (void*)ww; 166 ctx->SData_d->x = (void*)xx; 167 ctx->SData_d->y = (void*)yy; 168 ctx->SData_d->z = (void*)zz; 169 ctx->SData_d->invJ = (void*)invJ_a; 170 ctx->SData_d->mass_w = (void*)mass_w; 171 } 172 ierr = PetscLogEventEnd(ctx->events[7],0,0,0,0);CHKERRQ(ierr); 173 } 174 if (shift==0) { /* create dynamic point data */ 175 ierr = PetscLogEventBegin(ctx->events[1],0,0,0,0);CHKERRQ(ierr); 176 ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 177 flops = (PetscLogDouble)numCells*(PetscLogDouble)Nq*(PetscLogDouble)(5*dim*dim*Nf*Nf + 165); 178 for (fieldA=0;fieldA<Nf;fieldA++) { 179 Eq_m[fieldA] = ctx->Ez * ctx->t_0 * ctx->charges[fieldA] / (ctx->v_0 * ctx->masses[fieldA]); /* normalize dimensionless */ 180 if (dim==2) Eq_m[fieldA] *= 2 * PETSC_PI; /* add the 2pi term that is not in Landau */ 181 } 182 if (!ctx->gpu_assembly || !container) { 183 Vec locX; 184 ierr = DMGetLocalVector(ctx->plex, &locX);CHKERRQ(ierr); 185 ierr = VecZeroEntries(locX);CHKERRQ(ierr); /* zero BCs so don't set */ 186 ierr = DMGlobalToLocalBegin(ctx->plex, a_X, INSERT_VALUES, locX);CHKERRQ(ierr); 187 ierr = DMGlobalToLocalEnd (ctx->plex, a_X, INSERT_VALUES, locX);CHKERRQ(ierr); 188 ierr = PetscMalloc1(Nq*numCells*Nf,&IPf);CHKERRQ(ierr); 189 for (ej = 0 ; ej < numCells; ++ej) { 190 PetscScalar *coef = NULL; 191 ierr = DMPlexVecGetClosure(ctx->plex, section, locX, cStart+ej, NULL, &coef);CHKERRQ(ierr); 192 ierr = PetscMemcpy(&IPf[ej*Nb*Nf],coef,Nb*Nf*sizeof(PetscScalar));CHKERRQ(ierr); /* change if LandauIPReal != PetscScalar */ 193 ierr = DMPlexVecRestoreClosure(ctx->plex, section, locX, cStart+ej, NULL, &coef);CHKERRQ(ierr); 194 } /* ej */ 195 ierr = DMRestoreLocalVector(ctx->plex, &locX);CHKERRQ(ierr); 196 } else { 197 PetscMemType mtype; 198 ierr = VecGetArrayReadAndMemType(a_X,&xdata,&mtype);CHKERRQ(ierr); 199 } 200 ierr = PetscLogEventEnd(ctx->events[1],0,0,0,0);CHKERRQ(ierr); 201 } else { 202 flops = (PetscLogDouble)numCells*(PetscLogDouble)Nq*(PetscLogDouble)(5*dim*dim*Nf*Nf); 203 } 204 /* do it */ 205 if (ctx->deviceType == LANDAU_CUDA || ctx->deviceType == LANDAU_KOKKOS) { 206 if (ctx->deviceType == LANDAU_CUDA) { 207 #if defined(PETSC_HAVE_CUDA) 208 ierr = LandauCUDAJacobian(ctx->plex,Nq,Eq_m,IPf,N,xdata,ctx->SData_d,ctx->subThreadBlockSize,shift,ctx->events,JacP);CHKERRQ(ierr); 209 #else 210 SETERRQ1(ctx->comm,PETSC_ERR_ARG_WRONG,"-landau_device_type %s not built","cuda"); 211 #endif 212 } else if (ctx->deviceType == LANDAU_KOKKOS) { 213 #if defined(PETSC_HAVE_KOKKOS) 214 ierr = LandauKokkosJacobian(ctx->plex,Nq,Eq_m,IPf,N,xdata,ctx->SData_d,ctx->subThreadBlockSize,shift,ctx->events,JacP);CHKERRQ(ierr); 215 #else 216 SETERRQ1(ctx->comm,PETSC_ERR_ARG_WRONG,"-landau_device_type %s not built","kokkos"); 217 #endif 218 } 219 } else { /* CPU version */ 220 PetscInt ei, qi; 221 PetscScalar *elemMat,coef_buff[LANDAU_MAX_SPECIES*LANDAU_MAX_NQ]; 222 PetscReal *ff, *dudx, *dudy, *dudz, *invJ, *invJ_a = (PetscReal*)ctx->SData_d->invJ, *xx = (PetscReal*)ctx->SData_d->x, *yy = (PetscReal*)ctx->SData_d->y, *zz = (PetscReal*)ctx->SData_d->z, *ww = (PetscReal*)ctx->SData_d->w, *mass_w = (PetscReal*)ctx->SData_d->mass_w; 223 const PetscInt nip = Nq*numCells; 224 const PetscReal *const BB = Tf[0]->T[0], * const DD = Tf[0]->T[1]; 225 PetscReal Eq_m[LANDAU_MAX_SPECIES], invMass[LANDAU_MAX_SPECIES], nu_alpha[LANDAU_MAX_SPECIES], nu_beta[LANDAU_MAX_SPECIES]; 226 if (shift!=0.0) { // mass 227 ierr = PetscMalloc1(elemMatSize, &elemMat);CHKERRQ(ierr); 228 } else { /* compute f and df and init data for Jacobian */ 229 ierr = PetscLogEventBegin(ctx->events[8],0,0,0,0);CHKERRQ(ierr); 230 for (fieldA=0;fieldA<Nf;fieldA++) { 231 invMass[fieldA] = m_0/ctx->masses[fieldA]; 232 Eq_m[fieldA] = ctx->Ez * ctx->t_0 * ctx->charges[fieldA] / (ctx->v_0 * ctx->masses[fieldA]); /* normalize dimensionless */ 233 if (dim==2) Eq_m[fieldA] *= 2 * PETSC_PI; /* add the 2pi term that is not in Landau */ 234 nu_alpha[fieldA] = PetscSqr(ctx->charges[fieldA]/m_0)*m_0/ctx->masses[fieldA]; 235 nu_beta[fieldA] = PetscSqr(ctx->charges[fieldA]/ctx->epsilon0)*ctx->lnLam / (8*PETSC_PI) * ctx->t_0*ctx->n_0/PetscPowReal(ctx->v_0,3); 236 } 237 ierr = PetscMalloc5(elemMatSize, &elemMat, nip*Nf, &ff, nip*Nf, &dudx, nip*Nf, &dudy, dim==3 ? nip*Nf : 0, &dudz);CHKERRQ(ierr); 238 for (ei = cStart, invJ = invJ_a; ei < cEnd; ++ei, invJ += Nq*dim*dim) { 239 PetscScalar *coef; 240 PetscInt b,f,idx,q; 241 PetscReal u_x[LANDAU_MAX_SPECIES][LANDAU_DIM]; 242 if (IPf) { 243 coef = &IPf[ei*Nb*Nf]; // this is const 244 } else { 245 if (!maps) SETERRQ(ctx->comm,PETSC_ERR_ARG_WRONG,"!maps"); 246 coef = coef_buff; 247 for (f = 0; f < Nf; ++f) { 248 LandauIdx *const Idxs = &maps->gIdx[ei-cStart][f][0]; 249 for (b = 0; b < Nb; ++b) { 250 idx = Idxs[b]; 251 if (idx >= 0) { 252 coef[f*Nb+b] = xdata[idx]; 253 } else { 254 idx = -idx - 1; 255 coef[f*Nb+b] = 0; 256 for (q = 0; q < maps->num_face; q++) { 257 PetscInt id = maps->c_maps[idx][q].gid; 258 PetscScalar scale = maps->c_maps[idx][q].scale; 259 coef[f*Nb+b] += scale*xdata[id]; 260 } 261 } 262 } 263 } 264 } 265 /* get f and df */ 266 for (qi = 0; qi < Nq; ++qi) { 267 const PetscReal *Bq = &BB[qi*Nb]; 268 const PetscReal *Dq = &DD[qi*Nb*dim]; 269 const PetscInt gidx = ei*Nq + qi; 270 /* get f & df */ 271 for (f = 0; f < Nf; ++f) { 272 PetscInt b, e; 273 PetscReal refSpaceDer[LANDAU_DIM]; 274 ff[gidx + f*nip] = 0.0; 275 for (d = 0; d < LANDAU_DIM; ++d) refSpaceDer[d] = 0.0; 276 for (b = 0; b < Nb; ++b) { 277 const PetscInt cidx = b; 278 ff[gidx + f*nip] += Bq[cidx]*PetscRealPart(coef[f*Nb+cidx]); 279 for (d = 0; d < dim; ++d) refSpaceDer[d] += Dq[cidx*dim+d]*PetscRealPart(coef[f*Nb+cidx]); 280 } 281 for (d = 0; d < dim; ++d) { 282 for (e = 0, u_x[f][d] = 0.0; e < dim; ++e) { 283 u_x[f][d] += invJ[qi * dim * dim + e*dim+d]*refSpaceDer[e]; 284 } 285 } 286 } 287 for (f=0;f<Nf;f++) { 288 dudx[gidx + f*nip] = u_x[f][0]; 289 dudy[gidx + f*nip] = u_x[f][1]; 290 #if LANDAU_DIM==3 291 dudz[gidx + f*nip] = u_x[f][2]; 292 #endif 293 } 294 } 295 } 296 ierr = PetscLogEventEnd(ctx->events[8],0,0,0,0);CHKERRQ(ierr); 297 } 298 for (ej = cStart, invJ = invJ_a; ej < cEnd; ++ej, invJ += Nq*dim*dim) { 299 ierr = PetscMemzero(elemMat, totDim *totDim * sizeof(PetscScalar));CHKERRQ(ierr); 300 ierr = PetscLogEventBegin(ctx->events[4],0,0,0,0);CHKERRQ(ierr); 301 ierr = PetscLogFlops((PetscLogDouble)Nq*flops);CHKERRQ(ierr); 302 //printf("\t:%d.%d) Invj[0] = %e (%d)\n",ej,qj,invJ[0],(int)(invJ-invJ_a)); 303 for (qj = 0; qj < Nq; ++qj) { 304 const PetscReal * const BB = Tf[0]->T[0], * const DD = Tf[0]->T[1]; 305 PetscReal g0[LANDAU_MAX_SPECIES], g2[LANDAU_MAX_SPECIES][LANDAU_DIM], g3[LANDAU_MAX_SPECIES][LANDAU_DIM][LANDAU_DIM]; 306 PetscInt d,d2,dp,d3,ipidx,fieldA; 307 const PetscInt jpidx = Nq*(ej-cStart) + qj; 308 if (shift==0.0) { 309 const PetscReal * const invJj = &invJ[qj*dim*dim]; 310 PetscReal gg2[LANDAU_MAX_SPECIES][LANDAU_DIM],gg3[LANDAU_MAX_SPECIES][LANDAU_DIM][LANDAU_DIM], gg2_temp[LANDAU_DIM], gg3_temp[LANDAU_DIM][LANDAU_DIM]; 311 const PetscReal vj[3] = {xx[jpidx], yy[jpidx], zz ? zz[jpidx] : 0}, wj = ww[jpidx]; 312 313 // create g2 & g3 314 for (d=0;d<dim;d++) { // clear accumulation data D & K 315 gg2_temp[d] = 0; 316 for (d2=0;d2<dim;d2++) gg3_temp[d][d2] = 0; 317 } 318 for (ipidx = 0; ipidx < nip; ipidx++) { 319 const PetscReal wi = ww[ipidx], x = xx[ipidx], y = yy[ipidx]; 320 PetscReal temp1[3] = {0, 0, 0}, temp2 = 0; 321 #if LANDAU_DIM==2 322 PetscReal Ud[2][2], Uk[2][2]; 323 LandauTensor2D(vj, x, y, Ud, Uk, (ipidx==jpidx) ? 0. : 1.); 324 #else 325 PetscReal U[3][3], z = zz[ipidx]; 326 LandauTensor3D(vj, x, y, z, U, (ipidx==jpidx) ? 0. : 1.); 327 #endif 328 for (fieldA = 0; fieldA < Nf; ++fieldA) { 329 temp1[0] += dudx[ipidx + fieldA*nip]*nu_beta[fieldA]*invMass[fieldA]; 330 temp1[1] += dudy[ipidx + fieldA*nip]*nu_beta[fieldA]*invMass[fieldA]; 331 #if LANDAU_DIM==3 332 temp1[2] += dudz[ipidx + fieldA*nip]*nu_beta[fieldA]*invMass[fieldA]; 333 #endif 334 temp2 += ff[ipidx + fieldA*nip]*nu_beta[fieldA]; 335 } 336 temp1[0] *= wi; 337 temp1[1] *= wi; 338 #if LANDAU_DIM==3 339 temp1[2] *= wi; 340 #endif 341 temp2 *= wi; 342 #if LANDAU_DIM==2 343 for (d2 = 0; d2 < 2; d2++) { 344 for (d3 = 0; d3 < 2; ++d3) { 345 /* K = U * grad(f): g2=e: i,A */ 346 gg2_temp[d2] += Uk[d2][d3]*temp1[d3]; 347 /* D = -U * (I \kron (fx)): g3=f: i,j,A */ 348 gg3_temp[d2][d3] += Ud[d2][d3]*temp2; 349 } 350 } 351 #else 352 for (d2 = 0; d2 < 3; ++d2) { 353 for (d3 = 0; d3 < 3; ++d3) { 354 /* K = U * grad(f): g2 = e: i,A */ 355 gg2_temp[d2] += U[d2][d3]*temp1[d3]; 356 /* D = -U * (I \kron (fx)): g3 = f: i,j,A */ 357 gg3_temp[d2][d3] += U[d2][d3]*temp2; 358 } 359 } 360 #endif 361 } /* IPs */ 362 // add alpha and put in gg2/3 363 for (fieldA = 0; fieldA < Nf; ++fieldA) { 364 for (d2 = 0; d2 < dim; d2++) { 365 gg2[fieldA][d2] = gg2_temp[d2]*nu_alpha[fieldA]; 366 for (d3 = 0; d3 < dim; d3++) { 367 gg3[fieldA][d2][d3] = -gg3_temp[d2][d3]*nu_alpha[fieldA]*invMass[fieldA]; 368 } 369 } 370 } 371 /* add electric field term once per IP */ 372 for (fieldA = 0; fieldA < Nf; ++fieldA) { 373 gg2[fieldA][dim-1] += Eq_m[fieldA]; 374 } 375 /* Jacobian transform - g2, g3 */ 376 for (fieldA = 0; fieldA < Nf; ++fieldA) { 377 for (d = 0; d < dim; ++d) { 378 g2[fieldA][d] = 0.0; 379 for (d2 = 0; d2 < dim; ++d2) { 380 g2[fieldA][d] += invJj[d*dim+d2]*gg2[fieldA][d2]; 381 g3[fieldA][d][d2] = 0.0; 382 for (d3 = 0; d3 < dim; ++d3) { 383 for (dp = 0; dp < dim; ++dp) { 384 g3[fieldA][d][d2] += invJj[d*dim + d3]*gg3[fieldA][d3][dp]*invJj[d2*dim + dp]; 385 } 386 } 387 g3[fieldA][d][d2] *= wj; 388 } 389 g2[fieldA][d] *= wj; 390 } 391 } 392 } else { // mass 393 /* Jacobian transform - g0 */ 394 for (fieldA = 0; fieldA < Nf; ++fieldA) { 395 g0[fieldA] = mass_w[jpidx] * shift; // move this to below and remove g0 396 } 397 } 398 /* FE matrix construction */ 399 { 400 PetscInt fieldA,d,f,d2,g; 401 const PetscReal *BJq = &BB[qj*Nb], *DIq = &DD[qj*Nb*dim]; 402 /* assemble - on the diagonal (I,I) */ 403 for (fieldA = 0; fieldA < Nf ; fieldA++) { 404 for (f = 0; f < Nb ; f++) { 405 const PetscInt i = fieldA*Nb + f; /* Element matrix row */ 406 for (g = 0; g < Nb; ++g) { 407 const PetscInt j = fieldA*Nb + g; /* Element matrix column */ 408 const PetscInt fOff = i*totDim + j; 409 if (shift==0.0) { 410 for (d = 0; d < dim; ++d) { 411 elemMat[fOff] += DIq[f*dim+d]*g2[fieldA][d]*BJq[g]; 412 //printf("\t:%d.%d.%d.%d.%d.%d) elemMat=%e += %e %e %e\n",ej,qj,fieldA,f,g,d,elemMat[fOff],DIq[f*dim+d],g2[fieldA][d],BJq[g]); 413 for (d2 = 0; d2 < dim; ++d2) { 414 elemMat[fOff] += DIq[f*dim + d]*g3[fieldA][d][d2]*DIq[g*dim + d2]; 415 } 416 } 417 } else { // mass 418 elemMat[fOff] += BJq[f]*g0[fieldA]*BJq[g]; 419 } 420 } 421 } 422 } 423 } 424 } /* qj loop */ 425 ierr = PetscLogEventEnd(ctx->events[4],0,0,0,0);CHKERRQ(ierr); 426 /* assemble matrix */ 427 ierr = PetscLogEventBegin(ctx->events[6],0,0,0,0);CHKERRQ(ierr); 428 if (!maps) { 429 ierr = DMPlexMatSetClosure(ctx->plex, section, globsection, JacP, ej, elemMat, ADD_VALUES);CHKERRQ(ierr); 430 } else { // GPU like assembly for debugging 431 PetscInt fieldA,idx,q,f,g,d,nr,nc,rows0[LANDAU_MAX_Q_FACE],cols0[LANDAU_MAX_Q_FACE]={0},rows[LANDAU_MAX_Q_FACE],cols[LANDAU_MAX_Q_FACE]; 432 PetscScalar vals[LANDAU_MAX_Q_FACE*LANDAU_MAX_Q_FACE],row_scale[LANDAU_MAX_Q_FACE],col_scale[LANDAU_MAX_Q_FACE]={0}; 433 /* assemble - from the diagonal (I,I) in this format for DMPlexMatSetClosure */ 434 for (fieldA = 0; fieldA < Nf ; fieldA++) { 435 LandauIdx *const Idxs = &maps->gIdx[ej-cStart][fieldA][0]; 436 for (f = 0; f < Nb ; f++) { 437 idx = Idxs[f]; 438 if (idx >= 0) { 439 nr = 1; 440 rows0[0] = idx; 441 row_scale[0] = 1.; 442 } else { 443 idx = -idx - 1; 444 nr = maps->num_face; 445 for (q = 0; q < maps->num_face; q++) { 446 rows0[q] = maps->c_maps[idx][q].gid; 447 row_scale[q] = maps->c_maps[idx][q].scale; 448 } 449 } 450 for (g = 0; g < Nb; ++g) { 451 idx = Idxs[g]; 452 if (idx >= 0) { 453 nc = 1; 454 cols0[0] = idx; 455 col_scale[0] = 1.; 456 } else { 457 idx = -idx - 1; 458 nc = maps->num_face; 459 for (q = 0; q < maps->num_face; q++) { 460 cols0[q] = maps->c_maps[idx][q].gid; 461 col_scale[q] = maps->c_maps[idx][q].scale; 462 } 463 } 464 const PetscInt i = fieldA*Nb + f; /* Element matrix row */ 465 const PetscInt j = fieldA*Nb + g; /* Element matrix column */ 466 const PetscScalar Aij = elemMat[i*totDim + j]; 467 for (q = 0; q < nr; q++) rows[q] = rows0[q]; 468 for (q = 0; q < nc; q++) cols[q] = cols0[q]; 469 for (q = 0; q < nr; q++) { 470 for (d = 0; d < nc; d++) { 471 vals[q*nc + d] = row_scale[q]*col_scale[d]*Aij; 472 } 473 } 474 ierr = MatSetValues(JacP,nr,rows,nc,cols,vals,ADD_VALUES);CHKERRQ(ierr); 475 } 476 } 477 } 478 } 479 if (ej==-1) { 480 PetscErrorCode ierr2; 481 ierr2 = PetscPrintf(ctx->comm,"CPU Element matrix\n");CHKERRQ(ierr2); 482 for (d = 0; d < totDim; ++d) { 483 for (f = 0; f < totDim; ++f) {ierr2 = PetscPrintf(ctx->comm," %12.5e", PetscRealPart(elemMat[d*totDim + f]));CHKERRQ(ierr2);} 484 ierr2 = PetscPrintf(ctx->comm,"\n");CHKERRQ(ierr2); 485 } 486 exit(12); 487 } 488 ierr = PetscLogEventEnd(ctx->events[6],0,0,0,0);CHKERRQ(ierr); 489 } /* ej cells loop, not cuda */ 490 if (shift!=0.0) { // mass 491 ierr = PetscFree(elemMat);CHKERRQ(ierr); 492 } else { 493 ierr = PetscFree5(elemMat, ff, dudx, dudy, dudz);CHKERRQ(ierr); 494 } 495 } /* CPU version */ 496 497 /* assemble matrix or vector */ 498 ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 499 ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 500 #define MAP_BF_SIZE (128*LANDAU_DIM*LANDAU_MAX_Q_FACE*LANDAU_MAX_SPECIES) 501 if (ctx->gpu_assembly && !container) { 502 PetscScalar elemMatrix[LANDAU_MAX_NQ*LANDAU_MAX_NQ*LANDAU_MAX_SPECIES*LANDAU_MAX_SPECIES], *elMat; 503 pointInterpolationP4est pointMaps[MAP_BF_SIZE][LANDAU_MAX_Q_FACE]; 504 PetscInt q,eidx,fieldA; 505 MatType type; 506 ierr = PetscInfo1(ctx->plex, "Make GPU maps %D\n",1);CHKERRQ(ierr); 507 ierr = MatGetType(JacP,&type);CHKERRQ(ierr); 508 ierr = PetscLogEventBegin(ctx->events[2],0,0,0,0);CHKERRQ(ierr); 509 ierr = PetscMalloc(sizeof(P4estVertexMaps), &maps);CHKERRQ(ierr); 510 ierr = PetscContainerCreate(PETSC_COMM_SELF, &container);CHKERRQ(ierr); 511 ierr = PetscContainerSetPointer(container, (void *)maps);CHKERRQ(ierr); 512 ierr = PetscContainerSetUserDestroy(container, LandauGPUMapsDestroy);CHKERRQ(ierr); 513 ierr = PetscObjectCompose((PetscObject) JacP, "assembly_maps", (PetscObject) container);CHKERRQ(ierr); 514 ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 515 // make maps 516 maps->data = NULL; 517 maps->num_elements = numCells; 518 maps->num_face = (PetscInt)(pow(Nq,1./((double)dim))+.001); // Q 519 maps->num_face = (PetscInt)(pow(maps->num_face,(double)(dim-1))+.001); // Q^2 520 maps->num_reduced = 0; 521 maps->deviceType = ctx->deviceType; 522 523 // count reduced and get 524 ierr = PetscMalloc(maps->num_elements * sizeof *maps->gIdx, &maps->gIdx);CHKERRQ(ierr); 525 for (fieldA=0;fieldA<Nf;fieldA++) { 526 for (ej = cStart, eidx = 0 ; ej < cEnd; ++ej, ++eidx) { 527 for (q = 0; q < Nb; ++q) { 528 PetscInt numindices,*indices; 529 PetscScalar *valuesOrig = elMat = elemMatrix; 530 ierr = PetscMemzero(elMat, totDim*totDim*sizeof(PetscScalar));CHKERRQ(ierr); 531 elMat[ (fieldA*Nb + q)*totDim + fieldA*Nb + q] = 1; 532 ierr = DMPlexGetClosureIndices(ctx->plex, section, globsection, ej, PETSC_TRUE, &numindices, &indices, NULL, (PetscScalar **) &elMat);CHKERRQ(ierr); 533 for (f = 0 ; f < numindices ; ++f) { // look for a non-zero on the diagonal 534 if (PetscAbs(PetscRealPart(elMat[f*numindices + f])) > PETSC_MACHINE_EPSILON) { 535 // found it 536 if (PetscAbs(PetscRealPart(elMat[f*numindices + f] - 1.)) < PETSC_MACHINE_EPSILON) { 537 maps->gIdx[eidx][fieldA][q] = (LandauIdx)indices[f]; // normal vertex 1.0 538 } else { //found a constraint 539 int jj = 0; 540 PetscReal sum = 0; 541 const PetscInt ff = f; 542 maps->gIdx[eidx][fieldA][q] = -maps->num_reduced - 1; // gid = -(idx+1): idx = -gid - 1 543 do { // constraints are continous in Plex - exploit that here 544 int ii; 545 for (ii = 0, pointMaps[maps->num_reduced][jj].scale = 0; ii < maps->num_face; ii++) { // DMPlex puts them all together 546 if (ff + ii < numindices) { 547 pointMaps[maps->num_reduced][jj].scale += PetscRealPart(elMat[f*numindices + ff + ii]); 548 } 549 } 550 sum += pointMaps[maps->num_reduced][jj].scale; 551 if (pointMaps[maps->num_reduced][jj].scale == 0) pointMaps[maps->num_reduced][jj].gid = -1; // 3D has Q and Q^2 interps -- all contiguous??? 552 else pointMaps[maps->num_reduced][jj].gid = indices[f]; 553 } while (++jj < maps->num_face && ++f < numindices); // jj is incremented if we hit the end 554 while (jj++ < maps->num_face) { 555 pointMaps[maps->num_reduced][jj].scale = 0; 556 pointMaps[maps->num_reduced][jj].gid = -1; 557 } 558 if (PetscAbs(sum-1.0) > 10*PETSC_MACHINE_EPSILON) { // debug 559 int d,f; 560 PetscReal tmp = 0; 561 PetscPrintf(PETSC_COMM_SELF,"\t\t%D.%D.%D) ERROR total I = %22.16e (LANDAU_MAX_Q_FACE=%d, #face=%D)\n",eidx,q,fieldA,sum,LANDAU_MAX_Q_FACE,maps->num_face); 562 for (d = 0, tmp = 0; d < numindices; ++d) { 563 if (tmp!=0 && PetscAbs(tmp-1.0) > 10*PETSC_MACHINE_EPSILON) ierr = PetscPrintf(PETSC_COMM_WORLD,"%3D) %3D: ",d,indices[d]);CHKERRQ(ierr); 564 for (f = 0; f < numindices; ++f) { 565 tmp += PetscRealPart(elMat[d*numindices + f]); 566 } 567 if (tmp!=0) ierr = PetscPrintf(ctx->comm," | %22.16e\n",tmp);CHKERRQ(ierr); 568 } 569 } 570 maps->num_reduced++; 571 if (maps->num_reduced>=MAP_BF_SIZE) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "maps->num_reduced %d > %d",maps->num_reduced,MAP_BF_SIZE); 572 } 573 break; 574 } 575 } 576 // cleanup 577 ierr = DMPlexRestoreClosureIndices(ctx->plex, section, globsection, ej, PETSC_TRUE, &numindices, &indices, NULL, (PetscScalar **) &elMat);CHKERRQ(ierr); 578 if (elMat != valuesOrig) {ierr = DMRestoreWorkArray(ctx->plex, numindices*numindices, MPIU_SCALAR, &elMat);} 579 } 580 } 581 } 582 // allocate and copy point datamaps->gIdx[eidx][field][q] -- for CPU version of this code, for debugging 583 ierr = PetscMalloc(maps->num_reduced * sizeof *maps->c_maps, &maps->c_maps);CHKERRQ(ierr); 584 for (ej = 0; ej < maps->num_reduced; ++ej) { 585 for (q = 0; q < maps->num_face; ++q) { 586 maps->c_maps[ej][q].scale = pointMaps[ej][q].scale; 587 maps->c_maps[ej][q].gid = pointMaps[ej][q].gid; 588 } 589 } 590 #if defined(PETSC_HAVE_KOKKOS_KERNELS) 591 if (ctx->deviceType == LANDAU_KOKKOS) { 592 ierr = LandauKokkosCreateMatMaps(maps, pointMaps,Nf,Nq);CHKERRQ(ierr); // imples Kokkos does 593 } // else could be CUDA 594 #endif 595 #if defined(PETSC_HAVE_CUDA) 596 if (ctx->deviceType == LANDAU_CUDA) { 597 ierr = LandauCUDACreateMatMaps(maps, pointMaps,Nf,Nq);CHKERRQ(ierr); 598 } 599 #endif 600 601 ierr = PetscLogEventEnd(ctx->events[2],0,0,0,0);CHKERRQ(ierr); 602 } 603 /* clean up */ 604 if (IPf) { 605 ierr = PetscFree(IPf);CHKERRQ(ierr); 606 } 607 if (xdata) { 608 ierr = VecRestoreArrayReadAndMemType(a_X,&xdata);CHKERRQ(ierr); 609 } 610 611 PetscFunctionReturn(0); 612 } 613 614 #if defined(LANDAU_ADD_BCS) 615 static void zero_bc(PetscInt dim, PetscInt Nf, PetscInt NfAux, 616 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 617 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 618 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar uexact[]) 619 { 620 uexact[0] = 0; 621 } 622 #endif 623 624 #define MATVEC2(__a,__x,__p) {int i,j; for (i=0.; i<2; i++) {__p[i] = 0; for (j=0.; j<2; j++) __p[i] += __a[i][j]*__x[j]; }} 625 static void CircleInflate(PetscReal r1, PetscReal r2, PetscReal r0, PetscInt num_sections, PetscReal x, PetscReal y, 626 PetscReal *outX, PetscReal *outY) 627 { 628 PetscReal rr = PetscSqrtReal(x*x + y*y), outfact, efact; 629 if (rr < r1 + PETSC_SQRT_MACHINE_EPSILON) { 630 *outX = x; *outY = y; 631 } else { 632 const PetscReal xy[2] = {x,y}, sinphi=y/rr, cosphi=x/rr; 633 PetscReal cth,sth,xyprime[2],Rth[2][2],rotcos,newrr; 634 if (num_sections==2) { 635 rotcos = 0.70710678118654; 636 outfact = 1.5; efact = 2.5; 637 /* rotate normalized vector into [-pi/4,pi/4) */ 638 if (sinphi >= 0.) { /* top cell, -pi/2 */ 639 cth = 0.707106781186548; sth = -0.707106781186548; 640 } else { /* bottom cell -pi/8 */ 641 cth = 0.707106781186548; sth = .707106781186548; 642 } 643 } else if (num_sections==3) { 644 rotcos = 0.86602540378443; 645 outfact = 1.5; efact = 2.5; 646 /* rotate normalized vector into [-pi/6,pi/6) */ 647 if (sinphi >= 0.5) { /* top cell, -pi/3 */ 648 cth = 0.5; sth = -0.866025403784439; 649 } else if (sinphi >= -.5) { /* mid cell 0 */ 650 cth = 1.; sth = .0; 651 } else { /* bottom cell +pi/3 */ 652 cth = 0.5; sth = 0.866025403784439; 653 } 654 } else if (num_sections==4) { 655 rotcos = 0.9238795325112; 656 outfact = 1.5; efact = 3; 657 /* rotate normalized vector into [-pi/8,pi/8) */ 658 if (sinphi >= 0.707106781186548) { /* top cell, -3pi/8 */ 659 cth = 0.38268343236509; sth = -0.923879532511287; 660 } else if (sinphi >= 0.) { /* mid top cell -pi/8 */ 661 cth = 0.923879532511287; sth = -.38268343236509; 662 } else if (sinphi >= -0.707106781186548) { /* mid bottom cell + pi/8 */ 663 cth = 0.923879532511287; sth = 0.38268343236509; 664 } else { /* bottom cell + 3pi/8 */ 665 cth = 0.38268343236509; sth = .923879532511287; 666 } 667 } else { 668 cth = 0.; sth = 0.; rotcos = 0; efact = 0; 669 } 670 Rth[0][0] = cth; Rth[0][1] =-sth; 671 Rth[1][0] = sth; Rth[1][1] = cth; 672 MATVEC2(Rth,xy,xyprime); 673 if (num_sections==2) { 674 newrr = xyprime[0]/rotcos; 675 } else { 676 PetscReal newcosphi=xyprime[0]/rr, rin = r1, rout = rr - rin; 677 PetscReal routmax = r0*rotcos/newcosphi - rin, nroutmax = r0 - rin, routfrac = rout/routmax; 678 newrr = rin + routfrac*nroutmax; 679 } 680 *outX = cosphi*newrr; *outY = sinphi*newrr; 681 /* grade */ 682 PetscReal fact,tt,rs,re, rr = PetscSqrtReal(PetscSqr(*outX) + PetscSqr(*outY)); 683 if (rr > r2) { rs = r2; re = r0; fact = outfact;} /* outer zone */ 684 else { rs = r1; re = r2; fact = efact;} /* electron zone */ 685 tt = (rs + PetscPowReal((rr - rs)/(re - rs),fact) * (re-rs)) / rr; 686 *outX *= tt; 687 *outY *= tt; 688 } 689 } 690 691 static PetscErrorCode GeometryDMLandau(DM base, PetscInt point, PetscInt dim, const PetscReal abc[], PetscReal xyz[], void *a_ctx) 692 { 693 LandauCtx *ctx = (LandauCtx*)a_ctx; 694 PetscReal r = abc[0], z = abc[1]; 695 if (ctx->inflate) { 696 PetscReal absR, absZ; 697 absR = PetscAbs(r); 698 absZ = PetscAbs(z); 699 CircleInflate(ctx->i_radius,ctx->e_radius,ctx->radius,ctx->num_sections,absR,absZ,&absR,&absZ); 700 r = (r > 0) ? absR : -absR; 701 z = (z > 0) ? absZ : -absZ; 702 } 703 xyz[0] = r; 704 xyz[1] = z; 705 if (dim==3) xyz[2] = abc[2]; 706 707 PetscFunctionReturn(0); 708 } 709 710 static PetscErrorCode ErrorIndicator_Simple(PetscInt dim, PetscReal volume, PetscReal x[], PetscInt Nc, const PetscInt Nf[], const PetscScalar u[], const PetscScalar u_x[], PetscReal *error, void *actx) 711 { 712 PetscReal err = 0.0; 713 PetscInt f = *(PetscInt*)actx, j; 714 PetscFunctionBegin; 715 for (j = 0; j < dim; ++j) { 716 err += PetscSqr(PetscRealPart(u_x[f*dim+j])); 717 } 718 err = PetscRealPart(u[f]); /* just use rho */ 719 *error = volume * err; /* * (ctx->axisymmetric ? 2.*PETSC_PI * r : 1); */ 720 PetscFunctionReturn(0); 721 } 722 723 static PetscErrorCode LandauDMCreateVMesh(MPI_Comm comm, const PetscInt dim, const char prefix[], LandauCtx *ctx, DM *dm) 724 { 725 PetscErrorCode ierr; 726 PetscReal radius = ctx->radius; 727 size_t len; 728 char fname[128] = ""; /* we can add a file if we want */ 729 730 PetscFunctionBegin; 731 /* create DM */ 732 ierr = PetscStrlen(fname, &len);CHKERRQ(ierr); 733 if (len) { 734 PetscInt dim2; 735 ierr = DMPlexCreateFromFile(comm, fname, ctx->interpolate, dm);CHKERRQ(ierr); 736 ierr = DMGetDimension(*dm, &dim2);CHKERRQ(ierr); 737 if (LANDAU_DIM != dim2) SETERRQ2(comm, PETSC_ERR_PLIB, "dim %D != LANDAU_DIM %d",dim2,LANDAU_DIM); 738 } else { /* p4est, quads */ 739 /* Create plex mesh of Landau domain */ 740 if (!ctx->sphere) { 741 PetscInt cells[] = {2,2,2}; 742 PetscReal lo[] = {-radius,-radius,-radius}, hi[] = {radius,radius,radius}; 743 DMBoundaryType periodicity[3] = {DM_BOUNDARY_NONE, dim==2 ? DM_BOUNDARY_NONE : DM_BOUNDARY_NONE, DM_BOUNDARY_NONE}; 744 if (dim==2) { lo[0] = 0; cells[0] = 1; } 745 ierr = DMPlexCreateBoxMesh(comm, dim, PETSC_FALSE, cells, lo, hi, periodicity, PETSC_TRUE, dm);CHKERRQ(ierr); 746 ierr = DMLocalizeCoordinates(*dm);CHKERRQ(ierr); /* needed for periodic */ 747 if (dim==3) {ierr = PetscObjectSetName((PetscObject) *dm, "cube");CHKERRQ(ierr);} 748 else {ierr = PetscObjectSetName((PetscObject) *dm, "half-plane");CHKERRQ(ierr);} 749 } else if (dim==2) { 750 PetscInt numCells,cells[16][4],i,j; 751 PetscInt numVerts; 752 PetscReal inner_radius1 = ctx->i_radius, inner_radius2 = ctx->e_radius; 753 PetscReal *flatCoords = NULL; 754 PetscInt *flatCells = NULL, *pcell; 755 if (ctx->num_sections==2) { 756 #if 1 757 numCells = 5; 758 numVerts = 10; 759 int cells2[][4] = { {0,1,4,3}, 760 {1,2,5,4}, 761 {3,4,7,6}, 762 {4,5,8,7}, 763 {6,7,8,9} }; 764 for (i = 0; i < numCells; i++) for (j = 0; j < 4; j++) cells[i][j] = cells2[i][j]; 765 ierr = PetscMalloc2(numVerts * 2, &flatCoords, numCells * 4, &flatCells);CHKERRQ(ierr); 766 { 767 PetscReal (*coords)[2] = (PetscReal (*) [2]) flatCoords; 768 for (j = 0; j < numVerts-1; j++) { 769 PetscReal z, r, theta = -PETSC_PI/2 + (j%3) * PETSC_PI/2; 770 PetscReal rad = (j >= 6) ? inner_radius1 : (j >= 3) ? inner_radius2 : ctx->radius; 771 z = rad * PetscSinReal(theta); 772 coords[j][1] = z; 773 r = rad * PetscCosReal(theta); 774 coords[j][0] = r; 775 } 776 coords[numVerts-1][0] = coords[numVerts-1][1] = 0; 777 } 778 #else 779 numCells = 4; 780 numVerts = 8; 781 static int cells2[][4] = {{0,1,2,3}, 782 {4,5,1,0}, 783 {5,6,2,1}, 784 {6,7,3,2}}; 785 for (i = 0; i < numCells; i++) for (j = 0; j < 4; j++) cells[i][j] = cells2[i][j]; 786 ierr = loc2(numVerts * 2, &flatCoords, numCells * 4, &flatCells);CHKERRQ(ierr); 787 { 788 PetscReal (*coords)[2] = (PetscReal (*) [2]) flatCoords; 789 PetscInt j; 790 for (j = 0; j < 8; j++) { 791 PetscReal z, r; 792 PetscReal theta = -PETSC_PI/2 + (j%4) * PETSC_PI/3.; 793 PetscReal rad = ctx->radius * ((j < 4) ? 0.5 : 1.0); 794 z = rad * PetscSinReal(theta); 795 coords[j][1] = z; 796 r = rad * PetscCosReal(theta); 797 coords[j][0] = r; 798 } 799 } 800 #endif 801 } else if (ctx->num_sections==3) { 802 numCells = 7; 803 numVerts = 12; 804 int cells2[][4] = { {0,1,5,4}, 805 {1,2,6,5}, 806 {2,3,7,6}, 807 {4,5,9,8}, 808 {5,6,10,9}, 809 {6,7,11,10}, 810 {8,9,10,11} }; 811 for (i = 0; i < numCells; i++) for (j = 0; j < 4; j++) cells[i][j] = cells2[i][j]; 812 ierr = PetscMalloc2(numVerts * 2, &flatCoords, numCells * 4, &flatCells);CHKERRQ(ierr); 813 { 814 PetscReal (*coords)[2] = (PetscReal (*) [2]) flatCoords; 815 for (j = 0; j < numVerts; j++) { 816 PetscReal z, r, theta = -PETSC_PI/2 + (j%4) * PETSC_PI/3; 817 PetscReal rad = (j >= 8) ? inner_radius1 : (j >= 4) ? inner_radius2 : ctx->radius; 818 z = rad * PetscSinReal(theta); 819 coords[j][1] = z; 820 r = rad * PetscCosReal(theta); 821 coords[j][0] = r; 822 } 823 } 824 } else if (ctx->num_sections==4) { 825 numCells = 10; 826 numVerts = 16; 827 int cells2[][4] = { {0,1,6,5}, 828 {1,2,7,6}, 829 {2,3,8,7}, 830 {3,4,9,8}, 831 {5,6,11,10}, 832 {6,7,12,11}, 833 {7,8,13,12}, 834 {8,9,14,13}, 835 {10,11,12,15}, 836 {12,13,14,15}}; 837 for (i = 0; i < numCells; i++) for (j = 0; j < 4; j++) cells[i][j] = cells2[i][j]; 838 ierr = PetscMalloc2(numVerts * 2, &flatCoords, numCells * 4, &flatCells);CHKERRQ(ierr); 839 { 840 PetscReal (*coords)[2] = (PetscReal (*) [2]) flatCoords; 841 for (j = 0; j < numVerts-1; j++) { 842 PetscReal z, r, theta = -PETSC_PI/2 + (j%5) * PETSC_PI/4; 843 PetscReal rad = (j >= 10) ? inner_radius1 : (j >= 5) ? inner_radius2 : ctx->radius; 844 z = rad * PetscSinReal(theta); 845 coords[j][1] = z; 846 r = rad * PetscCosReal(theta); 847 coords[j][0] = r; 848 } 849 coords[numVerts-1][0] = coords[numVerts-1][1] = 0; 850 } 851 } else { 852 numCells = 0; 853 numVerts = 0; 854 } 855 for (j = 0, pcell = flatCells; j < numCells; j++, pcell += 4) { 856 pcell[0] = cells[j][0]; pcell[1] = cells[j][1]; 857 pcell[2] = cells[j][2]; pcell[3] = cells[j][3]; 858 } 859 ierr = DMPlexCreateFromCellListPetsc(comm,2,numCells,numVerts,4,ctx->interpolate,flatCells,2,flatCoords,dm);CHKERRQ(ierr); 860 ierr = PetscFree2(flatCoords,flatCells);CHKERRQ(ierr); 861 ierr = PetscObjectSetName((PetscObject) *dm, "semi-circle");CHKERRQ(ierr); 862 } else SETERRQ(ctx->comm, PETSC_ERR_PLIB, "Velocity space meshes does not support cubed sphere"); 863 } 864 ierr = PetscObjectSetOptionsPrefix((PetscObject)*dm,prefix);CHKERRQ(ierr); 865 866 ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); /* Plex refine */ 867 868 { /* p4est? */ 869 char convType[256]; 870 PetscBool flg; 871 ierr = PetscOptionsBegin(ctx->comm, prefix, "Mesh conversion options", "DMPLEX");CHKERRQ(ierr); 872 ierr = PetscOptionsFList("-dm_landau_type","Convert DMPlex to another format (should not be Plex!)","plexland.c",DMList,DMPLEX,convType,256,&flg);CHKERRQ(ierr); 873 ierr = PetscOptionsEnd(); 874 if (flg) { 875 DM dmforest; 876 ierr = DMConvert(*dm,convType,&dmforest);CHKERRQ(ierr); 877 if (dmforest) { 878 PetscBool isForest; 879 if (dmforest->prealloc_only != (*dm)->prealloc_only) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"plex->prealloc_only != dm->prealloc_only"); 880 ierr = PetscObjectSetOptionsPrefix((PetscObject)dmforest,prefix);CHKERRQ(ierr); 881 ierr = DMIsForest(dmforest,&isForest);CHKERRQ(ierr); 882 if (isForest) { 883 if (ctx->sphere && ctx->inflate) { 884 ierr = DMForestSetBaseCoordinateMapping(dmforest,GeometryDMLandau,ctx);CHKERRQ(ierr); 885 } 886 if (dmforest->prealloc_only != (*dm)->prealloc_only) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"plex->prealloc_only != dm->prealloc_only"); 887 ierr = DMDestroy(dm);CHKERRQ(ierr); 888 *dm = dmforest; 889 ctx->errorIndicator = ErrorIndicator_Simple; /* flag for Forest */ 890 } else SETERRQ(ctx->comm, PETSC_ERR_USER, "Converted to non Forest?"); 891 } else SETERRQ(ctx->comm, PETSC_ERR_USER, "Convert failed?"); 892 } 893 } 894 ierr = PetscObjectSetName((PetscObject) *dm, "Mesh");CHKERRQ(ierr); 895 PetscFunctionReturn(0); 896 } 897 898 static PetscErrorCode SetupDS(DM dm, PetscInt dim, LandauCtx *ctx) 899 { 900 PetscErrorCode ierr; 901 PetscInt ii; 902 PetscFunctionBegin; 903 for (ii=0;ii<ctx->num_species;ii++) { 904 char buf[256]; 905 if (ii==0) ierr = PetscSNPrintf(buf, 256, "e"); 906 else {ierr = PetscSNPrintf(buf, 256, "i%D", ii);CHKERRQ(ierr);} 907 /* Setup Discretization - FEM */ 908 ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) dm), dim, 1, PETSC_FALSE, NULL, PETSC_DECIDE, &ctx->fe[ii]);CHKERRQ(ierr); 909 ierr = PetscObjectSetName((PetscObject) ctx->fe[ii], buf);CHKERRQ(ierr); 910 ierr = DMSetField(dm, ii, NULL, (PetscObject) ctx->fe[ii]);CHKERRQ(ierr); 911 } 912 ierr = DMCreateDS(dm);CHKERRQ(ierr); 913 if (1) { 914 PetscInt ii; 915 PetscSection section; 916 ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 917 for (ii=0;ii<ctx->num_species;ii++) { 918 char buf[256]; 919 if (ii==0) ierr = PetscSNPrintf(buf, 256, "se"); 920 else ierr = PetscSNPrintf(buf, 256, "si%D", ii); 921 ierr = PetscSectionSetComponentName(section, ii, 0, buf);CHKERRQ(ierr); 922 } 923 } 924 PetscFunctionReturn(0); 925 } 926 927 /* Define a Maxwellian function for testing out the operator. */ 928 929 /* Using cartesian velocity space coordinates, the particle */ 930 /* density, [1/m^3], is defined according to */ 931 932 /* $$ n=\int_{R^3} dv^3 \left(\frac{m}{2\pi T}\right)^{3/2}\exp [- mv^2/(2T)] $$ */ 933 934 /* Using some constant, c, we normalize the velocity vector into a */ 935 /* dimensionless variable according to v=c*x. Thus the density, $n$, becomes */ 936 937 /* $$ n=\int_{R^3} dx^3 \left(\frac{mc^2}{2\pi T}\right)^{3/2}\exp [- mc^2/(2T)*x^2] $$ */ 938 939 /* Defining $\theta=2T/mc^2$, we thus find that the probability density */ 940 /* for finding the particle within the interval in a box dx^3 around x is */ 941 942 /* f(x;\theta)=\left(\frac{1}{\pi\theta}\right)^{3/2} \exp [ -x^2/\theta ] */ 943 944 typedef struct { 945 LandauCtx *ctx; 946 PetscReal kT_m; 947 PetscReal n; 948 PetscReal shift; 949 } MaxwellianCtx; 950 951 static PetscErrorCode maxwellian(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf_dummy, PetscScalar *u, void *actx) 952 { 953 MaxwellianCtx *mctx = (MaxwellianCtx*)actx; 954 LandauCtx *ctx = mctx->ctx; 955 PetscInt i; 956 PetscReal v2 = 0, theta = 2*mctx->kT_m/(ctx->v_0*ctx->v_0); /* theta = 2kT/mc^2 */ 957 PetscFunctionBegin; 958 /* compute the exponents, v^2 */ 959 for (i = 0; i < dim; ++i) v2 += x[i]*x[i]; 960 /* evaluate the Maxwellian */ 961 u[0] = mctx->n*PetscPowReal(PETSC_PI*theta,-1.5)*(PetscExpReal(-v2/theta)); 962 if (mctx->shift!=0.) { 963 v2 = 0; 964 for (i = 0; i < dim-1; ++i) v2 += x[i]*x[i]; 965 v2 += (x[dim-1]-mctx->shift)*(x[dim-1]-mctx->shift); 966 /* evaluate the shifted Maxwellian */ 967 u[0] += mctx->n*PetscPowReal(PETSC_PI*theta,-1.5)*(PetscExpReal(-v2/theta)); 968 } 969 PetscFunctionReturn(0); 970 } 971 972 /*@ 973 LandauAddMaxwellians - Add a Maxwellian distribution to a state 974 975 Collective on X 976 977 Input Parameters: 978 . dm - The mesh 979 + time - Current time 980 - temps - Temperatures of each species 981 . ns - Number density of each species 982 + actx - Landau context 983 984 Output Parameter: 985 . X - The state 986 987 Level: beginner 988 989 .keywords: mesh 990 .seealso: LandauCreateVelocitySpace() 991 @*/ 992 PetscErrorCode LandauAddMaxwellians(DM dm, Vec X, PetscReal time, PetscReal temps[], PetscReal ns[], void *actx) 993 { 994 LandauCtx *ctx = (LandauCtx*)actx; 995 PetscErrorCode (*initu[LANDAU_MAX_SPECIES])(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar [], void *); 996 PetscErrorCode ierr,ii; 997 PetscInt dim; 998 MaxwellianCtx *mctxs[LANDAU_MAX_SPECIES], data[LANDAU_MAX_SPECIES]; 999 1000 PetscFunctionBegin; 1001 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1002 if (!ctx) { ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr); } 1003 for (ii=0;ii<ctx->num_species;ii++) { 1004 mctxs[ii] = &data[ii]; 1005 data[ii].ctx = ctx; 1006 data[ii].kT_m = ctx->k*temps[ii]/ctx->masses[ii]; /* kT/m */ 1007 data[ii].n = ns[ii]; 1008 initu[ii] = maxwellian; 1009 data[ii].shift = 0; 1010 } 1011 data[0].shift = ctx->electronShift; 1012 /* need to make ADD_ALL_VALUES work - TODO */ 1013 ierr = DMProjectFunction(dm, time, initu, (void**)mctxs, INSERT_ALL_VALUES, X);CHKERRQ(ierr); 1014 PetscFunctionReturn(0); 1015 } 1016 1017 /* 1018 LandauSetInitialCondition - Addes Maxwellians with context 1019 1020 Collective on X 1021 1022 Input Parameters: 1023 . dm - The mesh 1024 + actx - Landau context with T and n 1025 1026 Output Parameter: 1027 . X - The state 1028 1029 Level: beginner 1030 1031 .keywords: mesh 1032 .seealso: LandauCreateVelocitySpace(), LandauAddMaxwellians() 1033 */ 1034 static PetscErrorCode LandauSetInitialCondition(DM dm, Vec X, void *actx) 1035 { 1036 LandauCtx *ctx = (LandauCtx*)actx; 1037 PetscErrorCode ierr; 1038 PetscFunctionBegin; 1039 if (!ctx) { ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr); } 1040 ierr = VecZeroEntries(X);CHKERRQ(ierr); 1041 ierr = LandauAddMaxwellians(dm, X, 0.0, ctx->thermal_temps, ctx->n, ctx);CHKERRQ(ierr); 1042 PetscFunctionReturn(0); 1043 } 1044 1045 static PetscErrorCode adaptToleranceFEM(PetscFE fem, Vec sol, PetscReal refineTol[], PetscReal coarsenTol[], PetscInt type, LandauCtx *ctx, DM *newDM) 1046 { 1047 DM dm, plex, adaptedDM = NULL; 1048 PetscDS prob; 1049 PetscBool isForest; 1050 PetscQuadrature quad; 1051 PetscInt Nq, *Nb, cStart, cEnd, c, dim, qj, k; 1052 DMLabel adaptLabel = NULL; 1053 PetscErrorCode ierr; 1054 1055 PetscFunctionBegin; 1056 ierr = VecGetDM(sol, &dm);CHKERRQ(ierr); 1057 ierr = DMCreateDS(dm);CHKERRQ(ierr); 1058 ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1059 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1060 ierr = DMIsForest(dm, &isForest);CHKERRQ(ierr); 1061 ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 1062 ierr = DMPlexGetHeightStratum(plex,0,&cStart,&cEnd);CHKERRQ(ierr); 1063 ierr = DMLabelCreate(PETSC_COMM_SELF,"adapt",&adaptLabel);CHKERRQ(ierr); 1064 ierr = PetscFEGetQuadrature(fem, &quad);CHKERRQ(ierr); 1065 ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1066 if (Nq >LANDAU_MAX_NQ) SETERRQ2(ctx->comm,PETSC_ERR_ARG_WRONG,"Order too high. Nq = %D > LANDAU_MAX_NQ (%D)",Nq,LANDAU_MAX_NQ); 1067 ierr = PetscDSGetDimensions(prob, &Nb);CHKERRQ(ierr); 1068 if (type==4) { 1069 for (c = cStart; c < cEnd; c++) { 1070 ierr = DMLabelSetValue(adaptLabel, c, DM_ADAPT_REFINE);CHKERRQ(ierr); 1071 } 1072 ierr = PetscInfo1(sol, "Phase:%s: Uniform refinement\n","adaptToleranceFEM");CHKERRQ(ierr); 1073 } else if (type==2) { 1074 PetscInt rCellIdx[8], eCellIdx[64], iCellIdx[64], eMaxIdx = -1, iMaxIdx = -1, nr = 0, nrmax = (dim==3) ? 8 : 2; 1075 PetscReal minRad = PETSC_INFINITY, r, eMinRad = PETSC_INFINITY, iMinRad = PETSC_INFINITY; 1076 for (c = 0; c < 64; c++) { eCellIdx[c] = iCellIdx[c] = -1; } 1077 for (c = cStart; c < cEnd; c++) { 1078 PetscReal tt, v0[LANDAU_MAX_NQ*3], detJ[LANDAU_MAX_NQ]; 1079 ierr = DMPlexComputeCellGeometryFEM(plex, c, quad, v0, NULL, NULL, detJ);CHKERRQ(ierr); 1080 for (qj = 0; qj < Nq; ++qj) { 1081 tt = PetscSqr(v0[dim*qj+0]) + PetscSqr(v0[dim*qj+1]) + PetscSqr(((dim==3) ? v0[dim*qj+2] : 0)); 1082 r = PetscSqrtReal(tt); 1083 if (r < minRad - PETSC_SQRT_MACHINE_EPSILON*10.) { 1084 minRad = r; 1085 nr = 0; 1086 rCellIdx[nr++]= c; 1087 ierr = PetscInfo4(sol, "\t\tPhase: adaptToleranceFEM Found first inner r=%e, cell %D, qp %D/%D\n", r, c, qj+1, Nq);CHKERRQ(ierr); 1088 } else if ((r-minRad) < PETSC_SQRT_MACHINE_EPSILON*100. && nr < nrmax) { 1089 for (k=0;k<nr;k++) if (c == rCellIdx[k]) break; 1090 if (k==nr) { 1091 rCellIdx[nr++]= c; 1092 ierr = PetscInfo5(sol, "\t\t\tPhase: adaptToleranceFEM Found another inner r=%e, cell %D, qp %D/%D, d=%e\n", r, c, qj+1, Nq, r-minRad);CHKERRQ(ierr); 1093 } 1094 } 1095 if (ctx->sphere) { 1096 if ((tt=r-ctx->e_radius) > 0) { 1097 PetscInfo2(sol, "\t\t\t %D cell r=%g\n",c,tt); 1098 if (tt < eMinRad - PETSC_SQRT_MACHINE_EPSILON*100.) { 1099 eMinRad = tt; 1100 eMaxIdx = 0; 1101 eCellIdx[eMaxIdx++] = c; 1102 } else if (eMaxIdx > 0 && (tt-eMinRad) <= PETSC_SQRT_MACHINE_EPSILON && c != eCellIdx[eMaxIdx-1]) { 1103 eCellIdx[eMaxIdx++] = c; 1104 } 1105 } 1106 if ((tt=r-ctx->i_radius) > 0) { 1107 if (tt < iMinRad - 1.e-5) { 1108 iMinRad = tt; 1109 iMaxIdx = 0; 1110 iCellIdx[iMaxIdx++] = c; 1111 } else if (iMaxIdx > 0 && (tt-iMinRad) <= PETSC_SQRT_MACHINE_EPSILON && c != iCellIdx[iMaxIdx-1]) { 1112 iCellIdx[iMaxIdx++] = c; 1113 } 1114 } 1115 } 1116 } 1117 } 1118 for (k=0;k<nr;k++) { 1119 ierr = DMLabelSetValue(adaptLabel, rCellIdx[k], DM_ADAPT_REFINE);CHKERRQ(ierr); 1120 } 1121 if (ctx->sphere) { 1122 for (c = 0; c < eMaxIdx; c++) { 1123 ierr = DMLabelSetValue(adaptLabel, eCellIdx[c], DM_ADAPT_REFINE);CHKERRQ(ierr); 1124 ierr = PetscInfo3(sol, "\t\tPhase:%s: refine sphere e cell %D r=%g\n","adaptToleranceFEM",eCellIdx[c],eMinRad); 1125 } 1126 for (c = 0; c < iMaxIdx; c++) { 1127 ierr = DMLabelSetValue(adaptLabel, iCellIdx[c], DM_ADAPT_REFINE);CHKERRQ(ierr); 1128 ierr = PetscInfo3(sol, "\t\tPhase:%s: refine sphere i cell %D r=%g\n","adaptToleranceFEM",iCellIdx[c],iMinRad); 1129 } 1130 } 1131 ierr = PetscInfo4(sol, "Phase:%s: Adaptive refine origin cells %D,%D r=%g\n","adaptToleranceFEM",rCellIdx[0],rCellIdx[1],minRad); 1132 } else if (type==0 || type==1 || type==3) { /* refine along r=0 axis */ 1133 PetscScalar *coef = NULL; 1134 Vec coords; 1135 PetscInt csize,Nv,d,nz; 1136 DM cdm; 1137 PetscSection cs; 1138 ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr); 1139 ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 1140 ierr = DMGetLocalSection(cdm, &cs);CHKERRQ(ierr); 1141 for (c = cStart; c < cEnd; c++) { 1142 PetscInt doit = 0, outside = 0; 1143 ierr = DMPlexVecGetClosure(cdm, cs, coords, c, &csize, &coef);CHKERRQ(ierr); 1144 Nv = csize/dim; 1145 for (nz = d = 0; d < Nv; d++) { 1146 PetscReal z = PetscRealPart(coef[d*dim + (dim-1)]), x = PetscSqr(PetscRealPart(coef[d*dim + 0])) + ((dim==3) ? PetscSqr(PetscRealPart(coef[d*dim + 1])) : 0); 1147 x = PetscSqrtReal(x); 1148 if (x < PETSC_MACHINE_EPSILON*10. && PetscAbs(z)<PETSC_MACHINE_EPSILON*10.) doit = 1; /* refine origin */ 1149 else if (type==0 && (z < -PETSC_MACHINE_EPSILON*10. || z > ctx->re_radius+PETSC_MACHINE_EPSILON*10.)) outside++; /* first pass don't refine bottom */ 1150 else if (type==1 && (z > ctx->vperp0_radius1 || z < -ctx->vperp0_radius1)) outside++; /* don't refine outside electron refine radius */ 1151 else if (type==3 && (z > ctx->vperp0_radius2 || z < -ctx->vperp0_radius2)) outside++; /* don't refine outside ion refine radius */ 1152 if (x < PETSC_MACHINE_EPSILON*10.) nz++; 1153 } 1154 ierr = DMPlexVecRestoreClosure(cdm, cs, coords, c, &csize, &coef);CHKERRQ(ierr); 1155 if (doit || (outside<Nv && nz)) { 1156 ierr = DMLabelSetValue(adaptLabel, c, DM_ADAPT_REFINE);CHKERRQ(ierr); 1157 } 1158 } 1159 ierr = PetscInfo1(sol, "Phase:%s: RE refinement\n","adaptToleranceFEM"); 1160 } 1161 ierr = DMDestroy(&plex);CHKERRQ(ierr); 1162 ierr = DMAdaptLabel(dm, adaptLabel, &adaptedDM);CHKERRQ(ierr); 1163 ierr = DMLabelDestroy(&adaptLabel);CHKERRQ(ierr); 1164 *newDM = adaptedDM; 1165 if (adaptedDM) { 1166 if (isForest) { 1167 ierr = DMForestSetAdaptivityForest(adaptedDM,NULL);CHKERRQ(ierr); 1168 } 1169 ierr = DMConvert(adaptedDM, DMPLEX, &plex);CHKERRQ(ierr); 1170 ierr = DMPlexGetHeightStratum(plex,0,&cStart,&cEnd);CHKERRQ(ierr); 1171 ierr = PetscInfo2(sol, "\tPhase: adaptToleranceFEM: %D cells, %d total quadrature points\n",cEnd-cStart,Nq*(cEnd-cStart));CHKERRQ(ierr); 1172 ierr = DMDestroy(&plex);CHKERRQ(ierr); 1173 } 1174 PetscFunctionReturn(0); 1175 } 1176 1177 static PetscErrorCode adapt(DM *dm, LandauCtx *ctx, Vec *uu) 1178 { 1179 PetscErrorCode ierr; 1180 PetscInt type, limits[5] = {ctx->numRERefine,ctx->nZRefine1,ctx->maxRefIts,ctx->nZRefine2,ctx->postAMRRefine}; 1181 PetscInt adaptIter; 1182 1183 PetscFunctionBegin; 1184 for (type=0;type<5;type++) { 1185 for (adaptIter = 0; adaptIter<limits[type];adaptIter++) { 1186 DM dmNew = NULL; 1187 ierr = adaptToleranceFEM(ctx->fe[0], *uu, ctx->refineTol, ctx->coarsenTol, type, ctx, &dmNew);CHKERRQ(ierr); 1188 if (!dmNew) SETERRQ(ctx->comm,PETSC_ERR_ARG_WRONG,"should not happen"); 1189 else { 1190 ierr = DMDestroy(dm);CHKERRQ(ierr); 1191 ierr = VecDestroy(uu);CHKERRQ(ierr); 1192 ierr = DMCreateGlobalVector(dmNew,uu);CHKERRQ(ierr); 1193 ierr = PetscObjectSetName((PetscObject) *uu, "u");CHKERRQ(ierr); 1194 ierr = LandauSetInitialCondition(dmNew, *uu, ctx);CHKERRQ(ierr); 1195 *dm = dmNew; 1196 } 1197 } 1198 } 1199 PetscFunctionReturn(0); 1200 } 1201 1202 static PetscErrorCode ProcessOptions(LandauCtx *ctx, const char prefix[]) 1203 { 1204 PetscErrorCode ierr; 1205 PetscBool flg, sph_flg; 1206 PetscInt ii,nt,nm,nc; 1207 DM dummy; 1208 1209 PetscFunctionBegin; 1210 ierr = DMCreate(ctx->comm,&dummy);CHKERRQ(ierr); 1211 /* get options - initialize context */ 1212 ctx->verbose = 1; 1213 ctx->interpolate = PETSC_TRUE; 1214 ctx->gpu_assembly = PETSC_TRUE; 1215 ctx->sphere = PETSC_FALSE; 1216 ctx->inflate = PETSC_FALSE; 1217 ctx->electronShift = 0; 1218 ctx->errorIndicator = NULL; 1219 ctx->radius = 5.; /* electron thermal radius (velocity) */ 1220 ctx->re_radius = 0.; 1221 ctx->vperp0_radius1 = 0; 1222 ctx->vperp0_radius2 = 0; 1223 ctx->e_radius = .1; 1224 ctx->i_radius = .01; 1225 ctx->maxRefIts = 5; 1226 ctx->postAMRRefine = 0; 1227 ctx->nZRefine1 = 0; 1228 ctx->nZRefine2 = 0; 1229 ctx->numRERefine = 0; 1230 ctx->aux_bool = PETSC_FALSE; 1231 ctx->num_sections = 3; /* 2, 3 or 4 */ 1232 /* species - [0] electrons, [1] one ion species eg, duetarium, [2] heavy impurity ion, ... */ 1233 ctx->charges[0] = -1; /* electron charge (MKS) */ 1234 ctx->masses[0] = 1/1835.5; /* temporary value in proton mass */ 1235 ctx->n[0] = 1; 1236 ctx->thermal_temps[0] = 1; 1237 /* constants, etc. */ 1238 ctx->epsilon0 = 8.8542e-12; /* permittivity of free space (MKS) F/m */ 1239 ctx->k = 1.38064852e-23; /* Boltzmann constant (MKS) J/K */ 1240 ctx->lnLam = 10; /* cross section ratio large - small angle collisions */ 1241 ctx->n_0 = 1.e20; /* typical plasma n, but could set it to 1 */ 1242 ctx->Ez = 0; 1243 ctx->v_0 = 1; /* in electron thermal velocity */ 1244 ctx->subThreadBlockSize = 1; /* for device and maybe OMP */ 1245 ctx->numConcurrency = 1; /* for device */ 1246 ctx->SData_d = NULL; /* for device */ 1247 ctx->times[0] = 0; 1248 ctx->init = PETSC_FALSE; // doit first time 1249 ctx->use_matrix_mass = PETSC_FALSE; /* fast but slightly fragile */ 1250 ctx->plex = NULL; /* cache as expensive to Convert */ 1251 ierr = PetscOptionsBegin(ctx->comm, prefix, "Options for Fokker-Plank-Landau collision operator", "none");CHKERRQ(ierr); 1252 { 1253 char opstring[256]; 1254 #if defined(PETSC_HAVE_KOKKOS) 1255 ctx->deviceType = LANDAU_KOKKOS; 1256 ierr = PetscStrcpy(opstring,"kokkos");CHKERRQ(ierr); 1257 #if defined(PETSC_HAVE_CUDA) 1258 ctx->subThreadBlockSize = 16; 1259 #endif 1260 #elif defined(PETSC_HAVE_CUDA) 1261 ctx->deviceType = LANDAU_CUDA; 1262 ierr = PetscStrcpy(opstring,"cuda");CHKERRQ(ierr); 1263 #else 1264 ctx->deviceType = LANDAU_CPU; 1265 ierr = PetscStrcpy(opstring,"cpu");CHKERRQ(ierr); 1266 ctx->subThreadBlockSize = 0; 1267 #endif 1268 ierr = PetscOptionsString("-dm_landau_device_type","Use kernels on 'cpu', 'cuda', or 'kokkos'","plexland.c",opstring,opstring,256,NULL);CHKERRQ(ierr); 1269 ierr = PetscStrcmp("cpu",opstring,&flg);CHKERRQ(ierr); 1270 if (flg) { 1271 ctx->deviceType = LANDAU_CPU; 1272 ctx->subThreadBlockSize = 0; 1273 } else { 1274 ierr = PetscStrcmp("cuda",opstring,&flg);CHKERRQ(ierr); 1275 if (flg) { 1276 ctx->deviceType = LANDAU_CUDA; 1277 ctx->subThreadBlockSize = 0; 1278 } else { 1279 ierr = PetscStrcmp("kokkos",opstring,&flg);CHKERRQ(ierr); 1280 if (flg) ctx->deviceType = LANDAU_KOKKOS; 1281 else SETERRQ1(ctx->comm,PETSC_ERR_ARG_WRONG,"-dm_landau_device_type %s",opstring); 1282 } 1283 } 1284 } 1285 ierr = PetscOptionsBool("-dm_landau_gpu_assembly", "Assemble Jacobian on GPU", "plexland.c", ctx->gpu_assembly, &ctx->gpu_assembly, NULL);CHKERRQ(ierr); 1286 ierr = PetscOptionsReal("-dm_landau_electron_shift","Shift in thermal velocity of electrons","none",ctx->electronShift,&ctx->electronShift, NULL);CHKERRQ(ierr); 1287 ierr = PetscOptionsBool("-dm_landau_sphere", "use sphere/semi-circle domain instead of rectangle", "plexland.c", ctx->sphere, &ctx->sphere, &sph_flg);CHKERRQ(ierr); 1288 ierr = PetscOptionsBool("-dm_landau_inflate", "With sphere, inflate for curved edges (no AMR)", "plexland.c", ctx->inflate, &ctx->inflate, NULL);CHKERRQ(ierr); 1289 ierr = PetscOptionsInt("-dm_landau_amr_re_levels", "Number of levels to refine along v_perp=0, z>0", "plexland.c", ctx->numRERefine, &ctx->numRERefine, NULL);CHKERRQ(ierr); 1290 ierr = PetscOptionsInt("-dm_landau_amr_z_refine1", "Number of levels to refine along v_perp=0", "plexland.c", ctx->nZRefine1, &ctx->nZRefine1, NULL);CHKERRQ(ierr); 1291 ierr = PetscOptionsInt("-dm_landau_amr_z_refine2", "Number of levels to refine along v_perp=0", "plexland.c", ctx->nZRefine2, &ctx->nZRefine2, NULL);CHKERRQ(ierr); 1292 ierr = PetscOptionsInt("-dm_landau_amr_levels_max", "Number of AMR levels of refinement around origin after r=0 refinements", "plexland.c", ctx->maxRefIts, &ctx->maxRefIts, NULL);CHKERRQ(ierr); 1293 ierr = PetscOptionsInt("-dm_landau_amr_post_refine", "Number of levels to uniformly refine after AMR", "plexland.c", ctx->postAMRRefine, &ctx->postAMRRefine, NULL);CHKERRQ(ierr); 1294 ierr = PetscOptionsInt("-dm_landau_verbose", "", "plexland.c", ctx->verbose, &ctx->verbose, NULL);CHKERRQ(ierr); 1295 ierr = PetscOptionsReal("-dm_landau_re_radius","velocity range to refine on positive (z>0) r=0 axis for runaways","plexland.c",ctx->re_radius,&ctx->re_radius, &flg);CHKERRQ(ierr); 1296 ierr = PetscOptionsReal("-dm_landau_z_radius1","velocity range to refine r=0 axis (for electrons)","plexland.c",ctx->vperp0_radius1,&ctx->vperp0_radius1, &flg);CHKERRQ(ierr); 1297 ierr = PetscOptionsReal("-dm_landau_z_radius2","velocity range to refine r=0 axis (for ions) after origin AMR","plexland.c",ctx->vperp0_radius2,&ctx->vperp0_radius2, &flg);CHKERRQ(ierr); 1298 ierr = PetscOptionsReal("-dm_landau_Ez","Initial parallel electric field in unites of Conner-Hastie criticle field","plexland.c",ctx->Ez,&ctx->Ez, NULL);CHKERRQ(ierr); 1299 ierr = PetscOptionsReal("-dm_landau_n_0","Normalization constant for number density","plexland.c",ctx->n_0,&ctx->n_0, NULL);CHKERRQ(ierr); 1300 ierr = PetscOptionsReal("-dm_landau_ln_lambda","Cross section parameter","plexland.c",ctx->lnLam,&ctx->lnLam, NULL);CHKERRQ(ierr); 1301 ierr = PetscOptionsInt("-dm_landau_num_sections", "Number of tangential section in (2D) grid, 2, 3, of 4", "plexland.c", ctx->num_sections, &ctx->num_sections, NULL);CHKERRQ(ierr); 1302 ierr = PetscOptionsInt("-dm_landau_num_thread_teams", "The number of other concurrent runs to make room for", "plexland.c", ctx->numConcurrency, &ctx->numConcurrency, NULL);CHKERRQ(ierr); 1303 ierr = PetscOptionsBool("-dm_landau_use_mataxpy_mass", "Use fast but slightly fragile MATAXPY to add mass term", "plexland.c", ctx->use_matrix_mass, &ctx->use_matrix_mass, NULL);CHKERRQ(ierr); 1304 1305 /* get num species with tempurature*/ 1306 { 1307 PetscReal arr[100]; 1308 nt = 100; 1309 ierr = PetscOptionsRealArray("-dm_landau_thermal_temps", "Temperature of each species [e,i_0,i_1,...] in keV", "plexland.c", arr, &nt, &flg);CHKERRQ(ierr); 1310 if (flg && nt > LANDAU_MAX_SPECIES) SETERRQ2(ctx->comm,PETSC_ERR_ARG_WRONG,"-thermal_temps ,t1,t2,.. number of species %D > MAX %D",nt,LANDAU_MAX_SPECIES); 1311 } 1312 nt = LANDAU_MAX_SPECIES; 1313 for (ii=1;ii<LANDAU_MAX_SPECIES;ii++) { 1314 ctx->thermal_temps[ii] = 1.; 1315 ctx->charges[ii] = 1; 1316 ctx->masses[ii] = 1; 1317 ctx->n[ii] = (ii==1) ? 1 : 0; 1318 } 1319 ierr = PetscOptionsRealArray("-dm_landau_thermal_temps", "Temperature of each species [e,i_0,i_1,...] in keV (must be set to set number of species)", "plexland.c", ctx->thermal_temps, &nt, &flg);CHKERRQ(ierr); 1320 if (flg) { 1321 PetscInfo1(dummy, "num_species set to number of thermal temps provided (%D)\n",nt); 1322 ctx->num_species = nt; 1323 } else SETERRQ(ctx->comm,PETSC_ERR_ARG_WRONG,"-dm_landau_thermal_temps ,t1,t2,.. must be provided to set the number of species"); 1324 for (ii=0;ii<ctx->num_species;ii++) ctx->thermal_temps[ii] *= 1.1604525e7; /* convert to Kelvin */ 1325 nm = LANDAU_MAX_SPECIES-1; 1326 ierr = PetscOptionsRealArray("-dm_landau_ion_masses", "Mass of each species in units of proton mass [i_0=2,i_1=40...]", "plexland.c", &ctx->masses[1], &nm, &flg);CHKERRQ(ierr); 1327 if (flg && nm != ctx->num_species-1) { 1328 SETERRQ2(ctx->comm,PETSC_ERR_ARG_WRONG,"num ion masses %D != num species %D",nm,ctx->num_species-1); 1329 } 1330 nm = LANDAU_MAX_SPECIES; 1331 ierr = PetscOptionsRealArray("-dm_landau_n", "Normalized (by -n_0) number density of each species", "plexland.c", ctx->n, &nm, &flg);CHKERRQ(ierr); 1332 if (flg && nm != ctx->num_species) SETERRQ2(ctx->comm,PETSC_ERR_ARG_WRONG,"wrong num n: %D != num species %D",nm,ctx->num_species); 1333 ctx->n_0 *= ctx->n[0]; /* normalized number density */ 1334 for (ii=1;ii<ctx->num_species;ii++) ctx->n[ii] = ctx->n[ii]/ctx->n[0]; 1335 ctx->n[0] = 1; 1336 for (ii=0;ii<LANDAU_MAX_SPECIES;ii++) ctx->masses[ii] *= 1.6720e-27; /* scale by proton mass kg */ 1337 ctx->masses[0] = 9.10938356e-31; /* electron mass kg (should be about right already) */ 1338 ctx->m_0 = ctx->masses[0]; /* arbitrary reference mass, electrons */ 1339 ierr = PetscOptionsReal("-dm_landau_v_0","Velocity to normalize with in units of initial electrons thermal velocity (not recommended to change default)","plexland.c",ctx->v_0,&ctx->v_0, NULL);CHKERRQ(ierr); 1340 ctx->v_0 *= PetscSqrtReal(ctx->k*ctx->thermal_temps[0]/(ctx->masses[0])); /* electron mean velocity in 1D (need 3D form in computing T from FE integral) */ 1341 nc = LANDAU_MAX_SPECIES-1; 1342 ierr = PetscOptionsRealArray("-dm_landau_ion_charges", "Charge of each species in units of proton charge [i_0=2,i_1=18,...]", "plexland.c", &ctx->charges[1], &nc, &flg);CHKERRQ(ierr); 1343 if (flg && nc != ctx->num_species-1) SETERRQ2(ctx->comm,PETSC_ERR_ARG_WRONG,"num charges %D != num species %D",nc,ctx->num_species-1); 1344 for (ii=0;ii<LANDAU_MAX_SPECIES;ii++) ctx->charges[ii] *= 1.6022e-19; /* electron/proton charge (MKS) */ 1345 ctx->t_0 = 8*PETSC_PI*PetscSqr(ctx->epsilon0*ctx->m_0/PetscSqr(ctx->charges[0]))/ctx->lnLam/ctx->n_0*PetscPowReal(ctx->v_0,3); /* note, this t_0 makes nu[0,0]=1 */ 1346 /* geometry */ 1347 for (ii=0;ii<ctx->num_species;ii++) ctx->refineTol[ii] = PETSC_MAX_REAL; 1348 for (ii=0;ii<ctx->num_species;ii++) ctx->coarsenTol[ii] = 0.; 1349 ii = LANDAU_MAX_SPECIES; 1350 ierr = PetscOptionsRealArray("-dm_landau_refine_tol","tolerance for refining cells in AMR","plexland.c",ctx->refineTol, &ii, &flg);CHKERRQ(ierr); 1351 if (flg && ii != ctx->num_species) ierr = PetscInfo2(dummy, "Phase: Warning, #refine_tol %D != num_species %D\n",ii,ctx->num_species);CHKERRQ(ierr); 1352 ii = LANDAU_MAX_SPECIES; 1353 ierr = PetscOptionsRealArray("-dm_landau_coarsen_tol","tolerance for coarsening cells in AMR","plexland.c",ctx->coarsenTol, &ii, &flg);CHKERRQ(ierr); 1354 if (flg && ii != ctx->num_species) ierr = PetscInfo2(dummy, "Phase: Warning, #coarsen_tol %D != num_species %D\n",ii,ctx->num_species);CHKERRQ(ierr); 1355 ierr = PetscOptionsReal("-dm_landau_domain_radius","Phase space size in units of electron thermal velocity","plexland.c",ctx->radius,&ctx->radius, &flg);CHKERRQ(ierr); 1356 if (flg && ctx->radius <= 0) { /* negative is ratio of c */ 1357 if (ctx->radius == 0) ctx->radius = 0.75; 1358 else ctx->radius = -ctx->radius; 1359 ctx->radius = ctx->radius*299792458.0/ctx->v_0; 1360 ierr = PetscInfo1(dummy, "Change domain radius to %e\n",ctx->radius);CHKERRQ(ierr); 1361 } 1362 ierr = PetscOptionsReal("-dm_landau_i_radius","Ion thermal velocity, used for circular meshes","plexland.c",ctx->i_radius,&ctx->i_radius, &flg);CHKERRQ(ierr); 1363 if (flg && !sph_flg) ctx->sphere = PETSC_TRUE; /* you gave me an ion radius but did not set sphere, user error really */ 1364 if (!flg) { 1365 ctx->i_radius = 1.5*PetscSqrtReal(8*ctx->k*ctx->thermal_temps[1]/ctx->masses[1]/PETSC_PI)/ctx->v_0; /* normalized radius with thermal velocity of first ion */ 1366 } 1367 ierr = PetscOptionsReal("-dm_landau_e_radius","Electron thermal velocity, used for circular meshes","plexland.c",ctx->e_radius,&ctx->e_radius, &flg);CHKERRQ(ierr); 1368 if (flg && !sph_flg) ctx->sphere = PETSC_TRUE; /* you gave me an e radius but did not set sphere, user error really */ 1369 if (!flg) { 1370 ctx->e_radius = 1.5*PetscSqrtReal(8*ctx->k*ctx->thermal_temps[0]/ctx->masses[0]/PETSC_PI)/ctx->v_0; /* normalized radius with thermal velocity of electrons */ 1371 } 1372 if (ctx->sphere && (ctx->e_radius <= ctx->i_radius || ctx->radius <= ctx->e_radius)) SETERRQ3(ctx->comm,PETSC_ERR_ARG_WRONG,"bad radii: %g < %g < %g",ctx->i_radius,ctx->e_radius,ctx->radius); 1373 ierr = PetscOptionsInt("-dm_landau_sub_thread_block_size", "Number of threads in Kokkos integration point subblock", "plexland.c", ctx->subThreadBlockSize, &ctx->subThreadBlockSize, NULL);CHKERRQ(ierr); 1374 ierr = PetscOptionsEnd();CHKERRQ(ierr); 1375 for (ii=ctx->num_species;ii<LANDAU_MAX_SPECIES;ii++) ctx->masses[ii] = ctx->thermal_temps[ii] = ctx->charges[ii] = 0; 1376 if (ctx->verbose > 0) { 1377 ierr = PetscPrintf(ctx->comm, "masses: e=%10.3e; ions in proton mass units: %10.3e %10.3e ...\n",ctx->masses[0],ctx->masses[1]/1.6720e-27,ctx->num_species>2 ? ctx->masses[2]/1.6720e-27 : 0);CHKERRQ(ierr); 1378 ierr = PetscPrintf(ctx->comm, "charges: e=%10.3e; charges in elementary units: %10.3e %10.3e\n", ctx->charges[0],-ctx->charges[1]/ctx->charges[0],ctx->num_species>2 ? -ctx->charges[2]/ctx->charges[0] : 0);CHKERRQ(ierr); 1379 ierr = PetscPrintf(ctx->comm, "thermal T (K): e=%10.3e i=%10.3e imp=%10.3e. v_0=%10.3e n_0=%10.3e t_0=%10.3e domain=%10.3e\n",ctx->thermal_temps[0],ctx->thermal_temps[1],ctx->num_species>2 ? ctx->thermal_temps[2] : 0,ctx->v_0,ctx->n_0,ctx->t_0,ctx->radius);CHKERRQ(ierr); 1380 } 1381 ierr = DMDestroy(&dummy);CHKERRQ(ierr); 1382 { 1383 PetscMPIInt rank; 1384 ierr = MPI_Comm_rank(ctx->comm, &rank);CHKERRMPI(ierr); 1385 /* PetscLogStage setup_stage; */ 1386 ierr = PetscLogEventRegister("Landau Operator", DM_CLASSID, &ctx->events[11]);CHKERRQ(ierr); /* 11 */ 1387 ierr = PetscLogEventRegister("Landau Jacobian", DM_CLASSID, &ctx->events[0]);CHKERRQ(ierr); /* 0 */ 1388 ierr = PetscLogEventRegister("Landau Mass", DM_CLASSID, &ctx->events[9]);CHKERRQ(ierr); /* 9 */ 1389 ierr = PetscLogEventRegister(" Preamble", DM_CLASSID, &ctx->events[10]);CHKERRQ(ierr); /* 10 */ 1390 ierr = PetscLogEventRegister(" static IP Data", DM_CLASSID, &ctx->events[7]);CHKERRQ(ierr); /* 7 */ 1391 ierr = PetscLogEventRegister(" dynamic IP-Jac", DM_CLASSID, &ctx->events[1]);CHKERRQ(ierr); /* 1 */ 1392 ierr = PetscLogEventRegister(" Kernel-init", DM_CLASSID, &ctx->events[3]);CHKERRQ(ierr); /* 3 */ 1393 ierr = PetscLogEventRegister(" Jac-f-df (GPU)", DM_CLASSID, &ctx->events[8]);CHKERRQ(ierr); /* 8 */ 1394 ierr = PetscLogEventRegister(" Kernel (GPU)", DM_CLASSID, &ctx->events[4]);CHKERRQ(ierr); /* 4 */ 1395 ierr = PetscLogEventRegister(" Copy to CPU", DM_CLASSID, &ctx->events[5]);CHKERRQ(ierr); /* 5 */ 1396 ierr = PetscLogEventRegister(" Jac-assemble", DM_CLASSID, &ctx->events[6]);CHKERRQ(ierr); /* 6 */ 1397 ierr = PetscLogEventRegister(" Jac asmbl setup", DM_CLASSID, &ctx->events[2]);CHKERRQ(ierr); /* 2 */ 1398 ierr = PetscLogEventRegister(" Other", DM_CLASSID, &ctx->events[13]);CHKERRQ(ierr); /* 13 */ 1399 1400 if (rank) { /* turn off output stuff for duplicate runs - do we need to add the prefix to all this? */ 1401 ierr = PetscOptionsClearValue(NULL,"-snes_converged_reason");CHKERRQ(ierr); 1402 ierr = PetscOptionsClearValue(NULL,"-ksp_converged_reason");CHKERRQ(ierr); 1403 ierr = PetscOptionsClearValue(NULL,"-snes_monitor");CHKERRQ(ierr); 1404 ierr = PetscOptionsClearValue(NULL,"-ksp_monitor");CHKERRQ(ierr); 1405 ierr = PetscOptionsClearValue(NULL,"-ts_monitor");CHKERRQ(ierr); 1406 ierr = PetscOptionsClearValue(NULL,"-ts_adapt_monitor");CHKERRQ(ierr); 1407 ierr = PetscOptionsClearValue(NULL,"-dm_landau_amr_dm_view");CHKERRQ(ierr); 1408 ierr = PetscOptionsClearValue(NULL,"-dm_landau_amr_vec_view");CHKERRQ(ierr); 1409 ierr = PetscOptionsClearValue(NULL,"-dm_landau_pre_dm_view");CHKERRQ(ierr); 1410 ierr = PetscOptionsClearValue(NULL,"-dm_landau_pre_vec_view");CHKERRQ(ierr); 1411 ierr = PetscOptionsClearValue(NULL,"-info");CHKERRQ(ierr); 1412 } 1413 } 1414 PetscFunctionReturn(0); 1415 } 1416 1417 /*@C 1418 LandauCreateVelocitySpace - Create a DMPlex velocity space mesh 1419 1420 Collective on comm 1421 1422 Input Parameters: 1423 + comm - The MPI communicator 1424 . dim - velocity space dimension (2 for axisymmetric, 3 for full 3X + 3V solver) 1425 - prefix - prefix for options 1426 1427 Output Parameter: 1428 . dm - The DM object representing the mesh 1429 + X - A vector (user destroys) 1430 - J - Optional matrix (object destroys) 1431 1432 Level: beginner 1433 1434 .keywords: mesh 1435 .seealso: DMPlexCreate(), LandauDestroyVelocitySpace() 1436 @*/ 1437 PetscErrorCode LandauCreateVelocitySpace(MPI_Comm comm, PetscInt dim, const char prefix[], Vec *X, Mat *J, DM *dm) 1438 { 1439 PetscErrorCode ierr; 1440 LandauCtx *ctx; 1441 PetscBool prealloc_only,flg; 1442 1443 PetscFunctionBegin; 1444 if (dim!=2 && dim!=3) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Only 2D and 3D supported"); 1445 ierr = PetscNew(&ctx);CHKERRQ(ierr); 1446 ctx->comm = comm; /* used for diagnostics and global errors */ 1447 /* process options */ 1448 ierr = ProcessOptions(ctx,prefix);CHKERRQ(ierr); 1449 /* Create Mesh */ 1450 ierr = LandauDMCreateVMesh(PETSC_COMM_SELF, dim, prefix, ctx, dm);CHKERRQ(ierr); 1451 prealloc_only = (*dm)->prealloc_only; 1452 ierr = DMViewFromOptions(*dm,NULL,"-dm_landau_pre_dm_view");CHKERRQ(ierr); 1453 ierr = DMSetApplicationContext(*dm, ctx);CHKERRQ(ierr); 1454 /* create FEM */ 1455 ierr = SetupDS(*dm,dim,ctx);CHKERRQ(ierr); 1456 /* set initial state */ 1457 ierr = DMCreateGlobalVector(*dm,X);CHKERRQ(ierr); 1458 ierr = PetscObjectSetName((PetscObject) *X, "u");CHKERRQ(ierr); 1459 /* initial static refinement, no solve */ 1460 ierr = LandauSetInitialCondition(*dm, *X, ctx);CHKERRQ(ierr); 1461 ierr = VecViewFromOptions(*X, NULL, "-dm_landau_pre_vec_view");CHKERRQ(ierr); 1462 /* forest refinement */ 1463 if (ctx->errorIndicator) { 1464 /* AMR */ 1465 ierr = adapt(dm,ctx,X);CHKERRQ(ierr); 1466 if ((*dm)->prealloc_only != prealloc_only) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"(*dm)->prealloc_only != prealloc_only"); 1467 ierr = DMViewFromOptions(*dm,NULL,"-dm_landau_amr_dm_view");CHKERRQ(ierr); 1468 ierr = VecViewFromOptions(*X, NULL, "-dm_landau_amr_vec_view");CHKERRQ(ierr); 1469 } 1470 ierr = DMSetApplicationContext(*dm, ctx);CHKERRQ(ierr); 1471 ctx->dmv = *dm; 1472 if (ctx->dmv->prealloc_only != prealloc_only) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"ctx->dmv->prealloc_only != prealloc_only"); 1473 ierr = DMCreateMatrix(ctx->dmv, &ctx->J);CHKERRQ(ierr); 1474 ierr = MatSetOption(ctx->J, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr); 1475 ierr = MatSetOption(ctx->J, MAT_STRUCTURALLY_SYMMETRIC, PETSC_TRUE);CHKERRQ(ierr); 1476 if (J) *J = ctx->J; 1477 /* check for types that we need */ 1478 #if defined(PETSC_HAVE_KOKKOS) 1479 if (ctx->deviceType == LANDAU_CPU) { 1480 ierr = PetscObjectTypeCompareAny((PetscObject)ctx->J,&flg,MATSEQAIJKOKKOS,MATMPIAIJKOKKOS,MATAIJKOKKOS,"");CHKERRQ(ierr); 1481 } 1482 #elif defined(PETSC_HAVE_CUDA) 1483 if (ctx->deviceType == LANDAU_CPU) { 1484 ierr = PetscObjectTypeCompareAny((PetscObject)ctx->J,&flg,MATSEQAIJCUSPARSE,MATMPIAIJCUSPARSE,MATAIJCUSPARSE,"");CHKERRQ(ierr); 1485 } 1486 #endif 1487 if (ctx->gpu_assembly) { /* we need GPU object with GPU assembly */ 1488 if (ctx->deviceType == LANDAU_CUDA) { 1489 ierr = PetscObjectTypeCompareAny((PetscObject)ctx->J,&flg,MATSEQAIJCUSPARSE,MATMPIAIJCUSPARSE,MATAIJCUSPARSE,"");CHKERRQ(ierr); 1490 if (!flg) SETERRQ(ctx->comm,PETSC_ERR_ARG_WRONG,"must use '-dm_mat_type aijcusparse -dm_vec_type cuda' for GPU assembly and Cuda"); 1491 } else if (ctx->deviceType == LANDAU_KOKKOS) { 1492 ierr = PetscObjectTypeCompareAny((PetscObject)ctx->J,&flg,MATSEQAIJKOKKOS,MATMPIAIJKOKKOS,MATAIJKOKKOS,"");CHKERRQ(ierr); 1493 #if defined(PETSC_HAVE_KOKKOS_KERNELS) 1494 if (!flg) SETERRQ(ctx->comm,PETSC_ERR_ARG_WRONG,"must use '-dm_mat_type aijkokkos -dm_vec_type kokkos' for GPU assembly and Kokkos"); 1495 #else 1496 if (!flg) SETERRQ(ctx->comm,PETSC_ERR_ARG_WRONG,"must configure with '--download-kokkos-kernels=1' for GPU assembly and Kokkos"); 1497 #endif 1498 } 1499 } 1500 PetscFunctionReturn(0); 1501 } 1502 1503 /*@ 1504 LandauDestroyVelocitySpace - Destroy a DMPlex velocity space mesh 1505 1506 Collective on dm 1507 1508 Input/Output Parameters: 1509 . dm - the dm to destroy 1510 1511 Level: beginner 1512 1513 .keywords: mesh 1514 .seealso: LandauCreateVelocitySpace() 1515 @*/ 1516 PetscErrorCode LandauDestroyVelocitySpace(DM *dm) 1517 { 1518 PetscErrorCode ierr,ii; 1519 LandauCtx *ctx; 1520 PetscContainer container = NULL; 1521 PetscFunctionBegin; 1522 ierr = DMGetApplicationContext(*dm, &ctx);CHKERRQ(ierr); 1523 ierr = PetscObjectQuery((PetscObject)ctx->J,"coloring", (PetscObject*)&container);CHKERRQ(ierr); 1524 if (container) { 1525 ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 1526 } 1527 ierr = MatDestroy(&ctx->M);CHKERRQ(ierr); 1528 ierr = MatDestroy(&ctx->J);CHKERRQ(ierr); 1529 for (ii=0;ii<ctx->num_species;ii++) { 1530 ierr = PetscFEDestroy(&ctx->fe[ii]);CHKERRQ(ierr); 1531 } 1532 if (ctx->deviceType == LANDAU_CUDA) { 1533 #if defined(PETSC_HAVE_CUDA) 1534 ierr = LandauCUDAStaticDataClear(ctx->SData_d);CHKERRQ(ierr); 1535 #else 1536 SETERRQ1(ctx->comm,PETSC_ERR_ARG_WRONG,"-landau_device_type %s not built","cuda"); 1537 #endif 1538 } else if (ctx->deviceType == LANDAU_KOKKOS) { 1539 #if defined(PETSC_HAVE_KOKKOS) 1540 ierr = LandauKokkosStaticDataClear(ctx->SData_d);CHKERRQ(ierr); 1541 #else 1542 SETERRQ1(ctx->comm,PETSC_ERR_ARG_WRONG,"-landau_device_type %s not built","kokkos"); 1543 #endif 1544 } else { 1545 if (ctx->SData_d) { /* in a CPU run */ 1546 PetscReal *invJ = (PetscReal*)ctx->SData_d->invJ, *xx = (PetscReal*)ctx->SData_d->x, *yy = (PetscReal*)ctx->SData_d->y, *zz = (PetscReal*)ctx->SData_d->z, *ww = (PetscReal*)ctx->SData_d->w, *mass_w = (PetscReal*)ctx->SData_d->mass_w; 1547 ierr = PetscFree5(mass_w,ww,xx,yy,invJ);CHKERRQ(ierr); 1548 if (zz) { 1549 ierr = PetscFree(zz);CHKERRQ(ierr); 1550 } 1551 } 1552 } 1553 ierr = PetscFree(ctx->SData_d);CHKERRQ(ierr); 1554 if (ctx->times[0] > 0) { 1555 ierr = PetscPrintf(ctx->comm, "Landau Operator %d 1.0 %10.3e ....\n",10000,ctx->times[0]);CHKERRQ(ierr); 1556 } 1557 if (ctx->plex != NULL) { 1558 ierr = DMDestroy(&ctx->plex);CHKERRQ(ierr); 1559 } 1560 PetscFree(ctx); 1561 ierr = DMDestroy(dm);CHKERRQ(ierr); 1562 PetscFunctionReturn(0); 1563 } 1564 1565 /* < v, ru > */ 1566 static void f0_s_den(PetscInt dim, PetscInt Nf, PetscInt NfAux, 1567 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 1568 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 1569 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar *f0) 1570 { 1571 PetscInt ii = (PetscInt)PetscRealPart(constants[0]); 1572 f0[0] = u[ii]; 1573 } 1574 1575 /* < v, ru > */ 1576 static void f0_s_mom(PetscInt dim, PetscInt Nf, PetscInt NfAux, 1577 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 1578 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 1579 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar *f0) 1580 { 1581 PetscInt ii = (PetscInt)PetscRealPart(constants[0]), jj = (PetscInt)PetscRealPart(constants[1]); 1582 f0[0] = x[jj]*u[ii]; /* x momentum */ 1583 } 1584 1585 static void f0_s_v2(PetscInt dim, PetscInt Nf, PetscInt NfAux, 1586 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 1587 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 1588 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar *f0) 1589 { 1590 PetscInt i, ii = (PetscInt)PetscRealPart(constants[0]); 1591 double tmp1 = 0.; 1592 for (i = 0; i < dim; ++i) tmp1 += x[i]*x[i]; 1593 f0[0] = tmp1*u[ii]; 1594 } 1595 1596 /* < v, ru > */ 1597 static void f0_s_rden(PetscInt dim, PetscInt Nf, PetscInt NfAux, 1598 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 1599 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 1600 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar *f0) 1601 { 1602 PetscInt ii = (PetscInt)PetscRealPart(constants[0]); 1603 f0[0] = 2.*PETSC_PI*x[0]*u[ii]; 1604 } 1605 1606 /* < v, ru > */ 1607 static void f0_s_rmom(PetscInt dim, PetscInt Nf, PetscInt NfAux, 1608 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 1609 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 1610 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar *f0) 1611 { 1612 PetscInt ii = (PetscInt)PetscRealPart(constants[0]); 1613 f0[0] = 2.*PETSC_PI*x[0]*x[1]*u[ii]; 1614 } 1615 1616 static void f0_s_rv2(PetscInt dim, PetscInt Nf, PetscInt NfAux, 1617 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 1618 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 1619 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar *f0) 1620 { 1621 PetscInt ii = (PetscInt)PetscRealPart(constants[0]); 1622 f0[0] = 2.*PETSC_PI*x[0]*(x[0]*x[0] + x[1]*x[1])*u[ii]; 1623 } 1624 1625 /*@ 1626 LandauPrintNorms - collects moments and prints them 1627 1628 Collective on dm 1629 1630 Input Parameters: 1631 + X - the state 1632 - stepi - current step to print 1633 1634 Level: beginner 1635 1636 .keywords: mesh 1637 .seealso: LandauCreateVelocitySpace() 1638 @*/ 1639 PetscErrorCode LandauPrintNorms(Vec X, PetscInt stepi) 1640 { 1641 PetscErrorCode ierr; 1642 LandauCtx *ctx; 1643 PetscDS prob; 1644 DM dm; 1645 PetscInt cStart, cEnd, dim, ii; 1646 PetscScalar xmomentumtot=0, ymomentumtot=0, zmomentumtot=0, energytot=0, densitytot=0, tt[LANDAU_MAX_SPECIES]; 1647 PetscScalar xmomentum[LANDAU_MAX_SPECIES], ymomentum[LANDAU_MAX_SPECIES], zmomentum[LANDAU_MAX_SPECIES], energy[LANDAU_MAX_SPECIES], density[LANDAU_MAX_SPECIES]; 1648 1649 PetscFunctionBegin; 1650 ierr = VecGetDM(X, &dm);CHKERRQ(ierr); 1651 if (!dm) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "no DM"); 1652 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1653 ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr); 1654 if (!ctx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "no context"); 1655 if (!ctx->plex) { 1656 ierr = DMConvert(ctx->dmv, DMPLEX, &ctx->plex);CHKERRQ(ierr); 1657 } 1658 ierr = DMCreateDS(ctx->plex);CHKERRQ(ierr); 1659 ierr = DMGetDS(ctx->plex, &prob);CHKERRQ(ierr); 1660 /* print momentum and energy */ 1661 for (ii=0;ii<ctx->num_species;ii++) { 1662 PetscScalar user[2] = { (PetscScalar)ii, (PetscScalar)ctx->charges[ii]}; 1663 ierr = PetscDSSetConstants(prob, 2, user);CHKERRQ(ierr); 1664 if (dim==2) { /* 2/3X + 3V (cylindrical coordinates) */ 1665 ierr = PetscDSSetObjective(prob, 0, &f0_s_rden);CHKERRQ(ierr); 1666 ierr = DMPlexComputeIntegralFEM(ctx->plex,X,tt,ctx);CHKERRQ(ierr); 1667 density[ii] = tt[0]*ctx->n_0*ctx->charges[ii]; 1668 ierr = PetscDSSetObjective(prob, 0, &f0_s_rmom);CHKERRQ(ierr); 1669 ierr = DMPlexComputeIntegralFEM(ctx->plex,X,tt,ctx);CHKERRQ(ierr); 1670 zmomentum[ii] = tt[0]*ctx->n_0*ctx->v_0*ctx->masses[ii]; 1671 ierr = PetscDSSetObjective(prob, 0, &f0_s_rv2);CHKERRQ(ierr); 1672 ierr = DMPlexComputeIntegralFEM(ctx->plex,X,tt,ctx);CHKERRQ(ierr); 1673 energy[ii] = tt[0]*0.5*ctx->n_0*ctx->v_0*ctx->v_0*ctx->masses[ii]; 1674 zmomentumtot += zmomentum[ii]; 1675 energytot += energy[ii]; 1676 densitytot += density[ii]; 1677 ierr = PetscPrintf(ctx->comm, "%3D) species-%D: charge density= %20.13e z-momentum= %20.13e energy= %20.13e",stepi,ii,PetscRealPart(density[ii]),PetscRealPart(zmomentum[ii]),PetscRealPart(energy[ii]));CHKERRQ(ierr); 1678 } else { /* 2/3X + 3V */ 1679 ierr = PetscDSSetObjective(prob, 0, &f0_s_den);CHKERRQ(ierr); 1680 ierr = DMPlexComputeIntegralFEM(ctx->plex,X,tt,ctx);CHKERRQ(ierr); 1681 density[ii] = tt[0]*ctx->n_0*ctx->charges[ii]; 1682 ierr = PetscDSSetObjective(prob, 0, &f0_s_mom);CHKERRQ(ierr); 1683 user[1] = 0; 1684 ierr = DMPlexComputeIntegralFEM(ctx->plex,X,tt,ctx);CHKERRQ(ierr); 1685 xmomentum[ii] = tt[0]*ctx->n_0*ctx->v_0*ctx->masses[ii]; 1686 user[1] = 1; 1687 ierr = DMPlexComputeIntegralFEM(ctx->plex,X,tt,ctx);CHKERRQ(ierr); 1688 ymomentum[ii] = tt[0]*ctx->n_0*ctx->v_0*ctx->masses[ii]; 1689 user[1] = 2; 1690 ierr = DMPlexComputeIntegralFEM(ctx->plex,X,tt,ctx);CHKERRQ(ierr); 1691 zmomentum[ii] = tt[0]*ctx->n_0*ctx->v_0*ctx->masses[ii]; 1692 ierr = PetscDSSetObjective(prob, 0, &f0_s_v2);CHKERRQ(ierr); 1693 ierr = DMPlexComputeIntegralFEM(ctx->plex,X,tt,ctx);CHKERRQ(ierr); 1694 energy[ii] = 0.5*tt[0]*ctx->n_0*ctx->v_0*ctx->v_0*ctx->masses[ii]; 1695 ierr = PetscPrintf(ctx->comm, "%3D) species %D: density=%20.13e, x-momentum=%20.13e, y-momentum=%20.13e, z-momentum=%20.13e, energy=%21.13e", 1696 stepi,ii,PetscRealPart(density[ii]),PetscRealPart(xmomentum[ii]),PetscRealPart(ymomentum[ii]),PetscRealPart(zmomentum[ii]),PetscRealPart(energy[ii]));CHKERRQ(ierr); 1697 xmomentumtot += xmomentum[ii]; 1698 ymomentumtot += ymomentum[ii]; 1699 zmomentumtot += zmomentum[ii]; 1700 energytot += energy[ii]; 1701 densitytot += density[ii]; 1702 } 1703 if (ctx->num_species>1) PetscPrintf(ctx->comm, "\n"); 1704 } 1705 /* totals */ 1706 ierr = DMPlexGetHeightStratum(ctx->plex,0,&cStart,&cEnd);CHKERRQ(ierr); 1707 if (ctx->num_species>1) { 1708 if (dim==2) { 1709 ierr = PetscPrintf(ctx->comm, "\t%3D) Total: charge density=%21.13e, momentum=%21.13e, energy=%21.13e (m_i[0]/m_e = %g, %D cells)", 1710 stepi,(double)PetscRealPart(densitytot),(double)PetscRealPart(zmomentumtot),(double)PetscRealPart(energytot),(double)(ctx->masses[1]/ctx->masses[0]),cEnd-cStart);CHKERRQ(ierr); 1711 } else { 1712 ierr = PetscPrintf(ctx->comm, "\t%3D) Total: charge density=%21.13e, x-momentum=%21.13e, y-momentum=%21.13e, z-momentum=%21.13e, energy=%21.13e (m_i[0]/m_e = %g, %D cells)", 1713 stepi,(double)PetscRealPart(densitytot),(double)PetscRealPart(xmomentumtot),(double)PetscRealPart(ymomentumtot),(double)PetscRealPart(zmomentumtot),(double)PetscRealPart(energytot),(double)(ctx->masses[1]/ctx->masses[0]),cEnd-cStart);CHKERRQ(ierr); 1714 } 1715 } else { 1716 ierr = PetscPrintf(ctx->comm, " -- %D cells",cEnd-cStart);CHKERRQ(ierr); 1717 } 1718 if (ctx->verbose > 1) {ierr = PetscPrintf(ctx->comm,", %D sub (vector) threads\n",ctx->subThreadBlockSize);CHKERRQ(ierr);} 1719 else {ierr = PetscPrintf(ctx->comm,"\n");CHKERRQ(ierr);} 1720 PetscFunctionReturn(0); 1721 } 1722 1723 static PetscErrorCode destroy_coloring (void *is) 1724 { 1725 ISColoring tmp = (ISColoring)is; 1726 return ISColoringDestroy(&tmp); 1727 } 1728 1729 /*@ 1730 LandauCreateColoring - create a coloring and add to matrix (Landau context used just for 'print' flag, should be in DMPlex) 1731 1732 Collective on JacP 1733 1734 Input Parameters: 1735 + JacP - matrix to add coloring to 1736 - plex - The DM 1737 1738 Output Parameter: 1739 . container - Container with coloring 1740 1741 Level: beginner 1742 1743 .keywords: mesh 1744 .seealso: LandauCreateVelocitySpace() 1745 @*/ 1746 PetscErrorCode LandauCreateColoring(Mat JacP, DM plex, PetscContainer *container) 1747 { 1748 PetscErrorCode ierr; 1749 PetscInt dim,cell,i,ej,nc,Nv,totDim,numGCells,cStart,cEnd; 1750 ISColoring iscoloring = NULL; 1751 Mat G,Q; 1752 PetscScalar ones[128]; 1753 MatColoring mc; 1754 IS *is; 1755 PetscInt csize,colour,j,k; 1756 const PetscInt *indices; 1757 PetscInt numComp[1]; 1758 PetscInt numDof[4]; 1759 PetscFE fe; 1760 DM colordm; 1761 PetscSection csection, section, globalSection; 1762 PetscDS prob; 1763 LandauCtx *ctx; 1764 1765 PetscFunctionBegin; 1766 ierr = DMGetApplicationContext(plex, &ctx);CHKERRQ(ierr); 1767 ierr = DMGetLocalSection(plex, §ion);CHKERRQ(ierr); 1768 ierr = DMGetGlobalSection(plex, &globalSection);CHKERRQ(ierr); 1769 ierr = DMGetDimension(plex, &dim);CHKERRQ(ierr); 1770 ierr = DMGetDS(plex, &prob);CHKERRQ(ierr); 1771 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1772 ierr = DMPlexGetHeightStratum(plex,0,&cStart,&cEnd);CHKERRQ(ierr); 1773 numGCells = cEnd - cStart; 1774 /* create cell centered DM */ 1775 ierr = DMClone(plex, &colordm);CHKERRQ(ierr); 1776 ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) plex), dim, 1, PETSC_FALSE, "color_", PETSC_DECIDE, &fe);CHKERRQ(ierr); 1777 ierr = PetscObjectSetName((PetscObject) fe, "color");CHKERRQ(ierr); 1778 ierr = DMSetField(colordm, 0, NULL, (PetscObject)fe);CHKERRQ(ierr); 1779 ierr = PetscFEDestroy(&fe);CHKERRQ(ierr); 1780 for (i = 0; i < (dim+1); ++i) numDof[i] = 0; 1781 numDof[dim] = 1; 1782 numComp[0] = 1; 1783 ierr = DMPlexCreateSection(colordm, NULL, numComp, numDof, 0, NULL, NULL, NULL, NULL, &csection);CHKERRQ(ierr); 1784 ierr = PetscSectionSetFieldName(csection, 0, "color");CHKERRQ(ierr); 1785 ierr = DMSetLocalSection(colordm, csection);CHKERRQ(ierr); 1786 ierr = DMViewFromOptions(colordm,NULL,"-color_dm_view");CHKERRQ(ierr); 1787 /* get vertex to element map Q and colroing graph G */ 1788 ierr = MatGetSize(JacP,NULL,&Nv);CHKERRQ(ierr); 1789 ierr = MatCreateAIJ(PETSC_COMM_SELF,PETSC_DECIDE,PETSC_DECIDE,numGCells,Nv,totDim,NULL,0,NULL,&Q);CHKERRQ(ierr); 1790 for (i=0;i<128;i++) ones[i] = 1.0; 1791 for (cell = cStart, ej = 0 ; cell < cEnd; ++cell, ++ej) { 1792 PetscInt numindices,*indices; 1793 ierr = DMPlexGetClosureIndices(plex, section, globalSection, cell, PETSC_TRUE, &numindices, &indices, NULL, NULL);CHKERRQ(ierr); 1794 if (numindices>128) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "too many indices. %D > %D",numindices,128); 1795 ierr = MatSetValues(Q,1,&ej,numindices,indices,ones,ADD_VALUES);CHKERRQ(ierr); 1796 ierr = DMPlexRestoreClosureIndices(plex, section, globalSection, cell, PETSC_TRUE, &numindices, &indices, NULL, NULL);CHKERRQ(ierr); 1797 } 1798 ierr = MatAssemblyBegin(Q, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1799 ierr = MatAssemblyEnd(Q, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1800 ierr = MatMatTransposeMult(Q,Q,MAT_INITIAL_MATRIX,4.0,&G);CHKERRQ(ierr); 1801 ierr = PetscObjectSetName((PetscObject) Q, "Q");CHKERRQ(ierr); 1802 ierr = PetscObjectSetName((PetscObject) G, "coloring graph");CHKERRQ(ierr); 1803 ierr = MatViewFromOptions(G,NULL,"-coloring_mat_view");CHKERRQ(ierr); 1804 ierr = MatViewFromOptions(Q,NULL,"-coloring_mat_view");CHKERRQ(ierr); 1805 ierr = MatDestroy(&Q);CHKERRQ(ierr); 1806 /* coloring */ 1807 ierr = MatColoringCreate(G,&mc);CHKERRQ(ierr); 1808 ierr = MatColoringSetDistance(mc,1);CHKERRQ(ierr); 1809 ierr = MatColoringSetType(mc,MATCOLORINGJP);CHKERRQ(ierr); 1810 ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 1811 ierr = MatColoringApply(mc,&iscoloring);CHKERRQ(ierr); 1812 ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 1813 /* view */ 1814 ierr = ISColoringViewFromOptions(iscoloring,NULL,"-coloring_is_view");CHKERRQ(ierr); 1815 ierr = ISColoringGetIS(iscoloring,PETSC_USE_POINTER,&nc,&is);CHKERRQ(ierr); 1816 if (ctx && ctx->verbose > 2) { 1817 PetscViewer viewer; 1818 Vec color_vec, eidx_vec; 1819 ierr = DMGetGlobalVector(colordm, &color_vec);CHKERRQ(ierr); 1820 ierr = DMGetGlobalVector(colordm, &eidx_vec);CHKERRQ(ierr); 1821 for (colour=0; colour<nc; colour++) { 1822 ierr = ISGetLocalSize(is[colour],&csize);CHKERRQ(ierr); 1823 ierr = ISGetIndices(is[colour],&indices);CHKERRQ(ierr); 1824 for (j=0; j<csize; j++) { 1825 PetscScalar v = (PetscScalar)colour; 1826 k = indices[j]; 1827 ierr = VecSetValues(color_vec,1,&k,&v,INSERT_VALUES); 1828 v = (PetscScalar)k; 1829 ierr = VecSetValues(eidx_vec,1,&k,&v,INSERT_VALUES); 1830 } 1831 ierr = ISRestoreIndices(is[colour],&indices);CHKERRQ(ierr); 1832 } 1833 /* view */ 1834 ierr = PetscViewerVTKOpen(ctx->comm, "color.vtu", FILE_MODE_WRITE, &viewer);CHKERRQ(ierr); 1835 ierr = PetscObjectSetName((PetscObject) color_vec, "color");CHKERRQ(ierr); 1836 ierr = VecView(color_vec, viewer);CHKERRQ(ierr); 1837 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1838 ierr = PetscViewerVTKOpen(ctx->comm, "eidx.vtu", FILE_MODE_WRITE, &viewer);CHKERRQ(ierr); 1839 ierr = PetscObjectSetName((PetscObject) eidx_vec, "element-idx");CHKERRQ(ierr); 1840 ierr = VecView(eidx_vec, viewer);CHKERRQ(ierr); 1841 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1842 ierr = DMRestoreGlobalVector(colordm, &color_vec);CHKERRQ(ierr); 1843 ierr = DMRestoreGlobalVector(colordm, &eidx_vec);CHKERRQ(ierr); 1844 } 1845 ierr = PetscSectionDestroy(&csection);CHKERRQ(ierr); 1846 ierr = DMDestroy(&colordm);CHKERRQ(ierr); 1847 ierr = ISColoringRestoreIS(iscoloring,PETSC_USE_POINTER,&is);CHKERRQ(ierr); 1848 ierr = MatDestroy(&G);CHKERRQ(ierr); 1849 /* stash coloring */ 1850 ierr = PetscContainerCreate(PETSC_COMM_SELF, container);CHKERRQ(ierr); 1851 ierr = PetscContainerSetPointer(*container,(void*)iscoloring);CHKERRQ(ierr); 1852 ierr = PetscContainerSetUserDestroy(*container, destroy_coloring);CHKERRQ(ierr); 1853 ierr = PetscObjectCompose((PetscObject)JacP,"coloring",(PetscObject)*container);CHKERRQ(ierr); 1854 if (ctx && ctx->verbose > 0) { 1855 ierr = PetscPrintf(ctx->comm, "Made coloring with %D colors\n", nc);CHKERRQ(ierr); 1856 } 1857 PetscFunctionReturn(0); 1858 } 1859 1860 PetscErrorCode LandauAssembleOpenMP(PetscInt cStart, PetscInt cEnd, PetscInt totDim, DM plex, PetscSection section, PetscSection globalSection, Mat JacP, PetscScalar elemMats[], PetscContainer container) 1861 { 1862 PetscErrorCode ierr; 1863 IS *is; 1864 PetscInt nc,colour,j; 1865 const PetscInt *clr_idxs; 1866 ISColoring iscoloring; 1867 PetscFunctionBegin; 1868 ierr = PetscContainerGetPointer(container,(void**)&iscoloring);CHKERRQ(ierr); 1869 ierr = ISColoringGetIS(iscoloring,PETSC_USE_POINTER,&nc,&is);CHKERRQ(ierr); 1870 for (colour=0; colour<nc; colour++) { 1871 PetscInt *idx_arr[1024]; /* need to make dynamic for general use */ 1872 PetscScalar *new_el_mats[1024]; 1873 PetscInt idx_size[1024],csize; 1874 ierr = ISGetLocalSize(is[colour],&csize);CHKERRQ(ierr); 1875 if (csize>1024) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "too many elements in color. %D > %D",csize,1024); 1876 ierr = ISGetIndices(is[colour],&clr_idxs);CHKERRQ(ierr); 1877 /* get indices and mats */ 1878 for (j=0; j<csize; j++) { 1879 PetscInt cell = cStart + clr_idxs[j]; 1880 PetscInt numindices,*indices; 1881 PetscScalar *elMat = &elemMats[clr_idxs[j]*totDim*totDim]; 1882 PetscScalar *valuesOrig = elMat; 1883 ierr = DMPlexGetClosureIndices(plex, section, globalSection, cell, PETSC_TRUE, &numindices, &indices, NULL, (PetscScalar **) &elMat);CHKERRQ(ierr); 1884 idx_size[j] = numindices; 1885 ierr = PetscMalloc2(numindices,&idx_arr[j],numindices*numindices,&new_el_mats[j]);CHKERRQ(ierr); 1886 ierr = PetscMemcpy(idx_arr[j],indices,numindices*sizeof(PetscInt));CHKERRQ(ierr); 1887 ierr = PetscMemcpy(new_el_mats[j],elMat,numindices*numindices*sizeof(PetscScalar));CHKERRQ(ierr); 1888 ierr = DMPlexRestoreClosureIndices(plex, section, globalSection, cell, PETSC_TRUE, &numindices, &indices, NULL, (PetscScalar **) &elMat);CHKERRQ(ierr); 1889 if (elMat != valuesOrig) {ierr = DMRestoreWorkArray(plex, numindices*numindices, MPIU_SCALAR, &elMat);} 1890 } 1891 /* assemble matrix */ 1892 for (j=0; j<csize; j++) { 1893 PetscInt numindices = idx_size[j], *indices = idx_arr[j]; 1894 PetscScalar *elMat = new_el_mats[j]; 1895 MatSetValues(JacP,numindices,indices,numindices,indices,elMat,ADD_VALUES); 1896 } 1897 /* free */ 1898 ierr = ISRestoreIndices(is[colour],&clr_idxs);CHKERRQ(ierr); 1899 for (j=0; j<csize; j++) { 1900 ierr = PetscFree2(idx_arr[j],new_el_mats[j]);CHKERRQ(ierr); 1901 } 1902 } 1903 ierr = ISColoringRestoreIS(iscoloring,PETSC_USE_POINTER,&is);CHKERRQ(ierr); 1904 PetscFunctionReturn(0); 1905 } 1906 1907 /* < v, u > */ 1908 static void g0_1(PetscInt dim, PetscInt Nf, PetscInt NfAux, 1909 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 1910 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 1911 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[]) 1912 { 1913 g0[0] = 1.; 1914 } 1915 1916 /* < v, u > */ 1917 static void g0_r(PetscInt dim, PetscInt Nf, PetscInt NfAux, 1918 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 1919 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 1920 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[]) 1921 { 1922 g0[0] = 2.*PETSC_PI*x[0]; 1923 } 1924 1925 /*@ 1926 LandauCreateMassMatrix - Create mass matrix for Landau 1927 1928 Collective on dm 1929 1930 Input Parameters: 1931 . dm - the DM object 1932 1933 Output Parameters: 1934 . Amat - The mass matrix (optional), mass matrix is added to the DM context 1935 1936 Level: beginner 1937 1938 .keywords: mesh 1939 .seealso: LandauCreateVelocitySpace() 1940 @*/ 1941 PetscErrorCode LandauCreateMassMatrix(DM dm, Mat *Amat) 1942 { 1943 DM massDM; 1944 PetscDS prob; 1945 PetscInt ii,dim,N1=1,N2; 1946 PetscErrorCode ierr; 1947 LandauCtx *ctx; 1948 Mat M; 1949 1950 PetscFunctionBegin; 1951 PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1952 if (Amat) PetscValidPointer(Amat,2); 1953 ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr); 1954 if (!ctx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "no context"); 1955 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1956 ierr = DMClone(dm, &massDM);CHKERRQ(ierr); 1957 ierr = DMCopyFields(dm, massDM);CHKERRQ(ierr); 1958 ierr = DMCreateDS(massDM);CHKERRQ(ierr); 1959 ierr = DMGetDS(massDM, &prob);CHKERRQ(ierr); 1960 for (ii=0;ii<ctx->num_species;ii++) { 1961 if (dim==3) {ierr = PetscDSSetJacobian(prob, ii, ii, g0_1, NULL, NULL, NULL);CHKERRQ(ierr);} 1962 else {ierr = PetscDSSetJacobian(prob, ii, ii, g0_r, NULL, NULL, NULL);CHKERRQ(ierr);} 1963 } 1964 ierr = DMViewFromOptions(massDM,NULL,"-dm_landau_mass_dm_view");CHKERRQ(ierr); 1965 ierr = DMCreateMatrix(massDM, &M);CHKERRQ(ierr); 1966 ierr = MatSetOption(M, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr); 1967 { 1968 Vec locX; 1969 DM plex; 1970 ierr = DMConvert(massDM, DMPLEX, &plex);CHKERRQ(ierr); 1971 ierr = DMGetLocalVector(massDM, &locX);CHKERRQ(ierr); 1972 /* Mass matrix is independent of the input, so no need to fill locX */ 1973 if (plex->prealloc_only != dm->prealloc_only) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "plex->prealloc_only = massDM->prealloc_only %D, =%D",plex->prealloc_only,massDM->prealloc_only); 1974 ierr = DMPlexSNESComputeJacobianFEM(plex, locX, M, M, ctx);CHKERRQ(ierr); 1975 ierr = DMRestoreLocalVector(massDM, &locX);CHKERRQ(ierr); 1976 ierr = DMDestroy(&plex);CHKERRQ(ierr); 1977 } 1978 ierr = DMDestroy(&massDM);CHKERRQ(ierr); 1979 ierr = MatGetSize(ctx->J, &N1, NULL);CHKERRQ(ierr); 1980 ierr = MatGetSize(M, &N2, NULL);CHKERRQ(ierr); 1981 if (N1 != N2) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Incorrect matrix sizes: |Jacobian| = %D, |Mass|=%D",N1,N2); 1982 ierr = PetscObjectSetName((PetscObject)M, "mass");CHKERRQ(ierr); 1983 ierr = MatViewFromOptions(M,NULL,"-dm_landau_mass_mat_view");CHKERRQ(ierr); 1984 ctx->M = M; /* this could be a noop, a = a */ 1985 if (Amat) *Amat = M; 1986 PetscFunctionReturn(0); 1987 } 1988 1989 /*@ 1990 LandauIFunction - TS residual calculation 1991 1992 Collective on ts 1993 1994 Input Parameters: 1995 + TS - The time stepping context 1996 . time_dummy - current time (not used) 1997 - X - Current state 1998 + X_t - Time derivative of current state 1999 . actx - Landau context 2000 2001 Output Parameter: 2002 . F - The residual 2003 2004 Level: beginner 2005 2006 .keywords: mesh 2007 .seealso: LandauCreateVelocitySpace(), LandauIJacobian() 2008 @*/ 2009 PetscErrorCode LandauIFunction(TS ts, PetscReal time_dummy, Vec X, Vec X_t, Vec F, void *actx) 2010 { 2011 PetscErrorCode ierr; 2012 LandauCtx *ctx=(LandauCtx*)actx; 2013 PetscInt dim; 2014 DM dm; 2015 #if defined(PETSC_HAVE_THREADSAFETY) 2016 double starttime, endtime; 2017 #endif 2018 2019 PetscFunctionBegin; 2020 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 2021 ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr); 2022 if (!ctx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "no context"); 2023 //ierr = MPI_Barrier(MPI_COMM_WORLD);CHKERRMPI(ierr); 2024 ierr = PetscLogEventBegin(ctx->events[11],0,0,0,0);CHKERRQ(ierr); 2025 ierr = PetscLogEventBegin(ctx->events[0],0,0,0,0);CHKERRQ(ierr); 2026 #if defined(PETSC_HAVE_THREADSAFETY) 2027 starttime = MPI_Wtime(); 2028 #endif 2029 ierr = DMGetDimension(ctx->dmv, &dim);CHKERRQ(ierr); 2030 if (!ctx->aux_bool) { 2031 ierr = PetscInfo3(ts, "Create Landau Jacobian t=%g X=%p %s\n",time_dummy,X_t,ctx->aux_bool ? " -- seems to be in line search" : "");CHKERRQ(ierr); 2032 ierr = LandauFormJacobian_Internal(X,ctx->J,dim,0.0,(void*)ctx);CHKERRQ(ierr); 2033 ctx->aux_bool = PETSC_TRUE; 2034 } else { 2035 ierr = PetscInfo(ts, "Skip forming Jacobian, has not changed (should check norm)\n");CHKERRQ(ierr); 2036 } 2037 ierr = MatViewFromOptions(ctx->J,NULL,"-landau_jacobian_mat_view");CHKERRQ(ierr); 2038 /* mat vec for op */ 2039 ierr = MatMult(ctx->J,X,F);CHKERRQ(ierr);CHKERRQ(ierr); /* C*f */ 2040 /* add time term */ 2041 if (X_t) { 2042 ierr = MatMultAdd(ctx->M,X_t,F,F);CHKERRQ(ierr); 2043 } 2044 #if defined(PETSC_HAVE_THREADSAFETY) 2045 endtime = MPI_Wtime(); 2046 ctx->times[0] += (endtime - starttime); 2047 #endif 2048 ierr = PetscLogEventEnd(ctx->events[0],0,0,0,0);CHKERRQ(ierr); 2049 ierr = PetscLogEventEnd(ctx->events[11],0,0,0,0);CHKERRQ(ierr); 2050 PetscFunctionReturn(0); 2051 } 2052 static PetscErrorCode MatrixNfDestroy(void *ptr) 2053 { 2054 PetscInt *nf = (PetscInt *)ptr; 2055 PetscErrorCode ierr; 2056 PetscFunctionBegin; 2057 ierr = PetscFree(nf);CHKERRQ(ierr); 2058 PetscFunctionReturn(0); 2059 } 2060 /*@ 2061 LandauIJacobian - TS Jacobian construction 2062 2063 Collective on ts 2064 2065 Input Parameters: 2066 + TS - The time stepping context 2067 . time_dummy - current time (not used) 2068 - X - Current state 2069 + U_tdummy - Time derivative of current state (not used) 2070 . shift - shift for du/dt term 2071 - actx - Landau context 2072 2073 Output Parameter: 2074 . Amat - Jacobian 2075 + Pmat - same as Amat 2076 2077 Level: beginner 2078 2079 .keywords: mesh 2080 .seealso: LandauCreateVelocitySpace(), LandauIFunction() 2081 @*/ 2082 PetscErrorCode LandauIJacobian(TS ts, PetscReal time_dummy, Vec X, Vec U_tdummy, PetscReal shift, Mat Amat, Mat Pmat, void *actx) 2083 { 2084 PetscErrorCode ierr; 2085 LandauCtx *ctx=(LandauCtx*)actx; 2086 PetscInt dim; 2087 DM dm; 2088 PetscContainer container; 2089 #if defined(PETSC_HAVE_THREADSAFETY) 2090 double starttime, endtime; 2091 #endif 2092 2093 PetscFunctionBegin; 2094 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 2095 ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr); 2096 if (!ctx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "no context"); 2097 if (Amat!=Pmat || Amat!=ctx->J) SETERRQ(ctx->comm, PETSC_ERR_PLIB, "Amat!=Pmat || Amat!=ctx->J"); 2098 ierr = DMGetDimension(ctx->dmv, &dim);CHKERRQ(ierr); 2099 /* get collision Jacobian into A */ 2100 ierr = PetscLogEventBegin(ctx->events[11],0,0,0,0);CHKERRQ(ierr); 2101 ierr = PetscLogEventBegin(ctx->events[9],0,0,0,0);CHKERRQ(ierr); 2102 #if defined(PETSC_HAVE_THREADSAFETY) 2103 starttime = MPI_Wtime(); 2104 #endif 2105 ierr = PetscInfo2(ts, "Adding just mass to Jacobian t=%g, shift=%g\n",(double)time_dummy,(double)shift);CHKERRQ(ierr); 2106 if (shift==0.0) SETERRQ(ctx->comm, PETSC_ERR_PLIB, "zero shift"); 2107 if (!ctx->aux_bool) SETERRQ(ctx->comm, PETSC_ERR_PLIB, "wrong state"); 2108 if (!ctx->use_matrix_mass) { 2109 ierr = LandauFormJacobian_Internal(X,ctx->J,dim,shift,(void*)ctx);CHKERRQ(ierr); 2110 } else { /* add mass */ 2111 ierr = MatAXPY(Pmat,shift,ctx->M,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 2112 } 2113 ctx->aux_bool = PETSC_FALSE; 2114 ierr = MatViewFromOptions(Pmat,NULL,"-landau_mat_view");CHKERRQ(ierr); 2115 /* set number species in Jacobian */ 2116 ierr = PetscObjectQuery((PetscObject) ctx->J, "Nf", (PetscObject *) &container);CHKERRQ(ierr); 2117 if (!container) { 2118 PetscInt *pNf; 2119 ierr = PetscContainerCreate(PETSC_COMM_SELF, &container);CHKERRQ(ierr); 2120 ierr = PetscMalloc(sizeof(PetscInt), &pNf);CHKERRQ(ierr); 2121 *pNf = ctx->num_species + 1000*ctx->numConcurrency; 2122 ierr = PetscContainerSetPointer(container, (void *)pNf);CHKERRQ(ierr); 2123 ierr = PetscContainerSetUserDestroy(container, MatrixNfDestroy);CHKERRQ(ierr); 2124 ierr = PetscObjectCompose((PetscObject)ctx->J, "Nf", (PetscObject) container);CHKERRQ(ierr); 2125 ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 2126 } 2127 #if defined(PETSC_HAVE_THREADSAFETY) 2128 endtime = MPI_Wtime(); 2129 ctx->times[0] += (endtime - starttime); 2130 #endif 2131 ierr = PetscLogEventEnd(ctx->events[9],0,0,0,0);CHKERRQ(ierr); 2132 ierr = PetscLogEventEnd(ctx->events[11],0,0,0,0);CHKERRQ(ierr); 2133 PetscFunctionReturn(0); 2134 } 2135