1 #include <petsc/private/dmimpl.h> /*I "petscdm.h" I*/ 2 #include <petscds.h> 3 4 PetscErrorCode DMCreateGlobalVector_Section_Private(DM dm,Vec *vec) 5 { 6 PetscSection gSection; 7 PetscInt localSize, bs, blockSize = -1, pStart, pEnd, p; 8 PetscErrorCode ierr; 9 PetscInt in[2],out[2]; 10 11 PetscFunctionBegin; 12 ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr); 13 ierr = PetscSectionGetChart(gSection, &pStart, &pEnd);CHKERRQ(ierr); 14 for (p = pStart; p < pEnd; ++p) { 15 PetscInt dof, cdof; 16 17 ierr = PetscSectionGetDof(gSection, p, &dof);CHKERRQ(ierr); 18 ierr = PetscSectionGetConstraintDof(gSection, p, &cdof);CHKERRQ(ierr); 19 20 if (dof > 0) { 21 if (blockSize < 0 && dof-cdof > 0) { 22 /* set blockSize */ 23 blockSize = dof-cdof; 24 } else if (dof-cdof != blockSize) { 25 /* non-identical blockSize, set it as 1 */ 26 blockSize = 1; 27 break; 28 } 29 } 30 } 31 32 in[0] = blockSize < 0 ? PETSC_MIN_INT : -blockSize; 33 in[1] = blockSize; 34 ierr = MPIU_Allreduce(in,out,2,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 35 /* -out[0] = min(blockSize), out[1] = max(blockSize) */ 36 if (-out[0] == out[1]) { 37 bs = out[1]; 38 } else bs = 1; 39 40 if (bs < 0) { /* Everyone was empty */ 41 blockSize = 1; 42 bs = 1; 43 } 44 45 ierr = PetscSectionGetConstrainedStorageSize(gSection, &localSize);CHKERRQ(ierr); 46 if (localSize%blockSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mismatch between blocksize %d and local storage size %d", blockSize, localSize); 47 ierr = VecCreate(PetscObjectComm((PetscObject)dm), vec);CHKERRQ(ierr); 48 ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr); 49 ierr = VecSetBlockSize(*vec, bs);CHKERRQ(ierr); 50 ierr = VecSetType(*vec,dm->vectype);CHKERRQ(ierr); 51 ierr = VecSetDM(*vec, dm);CHKERRQ(ierr); 52 /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */ 53 PetscFunctionReturn(0); 54 } 55 56 PetscErrorCode DMCreateLocalVector_Section_Private(DM dm,Vec *vec) 57 { 58 PetscSection section; 59 PetscInt localSize, blockSize = -1, pStart, pEnd, p; 60 PetscErrorCode ierr; 61 62 PetscFunctionBegin; 63 ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 64 ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr); 65 for (p = pStart; p < pEnd; ++p) { 66 PetscInt dof; 67 68 ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr); 69 if ((blockSize < 0) && (dof > 0)) blockSize = dof; 70 if ((dof > 0) && (dof != blockSize)) { 71 blockSize = 1; 72 break; 73 } 74 } 75 ierr = PetscSectionGetStorageSize(section, &localSize);CHKERRQ(ierr); 76 ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr); 77 ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr); 78 ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr); 79 ierr = VecSetType(*vec,dm->vectype);CHKERRQ(ierr); 80 ierr = VecSetDM(*vec, dm);CHKERRQ(ierr); 81 PetscFunctionReturn(0); 82 } 83 84 /* This assumes that the DM has been cloned prior to the call */ 85 PetscErrorCode DMCreateSubDM_Section_Private(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm) 86 { 87 PetscSection section, sectionGlobal; 88 PetscInt *subIndices; 89 PetscInt subSize = 0, subOff = 0, nF, f, pStart, pEnd, p; 90 PetscErrorCode ierr; 91 92 PetscFunctionBegin; 93 if (!numFields) PetscFunctionReturn(0); 94 ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 95 ierr = DMGetGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 96 if (!section) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting fields"); 97 if (!sectionGlobal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set default global section for DM before splitting fields"); 98 ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr); 99 if (numFields > nF) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Number of requested fields %d greater than number of DM fields %d", numFields, nF); 100 if (is) { 101 PetscInt bs, bsLocal[2], bsMinMax[2]; 102 103 for (f = 0, bs = 0; f < numFields; ++f) { 104 PetscInt Nc; 105 106 ierr = PetscSectionGetFieldComponents(section, fields[f], &Nc);CHKERRQ(ierr); 107 bs += Nc; 108 } 109 ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr); 110 for (p = pStart; p < pEnd; ++p) { 111 PetscInt gdof, pSubSize = 0; 112 113 ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 114 if (gdof > 0) { 115 for (f = 0; f < numFields; ++f) { 116 PetscInt fdof, fcdof; 117 118 ierr = PetscSectionGetFieldDof(section, p, fields[f], &fdof);CHKERRQ(ierr); 119 ierr = PetscSectionGetFieldConstraintDof(section, p, fields[f], &fcdof);CHKERRQ(ierr); 120 pSubSize += fdof-fcdof; 121 } 122 subSize += pSubSize; 123 if (pSubSize && bs != pSubSize) { 124 /* Layout does not admit a pointwise block size */ 125 bs = 1; 126 } 127 } 128 } 129 /* Must have same blocksize on all procs (some might have no points) */ 130 bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs; 131 ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr); 132 if (bsMinMax[0] != bsMinMax[1]) {bs = 1;} 133 else {bs = bsMinMax[0];} 134 ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr); 135 for (p = pStart; p < pEnd; ++p) { 136 PetscInt gdof, goff; 137 138 ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 139 if (gdof > 0) { 140 ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr); 141 for (f = 0; f < numFields; ++f) { 142 PetscInt fdof, fcdof, fc, f2, poff = 0; 143 144 /* Can get rid of this loop by storing field information in the global section */ 145 for (f2 = 0; f2 < fields[f]; ++f2) { 146 ierr = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr); 147 ierr = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr); 148 poff += fdof-fcdof; 149 } 150 ierr = PetscSectionGetFieldDof(section, p, fields[f], &fdof);CHKERRQ(ierr); 151 ierr = PetscSectionGetFieldConstraintDof(section, p, fields[f], &fcdof);CHKERRQ(ierr); 152 for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) { 153 subIndices[subOff] = goff+poff+fc; 154 } 155 } 156 } 157 } 158 ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr); 159 if (bs > 1) { 160 /* We need to check that the block size does not come from non-contiguous fields */ 161 PetscInt i, j, set = 1; 162 for (i = 0; i < subSize; i += bs) { 163 for (j = 0; j < bs; ++j) { 164 if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;} 165 } 166 } 167 if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);} 168 } 169 } 170 if (subdm) { 171 PetscSection subsection; 172 PetscBool haveNull = PETSC_FALSE; 173 PetscInt f, nf = 0; 174 175 ierr = PetscSectionCreateSubsection(section, numFields, fields, &subsection);CHKERRQ(ierr); 176 ierr = DMSetSection(*subdm, subsection);CHKERRQ(ierr); 177 ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr); 178 for (f = 0; f < numFields; ++f) { 179 (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[fields[f]]; 180 if ((*subdm)->nullspaceConstructors[f]) { 181 haveNull = PETSC_TRUE; 182 nf = f; 183 } 184 } 185 if (haveNull && is) { 186 MatNullSpace nullSpace; 187 188 ierr = (*(*subdm)->nullspaceConstructors[nf])(*subdm, nf, &nullSpace);CHKERRQ(ierr); 189 ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr); 190 ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 191 } 192 if (dm->probs) { 193 PetscInt Nf; 194 195 ierr = DMSetNumFields(*subdm, numFields);CHKERRQ(ierr); 196 for (f = 0; f < numFields; ++f) { 197 PetscObject disc; 198 199 ierr = DMGetField(dm, fields[f], NULL, &disc);CHKERRQ(ierr); 200 ierr = DMSetField(*subdm, f, NULL, disc);CHKERRQ(ierr); 201 } 202 ierr = DMCreateDS(*subdm);CHKERRQ(ierr); 203 ierr = PetscDSGetNumFields(dm->probs[0].ds, &Nf);CHKERRQ(ierr); 204 if (nF != Nf) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "The number of DM fields %d does not match the number of Section fields %d", Nf, nF); 205 if (numFields == 1 && is) { 206 PetscObject disc, space, pmat; 207 208 ierr = DMGetField(*subdm, 0, NULL, &disc);CHKERRQ(ierr); 209 ierr = PetscObjectQuery(disc, "nullspace", &space);CHKERRQ(ierr); 210 if (space) {ierr = PetscObjectCompose((PetscObject) *is, "nullspace", space);CHKERRQ(ierr);} 211 ierr = PetscObjectQuery(disc, "nearnullspace", &space);CHKERRQ(ierr); 212 if (space) {ierr = PetscObjectCompose((PetscObject) *is, "nearnullspace", space);CHKERRQ(ierr);} 213 ierr = PetscObjectQuery(disc, "pmat", &pmat);CHKERRQ(ierr); 214 if (pmat) {ierr = PetscObjectCompose((PetscObject) *is, "pmat", pmat);CHKERRQ(ierr);} 215 } 216 ierr = PetscDSCopyConstants(dm->probs[0].ds, (*subdm)->probs[0].ds);CHKERRQ(ierr); 217 ierr = PetscDSCopyBoundary(dm->probs[0].ds, (*subdm)->probs[0].ds);CHKERRQ(ierr); 218 ierr = PetscDSSelectEquations(dm->probs[0].ds, numFields, fields, (*subdm)->probs[0].ds);CHKERRQ(ierr); 219 } 220 if (dm->coarseMesh) { 221 ierr = DMCreateSubDM(dm->coarseMesh, numFields, fields, NULL, &(*subdm)->coarseMesh);CHKERRQ(ierr); 222 } 223 } 224 PetscFunctionReturn(0); 225 } 226 227 /* This assumes that the DM has been cloned prior to the call */ 228 PetscErrorCode DMCreateSuperDM_Section_Private(DM dms[], PetscInt len, IS **is, DM *superdm) 229 { 230 MPI_Comm comm; 231 PetscSection supersection, *sections, *sectionGlobals; 232 PetscInt *Nfs, Nf = 0, f, supf, nullf = -1, i; 233 PetscBool haveNull = PETSC_FALSE; 234 PetscErrorCode ierr; 235 236 PetscFunctionBegin; 237 ierr = PetscObjectGetComm((PetscObject)dms[0], &comm);CHKERRQ(ierr); 238 /* Pull out local and global sections */ 239 ierr = PetscMalloc3(len, &Nfs, len, §ions, len, §ionGlobals);CHKERRQ(ierr); 240 for (i = 0 ; i < len; ++i) { 241 ierr = DMGetSection(dms[i], §ions[i]);CHKERRQ(ierr); 242 ierr = DMGetGlobalSection(dms[i], §ionGlobals[i]);CHKERRQ(ierr); 243 if (!sections[i]) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting fields"); 244 if (!sectionGlobals[i]) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Must set default global section for DM before splitting fields"); 245 ierr = PetscSectionGetNumFields(sections[i], &Nfs[i]);CHKERRQ(ierr); 246 Nf += Nfs[i]; 247 } 248 /* Create the supersection */ 249 ierr = PetscSectionCreateSupersection(sections, len, &supersection);CHKERRQ(ierr); 250 ierr = DMSetSection(*superdm, supersection);CHKERRQ(ierr); 251 /* Create ISes */ 252 if (is) { 253 PetscSection supersectionGlobal; 254 PetscInt bs = -1, startf = 0; 255 256 ierr = PetscMalloc1(len, is);CHKERRQ(ierr); 257 ierr = DMGetDefaultGlobalSection(*superdm, &supersectionGlobal);CHKERRQ(ierr); 258 for (i = 0 ; i < len; startf += Nfs[i], ++i) { 259 PetscInt *subIndices; 260 PetscInt subSize, subOff, pStart, pEnd, p, start, end, dummy; 261 262 ierr = PetscSectionGetChart(sectionGlobals[i], &pStart, &pEnd);CHKERRQ(ierr); 263 ierr = PetscSectionGetConstrainedStorageSize(sectionGlobals[i], &subSize);CHKERRQ(ierr); 264 ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr); 265 for (p = pStart, subOff = 0; p < pEnd; ++p) { 266 PetscInt gdof, gcdof, gtdof, d; 267 268 ierr = PetscSectionGetDof(sectionGlobals[i], p, &gdof);CHKERRQ(ierr); 269 ierr = PetscSectionGetConstraintDof(sections[i], p, &gcdof);CHKERRQ(ierr); 270 gtdof = gdof - gcdof; 271 if (gdof > 0 && gtdof) { 272 if (bs < 0) {bs = gtdof;} 273 else if (bs != gtdof) {bs = 1;} 274 ierr = DMGetGlobalFieldOffset_Private(*superdm, p, startf, &start, &dummy);CHKERRQ(ierr); 275 ierr = DMGetGlobalFieldOffset_Private(*superdm, p, startf+Nfs[i]-1, &dummy, &end);CHKERRQ(ierr); 276 if (end-start != gtdof) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Invalid number of global dofs %D != %D for dm %D on point %D", end-start, gtdof, i, p); 277 for (d = start; d < end; ++d, ++subOff) subIndices[subOff] = d; 278 } 279 } 280 ierr = ISCreateGeneral(comm, subSize, subIndices, PETSC_OWN_POINTER, &(*is)[i]);CHKERRQ(ierr); 281 /* Must have same blocksize on all procs (some might have no points) */ 282 { 283 PetscInt bs = -1, bsLocal[2], bsMinMax[2]; 284 285 bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs; 286 ierr = PetscGlobalMinMaxInt(comm, bsLocal, bsMinMax);CHKERRQ(ierr); 287 if (bsMinMax[0] != bsMinMax[1]) {bs = 1;} 288 else {bs = bsMinMax[0];} 289 ierr = ISSetBlockSize((*is)[i], bs);CHKERRQ(ierr); 290 } 291 } 292 } 293 /* Preserve discretizations */ 294 if (len && dms[0]->probs) { 295 ierr = DMSetNumFields(*superdm, Nf);CHKERRQ(ierr); 296 for (i = 0, supf = 0; i < len; ++i) { 297 for (f = 0; f < Nfs[i]; ++f, ++supf) { 298 PetscObject disc; 299 300 ierr = DMGetField(dms[i], f, NULL, &disc);CHKERRQ(ierr); 301 ierr = DMSetField(*superdm, supf, NULL, disc);CHKERRQ(ierr); 302 } 303 } 304 ierr = DMCreateDS(*superdm);CHKERRQ(ierr); 305 } 306 /* Preserve nullspaces */ 307 for (i = 0, supf = 0; i < len; ++i) { 308 for (f = 0; f < Nfs[i]; ++f, ++supf) { 309 (*superdm)->nullspaceConstructors[supf] = dms[i]->nullspaceConstructors[f]; 310 if ((*superdm)->nullspaceConstructors[supf]) { 311 haveNull = PETSC_TRUE; 312 nullf = supf; 313 } 314 } 315 } 316 /* Attach nullspace to IS */ 317 if (haveNull && is) { 318 MatNullSpace nullSpace; 319 320 ierr = (*(*superdm)->nullspaceConstructors[nullf])(*superdm, nullf, &nullSpace);CHKERRQ(ierr); 321 ierr = PetscObjectCompose((PetscObject) (*is)[nullf], "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr); 322 ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 323 } 324 ierr = PetscSectionDestroy(&supersection);CHKERRQ(ierr); 325 ierr = PetscFree3(Nfs, sections, sectionGlobals);CHKERRQ(ierr); 326 PetscFunctionReturn(0); 327 } 328