131static PetscErrorCode
CopyOwnedOutputBox(Vec vec,
const PetscInt extent[3], PetscInt start_out[3], PetscInt size_out[3],
136 const PetscScalar *values = NULL;
137 PetscInt p = 0, start_box[3], size_box[3];
138 PetscInt *start = start_out ? start_out : start_box;
139 PetscInt *size = size_out ? size_out : size_box;
141 PetscFunctionBeginUser;
143 PetscCall(VecGetDM(vec, &dm));
144 PetscCheck(dm != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
"VTK output needs a vector attached to a DMDA.");
145 PetscCall(DMDAGetLocalInfo(dm, &info));
146 const PetscInt owned_start[3] = {info.xs, info.ys, info.zs}, owned[3] = {info.xm, info.ym, info.zm};
147 for (
int d = 0; d < 3; ++d) {
148 start[d] = owned_start[d];
149 size[d] = PetscMax(0, PetscMin(owned_start[d] + owned[d], extent[d]) - owned_start[d]);
151 PetscCall(PetscMalloc1(info.dof * size[0] * size[1] * size[2], out));
152 PetscCall(VecGetArrayRead(vec, &values));
153 for (PetscInt k = 0; k < size[2]; ++k)
154 for (PetscInt j = 0; j < size[1]; ++j)
155 for (PetscInt i = 0; i < size[0]; ++i)
156 for (PetscInt c = 0; c < info.dof; ++c)
157 (*out)[p++] = values[((k * owned[1] + j) * owned[0] + i) * info.dof + c];
158 PetscCall(VecRestoreArrayRead(vec, &values));
160 PetscFunctionReturn(0);
174 static const char footer[] =
"\n </AppendedData>\n</VTKFile>\n";
176 char tmp_filename[PETSC_MAX_PATH_LEN];
178 long long header_bytes = 0;
181 PetscInt blocks = 0, local_points = 1, boffset = 0;
182 struct VTKBlock { PetscInt components; MPI_Datatype type;
size_t size;
const void *data; } block[
MAX_POINT_DATA_FIELDS + 3];
184 PetscFunctionBeginUser;
186 PetscCheck(structured || meta->
fileType ==
VTK_POLYDATA, comm, PETSC_ERR_ARG_WRONG,
"Unknown VTK file type.");
187 PetscCallMPI(MPI_Comm_rank(comm, &rank));
188 PetscCall(PetscSNPrintf(tmp_filename,
sizeof(tmp_filename),
"%s.tmp", filename));
191 block[blocks++] = (
struct VTKBlock){3, MPIU_SCALAR,
sizeof(PetscScalar), meta->
coords};
196 block[blocks++] = (
struct VTKBlock){1, MPIU_INT,
sizeof(PetscInt), meta->
connectivity};
197 block[blocks++] = (
struct VTKBlock){1, MPIU_INT,
sizeof(PetscInt), meta->
offsets};
199 for (
int d = 0; d < (structured ? 3 : 1); ++d) local_points *= meta->
local_size[d];
202 FILE *fp = fopen(tmp_filename,
"wb");
204 else header_bytes = (
long long)ftell(fp);
205 if (fp && fclose(fp)) header_bytes = -1;
207 PetscCallMPI(MPI_Bcast(&header_bytes, 1, MPI_LONG_LONG, 0, comm));
208 PetscCheck(header_bytes >= 0, comm, PETSC_ERR_FILE_OPEN,
"Could not write the VTK header of '%s'.", tmp_filename);
211 PetscCallMPI(MPI_File_open(comm, tmp_filename, MPI_MODE_WRONLY, MPI_INFO_NULL, &fh));
212 position = (MPI_Offset)header_bytes;
213 for (PetscInt b = 0; b < blocks; ++b) {
214 const uint64_t bytes = (uint64_t)block[b].components * (uint64_t)meta->
npoints * block[b].size;
215 MPI_Datatype point, view;
218 PetscCallMPI(MPI_File_set_view(fh, 0, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL));
219 if (rank == 0) PetscCallMPI(MPI_File_write_at(fh, position, &bytes, (
int)
sizeof(bytes), MPI_BYTE, MPI_STATUS_IGNORE));
220 position += (MPI_Offset)
sizeof(bytes);
222 PetscCallMPI(MPI_Type_contiguous((PetscMPIInt)block[b].components, block[b].type, &point));
223 PetscCallMPI(MPI_Type_commit(&point));
225 if (structured && local_points > 0) {
226 const PetscMPIInt sizes[3] = {(PetscMPIInt)meta->
mz, (PetscMPIInt)meta->
my, (PetscMPIInt)meta->
mx};
229 PetscCallMPI(MPI_Type_create_subarray(3, sizes, subsizes, starts, MPI_ORDER_C, point, &view));
230 PetscCallMPI(MPI_Type_commit(&view));
232 PetscCallMPI(MPI_File_set_view(fh, position, point, view,
"native", MPI_INFO_NULL));
233 PetscCall(PetscMPIIntCast(local_points, &count));
234 if (structured) PetscCallMPI(MPI_File_write_all(fh, block[b].data, count, point, MPI_STATUS_IGNORE));
235 else PetscCallMPI(MPI_File_write_at_all(fh, (MPI_Offset)meta->
local_start[0], block[b].data, count, point, MPI_STATUS_IGNORE));
236 if (view != point) PetscCallMPI(MPI_Type_free(&view));
237 PetscCallMPI(MPI_Type_free(&point));
238 position += (MPI_Offset)bytes;
240 PetscCallMPI(MPI_File_set_view(fh, 0, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL));
241 if (rank == 0) PetscCallMPI(MPI_File_write_at(fh, position, footer, (
int)strlen(footer), MPI_BYTE, MPI_STATUS_IGNORE));
242 PetscCallMPI(MPI_File_close(&fh));
243 if (rank == 0) PetscCheck(rename(tmp_filename, filename) == 0, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE,
244 "Could not move '%s' to '%s'.", tmp_filename, filename);
247 PetscFunctionReturn(0);
387 PetscInt nlocal = 0, first = 0, before = 0, kept = 0, bs = 0;
389 PetscReal *position = NULL;
390 char *fields_copy = NULL, *field_name = NULL;
392 PetscFunctionBeginUser;
398 PetscCall(DMSwarmGetLocalSize(user->
swarm, &nlocal));
399 PetscCallMPI(MPI_Exscan(&nlocal, &before, 1, MPIU_INT, MPI_SUM, PETSC_COMM_WORLD));
401 PetscCall(DMSwarmGetSize(user->
swarm, p_n_total));
403 first = (stride - before % stride) % stride;
404 kept = first < nlocal ? (nlocal - 1 - first) / stride + 1 : 0;
406 meta->
npoints = (*p_n_total - 1) / stride + 1;
409 LOG_ALLOW(
GLOBAL,
LOG_DEBUG,
"Subsampling %" PetscInt_FMT
" particles with stride %" PetscInt_FMT
" -> %" PetscInt_FMT
" output particles.\n",
410 *p_n_total, stride, meta->
npoints);
413 PetscCheck(type == PETSC_REAL && bs == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
414 "Particle position field must be 3 PETSC_REAL components, not %" PetscInt_FMT
" of %s.", bs, PetscDataTypes[type]);
415 PetscCall(PetscMalloc1(3 * kept, &meta->
coords));
416 for (PetscInt i = 0; i < kept; ++i)
417 for (
int d = 0; d < 3; ++d) meta->
coords[3 * i + d] = position[3 * (first + i * stride) + d];
422 field_name = strtok(NULL,
",")) {
423 DM swarm_to_use = user->
swarm;
424 const char *internal_name = field_name;
425 PetscErrorCode check_ierr;
426 PetscInt field_local = 0, field_total = 0;
430 if (!*field_name || strcasecmp(field_name,
"position") == 0)
continue;
433 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
434 check_ierr = DMSwarmGetField(user->
post_swarm, field_name, NULL, NULL, NULL);
435 PetscCall(PetscPopErrorHandler());
438 PetscCall(DMSwarmRestoreField(user->
post_swarm, field_name, NULL, NULL, NULL));
442 if (strcasecmp(field_name,
"pid") == 0) internal_name =
"DMSwarm_pid";
443 else if (strcasecmp(field_name,
"CellID") == 0) internal_name =
"DMSwarm_CellID";
444 else if (strcasecmp(field_name,
"Migration Status") == 0) internal_name =
"DMSwarm_location_status";
446 PetscCall(DMSwarmGetLocalSize(swarm_to_use, &field_local));
447 PetscCall(DMSwarmGetSize(swarm_to_use, &field_total));
448 if (field_total != *p_n_total) {
450 field_name, field_total, *p_n_total);
453 PetscCheck(field_local == nlocal, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ,
454 "Field '%s' holds %" PetscInt_FMT
" particles on this rank, the solver swarm %" PetscInt_FMT
".",
455 field_name, field_local, nlocal);
458 PetscCall(DMSwarmGetField(swarm_to_use, internal_name, &bs, &type, &source));
461 PetscCall(PetscMalloc1(bs * kept, ¤t->
data));
462 for (PetscInt i = 0; i < kept; ++i) {
463 const PetscInt from = first + i * stride;
464 for (PetscInt c = 0; c < bs; ++c) {
465 PetscScalar *to = ¤t->
data[bs * i + c];
466 if (type == PETSC_INT64) *to = (PetscScalar)((PetscInt64 *)source)[bs * from + c];
467 else if (type == PETSC_INT) *to = (PetscScalar)((PetscInt *)source)[bs * from + c];
468 else if (type == PETSC_REAL) *to = ((PetscReal *)source)[bs * from + c];
469#if defined(PETSC_USE_COMPLEX)
470 else if (type == PETSC_SCALAR) *to = ((PetscScalar *)source)[bs * from + c];
472 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP,
"VTK particle output does not support field '%s' with PETSc type %s.",
473 field_name, PetscDataTypes[type]);
476 PetscCall(DMSwarmRestoreField(swarm_to_use, internal_name, &bs, &type, &source));
479 PetscCall(PetscFree(fields_copy));
482 PetscCall(PetscMalloc1(kept, &meta->
offsets));
483 for (PetscInt i = 0; i < kept; ++i) {
488 PetscFunctionReturn(0);
Typed identities and metadata for persistent solver-particle fields.
const char * ParticleFieldName(ParticleFieldId field_id)
Return the canonical PETSc DMSwarm name for an ID.
Holds all configuration parameters for a post-processing run.