41 while (isspace((
unsigned char)*start)) {
46 char *end = str + strlen(str) - 1;
48 while (end > start && isspace((
unsigned char)*end)) {
57 memmove(str, start, (end - start) + 2);
63#define __FUNCT__ "ReadGridGenerationInputs"
82 PetscInt block_index = user->
_this;
86 PetscInt *IMs = NULL, *JMs = NULL, *KMs = NULL, *cgrids = NULL;
87 PetscReal *xMins = NULL, *xMaxs = NULL, *rxs = NULL;
88 PetscReal *yMins = NULL, *yMaxs = NULL, *rys = NULL;
89 PetscReal *zMins = NULL, *zMaxs = NULL, *rzs = NULL;
91 PetscFunctionBeginUser;
96 if (block_index >= nblk) {
97 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
"Block index %d is out of range for nblk=%d", block_index, nblk);
101 ierr = PetscMalloc4(nblk, &IMs, nblk, &JMs, nblk, &KMs, nblk, &cgrids); CHKERRQ(ierr);
102 ierr = PetscMalloc6(nblk, &xMins, nblk, &xMaxs, nblk, &rxs, nblk, &yMins, nblk, &yMaxs, nblk, &rys); CHKERRQ(ierr);
103 ierr = PetscMalloc3(nblk, &zMins, nblk, &zMaxs, nblk, &rzs); CHKERRQ(ierr);
106 for (PetscInt i = 0; i < nblk; ++i) {
107 IMs[i] = 10; JMs[i] = 10; KMs[i] = 10; cgrids[i] = 0;
108 xMins[i] = 0.0; xMaxs[i] = 1.0; rxs[i] = 1.0;
109 yMins[i] = 0.0; yMaxs[i] = 1.0; rys[i] = 1.0;
110 zMins[i] = 0.0; zMaxs[i] = 1.0; rzs[i] = 1.0;
115 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-im", IMs, &count, &found); CHKERRQ(ierr);
116 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-jm", JMs, &count, &found); CHKERRQ(ierr);
117 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-km", KMs, &count, &found); CHKERRQ(ierr);
118 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-xMins", xMins, &count, &found); CHKERRQ(ierr);
119 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-xMaxs", xMaxs, &count, &found); CHKERRQ(ierr);
120 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-rxs", rxs, &count, &found); CHKERRQ(ierr);
121 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-yMins", yMins, &count, &found); CHKERRQ(ierr);
122 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-yMaxs", yMaxs, &count, &found); CHKERRQ(ierr);
123 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-rys", rys, &count, &found); CHKERRQ(ierr);
124 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-zMins", zMins, &count, &found); CHKERRQ(ierr);
125 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-zMaxs", zMaxs, &count, &found); CHKERRQ(ierr);
126 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-rzs", rzs, &count, &found); CHKERRQ(ierr);
127 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-cgrids", cgrids, &count, &found); CHKERRQ(ierr);
130 user->
IM = IMs[block_index];
131 user->
JM = JMs[block_index];
132 user->
KM = KMs[block_index];
133 user->
Min_X = xMins[block_index];
134 user->
Max_X = xMaxs[block_index];
135 user->
rx = rxs[block_index];
136 user->
Min_Y = yMins[block_index];
137 user->
Max_Y = yMaxs[block_index];
138 user->
ry = rys[block_index];
139 user->
Min_Z = zMins[block_index];
140 user->
Max_Z = zMaxs[block_index];
141 user->
rz = rzs[block_index];
142 user->
cgrid = cgrids[block_index];
145 simCtx->
rank, block_index, user->
IM, user->
JM, user->
KM);
150 ierr = PetscFree4(IMs, JMs, KMs, cgrids); CHKERRQ(ierr);
151 ierr = PetscFree6(xMins, xMaxs, rxs, yMins, yMaxs, rys); CHKERRQ(ierr);
152 ierr = PetscFree3(zMins, zMaxs, rzs); CHKERRQ(ierr);
155 PetscFunctionReturn(0);
160#define __FUNCT__ "ReadGridFile"
181 PetscInt block_index = user->
_this;
183 PetscFunctionBeginUser;
189 PetscMPIInt rank = simCtx->
rank;
193 FILE *fd = fopen(simCtx->
grid_file,
"r");
194 if (!fd) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
"Cannot open file: %s", simCtx->
grid_file);
198 char firstTok[32] = {0};
199 if (fscanf(fd,
"%31s", firstTok) != 1)
200 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
"Empty grid file: %s", simCtx->
grid_file);
202 if (strcmp(firstTok,
"PICGRID") == 0) {
205 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
"Expected number of blocks after \"PICGRID\" in %s", simCtx->
grid_file);
211 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED,
"Mismatch: -nblk is %d but grid file specifies %d blocks.", nblk,
g_nblk_from_file);
215 for (PetscInt i = 0; i < nblk; ++i) {
217 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
"Expected 3 integers for block %d in %s", i, simCtx->
grid_file);
224 ierr = MPI_Bcast(&
g_nblk_from_file, 1, MPI_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
232 ierr = MPI_Bcast(
g_IMs_from_file, nblk, MPI_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
233 ierr = MPI_Bcast(
g_JMs_from_file, nblk, MPI_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
234 ierr = MPI_Bcast(
g_KMs_from_file, nblk, MPI_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
246 simCtx->
rank, block_index, user->
IM, user->
JM, user->
KM);
249 PetscFunctionReturn(0);
265 while (current != NULL) {
267 PetscFree(current->
key);
268 PetscFree(current->
value);
282 else if (strcasecmp(str,
"+Xi") == 0) *face_out =
BC_FACE_POS_X;
283 else if (strcasecmp(str,
"-Eta") == 0) *face_out =
BC_FACE_NEG_Y;
284 else if (strcasecmp(str,
"+Eta") == 0) *face_out =
BC_FACE_POS_Y;
285 else if (strcasecmp(str,
"-Zeta") == 0) *face_out =
BC_FACE_NEG_Z;
286 else if (strcasecmp(str,
"+Zeta") == 0) *face_out =
BC_FACE_POS_Z;
287 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE,
"Unknown face specifier: %s", str);
298 if (strcasecmp(str,
"WALL") == 0) *type_out =
WALL;
299 else if (strcasecmp(str,
"SYMMETRY") == 0) *type_out =
SYMMETRY;
300 else if (strcasecmp(str,
"INLET") == 0) *type_out =
INLET;
301 else if (strcasecmp(str,
"OUTLET") == 0) *type_out =
OUTLET;
302 else if (strcasecmp(str,
"NOGRAD") == 0) *type_out =
NOGRAD;
304 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE,
"Unknown BC Type string: %s", str);
321 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE,
"Unknown BC Handler string: %s", str);
357 *found = PETSC_FALSE;
363 if (strcasecmp(current->
key, key) == 0) {
364 *value_out = atof(current->
value);
368 current = current->
next;
379#define __FUNCT__ "ParseAllBoundaryConditions"
407 PetscFunctionBeginUser;
409 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
413 char line_buffer[1024];
416 for (
int i = 0; i < 6; i++) {
420 configs_rank0[i].
params = NULL;
421 configs_rank0[i].
handler = NULL;
425 file = fopen(bcs_input_filename,
"r");
426 if (!file) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
"Could not open BCs file '%s'.", bcs_input_filename);
428 while (fgets(line_buffer,
sizeof(line_buffer), file)) {
429 char *current_pos = line_buffer;
430 while (isspace((
unsigned char)*current_pos)) current_pos++;
431 if (*current_pos ==
'#' || *current_pos ==
'\0' || *current_pos ==
'\n' || *current_pos ==
'\r')
continue;
433 char *face_str = strtok(current_pos,
" \t\n\r");
434 char *type_str = strtok(NULL,
" \t\n\r");
435 char *handler_str = strtok(NULL,
" \t\n\r");
437 if (!face_str || !type_str || !handler_str) {
445 const char* handler_name_for_log;
452 if (ierr) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
"Validation failed: Handler '%s' is not valid for Type '%s' on Face '%s'.\n", handler_str, type_str, face_str);
458 LOG_ALLOW(
GLOBAL,
LOG_DEBUG,
" Parsed Face '%s': Type=%s, Handler=%s \n", face_str, type_str, handler_name_for_log);
462 configs_rank0[face_enum].
params = NULL;
466 while ((token = strtok(NULL,
" \t\n\r")) != NULL) {
467 char* equals_ptr = strchr(token,
'=');
474 char* key_str = token;
475 char* value_str = equals_ptr + 1;
478 ierr = PetscMalloc1(1, &new_param); CHKERRQ(ierr);
479 ierr = PetscStrallocpy(key_str, &new_param->
key); CHKERRQ(ierr);
480 ierr = PetscStrallocpy(value_str, &new_param->
value); CHKERRQ(ierr);
481 new_param->
next = NULL;
483 *param_next_ptr = new_param;
484 param_next_ptr = &new_param->
next;
498 for (
int i = 0; i < 6; i++) {
507 PetscInt n_params = 0;
511 ierr = MPI_Bcast(&n_params, 1, MPI_INT, 0, PETSC_COMM_WORLD);CHKERRQ(ierr);
520 for (
int j = 0; j < n_params; j++) {
521 char key_buf[256] = {0}, val_buf[256] = {0};
525 for (
int k = 0; k < j; k++) p = p->
next;
526 strncpy(key_buf, p->
key, 255);
527 strncpy(val_buf, p->
value, 255);
530 ierr = MPI_Bcast(key_buf, 256, MPI_CHAR, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
531 ierr = MPI_Bcast(val_buf, 256, MPI_CHAR, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
536 ierr = PetscMalloc1(1, &new_param); CHKERRQ(ierr);
537 ierr = PetscStrallocpy(key_buf, &new_param->
key); CHKERRQ(ierr);
538 ierr = PetscStrallocpy(val_buf, &new_param->
value); CHKERRQ(ierr);
539 new_param->
next = NULL;
540 *param_next_ptr = new_param;
541 param_next_ptr = &new_param->
next;
544 param_next_ptr = &((*param_next_ptr)->next);
552 for (
int i=0; i<6; i++) {
570 PetscFunctionReturn(0);
590PetscErrorCode
VerifyPathExistence(
const char *path, PetscBool is_dir, PetscBool is_optional,
const char *description, PetscBool *exists)
594 MPI_Comm comm = PETSC_COMM_WORLD;
596 PetscFunctionBeginUser;
597 ierr = MPI_Comm_rank(comm, &rank); CHKERRQ(ierr);
601 ierr = PetscTestDirectory(path,
'r', exists); CHKERRQ(ierr);
603 ierr = PetscTestFile(path,
'r', exists); CHKERRQ(ierr);
618 PetscMPIInt exists_int = (rank == 0) ? (PetscMPIInt)(*exists) : 0;
619 ierr = MPI_Bcast(&exists_int, 1, MPI_INT, 0, comm); CHKERRMPI(ierr);
620 *exists = (PetscBool)exists_int;
623 if (!(*exists) && !is_optional) {
624 SETERRQ(comm, PETSC_ERR_FILE_OPEN,
"Mandatory %s not found. Rank 0 expected it at '%s'. Check path and permissions.", description, path);
627 PetscFunctionReturn(0);
645static PetscErrorCode
CheckDataFile(
UserCtx *user, PetscInt ti,
const char *fieldName,
const char *ext, PetscBool *fileExists)
649 MPI_Comm comm = PETSC_COMM_WORLD;
650 PetscInt placeholder_int = 0;
653 PetscFunctionBeginUser;
656 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_NULL,
"I/O context directory is NULL. Ensure it is set before calling CheckDataFile().");
659 ierr = MPI_Comm_rank(comm, &rank); CHKERRQ(ierr);
662 char filename[PETSC_MAX_PATH_LEN];
665 ierr = PetscSNPrintf(filename,
sizeof(filename),
"%s/%s%05"PetscInt_FMT
"_%d.%s",simCtx->
current_io_directory,fieldName, ti, placeholder_int, ext);
666 ierr = PetscTestFile(filename,
'r', fileExists); CHKERRQ(ierr);
667 if (!(*fileExists)) {
674 PetscMPIInt fileExists_int = (rank == 0) ? (PetscMPIInt)(*fileExists) : 0;
675 ierr = MPI_Bcast(&fileExists_int, 1, MPI_INT, 0, comm); CHKERRMPI(ierr);
676 *fileExists = (PetscBool)fileExists_int;
678 PetscFunctionReturn(0);
703static PetscErrorCode
ReadOptionalField(
UserCtx *user,
const char *field_name,
const char *field_label, Vec field_vec, PetscInt ti,
const char *ext)
706 PetscBool fileExists;
708 PetscFunctionBeginUser;
711 ierr =
CheckDataFile(user,ti, field_name, ext, &fileExists); CHKERRQ(ierr);
721 SETERRQ(PETSC_COMM_WORLD, ierr,
"Failed to read data for %s from existing file for step %d. The file may be corrupt or have an incorrect size.", field_label, ti);
730 PetscFunctionReturn(0);
756 PetscBool fileExists;
758 PetscFunctionBeginUser;
761 ierr =
CheckDataFile(user,ti, field_name, ext, &fileExists); CHKERRQ(ierr);
766 if(strcasecmp(field_name,
"DMSwarm_CellID") == 0 || strcasecmp(field_name,
"DMSwarm_pid")== 0 || strcasecmp(field_name,
"DMSwarm_location_status")== 0 ) {
777 SETERRQ(PETSC_COMM_WORLD, ierr,
"Failed to read data for %s from existing file for step %d. The file may be corrupt or have an incorrect size.", field_label, ti);
786 PetscFunctionReturn(0);
790#define __FUNCT__ "ReadFieldData"
824 const char *field_name,
830 char filename[PETSC_MAX_PATH_LEN];
832 PetscMPIInt rank,size;
836 PetscFunctionBeginUser;
840 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,
"I/O context directory was not set before calling ReadFieldData().");
844 ierr = PetscObjectGetComm((PetscObject)field_vec,&comm);CHKERRQ(ierr);
845 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
846 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
848 const char *source_path = NULL;
852 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,
"source_path was not set for the current execution mode.");
858 ierr = PetscSNPrintf(filename,
sizeof(filename),
859 "%s/%s%05" PetscInt_FMT
"_0.%s",
860 source_path,field_name,ti,ext);CHKERRQ(ierr);
863 "Attempting to read <%s> on rank %d/%d\n",
864 filename,(
int)rank,(
int)size);
874 PetscInt expectedSize,loadedSize;
876 ierr = PetscTestFile(filename,
'r',&found);CHKERRQ(ierr);
877 if(!found) SETERRQ(comm,PETSC_ERR_FILE_OPEN,
878 "Restart/Source file not found: %s",filename);
880 ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
884 ierr = VecCreate(PETSC_COMM_SELF, &temp_vec); CHKERRQ(ierr);
885 ierr = VecLoad(temp_vec,viewer);CHKERRQ(ierr);
886 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
889 ierr = VecGetSize(field_vec, &expectedSize);CHKERRQ(ierr);
890 ierr = VecGetSize(temp_vec, &loadedSize);CHKERRQ(ierr);
891 if (loadedSize != expectedSize) {
892 SETERRQ(comm,PETSC_ERR_FILE_UNEXPECTED,
893 "File %s holds %d entries – expected %d for field '%s'",
894 filename, loadedSize, expectedSize, field_name);
898 ierr = VecCopy(temp_vec, field_vec);CHKERRQ(ierr);
901 ierr = VecDestroy(&temp_vec);CHKERRQ(ierr);
917 "Loaded <%s> (serial path)\n",filename);
920 PetscFunctionReturn(0);
927 ierr = VecGetSize(field_vec,&globalSize);CHKERRQ(ierr);
931 const PetscScalar *seqArray = NULL;
938 ierr = PetscTestFile(filename,
'r',&found);CHKERRQ(ierr);
939 if(!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,
940 "Restart file not found: %s",filename);
943 ierr = VecCreate(PETSC_COMM_SELF,&seq_vec);CHKERRQ(ierr);
944 ierr = VecSetType(seq_vec,VECSEQ);CHKERRQ(ierr);
946 ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,
947 FILE_MODE_READ,&viewer);CHKERRQ(ierr);
948 ierr = VecLoad(seq_vec,viewer);CHKERRQ(ierr);
949 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
953 ierr = VecGetSize(seq_vec,&loaded);CHKERRQ(ierr);
954 if(loaded != globalSize)
955 SETERRQ(comm,PETSC_ERR_FILE_UNEXPECTED,
956 "File %s holds %d entries – expected %d",
957 filename,loaded,globalSize);
960 ierr = VecGetArrayRead(seq_vec,&seqArray);CHKERRQ(ierr);
963 "Rank 0 successfully loaded <%s>\n",filename);
967 PetscScalar *buffer = NULL;
971 buffer = (PetscScalar *)seqArray;
975 ierr = PetscMalloc1(globalSize,&buffer);CHKERRQ(ierr);
978 ierr = MPI_Bcast(buffer, (
int)globalSize, MPIU_SCALAR, 0, comm);CHKERRQ(ierr);
981 PetscInt rstart,rend,loc;
982 PetscScalar *locArray;
984 ierr = VecGetOwnershipRange(field_vec,&rstart,&rend);CHKERRQ(ierr);
987 ierr = VecGetArray(field_vec,&locArray);CHKERRQ(ierr);
988 ierr = PetscMemcpy(locArray,
990 loc*
sizeof(PetscScalar));CHKERRQ(ierr);
991 ierr = VecRestoreArray(field_vec,&locArray);CHKERRQ(ierr);
996 ierr = VecRestoreArrayRead(seq_vec,&seqArray);CHKERRQ(ierr);
997 ierr = VecDestroy(&seq_vec);CHKERRQ(ierr);
1001 ierr = PetscFree(buffer);CHKERRQ(ierr);
1005 "Loaded <%s> (parallel path)\n",filename);
1008 PetscFunctionReturn(0);
1026 PetscErrorCode ierr;
1029 const char *source_path = NULL;
1036 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
"Invalid execution mode for reading simulation fields.");
1052 ierr =
ReadFieldData(user,
"pfield", user->
P, ti,
"dat"); CHKERRQ(ierr);
1062 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"ParticleCount Vec is NULL but np>0");
1066 ierr =
ReadOptionalField(user,
"psifield",
"Scalar Psi Field", user->
Psi, ti,
"dat"); CHKERRQ(ierr);
1106 PetscErrorCode ierr;
1133 PetscErrorCode ierr;
1137 ierr =
ReadOptionalField(user,
"Nu_t",
"Turbulent Viscosity", user->
Nu_t, ti,
"dat"); CHKERRQ(ierr);
1138 ierr =
ReadOptionalField(user,
"cs",
"Smagorinsky Constant (Cs)", user->
CS, ti,
"dat"); CHKERRQ(ierr);
1140 DMGlobalToLocalBegin(user->
da, user->
CS, INSERT_VALUES, user->
lCs);
1141 DMGlobalToLocalEnd(user->
da, user->
CS, INSERT_VALUES, user->
lCs);
1143 DMGlobalToLocalBegin(user->
da, user->
Nu_t, INSERT_VALUES, user->
lNu_t);
1144 DMGlobalToLocalEnd(user->
da, user->
Nu_t, INSERT_VALUES, user->
lNu_t);
1164 PetscErrorCode ierr;
1169 ierr =
ReadOptionalField(user,
"Nu_t",
"Turbulent Viscosity", user->
Nu_t, ti,
"dat"); CHKERRQ(ierr);
1176 DMGlobalToLocalBegin(user->
da, user->
Nu_t, INSERT_VALUES, user->
lNu_t);
1177 DMGlobalToLocalEnd(user->
da, user->
Nu_t, INSERT_VALUES, user->
lNu_t);
1206 PetscErrorCode ierr;
1212 swarm = user->
swarm;
1217 ierr = DMSwarmCreateGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1222 ierr =
ReadFieldData(user, field_name, fieldVec, ti, ext);CHKERRQ(ierr);
1225 ierr = DMSwarmDestroyGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1227 PetscFunctionReturn(0);
1251 PetscErrorCode ierr;
1252 DM swarm = user->
swarm;
1254 PetscInt nlocal, nglobal, bs, i;
1255 const PetscScalar *scalar_array;
1256 void *field_array_void;
1259 PetscFunctionBeginUser;
1264 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
1265 ierr = DMSwarmGetSize(swarm, &nglobal); CHKERRQ(ierr);
1267 ierr = DMSwarmGetField(swarm, field_name, &bs, NULL, NULL); CHKERRQ(ierr);
1268 ierr = DMSwarmRestoreField(swarm, field_name, &bs, NULL, NULL); CHKERRQ(ierr);
1271 ierr = VecCreate(PETSC_COMM_WORLD, &temp_vec); CHKERRQ(ierr);
1272 ierr = VecSetType(temp_vec, VECMPI); CHKERRQ(ierr);
1273 ierr = VecSetSizes(temp_vec, nlocal * bs, nglobal * bs); CHKERRQ(ierr);
1274 ierr = VecSetBlockSize(temp_vec, bs); CHKERRQ(ierr);
1275 ierr = VecSetUp(temp_vec); CHKERRQ(ierr);
1278 ierr =
ReadFieldData(user, field_name, temp_vec, ti, ext); CHKERRQ(ierr);
1281 ierr = VecGetArrayRead(temp_vec, &scalar_array); CHKERRQ(ierr);
1282 ierr = DMSwarmGetField(swarm, field_name, NULL, NULL, &field_array_void); CHKERRQ(ierr);
1285 if (strcmp(field_name,
"DMSwarm_pid") == 0) {
1286 PetscInt64 *int64_array = (PetscInt64 *)field_array_void;
1287 for (i = 0; i < nlocal * bs; i++) {
1288 int64_array[i] = (PetscInt64)scalar_array[i];
1291 PetscInt *int_array = (PetscInt *)field_array_void;
1292 for (i = 0; i < nlocal * bs; i++) {
1293 int_array[i] = (PetscInt)scalar_array[i];
1298 ierr = DMSwarmRestoreField(swarm, field_name, NULL, NULL, &field_array_void); CHKERRQ(ierr);
1299 ierr = VecRestoreArrayRead(temp_vec, &scalar_array); CHKERRQ(ierr);
1302 ierr = VecDestroy(&temp_vec); CHKERRQ(ierr);
1304 PetscFunctionReturn(0);
1329 PetscErrorCode ierr;
1332 const char *source_path = NULL;
1334 PetscFunctionBeginUser;
1335 ierr = DMSwarmGetSize(user->
swarm, &nGlobal); CHKERRQ(ierr);
1340 PetscFunctionReturn(0);
1349 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE,
"Invalid execution mode for reading simulation fields.");
1362 SETERRQ(PETSC_COMM_WORLD, ierr,
"Failed to read MANDATORY 'position' field for step %d. Cannot continue.", ti);
1373 ierr =
ReadOptionalSwarmField(user,
"DMSwarm_location_status",
"Migration Status", ti,
"dat"); CHKERRQ(ierr);
1378 PetscFunctionReturn(0);
1383#define __FUNCT__ "WriteFieldData"
1430 const char *field_name,
1435 PetscErrorCode ierr;
1437 PetscMPIInt rank, size;
1439 const PetscInt placeholder_int = 0;
1440 char filename[PETSC_MAX_PATH_LEN];
1443 PetscFunctionBeginUser;
1447 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,
"Output directory was not set before calling WriteFieldData().");
1453 ierr = PetscObjectGetComm((PetscObject)field_vec,&comm);CHKERRQ(ierr);
1454 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1455 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1457 ierr = PetscSNPrintf(filename,
sizeof(filename),
1458 "%s/%s%05" PetscInt_FMT
"_%d.%s",
1462 " Preparing to write <%s> on rank %d/%d\n",
1463 filename,rank,size);
1471 ierr = PetscViewerBinaryOpen(comm,filename,
1472 FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
1473 ierr = VecView(field_vec,viewer);CHKERRQ(ierr);
1474 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1477 " Wrote <%s> (serial path)\n",filename);
1478 PetscFunctionReturn(0);
1488 ierr = VecScatterCreateToZero(field_vec,&scatter,&seq_vec);CHKERRQ(ierr);
1491 ierr = VecScatterBegin(scatter,field_vec,seq_vec,
1492 INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1493 ierr = VecScatterEnd (scatter,field_vec,seq_vec,
1494 INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1501 PetscReal vmin,vmax;
1502 ierr = VecMin(seq_vec,NULL,&vmin);CHKERRQ(ierr);
1503 ierr = VecMax(seq_vec,NULL,&vmax);CHKERRQ(ierr);
1505 " <%s> range = [%.4e … %.4e]\n",
1506 field_name,(
double)vmin,(
double)vmax);
1508 ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,
1509 FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
1510 ierr = VecView(seq_vec,viewer);CHKERRQ(ierr);
1511 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1514 " Wrote <%s> (parallel path)\n",filename);
1518 ierr = VecScatterDestroy(&scatter);CHKERRQ(ierr);
1519 ierr = VecDestroy(&seq_vec);CHKERRQ(ierr);
1522 PetscFunctionReturn(0);
1538 PetscErrorCode ierr;
1601 PetscErrorCode ierr;
1629 PetscErrorCode ierr;
1636 DMLocalToGlobalBegin(user->
da, user->
lCs, INSERT_VALUES, user->
CS);
1637 DMLocalToGlobalEnd(user->
da, user->
lCs, INSERT_VALUES, user->
CS);
1639 DMLocalToGlobalBegin(user->
da, user->
lNu_t, INSERT_VALUES, user->
Nu_t);
1640 DMLocalToGlobalEnd(user->
da, user->
lNu_t, INSERT_VALUES, user->
Nu_t);
1663 PetscErrorCode ierr;
1697 PetscErrorCode ierr;
1701 PetscFunctionBeginUser;
1707 swarm = user->
swarm;
1715 "Attempting to create global vector from field: %s\n",
1717 ierr = DMSwarmCreateGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1724 "Calling WriteFieldData for field: %s\n",
1726 ierr =
WriteFieldData(user, field_name, fieldVec, ti, ext);CHKERRQ(ierr);
1734 "Destroying the global vector for field: %s\n",
1736 ierr = DMSwarmDestroyGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1740 "Successfully wrote swarm data for field: %s\n",
1743 PetscFunctionReturn(0);
1766 PetscErrorCode ierr;
1767 DM swarm = user->
swarm;
1769 PetscInt nlocal, nglobal,bs,i;
1770 void *field_array_void;
1771 PetscScalar *scalar_array;
1773 PetscFunctionBeginUser;
1778 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
1779 ierr = DMSwarmGetSize(swarm, &nglobal); CHKERRQ(ierr);
1780 ierr = DMSwarmGetField(swarm, field_name, &bs, NULL, &field_array_void); CHKERRQ(ierr);
1783 ierr = VecCreate(PETSC_COMM_WORLD, &temp_vec); CHKERRQ(ierr);
1784 ierr = VecSetType(temp_vec, VECMPI); CHKERRQ(ierr);
1785 ierr = VecSetSizes(temp_vec, nlocal*bs, nglobal*bs); CHKERRQ(ierr);
1786 ierr = VecSetUp(temp_vec); CHKERRQ(ierr);
1789 DMSwarmVectorDefineField(swarm,
"position");
1791 ierr = VecGetArray(temp_vec, &scalar_array); CHKERRQ(ierr);
1793 if(strcasecmp(field_name,
"DMSwarm_pid") == 0){
1794 PetscInt64 *int64_array = (PetscInt64 *)field_array_void;
1796 for (i = 0; i < nlocal*bs; i++) {
1797 scalar_array[i] = (PetscScalar)int64_array[i];
1800 PetscInt *int_array = (PetscInt *)field_array_void;
1802 for (i = 0; i < nlocal*bs; i++) {
1803 scalar_array[i] = (PetscScalar)int_array[i];
1808 ierr = VecRestoreArray(temp_vec, &scalar_array); CHKERRQ(ierr);
1809 ierr = DMSwarmRestoreField(swarm, field_name, &bs, NULL, &field_array_void); CHKERRQ(ierr);
1812 ierr =
WriteFieldData(user, field_name, temp_vec, ti, ext); CHKERRQ(ierr);
1815 ierr = VecDestroy(&temp_vec); CHKERRQ(ierr);
1817 PetscFunctionReturn(0);
1838 PetscErrorCode ierr;
1841 PetscFunctionBeginUser;
1844 if (!user->
swarm || simCtx->
np <= 0) {
1845 PetscFunctionReturn(0);
1882 PetscFunctionReturn(0);
1896 PetscErrorCode ierr;
1899 PetscInt globalSize;
1901 const char *dmtype = NULL;
1904 Vec nat = NULL, seqNat = NULL;
1905 VecScatter scatNat = NULL;
1906 const PetscScalar *nar = NULL;
1907 PetscScalar *buf = NULL;
1911 VecScatter scat = NULL;
1912 const PetscScalar *sar = NULL;
1914 PetscFunctionBeginUser;
1916 ierr = PetscObjectGetComm((PetscObject)inVec, &comm); CHKERRQ(ierr);
1917 ierr = MPI_Comm_rank(comm, &rank); CHKERRQ(ierr);
1918 ierr = VecGetSize(inVec, &globalSize); CHKERRQ(ierr);
1922 ierr = VecGetDM(inVec, &dm); CHKERRQ(ierr);
1923 if (dm) { ierr = DMGetType(dm, &dmtype); CHKERRQ(ierr); }
1925 if (dmtype && !strcmp(dmtype, DMDA)) {
1927 ierr = DMDACreateNaturalVector(dm, &nat); CHKERRQ(ierr);
1928 ierr = DMDAGlobalToNaturalBegin(dm, inVec, INSERT_VALUES, nat); CHKERRQ(ierr);
1929 ierr = DMDAGlobalToNaturalEnd (dm, inVec, INSERT_VALUES, nat); CHKERRQ(ierr);
1931 ierr = VecScatterCreateToZero(nat, &scatNat, &seqNat); CHKERRQ(ierr);
1932 ierr = VecScatterBegin(scatNat, nat, seqNat, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1933 ierr = VecScatterEnd (scatNat, nat, seqNat, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1937 ierr = VecGetLocalSize(seqNat, &nseq); CHKERRQ(ierr);
1938 ierr = VecGetArrayRead(seqNat, &nar); CHKERRQ(ierr);
1940 ierr = PetscMalloc1(nseq, &buf); CHKERRQ(ierr);
1941 ierr = PetscMemcpy(buf, nar, (
size_t)nseq *
sizeof(PetscScalar)); CHKERRQ(ierr);
1943 ierr = VecRestoreArrayRead(seqNat, &nar); CHKERRQ(ierr);
1944 *arrayOut = (
double*)buf;
1947 ierr = VecScatterDestroy(&scatNat); CHKERRQ(ierr);
1948 ierr = VecDestroy(&seqNat); CHKERRQ(ierr);
1949 ierr = VecDestroy(&nat); CHKERRQ(ierr);
1952 ierr = VecScatterCreateToZero(inVec, &scat, &seq); CHKERRQ(ierr);
1953 ierr = VecScatterBegin(scat, inVec, seq, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1954 ierr = VecScatterEnd (scat, inVec, seq, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1958 ierr = VecGetLocalSize(seq, &nseq); CHKERRQ(ierr);
1959 ierr = VecGetArrayRead(seq, &sar); CHKERRQ(ierr);
1961 ierr = PetscMalloc1(nseq, &buf); CHKERRQ(ierr);
1962 ierr = PetscMemcpy(buf, sar, (
size_t)nseq *
sizeof(PetscScalar)); CHKERRQ(ierr);
1964 ierr = VecRestoreArrayRead(seq, &sar); CHKERRQ(ierr);
1965 *arrayOut = (
double*)buf;
1968 ierr = VecScatterDestroy(&scat); CHKERRQ(ierr);
1969 ierr = VecDestroy(&seq); CHKERRQ(ierr);
1972 PetscFunctionReturn(0);
1989PetscErrorCode
SwarmFieldToArrayOnRank0(DM swarm,
const char *field_name, PetscInt *n_total_particles, PetscInt *n_components,
void **gathered_array)
1991 PetscErrorCode ierr;
1992 PetscMPIInt rank, size;
1993 PetscInt nlocal, nglobal, bs;
1994 void *local_array_void;
1995 size_t element_size = 0;
1996 MPI_Datatype mpi_type = MPI_BYTE;
1998 PetscFunctionBeginUser;
2000 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
2001 ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size); CHKERRQ(ierr);
2004 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
2005 ierr = DMSwarmGetSize(swarm, &nglobal); CHKERRQ(ierr);
2006 ierr = DMSwarmGetField(swarm, field_name, &bs, NULL, &local_array_void); CHKERRQ(ierr);
2009 if (strcasecmp(field_name,
"DMSwarm_pid") == 0) {
2010 element_size =
sizeof(PetscInt64);
2011 }
else if (strcasecmp(field_name,
"DMSwarm_CellID") == 0 || strcasecmp(field_name,
"DMSwarm_location_status") == 0) {
2012 element_size =
sizeof(PetscInt);
2014 element_size =
sizeof(PetscScalar);
2018 *n_total_particles = nglobal;
2020 *gathered_array = NULL;
2025 ierr = PetscMalloc(nglobal * bs * element_size, gathered_array); CHKERRQ(ierr);
2026 ierr = PetscMemcpy(*gathered_array, local_array_void, nglobal * bs * element_size); CHKERRQ(ierr);
2029 PetscInt *recvcounts = NULL, *displs = NULL;
2031 ierr = PetscMalloc1(size, &recvcounts); CHKERRQ(ierr);
2032 ierr = PetscMalloc1(size, &displs); CHKERRQ(ierr);
2034 PetscInt sendcount = nlocal * bs;
2037 ierr = MPI_Gather(&sendcount, 1, MPIU_INT, recvcounts, 1, MPIU_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
2042 for (PetscMPIInt i = 0; i < size; i++) recvcounts[i] *= element_size;
2043 for (PetscMPIInt i = 1; i < size; i++) displs[i] = displs[i-1] + recvcounts[i-1];
2045 ierr = PetscMalloc(nglobal * bs * element_size, gathered_array); CHKERRQ(ierr);
2049 ierr = MPI_Gatherv(local_array_void, nlocal * bs * element_size, MPI_BYTE,
2050 *gathered_array, recvcounts, displs, MPI_BYTE,
2051 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
2054 ierr = PetscFree(recvcounts); CHKERRQ(ierr);
2055 ierr = PetscFree(displs); CHKERRQ(ierr);
2059 ierr = DMSwarmRestoreField(swarm, field_name, &bs, NULL, &local_array_void); CHKERRQ(ierr);
2061 PetscFunctionReturn(0);
2084 PetscErrorCode ierr;
2086 Cmpnts global_min_coords, global_max_coords;
2087 PetscReal StartTime;
2088 PetscInt StartStep,StepsToRun,total_num_particles;
2089 PetscMPIInt num_mpi_procs;
2093 num_mpi_procs = simCtx->
size;
2097 total_num_particles = simCtx->
np;
2101 PetscFunctionBeginUser;
2103 if (!user) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"DisplayBanner - UserCtx pointer is NULL.");
2104 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
2109 if (bboxlist_on_rank0 && num_mpi_procs > 0) {
2110 global_min_coords = bboxlist_on_rank0[0].
min_coords;
2111 global_max_coords = bboxlist_on_rank0[0].
max_coords;
2112 for (PetscMPIInt p = 1; p < num_mpi_procs; ++p) {
2113 global_min_coords.
x = PetscMin(global_min_coords.
x, bboxlist_on_rank0[p].
min_coords.
x);
2114 global_min_coords.
y = PetscMin(global_min_coords.
y, bboxlist_on_rank0[p].
min_coords.
y);
2115 global_min_coords.
z = PetscMin(global_min_coords.
z, bboxlist_on_rank0[p].
min_coords.
z);
2116 global_max_coords.
x = PetscMax(global_max_coords.
x, bboxlist_on_rank0[p].
max_coords.
x);
2117 global_max_coords.
y = PetscMax(global_max_coords.
y, bboxlist_on_rank0[p].
max_coords.
y);
2118 global_max_coords.
z = PetscMax(global_max_coords.
z, bboxlist_on_rank0[p].
max_coords.
z);
2125 LOG_ALLOW(PETSC_COMM_SELF,
LOG_WARNING,
"(Rank 0) - bboxlist not provided or num_mpi_procs <=0; using user->bbox for domain bounds.\n");
2131 ierr = PetscPrintf(PETSC_COMM_SELF,
"\n"); CHKERRQ(ierr);
2132 ierr = PetscPrintf(PETSC_COMM_SELF,
"=============================================================\n"); CHKERRQ(ierr);
2133 ierr = PetscPrintf(PETSC_COMM_SELF,
" CASE SUMMARY \n"); CHKERRQ(ierr);
2134 ierr = PetscPrintf(PETSC_COMM_SELF,
"=============================================================\n"); CHKERRQ(ierr);
2135 ierr = PetscPrintf(PETSC_COMM_SELF,
" Grid Points : %d X %d X %d\n", user->
IM, user->
JM, user->
KM); CHKERRQ(ierr);
2136 ierr = PetscPrintf(PETSC_COMM_SELF,
" Cells : %d X %d X %d\n", user->
IM - 1, user->
JM - 1, user->
KM - 1); CHKERRQ(ierr);
2137 ierr = PetscPrintf(PETSC_COMM_SELF,
" Global Domain Bounds (X) : %.6f to %.6f\n", (
double)global_min_coords.
x, (
double)global_max_coords.
x); CHKERRQ(ierr);
2138 ierr = PetscPrintf(PETSC_COMM_SELF,
" Global Domain Bounds (Y) : %.6f to %.6f\n", (
double)global_min_coords.
y, (
double)global_max_coords.
y); CHKERRQ(ierr);
2139 ierr = PetscPrintf(PETSC_COMM_SELF,
" Global Domain Bounds (Z) : %.6f to %.6f\n", (
double)global_min_coords.
z, (
double)global_max_coords.
z); CHKERRQ(ierr);
2140 ierr = PetscPrintf(PETSC_COMM_SELF,
"-------------------- Boundary Conditions --------------------\n"); CHKERRQ(ierr);
2141 const int face_name_width = 17;
2144 for (PetscInt i_face = 0; i_face < 6; ++i_face) {
2149 ierr = PetscPrintf(PETSC_COMM_SELF,
" Face %-*s : %s\n",
2150 face_name_width, face_str, bc_type_str); CHKERRQ(ierr);
2152 ierr = PetscPrintf(PETSC_COMM_SELF,
"-------------------------------------------------------------\n"); CHKERRQ(ierr);
2153 ierr = PetscPrintf(PETSC_COMM_SELF,
" Start Time : %.4f\n", (
double)StartTime); CHKERRQ(ierr);
2154 ierr = PetscPrintf(PETSC_COMM_SELF,
" Timestep Size : %.4f\n", (
double)simCtx->
dt); CHKERRQ(ierr);
2155 ierr = PetscPrintf(PETSC_COMM_SELF,
" Starting Step : %d\n", StartStep); CHKERRQ(ierr);
2156 ierr = PetscPrintf(PETSC_COMM_SELF,
" Total Steps to Run : %d\n", StepsToRun); CHKERRQ(ierr);
2157 ierr = PetscPrintf(PETSC_COMM_SELF,
" Number of MPI Processes : %d\n", num_mpi_procs); CHKERRQ(ierr);
2158 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Number of Particles : %d\n", total_num_particles); CHKERRQ(ierr);
2159 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Reynolds Number : %le\n", simCtx->
ren); CHKERRQ(ierr);
2160 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Stanton Number : %le\n", simCtx->
st); CHKERRQ(ierr);
2161 ierr = PetscPrintf(PETSC_COMM_WORLD,
" CFL Number : %le\n", simCtx->
cfl); CHKERRQ(ierr);
2162 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Von-Neumann Number : %le\n", simCtx->
vnn); CHKERRQ(ierr);
2163 ierr = PetscPrintf(PETSC_COMM_SELF,
" Particle Initialization Mode: %s\n", particle_init_str); CHKERRQ(ierr);
2164 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Large Eddy Simulation Model : %s\n",
LESFlagToString(simCtx->
les)); CHKERRQ(ierr);
2169 ierr = PetscPrintf(PETSC_COMM_SELF,
" Particles Initialized At : --- (No INLET face identified)\n"); CHKERRQ(ierr);
2173 ierr = PetscPrintf(PETSC_COMM_SELF,
" Field Initialization Mode : %s\n", field_init_str); CHKERRQ(ierr);
2178 ierr = PetscPrintf(PETSC_COMM_SELF,
"=============================================================\n"); CHKERRQ(ierr);
2179 ierr = PetscPrintf(PETSC_COMM_SELF,
"\n"); CHKERRQ(ierr);
2181 PetscFunctionReturn(0);
2185#define __FUNCT__ "ParsePostProcessingSettings"
2203 PetscBool startTimeSet, endTimeSet, timeStepSet;
2205 PetscFunctionBeginUser;
2208 if (!simCtx || !simCtx->
pps) {
2209 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_NULL,
"SimCtx or its pps member is NULL in ParsePostProcessingSettings.");
2236 file = fopen(configFile,
"r");
2239 while (fgets(line,
sizeof(line), file)) {
2240 char *key, *value, *comment;
2241 comment = strchr(line,
'#');
if (comment) *comment =
'\0';
2243 key = strtok(line,
"="); value = strtok(NULL,
"=");
2246 if (strcmp(key,
"startTime") == 0) pps->
startTime = atoi(value);
2247 else if (strcmp(key,
"endTime") == 0) pps->
endTime = atoi(value);
2248 else if (strcmp(key,
"timeStep") == 0) pps->
timeStep = atoi(value);
2249 else if (strcmp(key,
"output_particles") == 0) {
2250 if (strcasecmp(value,
"true") == 0) pps->
outputParticles = PETSC_TRUE;
2252 else if (strcasecmp(key,
"process_pipeline") == 0) {
2255 }
else if (strcasecmp(key,
"output_fields_instantaneous") == 0) {
2258 }
else if (strcasecmp(key,
"output_prefix") == 0) {
2261 }
else if (strcasecmp(key,
"particle_output_prefix") == 0) {
2264 }
else if (strcasecmp(key,
"particle_fields_instantaneous") == 0) {
2267 }
else if (strcasecmp(key,
"particle_pipeline") == 0) {
2270 }
else if (strcasecmp(key,
"particle_output_freq") == 0) {
2272 }
else if (strcmp(key,
"reference_ip") == 0) {pps->
reference[0] = atoi(value);
2273 }
else if (strcmp(key,
"reference_jp") == 0) {pps->
reference[1] = atoi(value);
2274 }
else if (strcmp(key,
"reference_kp") == 0) {pps->
reference[2] = atoi(value);
2275 }
else if (strcasecmp(key,
"source_directory") == 0) {
2286 LOG_ALLOW(
GLOBAL,
LOG_WARNING,
"Could not open post-processing config file '%s'. Using defaults and command-line overrides.\n", configFile);
2290 PetscOptionsGetInt(NULL, NULL,
"-startTime", &pps->
startTime, &startTimeSet);
2291 PetscOptionsGetInt(NULL, NULL,
"-endTime", &pps->
endTime, &endTimeSet);
2292 PetscOptionsGetInt(NULL, NULL,
"-timeStep", &pps->
timeStep, &timeStepSet);
2293 PetscOptionsGetBool(NULL, NULL,
"-output_particles", &pps->
outputParticles, NULL);
2300 if (startTimeSet && !endTimeSet) {
2304 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"Post-processing configured to run from t=%d to t=%d with step %d. Particle output: %s.\n",
2316 PetscFunctionReturn(0);
2321#define __FUNCT__ "ParseScalingInformation"
2336 PetscErrorCode ierr;
2339 PetscFunctionBeginUser;
2342 if (!simCtx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"SimCtx is NULL in ParseScalingInformation");
2351 ierr = PetscOptionsGetReal(NULL, NULL,
"-scaling_L_ref", &simCtx->
scaling.
L_ref, &flg); CHKERRQ(ierr);
2352 ierr = PetscOptionsGetReal(NULL, NULL,
"-scaling_U_ref", &simCtx->
scaling.
U_ref, &flg); CHKERRQ(ierr);
2353 ierr = PetscOptionsGetReal(NULL, NULL,
"-scaling_rho_ref", &simCtx->
scaling.
rho_ref, &flg); CHKERRQ(ierr);
2358 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
"Reference velocity U_ref must be positive. Got %g", (
double)simCtx->
scaling.
U_ref);
2363 LOG(
GLOBAL,
LOG_INFO,
"---------------- Physical Scales Initialized -----------------\n");
2364 LOG(
GLOBAL,
LOG_INFO,
" L_ref: %.4f, U_ref: %.4f, rho_ref: %.4f, P_ref: %.4f\n",
2366 LOG(
GLOBAL,
LOG_INFO,
"--------------------------------------------------------------\n");
2369 PetscFunctionReturn(0);
2390 PetscMPIInt rank, size;
2391 PetscErrorCode ierr;
2394 double *array = NULL;
2395 PetscInt fileExistsFlag = 0;
2398 "Start reading from file: %s\n",
2402 if (!filename || !data_out || !Nout) {
2404 "Null pointer argument provided.\n");
2409 MPI_Comm_rank(comm, &rank);
2410 MPI_Comm_size(comm, &size);
2414 fp = fopen(filename,
"r");
2423 ierr = MPI_Bcast(&fileExistsFlag, 1, MPI_INT, 0, comm); CHKERRQ(ierr);
2425 if (!fileExistsFlag) {
2429 "File '%s' not found.\n",
2437 fp = fopen(filename,
"r");
2440 "File '%s' could not be opened for reading.\n",
2448 while (fgets(line,
sizeof(line), fp)) {
2454 "File '%s' has %d lines.\n",
2458 array = (
double*)malloc(N *
sizeof(
double));
2462 "malloc failed for array.\n");
2471 while (fgets(line,
sizeof(line), fp)) {
2473 if (sscanf(line,
"%lf", &val) == 1) {
2481 "Successfully read %d values from '%s'.\n",
2486 ierr = MPI_Bcast(&N, 1, MPI_INT, 0, comm); CHKERRQ(ierr);
2490 array = (
double*)malloc(N *
sizeof(
double));
2493 "malloc failed on rank %d.\n",
2500 ierr = MPI_Bcast(array, N, MPI_DOUBLE, 0, comm); CHKERRQ(ierr);
2507 "Done. Provided array of length=%d to all ranks.\n",
2528 double **coordsArray,
2531 PetscFunctionBeginUser;
2533 PetscErrorCode ierr;
2537 ierr = VecCreate(PETSC_COMM_WORLD, &coordsVec);CHKERRQ(ierr);
2538 ierr = VecSetFromOptions(coordsVec);CHKERRQ(ierr);
2541 ierr =
ReadFieldData(user,
"position", coordsVec, timeIndex,
"dat");
2544 "Error reading position data (ti=%d).\n",
2546 PetscFunctionReturn(ierr);
2552 ierr = VecDestroy(&coordsVec);CHKERRQ(ierr);
2555 "Successfully gathered coordinates. Ncoords=%d.\n", *Ncoords);
2556 PetscFunctionReturn(0);
2575 const char *fieldName,
2577 double **scalarArray,
2580 PetscFunctionBeginUser;
2582 PetscErrorCode ierr;
2586 ierr = VecCreate(PETSC_COMM_WORLD, &fieldVec);CHKERRQ(ierr);
2587 ierr = VecSetFromOptions(fieldVec);CHKERRQ(ierr);
2589 ierr =
ReadFieldData(user, fieldName, fieldVec, timeIndex,
"dat");
2592 "Error reading field '%s' (ti=%d).\n",
2593 fieldName, timeIndex);
2594 PetscFunctionReturn(ierr);
2600 ierr = VecDestroy(&fieldVec);CHKERRQ(ierr);
2603 "Successfully gathered field '%s'. Nscalars=%d.\n",
2604 fieldName, *Nscalars);
2605 PetscFunctionReturn(0);
PetscErrorCode ReadStatisticalFields(UserCtx *user, PetscInt ti)
Reads statistical fields for averaging purposes.
PetscErrorCode ParsePostProcessingSettings(SimCtx *simCtx)
Initializes post-processing settings from a config file and command-line overrides.
PetscErrorCode WriteAllSwarmFields(UserCtx *user)
Writes a predefined set of PETSc Swarm fields to files.
PetscErrorCode SwarmFieldToArrayOnRank0(DM swarm, const char *field_name, PetscInt *n_total_particles, PetscInt *n_components, void **gathered_array)
Gathers any DMSwarm field from all ranks to a single, contiguous array on rank 0.
PetscErrorCode ParseScalingInformation(SimCtx *simCtx)
Parses physical scaling parameters from command-line options.
PetscInt ReadDataFileToArray(const char *filename, double **data_out, PetscInt *Nout, MPI_Comm comm)
PetscErrorCode ReadGridFile(UserCtx *user)
Sets grid dimensions from a file for a SINGLE block using a one-time read cache.
PetscErrorCode StringToBCHandlerType(const char *str, BCHandlerType *handler_out)
Converts a string representation of a handler to a BCHandlerType enum.
PetscErrorCode ReadRANSFields(UserCtx *user, PetscInt ti)
Reads RANS-related fields.
PetscErrorCode GetBCParamReal(BC_Param *params, const char *key, PetscReal *value_out, PetscBool *found)
Searches a BC_Param linked list for a key and returns its value as a double.
PetscErrorCode ReadSwarmIntField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext)
Reads integer swarm data by using ReadFieldData and casting the result.
PetscErrorCode WriteSwarmField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext)
Writes data from a specific field in a PETSc Swarm to a file.
static PetscInt * g_IMs_from_file
Caches the IM dimensions for all blocks read from the grid file.
PetscErrorCode WriteStatisticalFields(UserCtx *user)
Writes statistical fields for averaging purposes.
PetscErrorCode ReadSimulationFields(UserCtx *user, PetscInt ti)
Reads simulation fields from files into their respective PETSc vectors.
static PetscInt g_nblk_from_file
Stores the number of blocks read from the grid file.
PetscErrorCode WriteFieldData(UserCtx *user, const char *field_name, Vec field_vec, PetscInt ti, const char *ext)
Writes data from a specific PETSc vector to a single, sequential file.
PetscErrorCode ReadLESFields(UserCtx *user, PetscInt ti)
Reads LES-related fields.
PetscErrorCode ReadFieldData(UserCtx *user, const char *field_name, Vec field_vec, PetscInt ti, const char *ext)
Reads data for a specific field from a file into the provided vector.
PetscErrorCode ParseAllBoundaryConditions(UserCtx *user, const char *bcs_input_filename)
Parses the boundary conditions file to configure the type, handler, and any associated parameters for...
PetscErrorCode ValidateBCHandlerForBCType(BCType type, BCHandlerType handler)
Validates that a specific handler is compatible with a general BC type.
void TrimWhitespace(char *str)
Trims leading and trailing whitespace from a string in-place.
PetscErrorCode WriteSimulationFields(UserCtx *user)
Writes simulation fields to files.
PetscErrorCode ReadAllSwarmFields(UserCtx *user, PetscInt ti)
Reads multiple fields into a DMSwarm, gracefully handling missing optional files.
static PetscBool g_file_has_been_read
A flag to ensure the grid file is read only once.
PetscErrorCode ReadGridGenerationInputs(UserCtx *user)
Parses command-line options for a programmatically generated grid for a SINGLE block.
PetscErrorCode ReadSwarmField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext)
Reads data from a file into a specified field of a PETSc DMSwarm.
PetscErrorCode WriteRANSFields(UserCtx *user)
Writes RANS-related fields.
PetscErrorCode ReadFieldDataToRank0(PetscInt timeIndex, const char *fieldName, UserCtx *user, double **scalarArray, PetscInt *Nscalars)
Reads a named field from file into a PETSc Vec, then gathers it to rank 0.
PetscErrorCode VecToArrayOnRank0(Vec inVec, PetscInt *N, double **arrayOut)
Gather a (possibly distributed) PETSc Vec onto rank 0 as a contiguous C array.
PetscErrorCode WriteSwarmIntField(UserCtx *user, const char *field_name, PetscInt ti, const char *ext)
Writes integer swarm data by casting it to a temporary Vec and using WriteFieldData.
PetscErrorCode WriteLESFields(UserCtx *user)
Writes LES-related fields.
static PetscInt * g_KMs_from_file
Caches the KM dimensions for all blocks read from the grid file.
static PetscInt * g_JMs_from_file
Caches the JM dimensions for all blocks read from the grid file.
PetscErrorCode StringToBCFace(const char *str, BCFace *face_out)
Converts a string representation of a face to a BCFace enum.
PetscErrorCode ReadPositionsFromFile(PetscInt timeIndex, UserCtx *user, double **coordsArray, PetscInt *Ncoords)
Reads coordinate data (for particles) from file into a PETSc Vec, then gathers it to rank 0.
static PetscErrorCode ReadOptionalField(UserCtx *user, const char *field_name, const char *field_label, Vec field_vec, PetscInt ti, const char *ext)
Checks for and reads an optional Eulerian field from a file into a Vec.
PetscErrorCode VerifyPathExistence(const char *path, PetscBool is_dir, PetscBool is_optional, const char *description, PetscBool *exists)
A parallel-safe helper to verify the existence of a generic file or directory path.
void FreeBC_ParamList(BC_Param *head)
Frees the memory allocated for a linked list of BC_Param structs.
PetscErrorCode StringToBCType(const char *str, BCType *type_out)
Converts a string representation of a BC type to a BCType enum.
PetscErrorCode DisplayBanner(SimCtx *simCtx)
Displays a structured banner summarizing the simulation configuration.
static PetscErrorCode CheckDataFile(UserCtx *user, PetscInt ti, const char *fieldName, const char *ext, PetscBool *fileExists)
Checks for a data file's existence in a parallel-safe manner.
static PetscErrorCode ReadOptionalSwarmField(UserCtx *user, const char *field_name, const char *field_label, PetscInt ti, const char *ext)
Checks for and reads an optional DMSwarm field from a file.
Public interface for data input/output routines.
const char * BCHandlerTypeToString(BCHandlerType handler_type)
Converts a BCHandlerType enum to its string representation.
const char * FieldInitializationToString(PetscInt FieldInitialization)
Helper function to convert FieldInitialization to a string representation.
#define LOG_ALLOW_SYNC(scope, level, fmt,...)
----— DEBUG ---------------------------------------— #define LOG_ALLOW(scope, level,...
#define LOCAL
Logging scope definitions for controlling message output.
#define GLOBAL
Scope for global logging across all processes.
const char * BCFaceToString(BCFace face)
Helper function to convert BCFace enum to a string representation.
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
const char * ParticleInitializationToString(PetscInt ParticleInitialization)
Helper function to convert ParticleInitialization to a string representation.
#define LOG(scope, level, fmt,...)
Logging macro for PETSc-based applications with scope control.
const char * BCTypeToString(BCType type)
Helper function to convert BCType enum to a string representation.
@ LOG_ERROR
Critical errors that may halt the program.
@ LOG_TRACE
Very fine-grained tracing information for in-depth debugging.
@ LOG_INFO
Informational messages about program execution.
@ LOG_WARNING
Non-critical issues that warrant attention.
@ LOG_DEBUG
Detailed debugging information.
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled code block (typically a function).
const char * LESFlagToString(PetscInt LESFlag)
Helper function to convert LES Flag to a string representation.
BCType
Defines the general mathematical/physical category of a boundary.
PetscBool inletFaceDefined
char particle_output_prefix[256]
BoundaryFaceConfig boundary_faces[6]
BCFace identifiedInletBCFace
char euler_subdir[PETSC_MAX_PATH_LEN]
SimCtx * simCtx
Back-pointer to the master simulation context.
#define MAX_FIELD_LIST_LENGTH
BCHandlerType
Defines the specific computational "strategy" for a boundary handler.
@ BC_HANDLER_INLET_PARABOLIC
@ BC_HANDLER_INLET_CONSTANT_VELOCITY
@ BC_HANDLER_NOGRAD_COPY_GHOST
@ BC_HANDLER_OUTLET_CONSERVATION
char output_fields_averaged[1024]
PetscInt FieldInitialization
#define MAX_PIPELINE_LENGTH
BCHandlerType handler_type
Cmpnts max_coords
Maximum x, y, z coordinates of the bounding box.
char output_dir[PETSC_MAX_PATH_LEN]
#define MAX_FILENAME_LENGTH
Cmpnts min_coords
Minimum x, y, z coordinates of the bounding box.
char * current_io_directory
char grid_file[PETSC_MAX_PATH_LEN]
char output_fields_instantaneous[1024]
char particle_subdir[PETSC_MAX_PATH_LEN]
char source_dir[PETSC_MAX_PATH_LEN]
char particle_pipeline[1024]
char process_pipeline[1024]
PetscInt particle_output_freq
char particle_fields[1024]
PetscBool outputParticles
Cmpnts InitialConstantContra
@ EXEC_MODE_POSTPROCESSOR
char _io_context_buffer[PETSC_MAX_PATH_LEN]
PetscInt ParticleInitialization
char PostprocessingControlFile[PETSC_MAX_PATH_LEN]
char restart_dir[PETSC_MAX_PATH_LEN]
PetscInt LoggingFrequency
BCFace
Identifies the six logical faces of a structured computational block.
BoundaryCondition * handler
A node in a linked list for storing key-value parameters from the bcs.dat file.
Holds the complete configuration for one of the six boundary faces.
Defines a 3D axis-aligned bounding box.
A 3D point or vector with PetscScalar components.
Holds all configuration parameters for a post-processing run.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.