#include #include #include #include #include "data_bucket.h" PetscErrorCode subdivide_triangle(PetscReal v1[2],PetscReal v2[2],PetscReal v3[3],PetscInt depth,PetscInt max,PetscReal xi[],PetscInt *np) { PetscReal v12[2],v23[2],v31[2]; PetscInt i; PetscErrorCode ierr; PetscFunctionBegin; if (depth == max) { PetscReal cx[2]; cx[0] = (v1[0] + v2[0] + v3[0])/3.0; cx[1] = (v1[1] + v2[1] + v3[1])/3.0; xi[2*(*np)+0] = cx[0]; xi[2*(*np)+1] = cx[1]; (*np)++; PetscFunctionReturn(0); } /* calculate midpoints of each side */ for (i = 0; i < 2; i++) { v12[i] = (v1[i]+v2[i])/2.0; v23[i] = (v2[i]+v3[i])/2.0; v31[i] = (v3[i]+v1[i])/2.0; } /* recursively subdivide new triangles */ ierr = subdivide_triangle(v1,v12,v31,depth+1,max,xi,np);CHKERRQ(ierr); ierr = subdivide_triangle(v2,v23,v12,depth+1,max,xi,np);CHKERRQ(ierr); ierr = subdivide_triangle(v3,v31,v23,depth+1,max,xi,np);CHKERRQ(ierr); ierr = subdivide_triangle(v12,v23,v31,depth+1,max,xi,np);CHKERRQ(ierr); PetscFunctionReturn(0); } PetscErrorCode private_DMSwarmInsertPointsUsingCellDM_PLEX2D_SubDivide(DM dm,DM dmc,PetscInt nsub) { PetscErrorCode ierr; const PetscInt dim = 2; PetscInt q,npoints_q,e,nel,npe,pcnt,ps,pe,d,k,depth; PetscReal *xi; PetscReal **basis; Vec coorlocal; PetscSection coordSection; PetscScalar *elcoor = NULL; PetscReal *swarm_coor; PetscInt *swarm_cellid; PetscReal v1[2],v2[2],v3[2]; PetscFunctionBegin; npoints_q = 1; for (d=0; dcell, 1->edge, 2->vert */ ierr = DMPlexGetHeightStratum(dmc,0,&ps,&pe);CHKERRQ(ierr); nel = pe - ps; ierr = DMSwarmSetLocalSizes(dm,npoints_q*nel,-1);CHKERRQ(ierr); ierr = DMSwarmGetField(dm,DMSwarmPICField_coor,NULL,NULL,(void**)&swarm_coor);CHKERRQ(ierr); ierr = DMSwarmGetField(dm,DMSwarmPICField_cellid,NULL,NULL,(void**)&swarm_cellid);CHKERRQ(ierr); ierr = DMGetCoordinatesLocal(dmc,&coorlocal);CHKERRQ(ierr); ierr = DMGetCoordinateSection(dmc,&coordSection);CHKERRQ(ierr); pcnt = 0; for (e=0; ecell, 1->edge, 2->vert */ ierr = DMPlexGetHeightStratum(dmc,0,&ps,&pe);CHKERRQ(ierr); nel = pe - ps; ierr = DMSwarmSetLocalSizes(dm,npoints_q*nel,-1);CHKERRQ(ierr); ierr = DMSwarmGetField(dm,DMSwarmPICField_coor,NULL,NULL,(void**)&swarm_coor);CHKERRQ(ierr); ierr = DMSwarmGetField(dm,DMSwarmPICField_cellid,NULL,NULL,(void**)&swarm_cellid);CHKERRQ(ierr); ierr = DMGetCoordinatesLocal(dmc,&coorlocal);CHKERRQ(ierr); ierr = DMGetCoordinateSection(dmc,&coordSection);CHKERRQ(ierr); pcnt = 0; for (e=0; e mapped to element %D xi(%+1.4e,%+1.4e)\n",p,point.x,point.y,e,xi_p[0],xi_p[1]); } */ ierr = ComputeLocalCoordinateAffine2d(coor_p,elcoor,xi_p,&dJ);CHKERRQ(ierr); /* PetscPrintf(PETSC_COMM_SELF,"[p=%D] x(%+1.4e,%+1.4e) -> mapped to element %D xi(%+1.4e,%+1.4e)\n",p,point.x,point.y,e,xi_p[0],xi_p[1]); */ /* point_located = PETSC_TRUE; if (xi_p[0] < 0.0) { if (xi_p[0] > -PLEX_C_EPS) { xi_p[0] = 0.0; } else { point_located = PETSC_FALSE; } } if (xi_p[1] < 0.0) { if (xi_p[1] > -PLEX_C_EPS) { xi_p[1] = 0.0; } else { point_located = PETSC_FALSE; } } if (xi_p[1] > (1.0-xi_p[0])) { if ((xi_p[1] - 1.0 + xi_p[0]) < PLEX_C_EPS) { xi_p[1] = 1.0 - xi_p[0]; } else { point_located = PETSC_FALSE; } } if (!point_located){ PetscPrintf(PETSC_COMM_SELF,"[Error] xi,eta = %+1.8e, %+1.8e\n",xi_p[0],xi_p[1]); PetscPrintf(PETSC_COMM_SELF,"[Error] Failed to locate point (%1.8e,%1.8e) in local mesh (cell %D) with triangle coords (%1.8e,%1.8e) : (%1.8e,%1.8e) : (%1.8e,%1.8e)\n",coor_p[0],coor_p[1],e,elcoor[0],elcoor[1],elcoor[2],elcoor[3],elcoor[4],elcoor[5]); } if (!point_located) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_SUP,"Failed to locate point (%1.8e,%1.8e) in local mesh (cell %D)\n",coor_p[0],coor_p[1],e); */ Ni[0] = 1.0 - xi_p[0] - xi_p[1]; Ni[1] = xi_p[0]; Ni[2] = xi_p[1]; point_located = PETSC_TRUE; for (k=0; k<3; k++) { if (PetscRealPart(Ni[k]) < -PLEX_C_EPS) point_located = PETSC_FALSE; if (PetscRealPart(Ni[k]) > (1.0+PLEX_C_EPS)) point_located = PETSC_FALSE; } if (!point_located){ PetscPrintf(PETSC_COMM_SELF,"[Error] xi,eta = %+1.8e, %+1.8e\n",(double)xi_p[0],(double)xi_p[1]); PetscPrintf(PETSC_COMM_SELF,"[Error] Failed to locate point (%1.8e,%1.8e) in local mesh (cell %D) with triangle coords (%1.8e,%1.8e) : (%1.8e,%1.8e) : (%1.8e,%1.8e)\n",(double)coor_p[0],(double)coor_p[1],e,(double)PetscRealPart(elcoor[0]),(double)PetscRealPart(elcoor[1]),(double)PetscRealPart(elcoor[2]),(double)PetscRealPart(elcoor[3]),(double)PetscRealPart(elcoor[4]),(double)PetscRealPart(elcoor[5])); } if (!point_located) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_SUP,"Failed to locate point (%1.8e,%1.8e) in local mesh (cell %D)\n",(double)coor_p[0],(double)coor_p[1],e); for (k=0; k<3; k++) { Ni[k] = Ni[k] * dJ; elfield[k] = Ni[k] * swarm_field[p]; } ierr = DMPlexVecRestoreClosure(dm,coordSection,coor_l,e,NULL,&elcoor);CHKERRQ(ierr); ierr = DMPlexVecSetClosure(dm, NULL,v_field_l, e, elfield, ADD_VALUES);CHKERRQ(ierr); ierr = DMPlexVecSetClosure(dm, NULL,denom_l, e, Ni, ADD_VALUES);CHKERRQ(ierr); } ierr = DMSwarmRestoreField(swarm,DMSwarmPICField_cellid,NULL,NULL,(void**)&mpfield_cell);CHKERRQ(ierr); ierr = DMSwarmRestoreField(swarm,DMSwarmPICField_coor,NULL,NULL,(void**)&mpfield_coor);CHKERRQ(ierr); ierr = DMLocalToGlobalBegin(dm,v_field_l,ADD_VALUES,v_field);CHKERRQ(ierr); ierr = DMLocalToGlobalEnd(dm,v_field_l,ADD_VALUES,v_field);CHKERRQ(ierr); ierr = DMLocalToGlobalBegin(dm,denom_l,ADD_VALUES,denom);CHKERRQ(ierr); ierr = DMLocalToGlobalEnd(dm,denom_l,ADD_VALUES,denom);CHKERRQ(ierr); ierr = VecPointwiseDivide(v_field,v_field,denom);CHKERRQ(ierr); ierr = DMRestoreLocalVector(dm,&v_field_l);CHKERRQ(ierr); ierr = DMRestoreLocalVector(dm,&denom_l);CHKERRQ(ierr); ierr = DMRestoreGlobalVector(dm,&denom);CHKERRQ(ierr); PetscFunctionReturn(0); } PetscErrorCode private_DMSwarmProjectFields_PLEX(DM swarm,DM celldm,PetscInt project_type,PetscInt nfields,DataField dfield[],Vec vecs[]) { PetscErrorCode ierr; PetscInt f,dim; PetscFunctionBegin; ierr = DMGetDimension(swarm,&dim);CHKERRQ(ierr); switch (dim) { case 2: for (f=0; f