33#define __FUNCT__ "ReadGridGenerationInputs"
52 PetscInt block_index = user->
_this;
56 PetscInt *IMs = NULL, *JMs = NULL, *KMs = NULL, *cgrids = NULL;
57 PetscReal *xMins = NULL, *xMaxs = NULL, *rxs = NULL;
58 PetscReal *yMins = NULL, *yMaxs = NULL, *rys = NULL;
59 PetscReal *zMins = NULL, *zMaxs = NULL, *rzs = NULL;
61 PetscFunctionBeginUser;
65 if (block_index >= nblk) {
66 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
"Block index %d is out of range for nblk=%d", block_index, nblk);
70 ierr = PetscMalloc4(nblk, &IMs, nblk, &JMs, nblk, &KMs, nblk, &cgrids); CHKERRQ(ierr);
71 ierr = PetscMalloc6(nblk, &xMins, nblk, &xMaxs, nblk, &rxs, nblk, &yMins, nblk, &yMaxs, nblk, &rys); CHKERRQ(ierr);
72 ierr = PetscMalloc3(nblk, &zMins, nblk, &zMaxs, nblk, &rzs); CHKERRQ(ierr);
75 for (PetscInt i = 0; i < nblk; ++i) {
76 IMs[i] = 10; JMs[i] = 10; KMs[i] = 10; cgrids[i] = 0;
77 xMins[i] = 0.0; xMaxs[i] = 1.0; rxs[i] = 1.0;
78 yMins[i] = 0.0; yMaxs[i] = 1.0; rys[i] = 1.0;
79 zMins[i] = 0.0; zMaxs[i] = 1.0; rzs[i] = 1.0;
84 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-im", IMs, &count, &found); CHKERRQ(ierr);
85 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-jm", JMs, &count, &found); CHKERRQ(ierr);
86 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-km", KMs, &count, &found); CHKERRQ(ierr);
87 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-xMins", xMins, &count, &found); CHKERRQ(ierr);
88 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-xMaxs", xMaxs, &count, &found); CHKERRQ(ierr);
89 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-rxs", rxs, &count, &found); CHKERRQ(ierr);
90 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-yMins", yMins, &count, &found); CHKERRQ(ierr);
91 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-yMaxs", yMaxs, &count, &found); CHKERRQ(ierr);
92 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-rys", rys, &count, &found); CHKERRQ(ierr);
93 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-zMins", zMins, &count, &found); CHKERRQ(ierr);
94 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-zMaxs", zMaxs, &count, &found); CHKERRQ(ierr);
95 count = nblk; ierr = PetscOptionsGetRealArray(NULL, NULL,
"-rzs", rzs, &count, &found); CHKERRQ(ierr);
96 count = nblk; ierr = PetscOptionsGetIntArray(NULL, NULL,
"-cgrids", cgrids, &count, &found); CHKERRQ(ierr);
99 user->
IM = IMs[block_index];
100 user->
JM = JMs[block_index];
101 user->
KM = KMs[block_index];
102 user->
Min_X = xMins[block_index];
103 user->
Max_X = xMaxs[block_index];
104 user->
rx = rxs[block_index];
105 user->
Min_Y = yMins[block_index];
106 user->
Max_Y = yMaxs[block_index];
107 user->
ry = rys[block_index];
108 user->
Min_Z = zMins[block_index];
109 user->
Max_Z = zMaxs[block_index];
110 user->
rz = rzs[block_index];
111 user->
cgrid = cgrids[block_index];
114 simCtx->
rank, block_index, user->
IM, user->
JM, user->
KM);
119 ierr = PetscFree4(IMs, JMs, KMs, cgrids); CHKERRQ(ierr);
120 ierr = PetscFree6(xMins, xMaxs, rxs, yMins, yMaxs, rys); CHKERRQ(ierr);
121 ierr = PetscFree3(zMins, zMaxs, rzs); CHKERRQ(ierr);
123 PetscFunctionReturn(0);
128#define __FUNCT__ "ReadGridFile"
149 PetscInt block_index = user->
_this;
151 PetscFunctionBeginUser;
156 PetscMPIInt rank = simCtx->
rank;
160 FILE *fd = fopen(simCtx->
grid_file,
"r");
161 if (!fd) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
"Cannot open file: %s", simCtx->
grid_file);
165 char firstTok[32] = {0};
166 if (fscanf(fd,
"%31s", firstTok) != 1)
167 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
"Empty grid file: %s", simCtx->
grid_file);
169 if (strcmp(firstTok,
"PICGRID") == 0) {
172 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
"Expected number of blocks after \"PICGRID\" in %s", simCtx->
grid_file);
178 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED,
"Mismatch: -nblk is %d but grid file specifies %d blocks.", nblk,
g_nblk_from_file);
182 for (PetscInt i = 0; i < nblk; ++i) {
184 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_READ,
"Expected 3 integers for block %d in %s", i, simCtx->
grid_file);
191 ierr = MPI_Bcast(&
g_nblk_from_file, 1, MPI_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
199 ierr = MPI_Bcast(
g_IMs_from_file, nblk, MPI_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
200 ierr = MPI_Bcast(
g_JMs_from_file, nblk, MPI_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
201 ierr = MPI_Bcast(
g_KMs_from_file, nblk, MPI_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
213 simCtx->
rank, block_index, user->
IM, user->
JM, user->
KM);
215 PetscFunctionReturn(0);
231 while (current != NULL) {
233 PetscFree(current->
key);
234 PetscFree(current->
value);
248 else if (strcasecmp(str,
"+Xi") == 0) *face_out =
BC_FACE_POS_X;
249 else if (strcasecmp(str,
"-Eta") == 0) *face_out =
BC_FACE_NEG_Y;
250 else if (strcasecmp(str,
"+Eta") == 0) *face_out =
BC_FACE_POS_Y;
251 else if (strcasecmp(str,
"-Zeta") == 0) *face_out =
BC_FACE_NEG_Z;
252 else if (strcasecmp(str,
"+Zeta") == 0) *face_out =
BC_FACE_POS_Z;
253 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE,
"Unknown face specifier: %s", str);
264 if (strcasecmp(str,
"WALL") == 0) *type_out =
WALL;
265 else if (strcasecmp(str,
"SYMMETRY") == 0) *type_out =
SYMMETRY;
266 else if (strcasecmp(str,
"INLET") == 0) *type_out =
INLET;
267 else if (strcasecmp(str,
"OUTLET") == 0) *type_out =
OUTLET;
268 else if (strcasecmp(str,
"NOGRAD") == 0) *type_out =
NOGRAD;
270 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE,
"Unknown BC Type string: %s", str);
287 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE,
"Unknown BC Handler string: %s", str);
347 PetscFunctionBeginUser;
348 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
352 char line_buffer[1024];
355 for (
int i = 0; i < 6; i++) {
359 configs_rank0[i].
params = NULL;
360 configs_rank0[i].
handler = NULL;
364 file = fopen(bcs_input_filename,
"r");
365 if (!file) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
"Could not open BCs file '%s'.", bcs_input_filename);
367 while (fgets(line_buffer,
sizeof(line_buffer), file)) {
368 char *current_pos = line_buffer;
369 while (isspace((
unsigned char)*current_pos)) current_pos++;
370 if (*current_pos ==
'#' || *current_pos ==
'\0' || *current_pos ==
'\n' || *current_pos ==
'\r')
continue;
372 char *face_str = strtok(current_pos,
" \t\n\r");
373 char *type_str = strtok(NULL,
" \t\n\r");
374 char *handler_str = strtok(NULL,
" \t\n\r");
376 if (!face_str || !type_str || !handler_str) {
384 const char* handler_name_for_log;
391 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);
397 LOG_ALLOW(
GLOBAL,
LOG_DEBUG,
" Parsed Face '%s': Type=%s, Handler=%s \n", face_str, type_str, handler_name_for_log);
401 configs_rank0[face_enum].
params = NULL;
405 while ((token = strtok(NULL,
" \t\n\r")) != NULL) {
406 char* equals_ptr = strchr(token,
'=');
413 char* key_str = token;
414 char* value_str = equals_ptr + 1;
417 ierr = PetscMalloc1(1, &new_param); CHKERRQ(ierr);
418 ierr = PetscStrallocpy(key_str, &new_param->
key); CHKERRQ(ierr);
419 ierr = PetscStrallocpy(value_str, &new_param->
value); CHKERRQ(ierr);
420 new_param->
next = NULL;
422 *param_next_ptr = new_param;
423 param_next_ptr = &new_param->
next;
437 for (
int i = 0; i < 6; i++) {
446 PetscInt n_params = 0;
450 ierr = MPI_Bcast(&n_params, 1, MPI_INT, 0, PETSC_COMM_WORLD);CHKERRQ(ierr);
459 for (
int j = 0; j < n_params; j++) {
460 char key_buf[256] = {0}, val_buf[256] = {0};
464 for (
int k = 0; k < j; k++) p = p->
next;
465 strncpy(key_buf, p->
key, 255);
466 strncpy(val_buf, p->
value, 255);
469 ierr = MPI_Bcast(key_buf, 256, MPI_CHAR, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
470 ierr = MPI_Bcast(val_buf, 256, MPI_CHAR, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
475 ierr = PetscMalloc1(1, &new_param); CHKERRQ(ierr);
476 ierr = PetscStrallocpy(key_buf, &new_param->
key); CHKERRQ(ierr);
477 ierr = PetscStrallocpy(val_buf, &new_param->
value); CHKERRQ(ierr);
478 new_param->
next = NULL;
479 *param_next_ptr = new_param;
480 param_next_ptr = &new_param->
next;
483 param_next_ptr = &((*param_next_ptr)->next);
491 for (
int i=0; i<6; i++) {
505 PetscFunctionReturn(0);
521static PetscErrorCode
CheckDataFile(PetscInt ti,
const char *fieldName,
const char *ext, PetscBool *fileExists)
525 MPI_Comm comm = PETSC_COMM_WORLD;
526 PetscInt placeholder_int = 0;
528 PetscFunctionBeginUser;
529 ierr = MPI_Comm_rank(comm, &rank); CHKERRQ(ierr);
532 char filename[PETSC_MAX_PATH_LEN];
535 ierr = PetscSNPrintf(filename,
sizeof(filename),
"results/%s%05"PetscInt_FMT
"_%d.%s", fieldName, ti, placeholder_int, ext);
536 ierr = PetscTestFile(filename,
'r', fileExists); CHKERRQ(ierr);
537 if (!(*fileExists)) {
544 PetscMPIInt fileExists_int = (rank == 0) ? (PetscMPIInt)(*fileExists) : 0;
545 ierr = MPI_Bcast(&fileExists_int, 1, MPI_INT, 0, comm); CHKERRMPI(ierr);
546 *fileExists = (PetscBool)fileExists_int;
548 PetscFunctionReturn(0);
574 PetscBool fileExists;
576 PetscFunctionBeginUser;
579 ierr =
CheckDataFile(ti, field_name, ext, &fileExists); CHKERRQ(ierr);
584 if(strcasecmp(field_name,
"DMSwarm_CellID") == 0 || strcasecmp(field_name,
"DMSwarm_pid")== 0 || strcasecmp(field_name,
"DMSwarm_location_status")== 0 ) {
595 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);
604 PetscFunctionReturn(0);
640 const char *field_name,
646 char filename[PETSC_MAX_PATH_LEN];
648 PetscMPIInt rank,size;
650 PetscFunctionBeginUser;
652 ierr = PetscObjectGetComm((PetscObject)field_vec,&comm);CHKERRQ(ierr);
653 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
654 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
660 ierr = PetscSNPrintf(filename,
sizeof(filename),
661 "results/%s%05" PetscInt_FMT
"_0.%s",
662 field_name,ti,ext);CHKERRQ(ierr);
665 "Attempting to read <%s> on rank %d/%d\n",
666 filename,(
int)rank,(
int)size);
676 PetscInt expectedSize,loadedSize;
678 ierr = PetscTestFile(filename,
'r',&found);CHKERRQ(ierr);
679 if(!found) SETERRQ(comm,PETSC_ERR_FILE_OPEN,
680 "Restart file not found: %s",filename);
682 ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
686 ierr = VecCreate(PETSC_COMM_SELF, &temp_vec); CHKERRQ(ierr);
687 ierr = VecLoad(temp_vec,viewer);CHKERRQ(ierr);
688 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
691 ierr = VecGetSize(field_vec, &expectedSize);CHKERRQ(ierr);
692 ierr = VecGetSize(temp_vec, &loadedSize);CHKERRQ(ierr);
693 if (loadedSize != expectedSize) {
694 SETERRQ(comm,PETSC_ERR_FILE_UNEXPECTED,
695 "File %s holds %d entries – expected %d for field '%s'",
696 filename, loadedSize, expectedSize, field_name);
700 ierr = VecCopy(temp_vec, field_vec);CHKERRQ(ierr);
703 ierr = VecDestroy(&temp_vec);CHKERRQ(ierr);
719 "Loaded <%s> (serial path)\n",filename);
720 PetscFunctionReturn(0);
727 ierr = VecGetSize(field_vec,&globalSize);CHKERRQ(ierr);
731 const PetscScalar *seqArray = NULL;
738 ierr = PetscTestFile(filename,
'r',&found);CHKERRQ(ierr);
739 if(!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,
740 "Restart file not found: %s",filename);
743 ierr = VecCreate(PETSC_COMM_SELF,&seq_vec);CHKERRQ(ierr);
744 ierr = VecSetType(seq_vec,VECSEQ);CHKERRQ(ierr);
746 ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,
747 FILE_MODE_READ,&viewer);CHKERRQ(ierr);
748 ierr = VecLoad(seq_vec,viewer);CHKERRQ(ierr);
749 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
753 ierr = VecGetSize(seq_vec,&loaded);CHKERRQ(ierr);
754 if(loaded != globalSize)
755 SETERRQ(comm,PETSC_ERR_FILE_UNEXPECTED,
756 "File %s holds %d entries – expected %d",
757 filename,loaded,globalSize);
760 ierr = VecGetArrayRead(seq_vec,&seqArray);CHKERRQ(ierr);
763 "ReadFieldData - Rank 0 successfully loaded <%s>\n",filename);
767 PetscScalar *buffer = NULL;
771 buffer = (PetscScalar *)seqArray;
775 ierr = PetscMalloc1(globalSize,&buffer);CHKERRQ(ierr);
778 ierr = MPI_Bcast(buffer, (
int)globalSize, MPIU_SCALAR, 0, comm);CHKERRQ(ierr);
781 PetscInt rstart,rend,loc;
782 PetscScalar *locArray;
784 ierr = VecGetOwnershipRange(field_vec,&rstart,&rend);CHKERRQ(ierr);
787 ierr = VecGetArray(field_vec,&locArray);CHKERRQ(ierr);
788 ierr = PetscMemcpy(locArray,
790 loc*
sizeof(PetscScalar));CHKERRQ(ierr);
791 ierr = VecRestoreArray(field_vec,&locArray);CHKERRQ(ierr);
796 ierr = VecRestoreArrayRead(seq_vec,&seqArray);CHKERRQ(ierr);
797 ierr = VecDestroy(&seq_vec);CHKERRQ(ierr);
801 ierr = PetscFree(buffer);CHKERRQ(ierr);
805 "ReadFieldData - Loaded <%s> (parallel path)\n",filename);
807 PetscFunctionReturn(0);
838 ierr =
ReadFieldData(user,
"pfield", user->
P, ti,
"dat"); CHKERRQ(ierr);
846 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"ParticleCount Vec is NULL but np>0");
918 VecDuplicate(user->
P, &Cs);
919 ierr =
ReadFieldData(user,
"cs", Cs, ti,
"dat"); CHKERRQ(ierr);
920 DMGlobalToLocalBegin(user->
da, Cs, INSERT_VALUES, user->
lCs);
921 DMGlobalToLocalEnd(user->
da, Cs, INSERT_VALUES, user->
lCs);
990 ierr = DMSwarmCreateGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
995 ierr =
ReadFieldData(user, field_name, fieldVec, ti, ext);CHKERRQ(ierr);
998 ierr = DMSwarmDestroyGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1000 PetscFunctionReturn(0);
1024 PetscErrorCode ierr;
1025 DM swarm = user->
swarm;
1027 PetscInt nlocal, nglobal, bs, i;
1028 const PetscScalar *scalar_array;
1029 void *field_array_void;
1032 PetscFunctionBeginUser;
1037 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
1038 ierr = DMSwarmGetSize(swarm, &nglobal); CHKERRQ(ierr);
1040 ierr = DMSwarmGetField(swarm, field_name, &bs, NULL, NULL); CHKERRQ(ierr);
1041 ierr = DMSwarmRestoreField(swarm, field_name, &bs, NULL, NULL); CHKERRQ(ierr);
1044 ierr = VecCreate(PETSC_COMM_WORLD, &temp_vec); CHKERRQ(ierr);
1045 ierr = VecSetType(temp_vec, VECMPI); CHKERRQ(ierr);
1046 ierr = VecSetSizes(temp_vec, nlocal * bs, nglobal * bs); CHKERRQ(ierr);
1047 ierr = VecSetBlockSize(temp_vec, bs); CHKERRQ(ierr);
1048 ierr = VecSetUp(temp_vec); CHKERRQ(ierr);
1051 ierr =
ReadFieldData(user, field_name, temp_vec, ti, ext); CHKERRQ(ierr);
1054 ierr = VecGetArrayRead(temp_vec, &scalar_array); CHKERRQ(ierr);
1055 ierr = DMSwarmGetField(swarm, field_name, NULL, NULL, &field_array_void); CHKERRQ(ierr);
1058 if (strcmp(field_name,
"DMSwarm_pid") == 0) {
1059 PetscInt64 *int64_array = (PetscInt64 *)field_array_void;
1060 for (i = 0; i < nlocal * bs; i++) {
1061 int64_array[i] = (PetscInt64)scalar_array[i];
1064 PetscInt *int_array = (PetscInt *)field_array_void;
1065 for (i = 0; i < nlocal * bs; i++) {
1066 int_array[i] = (PetscInt)scalar_array[i];
1071 ierr = DMSwarmRestoreField(swarm, field_name, NULL, NULL, &field_array_void); CHKERRQ(ierr);
1072 ierr = VecRestoreArrayRead(temp_vec, &scalar_array); CHKERRQ(ierr);
1075 ierr = VecDestroy(&temp_vec); CHKERRQ(ierr);
1077 PetscFunctionReturn(0);
1102 PetscErrorCode ierr;
1105 PetscFunctionBeginUser;
1106 ierr = DMSwarmGetSize(user->
swarm, &nGlobal); CHKERRQ(ierr);
1111 PetscFunctionReturn(0);
1118 SETERRQ(PETSC_COMM_WORLD, ierr,
"Failed to read MANDATORY 'position' field for step %d. Cannot continue.", ti);
1129 ierr =
ReadOptionalSwarmField(user,
"DMSwarm_location_status",
"Migration Status", ti,
"dat"); CHKERRQ(ierr);
1132 PetscFunctionReturn(0);
1181 const char *field_name,
1186 PetscErrorCode ierr;
1188 PetscMPIInt rank, size;
1190 const PetscInt placeholder_int = 0;
1191 char filename[PETSC_MAX_PATH_LEN];
1193 PetscFunctionBeginUser;
1198 ierr = PetscObjectGetComm((PetscObject)field_vec,&comm);CHKERRQ(ierr);
1199 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1200 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1202 ierr = PetscSNPrintf(filename,
sizeof(filename),
1203 "results/%s%05" PetscInt_FMT
"_%d.%s",
1204 field_name,ti,placeholder_int,ext);CHKERRQ(ierr);
1207 "WriteFieldData - Preparing to write <%s> on rank %d/%d\n",
1208 filename,rank,size);
1216 ierr = PetscViewerBinaryOpen(comm,filename,
1217 FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
1218 ierr = VecView(field_vec,viewer);CHKERRQ(ierr);
1219 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1222 "WriteFieldData - Wrote <%s> (serial path)\n",filename);
1223 PetscFunctionReturn(0);
1233 ierr = VecScatterCreateToZero(field_vec,&scatter,&seq_vec);CHKERRQ(ierr);
1236 ierr = VecScatterBegin(scatter,field_vec,seq_vec,
1237 INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1238 ierr = VecScatterEnd (scatter,field_vec,seq_vec,
1239 INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
1246 PetscReal vmin,vmax;
1247 ierr = VecMin(seq_vec,NULL,&vmin);CHKERRQ(ierr);
1248 ierr = VecMax(seq_vec,NULL,&vmax);CHKERRQ(ierr);
1250 "WriteFieldData - <%s> range = [%.4e … %.4e]\n",
1251 field_name,(
double)vmin,(
double)vmax);
1253 ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,
1254 FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
1255 ierr = VecView(seq_vec,viewer);CHKERRQ(ierr);
1256 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1259 "WriteFieldData - Wrote <%s> (parallel path)\n",filename);
1263 ierr = VecScatterDestroy(&scatter);CHKERRQ(ierr);
1264 ierr = VecDestroy(&seq_vec);CHKERRQ(ierr);
1266 PetscFunctionReturn(0);
1282 PetscErrorCode ierr;
1337 PetscErrorCode ierr;
1365 PetscErrorCode ierr;
1372 VecDuplicate(user->
P, &Cs);
1373 DMLocalToGlobalBegin(user->
da, user->
lCs, INSERT_VALUES, Cs);
1374 DMLocalToGlobalEnd(user->
da, user->
lCs, INSERT_VALUES, Cs);
1395 PetscErrorCode ierr;
1429 PetscErrorCode ierr;
1433 PetscFunctionBeginUser;
1439 swarm = user->
swarm;
1447 "WriteSwarmField - Attempting to create global vector from field: %s\n",
1449 ierr = DMSwarmCreateGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1456 "WriteSwarmField - Calling WriteFieldData for field: %s\n",
1458 ierr =
WriteFieldData(user, field_name, fieldVec, ti, ext);CHKERRQ(ierr);
1466 "WriteSwarmField - Destroying the global vector for field: %s\n",
1468 ierr = DMSwarmDestroyGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1472 "WriteSwarmField - Successfully wrote swarm data for field: %s\n",
1475 PetscFunctionReturn(0);
1498 PetscErrorCode ierr;
1499 DM swarm = user->
swarm;
1501 PetscInt nlocal, nglobal,bs,i;
1502 void *field_array_void;
1503 PetscScalar *scalar_array;
1505 PetscFunctionBeginUser;
1510 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
1511 ierr = DMSwarmGetSize(swarm, &nglobal); CHKERRQ(ierr);
1512 ierr = DMSwarmGetField(swarm, field_name, &bs, NULL, &field_array_void); CHKERRQ(ierr);
1515 ierr = VecCreate(PETSC_COMM_WORLD, &temp_vec); CHKERRQ(ierr);
1516 ierr = VecSetType(temp_vec, VECMPI); CHKERRQ(ierr);
1517 ierr = VecSetSizes(temp_vec, nlocal*bs, nglobal*bs); CHKERRQ(ierr);
1518 ierr = VecSetUp(temp_vec); CHKERRQ(ierr);
1521 DMSwarmVectorDefineField(swarm,
"position");
1523 ierr = VecGetArray(temp_vec, &scalar_array); CHKERRQ(ierr);
1525 if(strcasecmp(field_name,
"DMSwarm_pid") == 0){
1526 PetscInt64 *int64_array = (PetscInt64 *)field_array_void;
1528 for (i = 0; i < nlocal*bs; i++) {
1529 scalar_array[i] = (PetscScalar)int64_array[i];
1532 PetscInt *int_array = (PetscInt *)field_array_void;
1534 for (i = 0; i < nlocal*bs; i++) {
1535 scalar_array[i] = (PetscScalar)int_array[i];
1540 ierr = VecRestoreArray(temp_vec, &scalar_array); CHKERRQ(ierr);
1541 ierr = DMSwarmRestoreField(swarm, field_name, &bs, NULL, &field_array_void); CHKERRQ(ierr);
1544 ierr =
WriteFieldData(user, field_name, temp_vec, ti, ext); CHKERRQ(ierr);
1547 ierr = VecDestroy(&temp_vec); CHKERRQ(ierr);
1549 PetscFunctionReturn(0);
1570 PetscErrorCode ierr;
1573 PetscFunctionBeginUser;
1576 if (!user->
swarm || simCtx->
np <= 0) {
1577 PetscFunctionReturn(0);
1607 PetscFunctionReturn(0);
1621 PetscErrorCode ierr;
1624 PetscInt globalSize;
1626 const char *dmtype = NULL;
1629 Vec nat = NULL, seqNat = NULL;
1630 VecScatter scatNat = NULL;
1631 const PetscScalar *nar = NULL;
1632 PetscScalar *buf = NULL;
1636 VecScatter scat = NULL;
1637 const PetscScalar *sar = NULL;
1639 PetscFunctionBeginUser;
1641 ierr = PetscObjectGetComm((PetscObject)inVec, &comm); CHKERRQ(ierr);
1642 ierr = MPI_Comm_rank(comm, &rank); CHKERRQ(ierr);
1643 ierr = VecGetSize(inVec, &globalSize); CHKERRQ(ierr);
1647 ierr = VecGetDM(inVec, &dm); CHKERRQ(ierr);
1648 if (dm) { ierr = DMGetType(dm, &dmtype); CHKERRQ(ierr); }
1650 if (dmtype && !strcmp(dmtype, DMDA)) {
1652 ierr = DMDACreateNaturalVector(dm, &nat); CHKERRQ(ierr);
1653 ierr = DMDAGlobalToNaturalBegin(dm, inVec, INSERT_VALUES, nat); CHKERRQ(ierr);
1654 ierr = DMDAGlobalToNaturalEnd (dm, inVec, INSERT_VALUES, nat); CHKERRQ(ierr);
1656 ierr = VecScatterCreateToZero(nat, &scatNat, &seqNat); CHKERRQ(ierr);
1657 ierr = VecScatterBegin(scatNat, nat, seqNat, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1658 ierr = VecScatterEnd (scatNat, nat, seqNat, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1662 ierr = VecGetLocalSize(seqNat, &nseq); CHKERRQ(ierr);
1663 ierr = VecGetArrayRead(seqNat, &nar); CHKERRQ(ierr);
1665 ierr = PetscMalloc1(nseq, &buf); CHKERRQ(ierr);
1666 ierr = PetscMemcpy(buf, nar, (
size_t)nseq *
sizeof(PetscScalar)); CHKERRQ(ierr);
1668 ierr = VecRestoreArrayRead(seqNat, &nar); CHKERRQ(ierr);
1669 *arrayOut = (
double*)buf;
1672 ierr = VecScatterDestroy(&scatNat); CHKERRQ(ierr);
1673 ierr = VecDestroy(&seqNat); CHKERRQ(ierr);
1674 ierr = VecDestroy(&nat); CHKERRQ(ierr);
1677 ierr = VecScatterCreateToZero(inVec, &scat, &seq); CHKERRQ(ierr);
1678 ierr = VecScatterBegin(scat, inVec, seq, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1679 ierr = VecScatterEnd (scat, inVec, seq, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1683 ierr = VecGetLocalSize(seq, &nseq); CHKERRQ(ierr);
1684 ierr = VecGetArrayRead(seq, &sar); CHKERRQ(ierr);
1686 ierr = PetscMalloc1(nseq, &buf); CHKERRQ(ierr);
1687 ierr = PetscMemcpy(buf, sar, (
size_t)nseq *
sizeof(PetscScalar)); CHKERRQ(ierr);
1689 ierr = VecRestoreArrayRead(seq, &sar); CHKERRQ(ierr);
1690 *arrayOut = (
double*)buf;
1693 ierr = VecScatterDestroy(&scat); CHKERRQ(ierr);
1694 ierr = VecDestroy(&seq); CHKERRQ(ierr);
1697 PetscFunctionReturn(0);
1714PetscErrorCode
SwarmFieldToArrayOnRank0(DM swarm,
const char *field_name, PetscInt *n_total_particles, PetscInt *n_components,
void **gathered_array)
1716 PetscErrorCode ierr;
1717 PetscMPIInt rank, size;
1718 PetscInt nlocal, nglobal, bs;
1719 void *local_array_void;
1720 size_t element_size = 0;
1721 MPI_Datatype mpi_type = MPI_BYTE;
1723 PetscFunctionBeginUser;
1725 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
1726 ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size); CHKERRQ(ierr);
1729 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
1730 ierr = DMSwarmGetSize(swarm, &nglobal); CHKERRQ(ierr);
1731 ierr = DMSwarmGetField(swarm, field_name, &bs, NULL, &local_array_void); CHKERRQ(ierr);
1734 if (strcasecmp(field_name,
"DMSwarm_pid") == 0) {
1735 element_size =
sizeof(PetscInt64);
1736 }
else if (strcasecmp(field_name,
"DMSwarm_CellID") == 0 || strcasecmp(field_name,
"DMSwarm_location_status") == 0) {
1737 element_size =
sizeof(PetscInt);
1739 element_size =
sizeof(PetscScalar);
1743 *n_total_particles = nglobal;
1745 *gathered_array = NULL;
1750 ierr = PetscMalloc(nglobal * bs * element_size, gathered_array); CHKERRQ(ierr);
1751 ierr = PetscMemcpy(*gathered_array, local_array_void, nglobal * bs * element_size); CHKERRQ(ierr);
1754 PetscInt *recvcounts = NULL, *displs = NULL;
1756 ierr = PetscMalloc1(size, &recvcounts); CHKERRQ(ierr);
1757 ierr = PetscMalloc1(size, &displs); CHKERRQ(ierr);
1759 PetscInt sendcount = nlocal * bs;
1762 ierr = MPI_Gather(&sendcount, 1, MPIU_INT, recvcounts, 1, MPIU_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
1767 for (PetscMPIInt i = 0; i < size; i++) recvcounts[i] *= element_size;
1768 for (PetscMPIInt i = 1; i < size; i++) displs[i] = displs[i-1] + recvcounts[i-1];
1770 ierr = PetscMalloc(nglobal * bs * element_size, gathered_array); CHKERRQ(ierr);
1774 ierr = MPI_Gatherv(local_array_void, nlocal * bs * element_size, MPI_BYTE,
1775 *gathered_array, recvcounts, displs, MPI_BYTE,
1776 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
1779 ierr = PetscFree(recvcounts); CHKERRQ(ierr);
1780 ierr = PetscFree(displs); CHKERRQ(ierr);
1784 ierr = DMSwarmRestoreField(swarm, field_name, &bs, NULL, &local_array_void); CHKERRQ(ierr);
1786 PetscFunctionReturn(0);
1809 PetscErrorCode ierr;
1811 Cmpnts global_min_coords, global_max_coords;
1812 PetscReal StartTime;
1813 PetscInt StartStep,StepsToRun,total_num_particles;
1814 PetscMPIInt num_mpi_procs;
1818 num_mpi_procs = simCtx->
size;
1822 total_num_particles = simCtx->
np;
1826 PetscFunctionBeginUser;
1828 if (!user) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"DisplayBanner - UserCtx pointer is NULL.");
1829 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
1834 if (bboxlist_on_rank0 && num_mpi_procs > 0) {
1835 global_min_coords = bboxlist_on_rank0[0].
min_coords;
1836 global_max_coords = bboxlist_on_rank0[0].
max_coords;
1837 for (PetscMPIInt p = 1; p < num_mpi_procs; ++p) {
1838 global_min_coords.
x = PetscMin(global_min_coords.
x, bboxlist_on_rank0[p].
min_coords.
x);
1839 global_min_coords.
y = PetscMin(global_min_coords.
y, bboxlist_on_rank0[p].
min_coords.
y);
1840 global_min_coords.
z = PetscMin(global_min_coords.
z, bboxlist_on_rank0[p].
min_coords.
z);
1841 global_max_coords.
x = PetscMax(global_max_coords.
x, bboxlist_on_rank0[p].
max_coords.
x);
1842 global_max_coords.
y = PetscMax(global_max_coords.
y, bboxlist_on_rank0[p].
max_coords.
y);
1843 global_max_coords.
z = PetscMax(global_max_coords.
z, bboxlist_on_rank0[p].
max_coords.
z);
1850 LOG_ALLOW(PETSC_COMM_SELF,
LOG_WARNING,
"DisplayBanner (Rank 0) - bboxlist not provided or num_mpi_procs <=0; using user->bbox for domain bounds.\n");
1856 ierr = PetscPrintf(PETSC_COMM_SELF,
"\n"); CHKERRQ(ierr);
1857 ierr = PetscPrintf(PETSC_COMM_SELF,
"=============================================================\n"); CHKERRQ(ierr);
1858 ierr = PetscPrintf(PETSC_COMM_SELF,
" CASE SUMMARY \n"); CHKERRQ(ierr);
1859 ierr = PetscPrintf(PETSC_COMM_SELF,
"=============================================================\n"); CHKERRQ(ierr);
1860 ierr = PetscPrintf(PETSC_COMM_SELF,
" Grid Points : %d X %d X %d\n", user->
IM, user->
JM, user->
KM); CHKERRQ(ierr);
1861 ierr = PetscPrintf(PETSC_COMM_SELF,
" Cells : %d X %d X %d\n", user->
IM - 1, user->
JM - 1, user->
KM - 1); CHKERRQ(ierr);
1862 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);
1863 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);
1864 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);
1865 ierr = PetscPrintf(PETSC_COMM_SELF,
"-------------------- Boundary Conditions --------------------\n"); CHKERRQ(ierr);
1866 const int face_name_width = 17;
1867 for (PetscInt i_face = 0; i_face < 6; ++i_face) {
1873 ierr = PetscPrintf(PETSC_COMM_SELF,
" Face %-*s : %s\n",
1874 face_name_width, face_str, bc_type_str); CHKERRQ(ierr);
1876 ierr = PetscPrintf(PETSC_COMM_SELF,
"-------------------------------------------------------------\n"); CHKERRQ(ierr);
1877 ierr = PetscPrintf(PETSC_COMM_SELF,
" Start Time : %.4f\n", (
double)StartTime); CHKERRQ(ierr);
1878 ierr = PetscPrintf(PETSC_COMM_SELF,
" Timestep Size : %.4f\n", (
double)simCtx->
dt); CHKERRQ(ierr);
1879 ierr = PetscPrintf(PETSC_COMM_SELF,
" Starting Step : %d\n", StartStep); CHKERRQ(ierr);
1880 ierr = PetscPrintf(PETSC_COMM_SELF,
" Total Steps to Run : %d\n", StepsToRun); CHKERRQ(ierr);
1881 ierr = PetscPrintf(PETSC_COMM_SELF,
" Number of MPI Processes : %d\n", num_mpi_procs); CHKERRQ(ierr);
1882 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Number of Particles : %d\n", total_num_particles); CHKERRQ(ierr);
1883 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Reynolds Number : %le\n", simCtx->
ren); CHKERRQ(ierr);
1884 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Stanton Number : %le\n", simCtx->
st); CHKERRQ(ierr);
1885 ierr = PetscPrintf(PETSC_COMM_WORLD,
" CFL Number : %le\n", simCtx->
cfl); CHKERRQ(ierr);
1886 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Von-Neumann Number : %le\n", simCtx->
vnn); CHKERRQ(ierr);
1887 ierr = PetscPrintf(PETSC_COMM_SELF,
" Particle Initialization Mode: %d\n", simCtx->
ParticleInitialization); CHKERRQ(ierr);
1892 ierr = PetscPrintf(PETSC_COMM_SELF,
" Particles Initialized At : --- (No INLET face identified)\n"); CHKERRQ(ierr);
1895 ierr = PetscPrintf(PETSC_COMM_SELF,
" Field Initialization Mode : %d\n", simCtx->
FieldInitialization); CHKERRQ(ierr);
1900 ierr = PetscPrintf(PETSC_COMM_SELF,
"=============================================================\n"); CHKERRQ(ierr);
1901 ierr = PetscPrintf(PETSC_COMM_SELF,
"\n"); CHKERRQ(ierr);
1903 PetscFunctionReturn(0);
1913 while(isspace((
unsigned char)*str)) str++;
1914 if(*str == 0)
return;
1917 end = str + strlen(str) - 1;
1918 while(end > str && isspace((
unsigned char)*end)) end--;
1939 PetscBool startTimeSet, endTimeSet, timeStepSet;
1941 PetscFunctionBeginUser;
1943 if (!simCtx || !simCtx->
pps) {
1944 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_NULL,
"SimCtx or its pps member is NULL in ParsePostProcessingSettings.");
1968 file = fopen(configFile,
"r");
1971 while (fgets(line,
sizeof(line), file)) {
1972 char *key, *value, *comment;
1973 comment = strchr(line,
'#');
if (comment) *comment =
'\0';
1975 key = strtok(line,
"="); value = strtok(NULL,
"=");
1978 if (strcmp(key,
"startTime") == 0) pps->
startTime = atoi(value);
1979 else if (strcmp(key,
"endTime") == 0) pps->
endTime = atoi(value);
1980 else if (strcmp(key,
"timeStep") == 0) pps->
timeStep = atoi(value);
1981 else if (strcmp(key,
"output_particles") == 0) {
1982 if (strcasecmp(value,
"true") == 0) pps->
outputParticles = PETSC_TRUE;
1984 else if (strcasecmp(key,
"process_pipeline") == 0) {
1987 }
else if (strcasecmp(key,
"output_fields_instantaneous") == 0) {
1990 }
else if (strcasecmp(key,
"output_prefix") == 0) {
1993 }
else if (strcasecmp(key,
"particle_output_prefix") == 0) {
1996 }
else if (strcasecmp(key,
"particle_fields_instantaneous") == 0) {
1999 }
else if (strcasecmp(key,
"particle_pipeline") == 0) {
2002 }
else if (strcasecmp(key,
"particle_output_freq") == 0) {
2012 LOG_ALLOW(
GLOBAL,
LOG_WARNING,
"Could not open post-processing config file '%s'. Using defaults and command-line overrides.\n", configFile);
2016 PetscOptionsGetInt(NULL, NULL,
"-startTime", &pps->
startTime, &startTimeSet);
2017 PetscOptionsGetInt(NULL, NULL,
"-endTime", &pps->
endTime, &endTimeSet);
2018 PetscOptionsGetInt(NULL, NULL,
"-timeStep", &pps->
timeStep, &timeStepSet);
2019 PetscOptionsGetBool(NULL, NULL,
"-output_particles", &pps->
outputParticles, NULL);
2022 if (startTimeSet && !endTimeSet) {
2026 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"Post-processing configured to run from t=%d to t=%d with step %d. Particle output: %s.\n",
2036 PetscFunctionReturn(0);
2056 PetscMPIInt rank, size;
2057 PetscErrorCode ierr;
2060 double *array = NULL;
2061 PetscInt fileExistsFlag = 0;
2064 "ReadDataFileToArray - Start reading from file: %s\n",
2068 if (!filename || !data_out || !Nout) {
2070 "ReadDataFileToArray - Null pointer argument provided.\n");
2075 MPI_Comm_rank(comm, &rank);
2076 MPI_Comm_size(comm, &size);
2080 fp = fopen(filename,
"r");
2089 ierr = MPI_Bcast(&fileExistsFlag, 1, MPI_INT, 0, comm); CHKERRQ(ierr);
2091 if (!fileExistsFlag) {
2095 "ReadDataFileToArray - File '%s' not found.\n",
2103 fp = fopen(filename,
"r");
2106 "ReadDataFileToArray - File '%s' could not be opened for reading.\n",
2114 while (fgets(line,
sizeof(line), fp)) {
2120 "ReadDataFileToArray - File '%s' has %d lines.\n",
2124 array = (
double*)malloc(N *
sizeof(
double));
2128 "ReadDataFileToArray - malloc failed for array.\n");
2137 while (fgets(line,
sizeof(line), fp)) {
2139 if (sscanf(line,
"%lf", &val) == 1) {
2147 "ReadDataFileToArray - Successfully read %d values from '%s'.\n",
2152 ierr = MPI_Bcast(&N, 1, MPI_INT, 0, comm); CHKERRQ(ierr);
2156 array = (
double*)malloc(N *
sizeof(
double));
2159 "ReadDataFileToArray - malloc failed on rank %d.\n",
2166 ierr = MPI_Bcast(array, N, MPI_DOUBLE, 0, comm); CHKERRQ(ierr);
2173 "ReadDataFileToArray - Done. Provided array of length=%d to all ranks.\n",
2194 double **coordsArray,
2197 PetscFunctionBeginUser;
2199 PetscErrorCode ierr;
2203 ierr = VecCreate(PETSC_COMM_WORLD, &coordsVec);CHKERRQ(ierr);
2204 ierr = VecSetFromOptions(coordsVec);CHKERRQ(ierr);
2207 ierr =
ReadFieldData(user,
"position", coordsVec, timeIndex,
"dat");
2210 "ReadPositionsFromFile - Error reading position data (ti=%d).\n",
2212 PetscFunctionReturn(ierr);
2218 ierr = VecDestroy(&coordsVec);CHKERRQ(ierr);
2221 "ReadPositionsFromFile - Successfully gathered coordinates. Ncoords=%d.\n", *Ncoords);
2222 PetscFunctionReturn(0);
2241 const char *fieldName,
2243 double **scalarArray,
2246 PetscFunctionBeginUser;
2248 PetscErrorCode ierr;
2252 ierr = VecCreate(PETSC_COMM_WORLD, &fieldVec);CHKERRQ(ierr);
2253 ierr = VecSetFromOptions(fieldVec);CHKERRQ(ierr);
2255 ierr =
ReadFieldData(user, fieldName, fieldVec, timeIndex,
"dat");
2258 "ReadFieldDataWrapper - Error reading field '%s' (ti=%d).\n",
2259 fieldName, timeIndex);
2260 PetscFunctionReturn(ierr);
2266 ierr = VecDestroy(&fieldVec);CHKERRQ(ierr);
2269 "ReadFieldDataWrapper - Successfully gathered field '%s'. Nscalars=%d.\n",
2270 fieldName, *Nscalars);
2271 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.
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 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)
Helper function to trim leading/trailing whitespace from a string.
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.
static PetscErrorCode CheckDataFile(PetscInt ti, const char *fieldName, const char *ext, PetscBool *fileExists)
Checks for a data file's existence in a parallel-safe manner.
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.
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 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.
#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...
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_INFO
Informational messages about program execution.
@ LOG_WARNING
Non-critical issues that warrant attention.
@ LOG_DEBUG
Detailed debugging information.
BCType
Defines the general mathematical/physical category of a boundary.
PetscBool inletFaceDefined
char particle_output_prefix[256]
BoundaryFaceConfig boundary_faces[6]
BCFace identifiedInletBCFace
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.
#define MAX_FILENAME_LENGTH
Cmpnts min_coords
Minimum x, y, z coordinates of the bounding box.
char grid_file[PETSC_MAX_PATH_LEN]
char output_fields_instantaneous[1024]
char particle_pipeline[1024]
char process_pipeline[1024]
PetscInt particle_output_freq
char particle_fields[1024]
PetscBool outputParticles
Cmpnts InitialConstantContra
PetscInt ParticleInitialization
char PostprocessingControlFile[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.