1cb1e1211SMatthew G Knepley #include <petsc-private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2cb1e1211SMatthew G Knepley 3cb1e1211SMatthew G Knepley #undef __FUNCT__ 4cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetScale" 5cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale) 6cb1e1211SMatthew G Knepley { 7cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 8cb1e1211SMatthew G Knepley 9cb1e1211SMatthew G Knepley PetscFunctionBegin; 10cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 11cb1e1211SMatthew G Knepley PetscValidPointer(scale, 3); 12cb1e1211SMatthew G Knepley *scale = mesh->scale[unit]; 13cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 14cb1e1211SMatthew G Knepley } 15cb1e1211SMatthew G Knepley 16cb1e1211SMatthew G Knepley #undef __FUNCT__ 17cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexSetScale" 18cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale) 19cb1e1211SMatthew G Knepley { 20cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 21cb1e1211SMatthew G Knepley 22cb1e1211SMatthew G Knepley PetscFunctionBegin; 23cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 24cb1e1211SMatthew G Knepley mesh->scale[unit] = scale; 25cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 26cb1e1211SMatthew G Knepley } 27cb1e1211SMatthew G Knepley 28cb1e1211SMatthew G Knepley PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k) 29cb1e1211SMatthew G Knepley { 30cb1e1211SMatthew G Knepley switch (i) { 31cb1e1211SMatthew G Knepley case 0: 32cb1e1211SMatthew G Knepley switch (j) { 33cb1e1211SMatthew G Knepley case 0: return 0; 34cb1e1211SMatthew G Knepley case 1: 35cb1e1211SMatthew G Knepley switch (k) { 36cb1e1211SMatthew G Knepley case 0: return 0; 37cb1e1211SMatthew G Knepley case 1: return 0; 38cb1e1211SMatthew G Knepley case 2: return 1; 39cb1e1211SMatthew G Knepley } 40cb1e1211SMatthew G Knepley case 2: 41cb1e1211SMatthew G Knepley switch (k) { 42cb1e1211SMatthew G Knepley case 0: return 0; 43cb1e1211SMatthew G Knepley case 1: return -1; 44cb1e1211SMatthew G Knepley case 2: return 0; 45cb1e1211SMatthew G Knepley } 46cb1e1211SMatthew G Knepley } 47cb1e1211SMatthew G Knepley case 1: 48cb1e1211SMatthew G Knepley switch (j) { 49cb1e1211SMatthew G Knepley case 0: 50cb1e1211SMatthew G Knepley switch (k) { 51cb1e1211SMatthew G Knepley case 0: return 0; 52cb1e1211SMatthew G Knepley case 1: return 0; 53cb1e1211SMatthew G Knepley case 2: return -1; 54cb1e1211SMatthew G Knepley } 55cb1e1211SMatthew G Knepley case 1: return 0; 56cb1e1211SMatthew G Knepley case 2: 57cb1e1211SMatthew G Knepley switch (k) { 58cb1e1211SMatthew G Knepley case 0: return 1; 59cb1e1211SMatthew G Knepley case 1: return 0; 60cb1e1211SMatthew G Knepley case 2: return 0; 61cb1e1211SMatthew G Knepley } 62cb1e1211SMatthew G Knepley } 63cb1e1211SMatthew G Knepley case 2: 64cb1e1211SMatthew G Knepley switch (j) { 65cb1e1211SMatthew G Knepley case 0: 66cb1e1211SMatthew G Knepley switch (k) { 67cb1e1211SMatthew G Knepley case 0: return 0; 68cb1e1211SMatthew G Knepley case 1: return 1; 69cb1e1211SMatthew G Knepley case 2: return 0; 70cb1e1211SMatthew G Knepley } 71cb1e1211SMatthew G Knepley case 1: 72cb1e1211SMatthew G Knepley switch (k) { 73cb1e1211SMatthew G Knepley case 0: return -1; 74cb1e1211SMatthew G Knepley case 1: return 0; 75cb1e1211SMatthew G Knepley case 2: return 0; 76cb1e1211SMatthew G Knepley } 77cb1e1211SMatthew G Knepley case 2: return 0; 78cb1e1211SMatthew G Knepley } 79cb1e1211SMatthew G Knepley } 80cb1e1211SMatthew G Knepley return 0; 81cb1e1211SMatthew G Knepley } 82cb1e1211SMatthew G Knepley 83cb1e1211SMatthew G Knepley #undef __FUNCT__ 84cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody" 85cb1e1211SMatthew G Knepley /*@C 86cb1e1211SMatthew G Knepley DMPlexCreateRigidBody - create rigid body modes from coordinates 87cb1e1211SMatthew G Knepley 88cb1e1211SMatthew G Knepley Collective on DM 89cb1e1211SMatthew G Knepley 90cb1e1211SMatthew G Knepley Input Arguments: 91cb1e1211SMatthew G Knepley + dm - the DM 92cb1e1211SMatthew G Knepley . section - the local section associated with the rigid field, or NULL for the default section 93cb1e1211SMatthew G Knepley - globalSection - the global section associated with the rigid field, or NULL for the default section 94cb1e1211SMatthew G Knepley 95cb1e1211SMatthew G Knepley Output Argument: 96cb1e1211SMatthew G Knepley . sp - the null space 97cb1e1211SMatthew G Knepley 98cb1e1211SMatthew G Knepley Note: This is necessary to take account of Dirichlet conditions on the displacements 99cb1e1211SMatthew G Knepley 100cb1e1211SMatthew G Knepley Level: advanced 101cb1e1211SMatthew G Knepley 102cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate() 103cb1e1211SMatthew G Knepley @*/ 104cb1e1211SMatthew G Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscSection section, PetscSection globalSection, MatNullSpace *sp) 105cb1e1211SMatthew G Knepley { 106cb1e1211SMatthew G Knepley MPI_Comm comm; 107cb1e1211SMatthew G Knepley Vec coordinates, localMode, mode[6]; 108cb1e1211SMatthew G Knepley PetscSection coordSection; 109cb1e1211SMatthew G Knepley PetscScalar *coords; 110cb1e1211SMatthew G Knepley PetscInt dim, vStart, vEnd, v, n, m, d, i, j; 111cb1e1211SMatthew G Knepley PetscErrorCode ierr; 112cb1e1211SMatthew G Knepley 113cb1e1211SMatthew G Knepley PetscFunctionBegin; 114cb1e1211SMatthew G Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 115cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 116cb1e1211SMatthew G Knepley if (dim == 1) { 117cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 118cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 119cb1e1211SMatthew G Knepley } 120cb1e1211SMatthew G Knepley if (!section) {ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr);} 121cb1e1211SMatthew G Knepley if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);} 122cb1e1211SMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 123cb1e1211SMatthew G Knepley ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 124cb1e1211SMatthew G Knepley ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 125cb1e1211SMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 126cb1e1211SMatthew G Knepley m = (dim*(dim+1))/2; 127cb1e1211SMatthew G Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 128cb1e1211SMatthew G Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 129cb1e1211SMatthew G Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 130cb1e1211SMatthew G Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 131cb1e1211SMatthew G Knepley /* Assume P1 */ 132cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localMode);CHKERRQ(ierr); 133cb1e1211SMatthew G Knepley for (d = 0; d < dim; ++d) { 134cb1e1211SMatthew G Knepley PetscScalar values[3] = {0.0, 0.0, 0.0}; 135cb1e1211SMatthew G Knepley 136cb1e1211SMatthew G Knepley values[d] = 1.0; 137cb1e1211SMatthew G Knepley ierr = VecSet(localMode, 0.0);CHKERRQ(ierr); 138cb1e1211SMatthew G Knepley for (v = vStart; v < vEnd; ++v) { 139cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr); 140cb1e1211SMatthew G Knepley } 141cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 142cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 143cb1e1211SMatthew G Knepley } 144cb1e1211SMatthew G Knepley ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 145cb1e1211SMatthew G Knepley for (d = dim; d < dim*(dim+1)/2; ++d) { 146cb1e1211SMatthew G Knepley PetscInt i, j, k = dim > 2 ? d - dim : d; 147cb1e1211SMatthew G Knepley 148cb1e1211SMatthew G Knepley ierr = VecSet(localMode, 0.0);CHKERRQ(ierr); 149cb1e1211SMatthew G Knepley for (v = vStart; v < vEnd; ++v) { 150cb1e1211SMatthew G Knepley PetscScalar values[3] = {0.0, 0.0, 0.0}; 151cb1e1211SMatthew G Knepley PetscInt off; 152cb1e1211SMatthew G Knepley 153cb1e1211SMatthew G Knepley ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr); 154cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) { 155cb1e1211SMatthew G Knepley for (j = 0; j < dim; ++j) { 156cb1e1211SMatthew G Knepley values[j] += epsilon(i, j, k)*PetscRealPart(coords[off+i]); 157cb1e1211SMatthew G Knepley } 158cb1e1211SMatthew G Knepley } 159cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr); 160cb1e1211SMatthew G Knepley } 161cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 162cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 163cb1e1211SMatthew G Knepley } 164cb1e1211SMatthew G Knepley ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 165cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localMode);CHKERRQ(ierr); 166cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 167cb1e1211SMatthew G Knepley /* Orthonormalize system */ 168cb1e1211SMatthew G Knepley for (i = dim; i < m; ++i) { 169cb1e1211SMatthew G Knepley PetscScalar dots[6]; 170cb1e1211SMatthew G Knepley 171cb1e1211SMatthew G Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 172cb1e1211SMatthew G Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 173cb1e1211SMatthew G Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 174cb1e1211SMatthew G Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 175cb1e1211SMatthew G Knepley } 176cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 177cb1e1211SMatthew G Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 178cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 179cb1e1211SMatthew G Knepley } 180cb1e1211SMatthew G Knepley /******************************************************************************* 181cb1e1211SMatthew G Knepley This should be in a separate Discretization object, but I am not sure how to lay 182cb1e1211SMatthew G Knepley it out yet, so I am stuffing things here while I experiment. 183cb1e1211SMatthew G Knepley *******************************************************************************/ 184cb1e1211SMatthew G Knepley #undef __FUNCT__ 185cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexSetFEMIntegration" 186cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetFEMIntegration(DM dm, 187cb1e1211SMatthew G Knepley PetscErrorCode (*integrateResidualFEM)(PetscInt, PetscInt, PetscInt, PetscQuadrature[], const PetscScalar[], 188cb1e1211SMatthew G Knepley const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[], 189cb1e1211SMatthew G Knepley void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 190cb1e1211SMatthew G Knepley void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), PetscScalar[]), 191cb1e1211SMatthew G Knepley PetscErrorCode (*integrateJacobianActionFEM)(PetscInt, PetscInt, PetscInt, PetscQuadrature[], const PetscScalar[], const PetscScalar[], 192cb1e1211SMatthew G Knepley const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[], 193cb1e1211SMatthew G Knepley void (**)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 194cb1e1211SMatthew G Knepley void (**)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 195cb1e1211SMatthew G Knepley void (**)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 196cb1e1211SMatthew G Knepley void (**)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), PetscScalar[]), 197cb1e1211SMatthew G Knepley PetscErrorCode (*integrateJacobianFEM)(PetscInt, PetscInt, PetscInt, PetscInt, PetscQuadrature[], const PetscScalar[], 198cb1e1211SMatthew G Knepley const PetscReal[], const PetscReal[], const PetscReal[], const PetscReal[], 199cb1e1211SMatthew G Knepley void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 200cb1e1211SMatthew G Knepley void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 201cb1e1211SMatthew G Knepley void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), 202cb1e1211SMatthew G Knepley void (*)(const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]), PetscScalar[])) 203cb1e1211SMatthew G Knepley { 204cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 205cb1e1211SMatthew G Knepley 206cb1e1211SMatthew G Knepley PetscFunctionBegin; 207cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 208cb1e1211SMatthew G Knepley mesh->integrateResidualFEM = integrateResidualFEM; 209cb1e1211SMatthew G Knepley mesh->integrateJacobianActionFEM = integrateJacobianActionFEM; 210cb1e1211SMatthew G Knepley mesh->integrateJacobianFEM = integrateJacobianFEM; 211cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 212cb1e1211SMatthew G Knepley } 213cb1e1211SMatthew G Knepley 214cb1e1211SMatthew G Knepley #undef __FUNCT__ 215cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal" 216cb1e1211SMatthew G Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, PetscInt numComp, PetscScalar (**funcs)(const PetscReal []), InsertMode mode, Vec localX) 217cb1e1211SMatthew G Knepley { 218cb1e1211SMatthew G Knepley Vec coordinates; 219cb1e1211SMatthew G Knepley PetscSection section, cSection; 220cb1e1211SMatthew G Knepley PetscInt dim, vStart, vEnd, v, c, d; 221cb1e1211SMatthew G Knepley PetscScalar *values, *cArray; 222cb1e1211SMatthew G Knepley PetscReal *coords; 223cb1e1211SMatthew G Knepley PetscErrorCode ierr; 224cb1e1211SMatthew G Knepley 225cb1e1211SMatthew G Knepley PetscFunctionBegin; 226cb1e1211SMatthew G Knepley ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 227cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 228cb1e1211SMatthew G Knepley ierr = DMPlexGetCoordinateSection(dm, &cSection);CHKERRQ(ierr); 229cb1e1211SMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 230cb1e1211SMatthew G Knepley ierr = PetscMalloc(numComp * sizeof(PetscScalar), &values);CHKERRQ(ierr); 231cb1e1211SMatthew G Knepley ierr = VecGetArray(coordinates, &cArray);CHKERRQ(ierr); 232cb1e1211SMatthew G Knepley ierr = PetscSectionGetDof(cSection, vStart, &dim);CHKERRQ(ierr); 233cb1e1211SMatthew G Knepley ierr = PetscMalloc(dim * sizeof(PetscReal),&coords);CHKERRQ(ierr); 234cb1e1211SMatthew G Knepley for (v = vStart; v < vEnd; ++v) { 235cb1e1211SMatthew G Knepley PetscInt dof, off; 236cb1e1211SMatthew G Knepley 237cb1e1211SMatthew G Knepley ierr = PetscSectionGetDof(cSection, v, &dof);CHKERRQ(ierr); 238cb1e1211SMatthew G Knepley ierr = PetscSectionGetOffset(cSection, v, &off);CHKERRQ(ierr); 239cb1e1211SMatthew G Knepley if (dof > dim) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cannot have more coordinates %d then dimensions %d", dof, dim); 240cb1e1211SMatthew G Knepley for (d = 0; d < dof; ++d) coords[d] = PetscRealPart(cArray[off+d]); 241cb1e1211SMatthew G Knepley for (c = 0; c < numComp; ++c) values[c] = (*funcs[c])(coords); 242cb1e1211SMatthew G Knepley ierr = VecSetValuesSection(localX, section, v, values, mode);CHKERRQ(ierr); 243cb1e1211SMatthew G Knepley } 244cb1e1211SMatthew G Knepley ierr = VecRestoreArray(coordinates, &cArray);CHKERRQ(ierr); 245cb1e1211SMatthew G Knepley /* Temporary, must be replaced by a projection on the finite element basis */ 246cb1e1211SMatthew G Knepley { 247cb1e1211SMatthew G Knepley PetscInt eStart = 0, eEnd = 0, e, depth; 248cb1e1211SMatthew G Knepley 249cb1e1211SMatthew G Knepley ierr = DMPlexGetLabelSize(dm, "depth", &depth);CHKERRQ(ierr); 250cb1e1211SMatthew G Knepley --depth; 251cb1e1211SMatthew G Knepley if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);} 252cb1e1211SMatthew G Knepley for (e = eStart; e < eEnd; ++e) { 253cb1e1211SMatthew G Knepley const PetscInt *cone = NULL; 254cb1e1211SMatthew G Knepley PetscInt coneSize, d; 255cb1e1211SMatthew G Knepley PetscScalar *coordsA, *coordsB; 256cb1e1211SMatthew G Knepley 257cb1e1211SMatthew G Knepley ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr); 258cb1e1211SMatthew G Knepley ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr); 259cb1e1211SMatthew G Knepley if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Cone size %d for point %d should be 2", coneSize, e); 260cb1e1211SMatthew G Knepley ierr = VecGetValuesSection(coordinates, cSection, cone[0], &coordsA);CHKERRQ(ierr); 261cb1e1211SMatthew G Knepley ierr = VecGetValuesSection(coordinates, cSection, cone[1], &coordsB);CHKERRQ(ierr); 262cb1e1211SMatthew G Knepley for (d = 0; d < dim; ++d) { 263cb1e1211SMatthew G Knepley coords[d] = 0.5*(PetscRealPart(coordsA[d]) + PetscRealPart(coordsB[d])); 264cb1e1211SMatthew G Knepley } 265cb1e1211SMatthew G Knepley for (c = 0; c < numComp; ++c) values[c] = (*funcs[c])(coords); 266cb1e1211SMatthew G Knepley ierr = VecSetValuesSection(localX, section, e, values, mode);CHKERRQ(ierr); 267cb1e1211SMatthew G Knepley } 268cb1e1211SMatthew G Knepley } 269cb1e1211SMatthew G Knepley 270cb1e1211SMatthew G Knepley ierr = PetscFree(coords);CHKERRQ(ierr); 271cb1e1211SMatthew G Knepley ierr = PetscFree(values);CHKERRQ(ierr); 272cb1e1211SMatthew G Knepley #if 0 273cb1e1211SMatthew G Knepley const PetscInt localDof = this->_mesh->sizeWithBC(s, *cells->begin()); 274cb1e1211SMatthew G Knepley PetscReal detJ; 275cb1e1211SMatthew G Knepley 276cb1e1211SMatthew G Knepley ierr = PetscMalloc(localDof * sizeof(PetscScalar), &values);CHKERRQ(ierr); 277cb1e1211SMatthew G Knepley ierr = PetscMalloc2(dim,PetscReal,&v0,dim*dim,PetscReal,&J);CHKERRQ(ierr); 278cb1e1211SMatthew G Knepley ALE::ISieveVisitor::PointRetriever<PETSC_MESH_TYPE::sieve_type> pV(PetscPowInt(this->_mesh->getSieve()->getMaxConeSize(),dim+1), true); 279cb1e1211SMatthew G Knepley 280cb1e1211SMatthew G Knepley for (PetscInt c = cStart; c < cEnd; ++c) { 281cb1e1211SMatthew G Knepley ALE::ISieveTraversal<PETSC_MESH_TYPE::sieve_type>::orientedClosure(*this->_mesh->getSieve(), c, pV); 282cb1e1211SMatthew G Knepley const PETSC_MESH_TYPE::point_type *oPoints = pV.getPoints(); 283cb1e1211SMatthew G Knepley const int oSize = pV.getSize(); 284cb1e1211SMatthew G Knepley int v = 0; 285cb1e1211SMatthew G Knepley 286cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, NULL, &detJ);CHKERRQ(ierr); 287cb1e1211SMatthew G Knepley for (PetscInt cl = 0; cl < oSize; ++cl) { 288cb1e1211SMatthew G Knepley const PetscInt fDim; 289cb1e1211SMatthew G Knepley 290cb1e1211SMatthew G Knepley ierr = PetscSectionGetDof(oPoints[cl], &fDim);CHKERRQ(ierr); 291cb1e1211SMatthew G Knepley if (pointDim) { 292cb1e1211SMatthew G Knepley for (PetscInt d = 0; d < fDim; ++d, ++v) { 293cb1e1211SMatthew G Knepley values[v] = (*this->_options.integrate)(v0, J, v, initFunc); 294cb1e1211SMatthew G Knepley } 295cb1e1211SMatthew G Knepley } 296cb1e1211SMatthew G Knepley } 297cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, NULL, localX, c, values);CHKERRQ(ierr); 298cb1e1211SMatthew G Knepley pV.clear(); 299cb1e1211SMatthew G Knepley } 300cb1e1211SMatthew G Knepley ierr = PetscFree2(v0,J);CHKERRQ(ierr); 301cb1e1211SMatthew G Knepley ierr = PetscFree(values);CHKERRQ(ierr); 302cb1e1211SMatthew G Knepley #endif 303cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 304cb1e1211SMatthew G Knepley } 305cb1e1211SMatthew G Knepley 306cb1e1211SMatthew G Knepley #undef __FUNCT__ 307cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunction" 308cb1e1211SMatthew G Knepley /*@C 309cb1e1211SMatthew G Knepley DMPlexProjectFunction - This projects the given function into the function space provided. 310cb1e1211SMatthew G Knepley 311cb1e1211SMatthew G Knepley Input Parameters: 312cb1e1211SMatthew G Knepley + dm - The DM 313cb1e1211SMatthew G Knepley . numComp - The number of components (functions) 314cb1e1211SMatthew G Knepley . funcs - The coordinate functions to evaluate 315cb1e1211SMatthew G Knepley - mode - The insertion mode for values 316cb1e1211SMatthew G Knepley 317cb1e1211SMatthew G Knepley Output Parameter: 318cb1e1211SMatthew G Knepley . X - vector 319cb1e1211SMatthew G Knepley 320cb1e1211SMatthew G Knepley Level: developer 321cb1e1211SMatthew G Knepley 322cb1e1211SMatthew G Knepley Note: 323cb1e1211SMatthew G Knepley This currently just calls the function with the coordinates of each vertex and edge midpoint, and stores the result in a vector. 324cb1e1211SMatthew G Knepley We will eventually fix it. 325cb1e1211SMatthew G Knepley 326878cb397SSatish Balay .seealso: DMPlexComputeL2Diff() 327878cb397SSatish Balay @*/ 328cb1e1211SMatthew G Knepley PetscErrorCode DMPlexProjectFunction(DM dm, PetscInt numComp, PetscScalar (**funcs)(const PetscReal []), InsertMode mode, Vec X) 329cb1e1211SMatthew G Knepley { 330cb1e1211SMatthew G Knepley Vec localX; 331cb1e1211SMatthew G Knepley PetscErrorCode ierr; 332cb1e1211SMatthew G Knepley 333cb1e1211SMatthew G Knepley PetscFunctionBegin; 334cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 335cb1e1211SMatthew G Knepley ierr = DMPlexProjectFunctionLocal(dm, numComp, funcs, mode, localX);CHKERRQ(ierr); 336cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr); 337cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr); 338cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 339cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 340cb1e1211SMatthew G Knepley } 341cb1e1211SMatthew G Knepley 342cb1e1211SMatthew G Knepley #undef __FUNCT__ 343cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff" 344cb1e1211SMatthew G Knepley /*@C 345cb1e1211SMatthew G Knepley DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 346cb1e1211SMatthew G Knepley 347cb1e1211SMatthew G Knepley Input Parameters: 348cb1e1211SMatthew G Knepley + dm - The DM 349cb1e1211SMatthew G Knepley . quad - The PetscQuadrature object for each field 350cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component 351cb1e1211SMatthew G Knepley - X - The coefficient vector u_h 352cb1e1211SMatthew G Knepley 353cb1e1211SMatthew G Knepley Output Parameter: 354cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2 355cb1e1211SMatthew G Knepley 356cb1e1211SMatthew G Knepley Level: developer 357cb1e1211SMatthew G Knepley 358cb1e1211SMatthew G Knepley .seealso: DMPlexProjectFunction() 359878cb397SSatish Balay @*/ 360cb1e1211SMatthew G Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, PetscQuadrature quad[], PetscScalar (**funcs)(const PetscReal []), Vec X, PetscReal *diff) 361cb1e1211SMatthew G Knepley { 362cb1e1211SMatthew G Knepley const PetscInt debug = 0; 363cb1e1211SMatthew G Knepley PetscSection section; 364cb1e1211SMatthew G Knepley Vec localX; 365cb1e1211SMatthew G Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 366cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 367cb1e1211SMatthew G Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp; 368cb1e1211SMatthew G Knepley PetscErrorCode ierr; 369cb1e1211SMatthew G Knepley 370cb1e1211SMatthew G Knepley PetscFunctionBegin; 371cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 372cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 373cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 374cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 375cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 376cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 377cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 378cb1e1211SMatthew G Knepley numComponents += quad[field].numComponents; 379cb1e1211SMatthew G Knepley } 380cb1e1211SMatthew G Knepley ierr = DMPlexProjectFunctionLocal(dm, numComponents, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 381cb1e1211SMatthew G Knepley ierr = PetscMalloc4(dim,PetscReal,&coords,dim,PetscReal,&v0,dim*dim,PetscReal,&J,dim*dim,PetscReal,&invJ);CHKERRQ(ierr); 382cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 383cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 384cb1e1211SMatthew G Knepley PetscScalar *x; 385cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 386cb1e1211SMatthew G Knepley 387cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr); 388cb1e1211SMatthew G Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 389cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 390cb1e1211SMatthew G Knepley 391cb1e1211SMatthew G Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 392cb1e1211SMatthew G Knepley const PetscInt numQuadPoints = quad[field].numQuadPoints; 393cb1e1211SMatthew G Knepley const PetscReal *quadPoints = quad[field].quadPoints; 394cb1e1211SMatthew G Knepley const PetscReal *quadWeights = quad[field].quadWeights; 395cb1e1211SMatthew G Knepley const PetscInt numBasisFuncs = quad[field].numBasisFuncs; 396cb1e1211SMatthew G Knepley const PetscInt numBasisComps = quad[field].numComponents; 397cb1e1211SMatthew G Knepley const PetscReal *basis = quad[field].basis; 398cb1e1211SMatthew G Knepley PetscInt q, d, e, fc, f; 399cb1e1211SMatthew G Knepley 400cb1e1211SMatthew G Knepley if (debug) { 401cb1e1211SMatthew G Knepley char title[1024]; 402cb1e1211SMatthew G Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 403cb1e1211SMatthew G Knepley ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr); 404cb1e1211SMatthew G Knepley } 405cb1e1211SMatthew G Knepley for (q = 0; q < numQuadPoints; ++q) { 406cb1e1211SMatthew G Knepley for (d = 0; d < dim; d++) { 407cb1e1211SMatthew G Knepley coords[d] = v0[d]; 408cb1e1211SMatthew G Knepley for (e = 0; e < dim; e++) { 409cb1e1211SMatthew G Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 410cb1e1211SMatthew G Knepley } 411cb1e1211SMatthew G Knepley } 412cb1e1211SMatthew G Knepley for (fc = 0; fc < numBasisComps; ++fc) { 413cb1e1211SMatthew G Knepley const PetscReal funcVal = PetscRealPart((*funcs[comp+fc])(coords)); 414cb1e1211SMatthew G Knepley PetscReal interpolant = 0.0; 415cb1e1211SMatthew G Knepley for (f = 0; f < numBasisFuncs; ++f) { 416cb1e1211SMatthew G Knepley const PetscInt fidx = f*numBasisComps+fc; 417cb1e1211SMatthew G Knepley interpolant += PetscRealPart(x[fieldOffset+fidx])*basis[q*numBasisFuncs*numBasisComps+fidx]; 418cb1e1211SMatthew G Knepley } 419cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d diff %g\n", c, field, PetscSqr(interpolant - funcVal)*quadWeights[q]*detJ);CHKERRQ(ierr);} 420cb1e1211SMatthew G Knepley elemDiff += PetscSqr(interpolant - funcVal)*quadWeights[q]*detJ; 421cb1e1211SMatthew G Knepley } 422cb1e1211SMatthew G Knepley } 423cb1e1211SMatthew G Knepley comp += numBasisComps; 424cb1e1211SMatthew G Knepley fieldOffset += numBasisFuncs*numBasisComps; 425cb1e1211SMatthew G Knepley } 426cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 427cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 428cb1e1211SMatthew G Knepley localDiff += elemDiff; 429cb1e1211SMatthew G Knepley } 430cb1e1211SMatthew G Knepley ierr = PetscFree4(coords,v0,J,invJ);CHKERRQ(ierr); 431cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 432cb1e1211SMatthew G Knepley ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PETSC_COMM_WORLD);CHKERRQ(ierr); 433cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 434cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 435cb1e1211SMatthew G Knepley } 436cb1e1211SMatthew G Knepley 437cb1e1211SMatthew G Knepley #undef __FUNCT__ 438cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeResidualFEM" 439cb1e1211SMatthew G Knepley /*@ 440cb1e1211SMatthew G Knepley DMPlexComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 441cb1e1211SMatthew G Knepley 442cb1e1211SMatthew G Knepley Input Parameters: 443cb1e1211SMatthew G Knepley + dm - The mesh 444cb1e1211SMatthew G Knepley . X - Local input vector 445cb1e1211SMatthew G Knepley - user - The user context 446cb1e1211SMatthew G Knepley 447cb1e1211SMatthew G Knepley Output Parameter: 448cb1e1211SMatthew G Knepley . F - Local output vector 449cb1e1211SMatthew G Knepley 450cb1e1211SMatthew G Knepley Note: 451cb1e1211SMatthew G Knepley The second member of the user context must be an FEMContext. 452cb1e1211SMatthew G Knepley 453cb1e1211SMatthew G Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 454cb1e1211SMatthew G Knepley like a GPU, or vectorize on a multicore machine. 455cb1e1211SMatthew G Knepley 4560059ad2aSSatish Balay Level: developer 4570059ad2aSSatish Balay 458cb1e1211SMatthew G Knepley .seealso: DMPlexComputeJacobianActionFEM() 459878cb397SSatish Balay @*/ 460cb1e1211SMatthew G Knepley PetscErrorCode DMPlexComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 461cb1e1211SMatthew G Knepley { 462cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 463cb1e1211SMatthew G Knepley PetscFEM *fem = (PetscFEM*) &((DM*) user)[1]; 464cb1e1211SMatthew G Knepley PetscQuadrature *quad = fem->quad; 465*652b88e8SMatthew G. Knepley PetscQuadrature *quadBd = fem->quadBd; 466cb1e1211SMatthew G Knepley PetscSection section; 467cb1e1211SMatthew G Knepley PetscReal *v0, *J, *invJ, *detJ; 468cb1e1211SMatthew G Knepley PetscScalar *elemVec, *u; 469cb1e1211SMatthew G Knepley PetscInt dim, numFields, field, numBatchesTmp = 1, numCells, cStart, cEnd, c; 470*652b88e8SMatthew G. Knepley PetscInt cellDof, numComponents; 471*652b88e8SMatthew G. Knepley PetscBool has; 472cb1e1211SMatthew G Knepley PetscErrorCode ierr; 473cb1e1211SMatthew G Knepley 474cb1e1211SMatthew G Knepley PetscFunctionBegin; 475cb1e1211SMatthew G Knepley /* ierr = PetscLogEventBegin(ResidualFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 476cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 477cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 478cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 479cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 480cb1e1211SMatthew G Knepley numCells = cEnd - cStart; 481*652b88e8SMatthew G. Knepley for (field = 0, cellDof = 0, numComponents = 0; field < numFields; ++field) { 482cb1e1211SMatthew G Knepley cellDof += quad[field].numBasisFuncs*quad[field].numComponents; 483cb1e1211SMatthew G Knepley numComponents += quad[field].numComponents; 484cb1e1211SMatthew G Knepley } 485cb1e1211SMatthew G Knepley ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr); 486cb1e1211SMatthew G Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 487cb1e1211SMatthew G Knepley ierr = PetscMalloc6(numCells*cellDof,PetscScalar,&u,numCells*dim,PetscReal,&v0,numCells*dim*dim,PetscReal,&J,numCells*dim*dim,PetscReal,&invJ,numCells,PetscReal,&detJ,numCells*cellDof,PetscScalar,&elemVec);CHKERRQ(ierr); 488cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 489cb1e1211SMatthew G Knepley PetscScalar *x; 490cb1e1211SMatthew G Knepley PetscInt i; 491cb1e1211SMatthew G Knepley 492cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 493cb1e1211SMatthew G Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 494cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 495cb1e1211SMatthew G Knepley 496cb1e1211SMatthew G Knepley for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 497cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 498cb1e1211SMatthew G Knepley } 499cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 500cb1e1211SMatthew G Knepley const PetscInt numQuadPoints = quad[field].numQuadPoints; 501cb1e1211SMatthew G Knepley const PetscInt numBasisFuncs = quad[field].numBasisFuncs; 502cb1e1211SMatthew G Knepley void (*f0)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f0[]) = fem->f0Funcs[field]; 503cb1e1211SMatthew G Knepley void (*f1)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f1[]) = fem->f1Funcs[field]; 504cb1e1211SMatthew G Knepley /* Conforming batches */ 505cb1e1211SMatthew G Knepley PetscInt blockSize = numBasisFuncs*numQuadPoints; 506cb1e1211SMatthew G Knepley PetscInt numBlocks = 1; 507cb1e1211SMatthew G Knepley PetscInt batchSize = numBlocks * blockSize; 508cb1e1211SMatthew G Knepley PetscInt numBatches = numBatchesTmp; 509cb1e1211SMatthew G Knepley PetscInt numChunks = numCells / (numBatches*batchSize); 510cb1e1211SMatthew G Knepley /* Remainder */ 511cb1e1211SMatthew G Knepley PetscInt numRemainder = numCells % (numBatches * batchSize); 512cb1e1211SMatthew G Knepley PetscInt offset = numCells - numRemainder; 513cb1e1211SMatthew G Knepley 514cb1e1211SMatthew G Knepley ierr = (*mesh->integrateResidualFEM)(numChunks*numBatches*batchSize, numFields, field, quad, u, v0, J, invJ, detJ, f0, f1, elemVec);CHKERRQ(ierr); 515cb1e1211SMatthew G Knepley ierr = (*mesh->integrateResidualFEM)(numRemainder, numFields, field, quad, &u[offset*cellDof], &v0[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset], 516cb1e1211SMatthew G Knepley f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr); 517cb1e1211SMatthew G Knepley } 518cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 519cb1e1211SMatthew G Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, "Residual", cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);} 520cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, NULL, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr); 521cb1e1211SMatthew G Knepley } 522cb1e1211SMatthew G Knepley ierr = PetscFree6(u,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 523*652b88e8SMatthew G. Knepley /* Integration over the boundary: 524*652b88e8SMatthew G. Knepley - This can probably be generalized to integration over a set of labels, however 525*652b88e8SMatthew G. Knepley the idea here is to do integration where we need the cell normal 526*652b88e8SMatthew G. Knepley - We can replace hardcoding with a registration process, and this is how we hook 527*652b88e8SMatthew G. Knepley up the system to something like FEniCS 528*652b88e8SMatthew G. Knepley */ 529*652b88e8SMatthew G. Knepley ierr = DMPlexHasLabel(dm, "boundary", &has);CHKERRQ(ierr); 530*652b88e8SMatthew G. Knepley if (has && quadBd) { 531*652b88e8SMatthew G. Knepley DMLabel label; 532*652b88e8SMatthew G. Knepley IS pointIS; 533*652b88e8SMatthew G. Knepley const PetscInt *points; 534*652b88e8SMatthew G. Knepley PetscInt numPoints, p; 535*652b88e8SMatthew G. Knepley 536*652b88e8SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "boundary", &label);CHKERRQ(ierr); 537*652b88e8SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 538*652b88e8SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 539*652b88e8SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 540*652b88e8SMatthew G. Knepley ierr = DMPlexGetLabelValue(dm, "depth", points[0], &dim);CHKERRQ(ierr); 541*652b88e8SMatthew G. Knepley for (field = 0, cellDof = 0, numComponents = 0; field < numFields; ++field) { 542*652b88e8SMatthew G. Knepley cellDof += quadBd[field].numBasisFuncs*quadBd[field].numComponents; 543*652b88e8SMatthew G. Knepley numComponents += quadBd[field].numComponents; 544*652b88e8SMatthew G. Knepley } 545*652b88e8SMatthew G. Knepley ierr = PetscMalloc6(numPoints*cellDof,PetscScalar,&u,numPoints*dim,PetscReal,&v0,numPoints*dim*dim,PetscReal,&J,numPoints*dim*dim,PetscReal,&invJ,numPoints,PetscReal,&detJ,numPoints*cellDof,PetscScalar,&elemVec);CHKERRQ(ierr); 546*652b88e8SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 547*652b88e8SMatthew G. Knepley const PetscInt point = points[p]; 548*652b88e8SMatthew G. Knepley PetscScalar *x; 549*652b88e8SMatthew G. Knepley PetscInt i; 550*652b88e8SMatthew G. Knepley 551*652b88e8SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, point, &v0[p*dim], &J[p*dim*dim], &invJ[p*dim*dim], &detJ[p]);CHKERRQ(ierr); 552*652b88e8SMatthew G. Knepley if (detJ[p] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[p], p); 553*652b88e8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, X, point, NULL, &x);CHKERRQ(ierr); 554*652b88e8SMatthew G. Knepley 555*652b88e8SMatthew G. Knepley for (i = 0; i < cellDof; ++i) u[p*cellDof+i] = x[i]; 556*652b88e8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, X, point, NULL, &x);CHKERRQ(ierr); 557*652b88e8SMatthew G. Knepley } 558*652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 559*652b88e8SMatthew G. Knepley const PetscInt numQuadPoints = quadBd[field].numQuadPoints; 560*652b88e8SMatthew G. Knepley const PetscInt numBasisFuncs = quadBd[field].numBasisFuncs; 561*652b88e8SMatthew G. Knepley void (*f0)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f0[]) = fem->f0BdFuncs[field]; 562*652b88e8SMatthew G. Knepley void (*f1)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f1[]) = fem->f1BdFuncs[field]; 563*652b88e8SMatthew G. Knepley /* Conforming batches */ 564*652b88e8SMatthew G. Knepley PetscInt blockSize = numBasisFuncs*numQuadPoints; 565*652b88e8SMatthew G. Knepley PetscInt numBlocks = 1; 566*652b88e8SMatthew G. Knepley PetscInt batchSize = numBlocks * blockSize; 567*652b88e8SMatthew G. Knepley PetscInt numBatches = numBatchesTmp; 568*652b88e8SMatthew G. Knepley PetscInt numChunks = numPoints / (numBatches*batchSize); 569*652b88e8SMatthew G. Knepley /* Remainder */ 570*652b88e8SMatthew G. Knepley PetscInt numRemainder = numPoints % (numBatches * batchSize); 571*652b88e8SMatthew G. Knepley PetscInt offset = numPoints - numRemainder; 572*652b88e8SMatthew G. Knepley 573*652b88e8SMatthew G. Knepley ierr = (*mesh->integrateResidualFEM)(numChunks*numBatches*batchSize, numFields, field, quad, u, v0, J, invJ, detJ, f0, f1, elemVec);CHKERRQ(ierr); 574*652b88e8SMatthew G. Knepley ierr = (*mesh->integrateResidualFEM)(numRemainder, numFields, field, quad, &u[offset*cellDof], &v0[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset], 575*652b88e8SMatthew G. Knepley f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr); 576*652b88e8SMatthew G. Knepley } 577*652b88e8SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 578*652b88e8SMatthew G. Knepley const PetscInt point = points[p]; 579*652b88e8SMatthew G. Knepley 580*652b88e8SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "Residual", cellDof, &elemVec[p*cellDof]);CHKERRQ(ierr);} 581*652b88e8SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[p*cellDof], ADD_VALUES);CHKERRQ(ierr); 582*652b88e8SMatthew G. Knepley } 583*652b88e8SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 584*652b88e8SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 585*652b88e8SMatthew G. Knepley ierr = PetscFree6(u,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 586*652b88e8SMatthew G. Knepley } 587cb1e1211SMatthew G Knepley if (mesh->printFEM) { 588cb1e1211SMatthew G Knepley PetscMPIInt rank, numProcs; 589cb1e1211SMatthew G Knepley PetscInt p; 590cb1e1211SMatthew G Knepley 591cb1e1211SMatthew G Knepley ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr); 592cb1e1211SMatthew G Knepley ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &numProcs);CHKERRQ(ierr); 593cb1e1211SMatthew G Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Residual:\n");CHKERRQ(ierr); 594cb1e1211SMatthew G Knepley for (p = 0; p < numProcs; ++p) { 595cb1e1211SMatthew G Knepley if (p == rank) { 596cb1e1211SMatthew G Knepley Vec f; 597cb1e1211SMatthew G Knepley 598cb1e1211SMatthew G Knepley ierr = VecDuplicate(F, &f);CHKERRQ(ierr); 599cb1e1211SMatthew G Knepley ierr = VecCopy(F, f);CHKERRQ(ierr); 600cb1e1211SMatthew G Knepley ierr = VecChop(f, 1.0e-10);CHKERRQ(ierr); 601cb1e1211SMatthew G Knepley ierr = VecView(f, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); 602cb1e1211SMatthew G Knepley ierr = VecDestroy(&f);CHKERRQ(ierr); 603cb1e1211SMatthew G Knepley ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); 604cb1e1211SMatthew G Knepley } 605cb1e1211SMatthew G Knepley ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr); 606cb1e1211SMatthew G Knepley } 607cb1e1211SMatthew G Knepley } 608cb1e1211SMatthew G Knepley /* ierr = PetscLogEventEnd(ResidualFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 609cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 610cb1e1211SMatthew G Knepley } 611cb1e1211SMatthew G Knepley 612cb1e1211SMatthew G Knepley #undef __FUNCT__ 613cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeJacobianActionFEM" 614cb1e1211SMatthew G Knepley /*@C 615cb1e1211SMatthew G Knepley DMPlexComputeJacobianActionFEM - Form the local action of Jacobian J(u) on the local input X using pointwise functions specified by the user 616cb1e1211SMatthew G Knepley 617cb1e1211SMatthew G Knepley Input Parameters: 618cb1e1211SMatthew G Knepley + dm - The mesh 619cb1e1211SMatthew G Knepley . J - The Jacobian shell matrix 620cb1e1211SMatthew G Knepley . X - Local input vector 621cb1e1211SMatthew G Knepley - user - The user context 622cb1e1211SMatthew G Knepley 623cb1e1211SMatthew G Knepley Output Parameter: 624cb1e1211SMatthew G Knepley . F - Local output vector 625cb1e1211SMatthew G Knepley 626cb1e1211SMatthew G Knepley Note: 627cb1e1211SMatthew G Knepley The second member of the user context must be an FEMContext. 628cb1e1211SMatthew G Knepley 629cb1e1211SMatthew G Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 630cb1e1211SMatthew G Knepley like a GPU, or vectorize on a multicore machine. 631cb1e1211SMatthew G Knepley 6320059ad2aSSatish Balay Level: developer 6330059ad2aSSatish Balay 634cb1e1211SMatthew G Knepley .seealso: DMPlexComputeResidualFEM() 635878cb397SSatish Balay @*/ 636cb1e1211SMatthew G Knepley PetscErrorCode DMPlexComputeJacobianActionFEM(DM dm, Mat Jac, Vec X, Vec F, void *user) 637cb1e1211SMatthew G Knepley { 638cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 639cb1e1211SMatthew G Knepley PetscFEM *fem = (PetscFEM*) &((DM*) user)[1]; 640cb1e1211SMatthew G Knepley PetscQuadrature *quad = fem->quad; 641cb1e1211SMatthew G Knepley PetscSection section; 642cb1e1211SMatthew G Knepley JacActionCtx *jctx; 643cb1e1211SMatthew G Knepley PetscReal *v0, *J, *invJ, *detJ; 644cb1e1211SMatthew G Knepley PetscScalar *elemVec, *u, *a; 645cb1e1211SMatthew G Knepley PetscInt dim, numFields, field, numBatchesTmp = 1, numCells, cStart, cEnd, c; 646cb1e1211SMatthew G Knepley PetscInt cellDof = 0; 647cb1e1211SMatthew G Knepley PetscErrorCode ierr; 648cb1e1211SMatthew G Knepley 649cb1e1211SMatthew G Knepley PetscFunctionBegin; 650cb1e1211SMatthew G Knepley /* ierr = PetscLogEventBegin(JacobianActionFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 651cb1e1211SMatthew G Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 652cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 653cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 654cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 655cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 656cb1e1211SMatthew G Knepley numCells = cEnd - cStart; 657cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 658cb1e1211SMatthew G Knepley cellDof += quad[field].numBasisFuncs*quad[field].numComponents; 659cb1e1211SMatthew G Knepley } 660cb1e1211SMatthew G Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 661cb1e1211SMatthew G Knepley ierr = PetscMalloc7(numCells*cellDof,PetscScalar,&u,numCells*cellDof,PetscScalar,&a,numCells*dim,PetscReal,&v0,numCells*dim*dim,PetscReal,&J,numCells*dim*dim,PetscReal,&invJ,numCells,PetscReal,&detJ,numCells*cellDof,PetscScalar,&elemVec);CHKERRQ(ierr); 662cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 663cb1e1211SMatthew G Knepley PetscScalar *x; 664cb1e1211SMatthew G Knepley PetscInt i; 665cb1e1211SMatthew G Knepley 666cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 667cb1e1211SMatthew G Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 668cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr); 669cb1e1211SMatthew G Knepley for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 670cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr); 671cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 672cb1e1211SMatthew G Knepley for (i = 0; i < cellDof; ++i) a[c*cellDof+i] = x[i]; 673cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 674cb1e1211SMatthew G Knepley } 675cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 676cb1e1211SMatthew G Knepley const PetscInt numQuadPoints = quad[field].numQuadPoints; 677cb1e1211SMatthew G Knepley const PetscInt numBasisFuncs = quad[field].numBasisFuncs; 678cb1e1211SMatthew G Knepley /* Conforming batches */ 679cb1e1211SMatthew G Knepley PetscInt blockSize = numBasisFuncs*numQuadPoints; 680cb1e1211SMatthew G Knepley PetscInt numBlocks = 1; 681cb1e1211SMatthew G Knepley PetscInt batchSize = numBlocks * blockSize; 682cb1e1211SMatthew G Knepley PetscInt numBatches = numBatchesTmp; 683cb1e1211SMatthew G Knepley PetscInt numChunks = numCells / (numBatches*batchSize); 684cb1e1211SMatthew G Knepley /* Remainder */ 685cb1e1211SMatthew G Knepley PetscInt numRemainder = numCells % (numBatches * batchSize); 686cb1e1211SMatthew G Knepley PetscInt offset = numCells - numRemainder; 687cb1e1211SMatthew G Knepley 688cb1e1211SMatthew G Knepley ierr = (*mesh->integrateJacobianActionFEM)(numChunks*numBatches*batchSize, numFields, field, quad, u, a, v0, J, invJ, detJ, fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, elemVec);CHKERRQ(ierr); 689cb1e1211SMatthew G Knepley ierr = (*mesh->integrateJacobianActionFEM)(numRemainder, numFields, field, quad, &u[offset*cellDof], &a[offset*cellDof], &v0[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset], 690cb1e1211SMatthew G Knepley fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, &elemVec[offset*cellDof]);CHKERRQ(ierr); 691cb1e1211SMatthew G Knepley } 692cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 693cb1e1211SMatthew G Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, "Jacobian Action", cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);} 694cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, NULL, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr); 695cb1e1211SMatthew G Knepley } 696cb1e1211SMatthew G Knepley ierr = PetscFree7(u,a,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 697cb1e1211SMatthew G Knepley if (mesh->printFEM) { 698cb1e1211SMatthew G Knepley PetscMPIInt rank, numProcs; 699cb1e1211SMatthew G Knepley PetscInt p; 700cb1e1211SMatthew G Knepley 701cb1e1211SMatthew G Knepley ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr); 702cb1e1211SMatthew G Knepley ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &numProcs);CHKERRQ(ierr); 703cb1e1211SMatthew G Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Jacobian Action:\n");CHKERRQ(ierr); 704cb1e1211SMatthew G Knepley for (p = 0; p < numProcs; ++p) { 705cb1e1211SMatthew G Knepley if (p == rank) {ierr = VecView(F, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);} 706cb1e1211SMatthew G Knepley ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr); 707cb1e1211SMatthew G Knepley } 708cb1e1211SMatthew G Knepley } 709cb1e1211SMatthew G Knepley /* ierr = PetscLogEventEnd(JacobianActionFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 710cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 711cb1e1211SMatthew G Knepley } 712cb1e1211SMatthew G Knepley 713cb1e1211SMatthew G Knepley #undef __FUNCT__ 714cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM" 715cb1e1211SMatthew G Knepley /*@ 716cb1e1211SMatthew G Knepley DMPlexComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 717cb1e1211SMatthew G Knepley 718cb1e1211SMatthew G Knepley Input Parameters: 719cb1e1211SMatthew G Knepley + dm - The mesh 720cb1e1211SMatthew G Knepley . X - Local input vector 721cb1e1211SMatthew G Knepley - user - The user context 722cb1e1211SMatthew G Knepley 723cb1e1211SMatthew G Knepley Output Parameter: 724cb1e1211SMatthew G Knepley . Jac - Jacobian matrix 725cb1e1211SMatthew G Knepley 726cb1e1211SMatthew G Knepley Note: 727cb1e1211SMatthew G Knepley The second member of the user context must be an FEMContext. 728cb1e1211SMatthew G Knepley 729cb1e1211SMatthew G Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 730cb1e1211SMatthew G Knepley like a GPU, or vectorize on a multicore machine. 731cb1e1211SMatthew G Knepley 7320059ad2aSSatish Balay Level: developer 7330059ad2aSSatish Balay 734cb1e1211SMatthew G Knepley .seealso: FormFunctionLocal() 735878cb397SSatish Balay @*/ 736cb1e1211SMatthew G Knepley PetscErrorCode DMPlexComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP, MatStructure *str,void *user) 737cb1e1211SMatthew G Knepley { 738cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 739cb1e1211SMatthew G Knepley PetscFEM *fem = (PetscFEM*) &((DM*) user)[1]; 740cb1e1211SMatthew G Knepley PetscQuadrature *quad = fem->quad; 741cb1e1211SMatthew G Knepley PetscSection section; 742cb1e1211SMatthew G Knepley PetscReal *v0, *J, *invJ, *detJ; 743cb1e1211SMatthew G Knepley PetscScalar *elemMat, *u; 744cb1e1211SMatthew G Knepley PetscInt dim, numFields, field, fieldI, numBatchesTmp = 1, numCells, cStart, cEnd, c; 745cb1e1211SMatthew G Knepley PetscInt cellDof = 0, numComponents = 0; 746cb1e1211SMatthew G Knepley PetscBool isShell; 747cb1e1211SMatthew G Knepley PetscErrorCode ierr; 748cb1e1211SMatthew G Knepley 749cb1e1211SMatthew G Knepley PetscFunctionBegin; 750cb1e1211SMatthew G Knepley /* ierr = PetscLogEventBegin(JacobianFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 751cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 752cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 753cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 754cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 755cb1e1211SMatthew G Knepley numCells = cEnd - cStart; 756cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 757cb1e1211SMatthew G Knepley cellDof += quad[field].numBasisFuncs*quad[field].numComponents; 758cb1e1211SMatthew G Knepley numComponents += quad[field].numComponents; 759cb1e1211SMatthew G Knepley } 760cb1e1211SMatthew G Knepley ierr = DMPlexProjectFunctionLocal(dm, numComponents, fem->bcFuncs, INSERT_BC_VALUES, X);CHKERRQ(ierr); 761cb1e1211SMatthew G Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 762cb1e1211SMatthew G Knepley ierr = PetscMalloc6(numCells*cellDof,PetscScalar,&u,numCells*dim,PetscReal,&v0,numCells*dim*dim,PetscReal,&J,numCells*dim*dim,PetscReal,&invJ,numCells,PetscReal,&detJ,numCells*cellDof*cellDof,PetscScalar,&elemMat);CHKERRQ(ierr); 763cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 764cb1e1211SMatthew G Knepley PetscScalar *x; 765cb1e1211SMatthew G Knepley PetscInt i; 766cb1e1211SMatthew G Knepley 767cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 768cb1e1211SMatthew G Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 769cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 770cb1e1211SMatthew G Knepley 771cb1e1211SMatthew G Knepley for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 772cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 773cb1e1211SMatthew G Knepley } 774cb1e1211SMatthew G Knepley ierr = PetscMemzero(elemMat, numCells*cellDof*cellDof * sizeof(PetscScalar));CHKERRQ(ierr); 775cb1e1211SMatthew G Knepley for (fieldI = 0; fieldI < numFields; ++fieldI) { 776cb1e1211SMatthew G Knepley const PetscInt numQuadPoints = quad[fieldI].numQuadPoints; 777cb1e1211SMatthew G Knepley const PetscInt numBasisFuncs = quad[fieldI].numBasisFuncs; 778cb1e1211SMatthew G Knepley PetscInt fieldJ; 779cb1e1211SMatthew G Knepley 780cb1e1211SMatthew G Knepley for (fieldJ = 0; fieldJ < numFields; ++fieldJ) { 781cb1e1211SMatthew G Knepley void (*g0)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar g0[]) = fem->g0Funcs[fieldI*numFields+fieldJ]; 782cb1e1211SMatthew G Knepley void (*g1)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar g1[]) = fem->g1Funcs[fieldI*numFields+fieldJ]; 783cb1e1211SMatthew G Knepley void (*g2)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar g2[]) = fem->g2Funcs[fieldI*numFields+fieldJ]; 784cb1e1211SMatthew G Knepley void (*g3)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar g3[]) = fem->g3Funcs[fieldI*numFields+fieldJ]; 785cb1e1211SMatthew G Knepley /* Conforming batches */ 786cb1e1211SMatthew G Knepley PetscInt blockSize = numBasisFuncs*numQuadPoints; 787cb1e1211SMatthew G Knepley PetscInt numBlocks = 1; 788cb1e1211SMatthew G Knepley PetscInt batchSize = numBlocks * blockSize; 789cb1e1211SMatthew G Knepley PetscInt numBatches = numBatchesTmp; 790cb1e1211SMatthew G Knepley PetscInt numChunks = numCells / (numBatches*batchSize); 791cb1e1211SMatthew G Knepley /* Remainder */ 792cb1e1211SMatthew G Knepley PetscInt numRemainder = numCells % (numBatches * batchSize); 793cb1e1211SMatthew G Knepley PetscInt offset = numCells - numRemainder; 794cb1e1211SMatthew G Knepley 795cb1e1211SMatthew G Knepley ierr = (*mesh->integrateJacobianFEM)(numChunks*numBatches*batchSize, numFields, fieldI, fieldJ, quad, u, v0, J, invJ, detJ, g0, g1, g2, g3, elemMat);CHKERRQ(ierr); 796cb1e1211SMatthew G Knepley ierr = (*mesh->integrateJacobianFEM)(numRemainder, numFields, fieldI, fieldJ, quad, &u[offset*cellDof], &v0[offset*dim], &J[offset*dim*dim], &invJ[offset*dim*dim], &detJ[offset], 797cb1e1211SMatthew G Knepley g0, g1, g2, g3, &elemMat[offset*cellDof*cellDof]);CHKERRQ(ierr); 798cb1e1211SMatthew G Knepley } 799cb1e1211SMatthew G Knepley } 800cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 801cb1e1211SMatthew G Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, "Jacobian", cellDof, cellDof, &elemMat[c*cellDof*cellDof]);CHKERRQ(ierr);} 802cb1e1211SMatthew G Knepley ierr = DMPlexMatSetClosure(dm, NULL, NULL, JacP, c, &elemMat[c*cellDof*cellDof], ADD_VALUES);CHKERRQ(ierr); 803cb1e1211SMatthew G Knepley } 804cb1e1211SMatthew G Knepley ierr = PetscFree6(u,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr); 805cb1e1211SMatthew G Knepley 806cb1e1211SMatthew G Knepley /* Assemble matrix, using the 2-step process: 807cb1e1211SMatthew G Knepley MatAssemblyBegin(), MatAssemblyEnd(). */ 808cb1e1211SMatthew G Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 809cb1e1211SMatthew G Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 810cb1e1211SMatthew G Knepley 811cb1e1211SMatthew G Knepley if (mesh->printFEM) { 812cb1e1211SMatthew G Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Jacobian:\n");CHKERRQ(ierr); 813cb1e1211SMatthew G Knepley ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 814cb1e1211SMatthew G Knepley ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 815cb1e1211SMatthew G Knepley } 816cb1e1211SMatthew G Knepley /* ierr = PetscLogEventEnd(JacobianFEMEvent,0,0,0,0);CHKERRQ(ierr); */ 817cb1e1211SMatthew G Knepley ierr = PetscObjectTypeCompare((PetscObject)Jac, MATSHELL, &isShell);CHKERRQ(ierr); 818cb1e1211SMatthew G Knepley if (isShell) { 819cb1e1211SMatthew G Knepley JacActionCtx *jctx; 820cb1e1211SMatthew G Knepley 821cb1e1211SMatthew G Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 822cb1e1211SMatthew G Knepley ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 823cb1e1211SMatthew G Knepley } 824cb1e1211SMatthew G Knepley *str = SAME_NONZERO_PATTERN; 825cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 826cb1e1211SMatthew G Knepley } 827