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,
"PERIODIC") == 0) *type_out =
PERIODIC;
304 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE,
"Unknown BC Type string: %s", str);
323 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE,
"Unknown BC Handler string: %s", str);
361 *found = PETSC_FALSE;
367 if (strcasecmp(current->
key, key) == 0) {
368 *value_out = atof(current->
value);
372 current = current->
next;
386 *found = PETSC_FALSE;
387 *value_out = PETSC_FALSE;
392 if (strcasecmp(current->
key, key) == 0) {
397 if (current->
value &&
398 (strcasecmp(current->
value,
"true") == 0 ||
399 strcmp(current->
value,
"1") == 0 ||
400 strcasecmp(current->
value,
"yes") == 0))
402 *value_out = PETSC_TRUE;
404 *value_out = PETSC_FALSE;
408 current = current->
next;
419#define __FUNCT__ "ParseAllBoundaryConditions"
447 PetscFunctionBeginUser;
449 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
453 char line_buffer[1024];
456 for (
int i = 0; i < 6; i++) {
460 configs_rank0[i].
params = NULL;
461 configs_rank0[i].
handler = NULL;
465 file = fopen(bcs_input_filename,
"r");
466 if (!file) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN,
"Could not open BCs file '%s'.", bcs_input_filename);
468 while (fgets(line_buffer,
sizeof(line_buffer), file)) {
469 char *current_pos = line_buffer;
470 while (isspace((
unsigned char)*current_pos)) current_pos++;
471 if (*current_pos ==
'#' || *current_pos ==
'\0' || *current_pos ==
'\n' || *current_pos ==
'\r')
continue;
473 char *face_str = strtok(current_pos,
" \t\n\r");
474 char *type_str = strtok(NULL,
" \t\n\r");
475 char *handler_str = strtok(NULL,
" \t\n\r");
477 if (!face_str || !type_str || !handler_str) {
485 const char* handler_name_for_log;
492 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);
498 LOG_ALLOW(
GLOBAL,
LOG_DEBUG,
" Parsed Face '%s': Type=%s, Handler=%s \n", face_str, type_str, handler_name_for_log);
502 configs_rank0[face_enum].
params = NULL;
506 while ((token = strtok(NULL,
" \t\n\r")) != NULL) {
507 char* equals_ptr = strchr(token,
'=');
514 char* key_str = token;
515 char* value_str = equals_ptr + 1;
518 ierr = PetscMalloc1(1, &new_param); CHKERRQ(ierr);
519 ierr = PetscStrallocpy(key_str, &new_param->
key); CHKERRQ(ierr);
520 ierr = PetscStrallocpy(value_str, &new_param->
value); CHKERRQ(ierr);
521 new_param->
next = NULL;
523 *param_next_ptr = new_param;
524 param_next_ptr = &new_param->
next;
538 for (
int i = 0; i < 6; i++) {
547 PetscInt n_params = 0;
551 ierr = MPI_Bcast(&n_params, 1, MPI_INT, 0, PETSC_COMM_WORLD);CHKERRQ(ierr);
560 for (
int j = 0; j < n_params; j++) {
561 char key_buf[256] = {0}, val_buf[256] = {0};
565 for (
int k = 0; k < j; k++) p = p->
next;
566 strncpy(key_buf, p->
key, 255);
567 strncpy(val_buf, p->
value, 255);
570 ierr = MPI_Bcast(key_buf, 256, MPI_CHAR, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
571 ierr = MPI_Bcast(val_buf, 256, MPI_CHAR, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
576 ierr = PetscMalloc1(1, &new_param); CHKERRQ(ierr);
577 ierr = PetscStrallocpy(key_buf, &new_param->
key); CHKERRQ(ierr);
578 ierr = PetscStrallocpy(val_buf, &new_param->
value); CHKERRQ(ierr);
579 new_param->
next = NULL;
580 *param_next_ptr = new_param;
581 param_next_ptr = &new_param->
next;
584 param_next_ptr = &((*param_next_ptr)->next);
592 for (
int i=0; i<6; i++) {
610 PetscFunctionReturn(0);
621#define __FUNCT__ "DeterminePeriodicity"
649 PetscInt periodic_flags[3] = {0, 0, 0};
651 PetscFunctionBeginUser;
652 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
655 for (PetscInt bi = 0; bi < simCtx->
block_number; bi++) {
656 const char *bcs_filename = simCtx->
bcs_files[bi];
657 if (!bcs_filename) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_NULL,
"BCS filename for block %d is not set in SimCtx.", bi);
659 PetscBool file_exists;
660 snprintf(desc_buf,
sizeof(desc_buf),
"BCS file for block %d", bi);
661 ierr =
VerifyPathExistence(bcs_filename, PETSC_FALSE, PETSC_FALSE, desc_buf, &file_exists); CHKERRQ(ierr);
666 PetscBool global_is_periodic[3] = {PETSC_FALSE, PETSC_FALSE, PETSC_FALSE};
667 PetscBool is_set = PETSC_FALSE;
669 for (PetscInt bi = 0; bi < simCtx->
block_number; bi++) {
670 const char *bcs_filename = simCtx->
bcs_files[bi];
671 FILE *file = fopen(bcs_filename,
"r");
673 PetscBool face_is_periodic[6] = {PETSC_FALSE};
674 char line_buffer[1024];
676 while (fgets(line_buffer,
sizeof(line_buffer), file)) {
677 char *current_pos = line_buffer;
678 while (isspace((
unsigned char)*current_pos)) current_pos++;
679 if (*current_pos ==
'#' || *current_pos ==
'\0' || *current_pos ==
'\n')
continue;
682 char *face_str = strtok(current_pos,
" \t\n\r");
683 char *type_str = strtok(NULL,
" \t\n\r");
686 if (!face_str || !type_str)
continue;
689 if (strcasecmp(type_str,
"PERIODIC") == 0) {
693 face_is_periodic[face_enum] = PETSC_TRUE;
701 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
"Inconsistent X-periodicity in file '%s' for block %d. Both -Xi and +Xi must be periodic or neither.", bcs_filename, bi);
703 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
"Inconsistent Y-periodicity in file '%s' for block %d. Both -Eta and +Eta must be periodic or neither.", bcs_filename, bi);
705 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
"Inconsistent Z-periodicity in file '%s' for block %d. Both -Zeta and +Zeta must be periodic or neither.", bcs_filename, bi);
711 global_is_periodic[0] = local_is_periodic[0];
712 global_is_periodic[1] = local_is_periodic[1];
713 global_is_periodic[2] = local_is_periodic[2];
716 if (global_is_periodic[0] != local_is_periodic[0] || global_is_periodic[1] != local_is_periodic[1] || global_is_periodic[2] != local_is_periodic[2]) {
717 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
718 "Periodicity mismatch between blocks. Block 0 requires (I:%d, J:%d, K:%d), but block %d (file '%s') has (I:%d, J:%d, K:%d).",
719 (
int)global_is_periodic[0], (
int)global_is_periodic[1], (
int)global_is_periodic[2],
721 (
int)local_is_periodic[0], (
int)local_is_periodic[1], (
int)local_is_periodic[2]);
726 periodic_flags[0] = (global_is_periodic[0]) ? 1 : 0;
727 periodic_flags[1] = (global_is_periodic[1]) ? 1 : 0;
728 periodic_flags[2] = (global_is_periodic[2]) ? 1 : 0;
731 periodic_flags[0], periodic_flags[1], periodic_flags[2]);
735 ierr = MPI_Bcast(periodic_flags, 3, MPIU_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
742 PetscFunctionReturn(0);
762PetscErrorCode
VerifyPathExistence(
const char *path, PetscBool is_dir, PetscBool is_optional,
const char *description, PetscBool *exists)
766 MPI_Comm comm = PETSC_COMM_WORLD;
768 PetscFunctionBeginUser;
769 ierr = MPI_Comm_rank(comm, &rank); CHKERRQ(ierr);
773 ierr = PetscTestDirectory(path,
'r', exists); CHKERRQ(ierr);
775 ierr = PetscTestFile(path,
'r', exists); CHKERRQ(ierr);
790 PetscMPIInt exists_int = (rank == 0) ? (PetscMPIInt)(*exists) : 0;
791 ierr = MPI_Bcast(&exists_int, 1, MPI_INT, 0, comm); CHKERRMPI(ierr);
792 *exists = (PetscBool)exists_int;
795 if (!(*exists) && !is_optional) {
796 SETERRQ(comm, PETSC_ERR_FILE_OPEN,
"Mandatory %s not found. Rank 0 expected it at '%s'. Check path and permissions.", description, path);
799 PetscFunctionReturn(0);
817static PetscErrorCode
CheckDataFile(
UserCtx *user, PetscInt ti,
const char *fieldName,
const char *ext, PetscBool *fileExists)
821 MPI_Comm comm = PETSC_COMM_WORLD;
822 PetscInt placeholder_int = 0;
825 PetscFunctionBeginUser;
828 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_NULL,
"I/O context directory is NULL. Ensure it is set before calling CheckDataFile().");
831 ierr = MPI_Comm_rank(comm, &rank); CHKERRQ(ierr);
834 char filename[PETSC_MAX_PATH_LEN];
837 ierr = PetscSNPrintf(filename,
sizeof(filename),
"%s/%s%05"PetscInt_FMT
"_%d.%s",simCtx->
current_io_directory,fieldName, ti, placeholder_int, ext);
838 ierr = PetscTestFile(filename,
'r', fileExists); CHKERRQ(ierr);
839 if (!(*fileExists)) {
846 PetscMPIInt fileExists_int = (rank == 0) ? (PetscMPIInt)(*fileExists) : 0;
847 ierr = MPI_Bcast(&fileExists_int, 1, MPI_INT, 0, comm); CHKERRMPI(ierr);
848 *fileExists = (PetscBool)fileExists_int;
850 PetscFunctionReturn(0);
875static PetscErrorCode
ReadOptionalField(
UserCtx *user,
const char *field_name,
const char *field_label, Vec field_vec, PetscInt ti,
const char *ext)
878 PetscBool fileExists;
880 PetscFunctionBeginUser;
883 ierr =
CheckDataFile(user,ti, field_name, ext, &fileExists); CHKERRQ(ierr);
893 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);
902 PetscFunctionReturn(0);
928 PetscBool fileExists;
930 PetscFunctionBeginUser;
933 ierr =
CheckDataFile(user,ti, field_name, ext, &fileExists); CHKERRQ(ierr);
938 if(strcasecmp(field_name,
"DMSwarm_CellID") == 0 || strcasecmp(field_name,
"DMSwarm_pid")== 0 || strcasecmp(field_name,
"DMSwarm_location_status")== 0 ) {
949 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);
958 PetscFunctionReturn(0);
962#define __FUNCT__ "ReadFieldData"
996 const char *field_name,
1001 PetscErrorCode ierr;
1002 char filename[PETSC_MAX_PATH_LEN];
1004 PetscMPIInt rank,size;
1008 PetscFunctionBeginUser;
1012 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,
"I/O context directory was not set before calling ReadFieldData().");
1016 ierr = PetscObjectGetComm((PetscObject)field_vec,&comm);CHKERRQ(ierr);
1017 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1018 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1020 const char *source_path = NULL;
1024 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,
"source_path was not set for the current execution mode.");
1030 ierr = PetscSNPrintf(filename,
sizeof(filename),
1031 "%s/%s%05" PetscInt_FMT
"_0.%s",
1032 source_path,field_name,ti,ext);CHKERRQ(ierr);
1035 "Attempting to read <%s> on rank %d/%d\n",
1036 filename,(
int)rank,(
int)size);
1046 PetscInt expectedSize,loadedSize;
1048 ierr = PetscTestFile(filename,
'r',&found);CHKERRQ(ierr);
1049 if(!found) SETERRQ(comm,PETSC_ERR_FILE_OPEN,
1050 "Restart/Source file not found: %s",filename);
1052 ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
1056 ierr = VecCreate(PETSC_COMM_SELF, &temp_vec); CHKERRQ(ierr);
1057 ierr = VecLoad(temp_vec,viewer);CHKERRQ(ierr);
1058 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1061 ierr = VecGetSize(field_vec, &expectedSize);CHKERRQ(ierr);
1062 ierr = VecGetSize(temp_vec, &loadedSize);CHKERRQ(ierr);
1063 if (loadedSize != expectedSize) {
1064 SETERRQ(comm,PETSC_ERR_FILE_UNEXPECTED,
1065 "File %s holds %d entries – expected %d for field '%s'",
1066 filename, loadedSize, expectedSize, field_name);
1070 ierr = VecCopy(temp_vec, field_vec);CHKERRQ(ierr);
1073 ierr = VecDestroy(&temp_vec);CHKERRQ(ierr);
1089 "Loaded <%s> (serial path)\n",filename);
1092 PetscFunctionReturn(0);
1098 PetscInt globalSize;
1099 ierr = VecGetSize(field_vec,&globalSize);CHKERRQ(ierr);
1102 const char *dmtype = NULL;
1107 const PetscScalar *seqArray = NULL;
1114 ierr = PetscTestFile(filename,
'r',&found);CHKERRQ(ierr);
1115 if(!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,
1116 "Restart file not found: %s",filename);
1119 ierr = VecCreate(PETSC_COMM_SELF,&seq_vec);CHKERRQ(ierr);
1120 ierr = VecSetType(seq_vec,VECSEQ);CHKERRQ(ierr);
1122 ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,
1123 FILE_MODE_READ,&viewer);CHKERRQ(ierr);
1124 ierr = VecLoad(seq_vec,viewer);CHKERRQ(ierr);
1125 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1129 ierr = VecGetSize(seq_vec,&loaded);CHKERRQ(ierr);
1130 if(loaded != globalSize)
1131 SETERRQ(comm,PETSC_ERR_FILE_UNEXPECTED,
1132 "File %s holds %d entries – expected %d",
1133 filename,loaded,globalSize);
1136 ierr = VecGetArrayRead(seq_vec,&seqArray);CHKERRQ(ierr);
1139 "Rank 0 successfully loaded <%s>\n",filename);
1143 ierr = VecGetDM(field_vec, &dm); CHKERRQ(ierr);
1144 if (dm) { ierr = DMGetType(dm, &dmtype); CHKERRQ(ierr); }
1146 if (dmtype && !strcmp(dmtype, DMDA)) {
1152 ierr = DMDACreateNaturalVector(dm, &nat); CHKERRQ(ierr);
1158 ierr = VecScatterCreateToZero(nat, &scatter, &nat_seq); CHKERRQ(ierr);
1161 ierr = VecScatterBegin(scatter, (rank == 0 ? seq_vec : nat_seq), nat,
1162 INSERT_VALUES, SCATTER_REVERSE); CHKERRQ(ierr);
1163 ierr = VecScatterEnd(scatter, (rank == 0 ? seq_vec : nat_seq), nat,
1164 INSERT_VALUES, SCATTER_REVERSE); CHKERRQ(ierr);
1167 ierr = DMDANaturalToGlobalBegin(dm, nat, INSERT_VALUES, field_vec); CHKERRQ(ierr);
1168 ierr = DMDANaturalToGlobalEnd(dm, nat, INSERT_VALUES, field_vec); CHKERRQ(ierr);
1171 ierr = VecScatterDestroy(&scatter); CHKERRQ(ierr);
1172 ierr = VecDestroy(&nat_seq); CHKERRQ(ierr);
1173 ierr = VecDestroy(&nat); CHKERRQ(ierr);
1180 PetscScalar *buffer = NULL;
1182 buffer = (PetscScalar *)seqArray;
1184 ierr = PetscMalloc1(globalSize, &buffer); CHKERRQ(ierr);
1187 ierr = MPI_Bcast(buffer, (
int)globalSize, MPIU_SCALAR, 0, comm); CHKERRQ(ierr);
1190 PetscInt rstart, rend, loc;
1191 PetscScalar *locArray;
1193 ierr = VecGetOwnershipRange(field_vec, &rstart, &rend); CHKERRQ(ierr);
1194 loc = rend - rstart;
1196 ierr = VecGetArray(field_vec, &locArray); CHKERRQ(ierr);
1197 ierr = PetscMemcpy(locArray, buffer + rstart, loc *
sizeof(PetscScalar)); CHKERRQ(ierr);
1198 ierr = VecRestoreArray(field_vec, &locArray); CHKERRQ(ierr);
1201 ierr = PetscFree(buffer); CHKERRQ(ierr);
1207 ierr = VecRestoreArrayRead(seq_vec, &seqArray); CHKERRQ(ierr);
1208 ierr = VecDestroy(&seq_vec); CHKERRQ(ierr);
1212 "Loaded <%s> (parallel path)\n",filename);
1215 PetscFunctionReturn(0);
1233 PetscErrorCode ierr;
1236 const char *source_path = NULL;
1243 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
"Invalid execution mode for reading simulation fields.");
1259 ierr =
ReadFieldData(user,
"pfield", user->
P, ti,
"dat"); CHKERRQ(ierr);
1269 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"ParticleCount Vec is NULL but np>0");
1273 ierr =
ReadOptionalField(user,
"psifield",
"Scalar Psi Field", user->
Psi, ti,
"dat"); CHKERRQ(ierr);
1313 PetscErrorCode ierr;
1340 PetscErrorCode ierr;
1344 ierr =
ReadOptionalField(user,
"Nu_t",
"Turbulent Viscosity", user->
Nu_t, ti,
"dat"); CHKERRQ(ierr);
1345 ierr =
ReadOptionalField(user,
"cs",
"Smagorinsky Constant (Cs)", user->
CS, ti,
"dat"); CHKERRQ(ierr);
1347 DMGlobalToLocalBegin(user->
da, user->
CS, INSERT_VALUES, user->
lCs);
1348 DMGlobalToLocalEnd(user->
da, user->
CS, INSERT_VALUES, user->
lCs);
1350 DMGlobalToLocalBegin(user->
da, user->
Nu_t, INSERT_VALUES, user->
lNu_t);
1351 DMGlobalToLocalEnd(user->
da, user->
Nu_t, INSERT_VALUES, user->
lNu_t);
1371 PetscErrorCode ierr;
1376 ierr =
ReadOptionalField(user,
"Nu_t",
"Turbulent Viscosity", user->
Nu_t, ti,
"dat"); CHKERRQ(ierr);
1383 DMGlobalToLocalBegin(user->
da, user->
Nu_t, INSERT_VALUES, user->
lNu_t);
1384 DMGlobalToLocalEnd(user->
da, user->
Nu_t, INSERT_VALUES, user->
lNu_t);
1413 PetscErrorCode ierr;
1419 swarm = user->
swarm;
1424 ierr = DMSwarmCreateGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1429 ierr =
ReadFieldData(user, field_name, fieldVec, ti, ext);CHKERRQ(ierr);
1432 ierr = DMSwarmDestroyGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1434 PetscFunctionReturn(0);
1458 PetscErrorCode ierr;
1459 DM swarm = user->
swarm;
1461 PetscInt nlocal, nglobal, bs, i;
1462 const PetscScalar *scalar_array;
1463 void *field_array_void;
1466 PetscFunctionBeginUser;
1471 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
1472 ierr = DMSwarmGetSize(swarm, &nglobal); CHKERRQ(ierr);
1474 ierr = DMSwarmGetField(swarm, field_name, &bs, NULL, NULL); CHKERRQ(ierr);
1475 ierr = DMSwarmRestoreField(swarm, field_name, &bs, NULL, NULL); CHKERRQ(ierr);
1478 ierr = VecCreate(PETSC_COMM_WORLD, &temp_vec); CHKERRQ(ierr);
1479 ierr = VecSetType(temp_vec, VECMPI); CHKERRQ(ierr);
1480 ierr = VecSetSizes(temp_vec, nlocal * bs, nglobal * bs); CHKERRQ(ierr);
1481 ierr = VecSetBlockSize(temp_vec, bs); CHKERRQ(ierr);
1482 ierr = VecSetUp(temp_vec); CHKERRQ(ierr);
1485 ierr =
ReadFieldData(user, field_name, temp_vec, ti, ext); CHKERRQ(ierr);
1488 ierr = VecGetArrayRead(temp_vec, &scalar_array); CHKERRQ(ierr);
1489 ierr = DMSwarmGetField(swarm, field_name, NULL, NULL, &field_array_void); CHKERRQ(ierr);
1492 if (strcmp(field_name,
"DMSwarm_pid") == 0) {
1493 PetscInt64 *int64_array = (PetscInt64 *)field_array_void;
1494 for (i = 0; i < nlocal * bs; i++) {
1495 int64_array[i] = (PetscInt64)scalar_array[i];
1498 PetscInt *int_array = (PetscInt *)field_array_void;
1499 for (i = 0; i < nlocal * bs; i++) {
1500 int_array[i] = (PetscInt)scalar_array[i];
1505 ierr = DMSwarmRestoreField(swarm, field_name, NULL, NULL, &field_array_void); CHKERRQ(ierr);
1506 ierr = VecRestoreArrayRead(temp_vec, &scalar_array); CHKERRQ(ierr);
1509 ierr = VecDestroy(&temp_vec); CHKERRQ(ierr);
1511 PetscFunctionReturn(0);
1536 PetscErrorCode ierr;
1539 const char *source_path = NULL;
1541 PetscFunctionBeginUser;
1542 ierr = DMSwarmGetSize(user->
swarm, &nGlobal); CHKERRQ(ierr);
1547 PetscFunctionReturn(0);
1556 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE,
"Invalid execution mode for reading simulation fields.");
1569 SETERRQ(PETSC_COMM_WORLD, ierr,
"Failed to read MANDATORY 'position' field for step %d. Cannot continue.", ti);
1580 ierr =
ReadOptionalSwarmField(user,
"DMSwarm_location_status",
"Migration Status", ti,
"dat"); CHKERRQ(ierr);
1585 PetscFunctionReturn(0);
1590#define __FUNCT__ "WriteFieldData"
1637 const char *field_name,
1642 PetscErrorCode ierr;
1644 PetscMPIInt rank, size;
1646 const PetscInt placeholder_int = 0;
1647 char filename[PETSC_MAX_PATH_LEN];
1650 PetscFunctionBeginUser;
1654 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,
"Output directory was not set before calling WriteFieldData().");
1660 ierr = PetscObjectGetComm((PetscObject)field_vec,&comm);CHKERRQ(ierr);
1661 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1662 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
1664 ierr = PetscSNPrintf(filename,
sizeof(filename),
1665 "%s/%s%05" PetscInt_FMT
"_%d.%s",
1669 " Preparing to write <%s> on rank %d/%d\n",
1670 filename,rank,size);
1678 ierr = PetscViewerBinaryOpen(comm,filename,
1679 FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
1680 ierr = VecView(field_vec,viewer);CHKERRQ(ierr);
1681 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1684 " Wrote <%s> (serial path)\n",filename);
1685 PetscFunctionReturn(0);
1694 const char *dmtype = NULL;
1698 ierr = VecGetDM(field_vec, &dm); CHKERRQ(ierr);
1699 if (dm) { ierr = DMGetType(dm, &dmtype); CHKERRQ(ierr); }
1701 if (dmtype && !strcmp(dmtype, DMDA)) {
1703 ierr = DMDACreateNaturalVector(dm, &nat); CHKERRQ(ierr);
1704 ierr = DMDAGlobalToNaturalBegin(dm, field_vec, INSERT_VALUES, nat); CHKERRQ(ierr);
1705 ierr = DMDAGlobalToNaturalEnd(dm, field_vec, INSERT_VALUES, nat); CHKERRQ(ierr);
1708 ierr = VecScatterCreateToZero(nat, &scatter, &seq_vec); CHKERRQ(ierr);
1711 ierr = VecScatterCreateToZero(field_vec, &scatter, &seq_vec); CHKERRQ(ierr);
1715 ierr = VecScatterBegin(scatter, (nat ? nat : field_vec), seq_vec,
1716 INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1717 ierr = VecScatterEnd(scatter, (nat ? nat : field_vec), seq_vec,
1718 INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
1725 PetscReal vmin,vmax;
1726 ierr = VecMin(seq_vec,NULL,&vmin);CHKERRQ(ierr);
1727 ierr = VecMax(seq_vec,NULL,&vmax);CHKERRQ(ierr);
1729 " <%s> range = [%.4e … %.4e]\n",
1730 field_name,(
double)vmin,(
double)vmax);
1732 ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,
1733 FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
1734 ierr = VecView(seq_vec,viewer);CHKERRQ(ierr);
1735 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1738 " Wrote <%s> (parallel path)\n",filename);
1742 ierr = VecScatterDestroy(&scatter);CHKERRQ(ierr);
1743 ierr = VecDestroy(&seq_vec);CHKERRQ(ierr);
1744 if (nat) { ierr = VecDestroy(&nat); CHKERRQ(ierr); }
1747 PetscFunctionReturn(0);
1763 PetscErrorCode ierr;
1826 PetscErrorCode ierr;
1854 PetscErrorCode ierr;
1861 DMLocalToGlobalBegin(user->
da, user->
lCs, INSERT_VALUES, user->
CS);
1862 DMLocalToGlobalEnd(user->
da, user->
lCs, INSERT_VALUES, user->
CS);
1864 DMLocalToGlobalBegin(user->
da, user->
lNu_t, INSERT_VALUES, user->
Nu_t);
1865 DMLocalToGlobalEnd(user->
da, user->
lNu_t, INSERT_VALUES, user->
Nu_t);
1888 PetscErrorCode ierr;
1922 PetscErrorCode ierr;
1926 PetscFunctionBeginUser;
1932 swarm = user->
swarm;
1940 "Attempting to create global vector from field: %s\n",
1942 ierr = DMSwarmCreateGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1949 "Calling WriteFieldData for field: %s\n",
1951 ierr =
WriteFieldData(user, field_name, fieldVec, ti, ext);CHKERRQ(ierr);
1959 "Destroying the global vector for field: %s\n",
1961 ierr = DMSwarmDestroyGlobalVectorFromField(swarm, field_name, &fieldVec);CHKERRQ(ierr);
1965 "Successfully wrote swarm data for field: %s\n",
1968 PetscFunctionReturn(0);
1991 PetscErrorCode ierr;
1992 DM swarm = user->
swarm;
1994 PetscInt nlocal, nglobal,bs,i;
1995 void *field_array_void;
1996 PetscScalar *scalar_array;
1998 PetscFunctionBeginUser;
2003 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
2004 ierr = DMSwarmGetSize(swarm, &nglobal); CHKERRQ(ierr);
2005 ierr = DMSwarmGetField(swarm, field_name, &bs, NULL, &field_array_void); CHKERRQ(ierr);
2008 ierr = VecCreate(PETSC_COMM_WORLD, &temp_vec); CHKERRQ(ierr);
2009 ierr = VecSetType(temp_vec, VECMPI); CHKERRQ(ierr);
2010 ierr = VecSetSizes(temp_vec, nlocal*bs, nglobal*bs); CHKERRQ(ierr);
2011 ierr = VecSetUp(temp_vec); CHKERRQ(ierr);
2014 DMSwarmVectorDefineField(swarm,
"position");
2016 ierr = VecGetArray(temp_vec, &scalar_array); CHKERRQ(ierr);
2018 if(strcasecmp(field_name,
"DMSwarm_pid") == 0){
2019 PetscInt64 *int64_array = (PetscInt64 *)field_array_void;
2021 for (i = 0; i < nlocal*bs; i++) {
2022 scalar_array[i] = (PetscScalar)int64_array[i];
2025 PetscInt *int_array = (PetscInt *)field_array_void;
2027 for (i = 0; i < nlocal*bs; i++) {
2028 scalar_array[i] = (PetscScalar)int_array[i];
2033 ierr = VecRestoreArray(temp_vec, &scalar_array); CHKERRQ(ierr);
2034 ierr = DMSwarmRestoreField(swarm, field_name, &bs, NULL, &field_array_void); CHKERRQ(ierr);
2037 ierr =
WriteFieldData(user, field_name, temp_vec, ti, ext); CHKERRQ(ierr);
2040 ierr = VecDestroy(&temp_vec); CHKERRQ(ierr);
2042 PetscFunctionReturn(0);
2063 PetscErrorCode ierr;
2066 PetscFunctionBeginUser;
2069 if (!user->
swarm || simCtx->
np <= 0) {
2070 PetscFunctionReturn(0);
2107 PetscFunctionReturn(0);
2121 PetscErrorCode ierr;
2124 PetscInt globalSize;
2126 const char *dmtype = NULL;
2129 Vec nat = NULL, seqNat = NULL;
2130 VecScatter scatNat = NULL;
2131 const PetscScalar *nar = NULL;
2132 PetscScalar *buf = NULL;
2136 VecScatter scat = NULL;
2137 const PetscScalar *sar = NULL;
2139 PetscFunctionBeginUser;
2141 ierr = PetscObjectGetComm((PetscObject)inVec, &comm); CHKERRQ(ierr);
2142 ierr = MPI_Comm_rank(comm, &rank); CHKERRQ(ierr);
2143 ierr = VecGetSize(inVec, &globalSize); CHKERRQ(ierr);
2147 ierr = VecGetDM(inVec, &dm); CHKERRQ(ierr);
2148 if (dm) { ierr = DMGetType(dm, &dmtype); CHKERRQ(ierr); }
2150 if (dmtype && !strcmp(dmtype, DMDA)) {
2152 ierr = DMDACreateNaturalVector(dm, &nat); CHKERRQ(ierr);
2153 ierr = DMDAGlobalToNaturalBegin(dm, inVec, INSERT_VALUES, nat); CHKERRQ(ierr);
2154 ierr = DMDAGlobalToNaturalEnd (dm, inVec, INSERT_VALUES, nat); CHKERRQ(ierr);
2156 ierr = VecScatterCreateToZero(nat, &scatNat, &seqNat); CHKERRQ(ierr);
2157 ierr = VecScatterBegin(scatNat, nat, seqNat, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
2158 ierr = VecScatterEnd (scatNat, nat, seqNat, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
2162 ierr = VecGetLocalSize(seqNat, &nseq); CHKERRQ(ierr);
2163 ierr = VecGetArrayRead(seqNat, &nar); CHKERRQ(ierr);
2165 ierr = PetscMalloc1(nseq, &buf); CHKERRQ(ierr);
2166 ierr = PetscMemcpy(buf, nar, (
size_t)nseq *
sizeof(PetscScalar)); CHKERRQ(ierr);
2168 ierr = VecRestoreArrayRead(seqNat, &nar); CHKERRQ(ierr);
2169 *arrayOut = (
double*)buf;
2172 ierr = VecScatterDestroy(&scatNat); CHKERRQ(ierr);
2173 ierr = VecDestroy(&seqNat); CHKERRQ(ierr);
2174 ierr = VecDestroy(&nat); CHKERRQ(ierr);
2177 ierr = VecScatterCreateToZero(inVec, &scat, &seq); CHKERRQ(ierr);
2178 ierr = VecScatterBegin(scat, inVec, seq, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
2179 ierr = VecScatterEnd (scat, inVec, seq, INSERT_VALUES, SCATTER_FORWARD); CHKERRQ(ierr);
2183 ierr = VecGetLocalSize(seq, &nseq); CHKERRQ(ierr);
2184 ierr = VecGetArrayRead(seq, &sar); CHKERRQ(ierr);
2186 ierr = PetscMalloc1(nseq, &buf); CHKERRQ(ierr);
2187 ierr = PetscMemcpy(buf, sar, (
size_t)nseq *
sizeof(PetscScalar)); CHKERRQ(ierr);
2189 ierr = VecRestoreArrayRead(seq, &sar); CHKERRQ(ierr);
2190 *arrayOut = (
double*)buf;
2193 ierr = VecScatterDestroy(&scat); CHKERRQ(ierr);
2194 ierr = VecDestroy(&seq); CHKERRQ(ierr);
2197 PetscFunctionReturn(0);
2214PetscErrorCode
SwarmFieldToArrayOnRank0(DM swarm,
const char *field_name, PetscInt *n_total_particles, PetscInt *n_components,
void **gathered_array)
2216 PetscErrorCode ierr;
2217 PetscMPIInt rank, size;
2218 PetscInt nlocal, nglobal, bs;
2219 void *local_array_void;
2220 size_t element_size = 0;
2221 MPI_Datatype mpi_type = MPI_BYTE;
2223 PetscFunctionBeginUser;
2225 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
2226 ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size); CHKERRQ(ierr);
2229 ierr = DMSwarmGetLocalSize(swarm, &nlocal); CHKERRQ(ierr);
2230 ierr = DMSwarmGetSize(swarm, &nglobal); CHKERRQ(ierr);
2231 ierr = DMSwarmGetField(swarm, field_name, &bs, NULL, &local_array_void); CHKERRQ(ierr);
2234 if (strcasecmp(field_name,
"DMSwarm_pid") == 0) {
2235 element_size =
sizeof(PetscInt64);
2236 }
else if (strcasecmp(field_name,
"DMSwarm_CellID") == 0 || strcasecmp(field_name,
"DMSwarm_location_status") == 0) {
2237 element_size =
sizeof(PetscInt);
2239 element_size =
sizeof(PetscScalar);
2243 *n_total_particles = nglobal;
2245 *gathered_array = NULL;
2250 ierr = PetscMalloc(nglobal * bs * element_size, gathered_array); CHKERRQ(ierr);
2251 ierr = PetscMemcpy(*gathered_array, local_array_void, nglobal * bs * element_size); CHKERRQ(ierr);
2254 PetscInt *recvcounts = NULL, *displs = NULL;
2256 ierr = PetscMalloc1(size, &recvcounts); CHKERRQ(ierr);
2257 ierr = PetscMalloc1(size, &displs); CHKERRQ(ierr);
2259 PetscInt sendcount = nlocal * bs;
2262 ierr = MPI_Gather(&sendcount, 1, MPIU_INT, recvcounts, 1, MPIU_INT, 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
2267 for (PetscMPIInt i = 0; i < size; i++) recvcounts[i] *= element_size;
2268 for (PetscMPIInt i = 1; i < size; i++) displs[i] = displs[i-1] + recvcounts[i-1];
2270 ierr = PetscMalloc(nglobal * bs * element_size, gathered_array); CHKERRQ(ierr);
2274 ierr = MPI_Gatherv(local_array_void, nlocal * bs * element_size, MPI_BYTE,
2275 *gathered_array, recvcounts, displs, MPI_BYTE,
2276 0, PETSC_COMM_WORLD); CHKERRQ(ierr);
2279 ierr = PetscFree(recvcounts); CHKERRQ(ierr);
2280 ierr = PetscFree(displs); CHKERRQ(ierr);
2284 ierr = DMSwarmRestoreField(swarm, field_name, &bs, NULL, &local_array_void); CHKERRQ(ierr);
2286 PetscFunctionReturn(0);
2309 PetscErrorCode ierr;
2311 Cmpnts global_min_coords, global_max_coords;
2312 PetscReal StartTime;
2313 PetscInt StartStep,StepsToRun,total_num_particles;
2314 PetscMPIInt num_mpi_procs;
2318 num_mpi_procs = simCtx->
size;
2322 total_num_particles = simCtx->
np;
2326 PetscFunctionBeginUser;
2328 if (!user) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"DisplayBanner - UserCtx pointer is NULL.");
2329 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(ierr);
2334 if (bboxlist_on_rank0 && num_mpi_procs > 0) {
2335 global_min_coords = bboxlist_on_rank0[0].
min_coords;
2336 global_max_coords = bboxlist_on_rank0[0].
max_coords;
2337 for (PetscMPIInt p = 1; p < num_mpi_procs; ++p) {
2338 global_min_coords.
x = PetscMin(global_min_coords.
x, bboxlist_on_rank0[p].
min_coords.
x);
2339 global_min_coords.
y = PetscMin(global_min_coords.
y, bboxlist_on_rank0[p].
min_coords.
y);
2340 global_min_coords.
z = PetscMin(global_min_coords.
z, bboxlist_on_rank0[p].
min_coords.
z);
2341 global_max_coords.
x = PetscMax(global_max_coords.
x, bboxlist_on_rank0[p].
max_coords.
x);
2342 global_max_coords.
y = PetscMax(global_max_coords.
y, bboxlist_on_rank0[p].
max_coords.
y);
2343 global_max_coords.
z = PetscMax(global_max_coords.
z, bboxlist_on_rank0[p].
max_coords.
z);
2350 LOG_ALLOW(
LOCAL,
LOG_WARNING,
"(Rank 0) - bboxlist not provided or num_mpi_procs <=0; using user->bbox for domain bounds.\n");
2355 ierr = PetscPrintf(PETSC_COMM_SELF,
"\n"); CHKERRQ(ierr);
2356 ierr = PetscPrintf(PETSC_COMM_SELF,
"=============================================================\n"); CHKERRQ(ierr);
2357 ierr = PetscPrintf(PETSC_COMM_SELF,
" CASE SUMMARY \n"); CHKERRQ(ierr);
2358 ierr = PetscPrintf(PETSC_COMM_SELF,
"=============================================================\n"); CHKERRQ(ierr);
2359 ierr = PetscPrintf(PETSC_COMM_SELF,
" Grid Points : %d X %d X %d\n", user->
IM, user->
JM, user->
KM); CHKERRQ(ierr);
2360 ierr = PetscPrintf(PETSC_COMM_SELF,
" Cells : %d X %d X %d\n", user->
IM - 1, user->
JM - 1, user->
KM - 1); CHKERRQ(ierr);
2361 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);
2362 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);
2363 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);
2365 ierr = PetscPrintf(PETSC_COMM_SELF,
"-------------------- Boundary Conditions --------------------\n"); CHKERRQ(ierr);
2366 const int face_name_width = 17;
2367 for (PetscInt i_face = 0; i_face < 6; ++i_face) {
2375 Cmpnts inlet_velocity = {0.0,0.0,0.0};
2380 ierr = PetscPrintf(PETSC_COMM_SELF,
" Face %-*s : %s - %s - [%.4f,%.4f,%.4f]\n",
2381 face_name_width, face_str, bc_type_str, bc_handler_type_str,inlet_velocity.
x,inlet_velocity.
y,inlet_velocity.
z); CHKERRQ(ierr);
2383 PetscReal v_max = 0.0;
2386 ierr = PetscPrintf(PETSC_COMM_SELF,
" Face %-*s : %s - %s - v_max=%.4f\n",
2387 face_name_width, face_str, bc_type_str, bc_handler_type_str, v_max); CHKERRQ(ierr);
2391 PetscBool trimflag,foundflux,foundtrimflag;
2394 ierr = PetscPrintf(PETSC_COMM_SELF,
" Face %-*s : %s - %s - [%.4f] - %s\n",
2395 face_name_width, face_str, bc_type_str, bc_handler_type_str,flux,trimflag?
"Trim Ucont":
"No Trim"); CHKERRQ(ierr);
2397 ierr = PetscPrintf(PETSC_COMM_SELF,
" Face %-*s : %s - %s\n",
2398 face_name_width, face_str, bc_type_str,bc_handler_type_str); CHKERRQ(ierr);
2402 ierr = PetscPrintf(PETSC_COMM_SELF,
"-------------------------------------------------------------\n"); CHKERRQ(ierr);
2403 ierr = PetscPrintf(PETSC_COMM_SELF,
" Start Time : %.4f\n", (
double)StartTime); CHKERRQ(ierr);
2404 ierr = PetscPrintf(PETSC_COMM_SELF,
" Timestep Size : %.4f\n", (
double)simCtx->
dt); CHKERRQ(ierr);
2405 ierr = PetscPrintf(PETSC_COMM_SELF,
" Starting Step : %d\n", StartStep); CHKERRQ(ierr);
2406 ierr = PetscPrintf(PETSC_COMM_SELF,
" Total Steps to Run : %d\n", StepsToRun); CHKERRQ(ierr);
2407 ierr = PetscPrintf(PETSC_COMM_SELF,
" Number of MPI Processes : %d\n", num_mpi_procs); CHKERRQ(ierr);
2408 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Number of Particles : %d\n", total_num_particles); CHKERRQ(ierr);
2412 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Reynolds Number : %le\n", simCtx->
ren); CHKERRQ(ierr);
2414 ierr = PetscPrintf(PETSC_COMM_SELF,
" Particle Initialization Mode: %s\n", particle_init_str); CHKERRQ(ierr);
2418 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Initial Pseudo-CFL : %le\n", simCtx->
pseudo_cfl); CHKERRQ(ierr);
2419 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Large Eddy Simulation Model : %s\n",
LESModelToString(simCtx->
les)); CHKERRQ(ierr);
2426 ierr = PetscPrintf(PETSC_COMM_SELF,
" Particles Initialized At : --- (No INLET face identified)\n"); CHKERRQ(ierr);
2430 ierr = PetscPrintf(PETSC_COMM_SELF,
" Field Initialization Mode : %s\n", field_init_str); CHKERRQ(ierr);
2435 ierr = PetscPrintf(PETSC_COMM_WORLD,
" Analytical Solution Type : %s\n", simCtx->
AnalyticalSolutionType); CHKERRQ(ierr);
2437 ierr = PetscPrintf(PETSC_COMM_SELF,
"=============================================================\n"); CHKERRQ(ierr);
2438 ierr = PetscPrintf(PETSC_COMM_SELF,
"\n"); CHKERRQ(ierr);
2440 PetscFunctionReturn(0);
2444#define __FUNCT__ "ParsePostProcessingSettings"
2462 PetscBool startTimeSet, endTimeSet, timeStepSet;
2464 PetscFunctionBeginUser;
2467 if (!simCtx || !simCtx->
pps) {
2468 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_NULL,
"SimCtx or its pps member is NULL in ParsePostProcessingSettings.");
2495 file = fopen(configFile,
"r");
2498 while (fgets(line,
sizeof(line), file)) {
2499 char *key, *value, *comment;
2500 comment = strchr(line,
'#');
if (comment) *comment =
'\0';
2502 key = strtok(line,
"="); value = strtok(NULL,
"=");
2505 if (strcmp(key,
"startTime") == 0) pps->
startTime = atoi(value);
2506 else if (strcmp(key,
"endTime") == 0) pps->
endTime = atoi(value);
2507 else if (strcmp(key,
"timeStep") == 0) pps->
timeStep = atoi(value);
2508 else if (strcmp(key,
"output_particles") == 0) {
2509 if (strcasecmp(value,
"true") == 0) pps->
outputParticles = PETSC_TRUE;
2511 else if (strcasecmp(key,
"process_pipeline") == 0) {
2514 }
else if (strcasecmp(key,
"output_fields_instantaneous") == 0) {
2517 }
else if (strcasecmp(key,
"output_prefix") == 0) {
2520 }
else if (strcasecmp(key,
"particle_output_prefix") == 0) {
2523 }
else if (strcasecmp(key,
"particle_fields_instantaneous") == 0) {
2526 }
else if (strcasecmp(key,
"particle_pipeline") == 0) {
2529 }
else if (strcasecmp(key,
"particle_output_freq") == 0) {
2531 }
else if (strcmp(key,
"reference_ip") == 0) {pps->
reference[0] = atoi(value);
2532 }
else if (strcmp(key,
"reference_jp") == 0) {pps->
reference[1] = atoi(value);
2533 }
else if (strcmp(key,
"reference_kp") == 0) {pps->
reference[2] = atoi(value);
2534 }
else if (strcasecmp(key,
"source_directory") == 0) {
2545 LOG_ALLOW(
GLOBAL,
LOG_WARNING,
"Could not open post-processing config file '%s'. Using defaults and command-line overrides.\n", configFile);
2549 PetscOptionsGetInt(NULL, NULL,
"-startTime", &pps->
startTime, &startTimeSet);
2550 PetscOptionsGetInt(NULL, NULL,
"-endTime", &pps->
endTime, &endTimeSet);
2551 PetscOptionsGetInt(NULL, NULL,
"-timeStep", &pps->
timeStep, &timeStepSet);
2552 PetscOptionsGetBool(NULL, NULL,
"-output_particles", &pps->
outputParticles, NULL);
2559 if (startTimeSet && !endTimeSet) {
2563 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"Post-processing configured to run from t=%d to t=%d with step %d. Particle output: %s.\n",
2575 PetscFunctionReturn(0);
2580#define __FUNCT__ "ParseScalingInformation"
2595 PetscErrorCode ierr;
2598 PetscFunctionBeginUser;
2601 if (!simCtx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"SimCtx is NULL in ParseScalingInformation");
2610 ierr = PetscOptionsGetReal(NULL, NULL,
"-scaling_L_ref", &simCtx->
scaling.
L_ref, &flg); CHKERRQ(ierr);
2611 ierr = PetscOptionsGetReal(NULL, NULL,
"-scaling_U_ref", &simCtx->
scaling.
U_ref, &flg); CHKERRQ(ierr);
2612 ierr = PetscOptionsGetReal(NULL, NULL,
"-scaling_rho_ref", &simCtx->
scaling.
rho_ref, &flg); CHKERRQ(ierr);
2617 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
"Reference velocity U_ref must be positive. Got %g", (
double)simCtx->
scaling.
U_ref);
2622 LOG(
GLOBAL,
LOG_INFO,
"---------------- Physical Scales Initialized -----------------\n");
2623 LOG(
GLOBAL,
LOG_INFO,
" L_ref: %.4f, U_ref: %.4f, rho_ref: %.4f, P_ref: %.4f\n",
2625 LOG(
GLOBAL,
LOG_INFO,
"--------------------------------------------------------------\n");
2628 PetscFunctionReturn(0);
2649 PetscMPIInt rank, size;
2650 PetscErrorCode ierr;
2653 double *array = NULL;
2654 PetscInt fileExistsFlag = 0;
2657 "Start reading from file: %s\n",
2661 if (!filename || !data_out || !Nout) {
2663 "Null pointer argument provided.\n");
2668 MPI_Comm_rank(comm, &rank);
2669 MPI_Comm_size(comm, &size);
2673 fp = fopen(filename,
"r");
2682 ierr = MPI_Bcast(&fileExistsFlag, 1, MPI_INT, 0, comm); CHKERRQ(ierr);
2684 if (!fileExistsFlag) {
2688 "File '%s' not found.\n",
2696 fp = fopen(filename,
"r");
2699 "File '%s' could not be opened for reading.\n",
2707 while (fgets(line,
sizeof(line), fp)) {
2713 "File '%s' has %d lines.\n",
2717 array = (
double*)malloc(N *
sizeof(
double));
2721 "malloc failed for array.\n");
2730 while (fgets(line,
sizeof(line), fp)) {
2732 if (sscanf(line,
"%lf", &val) == 1) {
2740 "Successfully read %d values from '%s'.\n",
2745 ierr = MPI_Bcast(&N, 1, MPI_INT, 0, comm); CHKERRQ(ierr);
2749 array = (
double*)malloc(N *
sizeof(
double));
2752 "malloc failed on rank %d.\n",
2759 ierr = MPI_Bcast(array, N, MPI_DOUBLE, 0, comm); CHKERRQ(ierr);
2766 "Done. Provided array of length=%d to all ranks.\n",
2787 double **coordsArray,
2790 PetscFunctionBeginUser;
2792 PetscErrorCode ierr;
2796 ierr = VecCreate(PETSC_COMM_WORLD, &coordsVec);CHKERRQ(ierr);
2797 ierr = VecSetFromOptions(coordsVec);CHKERRQ(ierr);
2800 ierr =
ReadFieldData(user,
"position", coordsVec, timeIndex,
"dat");
2803 "Error reading position data (ti=%d).\n",
2805 PetscFunctionReturn(ierr);
2811 ierr = VecDestroy(&coordsVec);CHKERRQ(ierr);
2814 "Successfully gathered coordinates. Ncoords=%d.\n", *Ncoords);
2815 PetscFunctionReturn(0);
2834 const char *fieldName,
2836 double **scalarArray,
2839 PetscFunctionBeginUser;
2841 PetscErrorCode ierr;
2845 ierr = VecCreate(PETSC_COMM_WORLD, &fieldVec);CHKERRQ(ierr);
2846 ierr = VecSetFromOptions(fieldVec);CHKERRQ(ierr);
2848 ierr =
ReadFieldData(user, fieldName, fieldVec, timeIndex,
"dat");
2851 "Error reading field '%s' (ti=%d).\n",
2852 fieldName, timeIndex);
2853 PetscFunctionReturn(ierr);
2859 ierr = VecDestroy(&fieldVec);CHKERRQ(ierr);
2862 "Successfully gathered field '%s'. Nscalars=%d.\n",
2863 fieldName, *Nscalars);
2864 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 DeterminePeriodicity(SimCtx *simCtx)
Scans all block-specific boundary condition files to determine a globally consistent periodicity for ...
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.
PetscErrorCode GetBCParamBool(BC_Param *params, const char *key, PetscBool *value_out, PetscBool *found)
Searches a BC_Param linked list for a key and returns its value as a bool.
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.
#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 * LESModelToString(LESModelType LESFlag)
Helper function to convert LES Flag to a string representation.
const char * MomentumSolverTypeToString(MomentumSolverType SolverFlag)
Helper function to convert Momentum Solver flag to a string representation.
const char * ParticleInitializationToString(ParticleInitializationType ParticleInitialization)
Helper function to convert ParticleInitialization 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.
@ PARTICLE_INIT_SURFACE_RANDOM
Random placement on the inlet face.
@ PARTICLE_INIT_SURFACE_EDGES
Deterministic placement at inlet face edges.
#define MAX_FIELD_LIST_LENGTH
BCHandlerType
Defines the specific computational "strategy" for a boundary handler.
@ BC_HANDLER_PERIODIC_GEOMETRIC
@ BC_HANDLER_INLET_PARABOLIC
@ BC_HANDLER_INLET_CONSTANT_VELOCITY
@ BC_HANDLER_PERIODIC_DRIVEN_INITIAL_FLUX
@ BC_HANDLER_PERIODIC_DRIVEN_CONSTANT_FLUX
@ 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 eulerianSource[PETSC_MAX_PATH_LEN]
ParticleInitializationType ParticleInitialization
char process_pipeline[1024]
PetscInt particle_output_freq
char particle_fields[1024]
PetscBool outputParticles
char AnalyticalSolutionType[PETSC_MAX_PATH_LEN]
Cmpnts InitialConstantContra
@ EXEC_MODE_POSTPROCESSOR
char _io_context_buffer[PETSC_MAX_PATH_LEN]
MomentumSolverType mom_solver_type
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.