I am using a user defined subroutine to read in the external spatially varying body force from external file. I was working well with 1 process. I notice that, the at the initialization stage the routine has been called the number of time which exactly equals to the number of processes. I am wondering whether this could destroy the mechanism I introduced here. I understand the model will be partitioned internally during the calculation. But not very sure about the initialization stage.
You may see my code go through all cells one-by-one, and assign an value read from a file to the corresponding cell. My question is: Are external values read multiple times, or the values are mismatched to the cell? I do not mind the data have been read and assigned for multiple times as long as the right value gave to the corresponding cell.
Code: Select all
void
cs_user_initialization(cs_domain_t *domain)
{
const cs_mesh_t *m = domain->mesh;
FILE *fptrx;
FILE *fptry;
FILE *fptrz;
char linex[256], liney[256], linez[256];
float num_x, num_y, num_z;
/* If this is restarted computation, do not reinitialize values */
if (domain->time_step->nt_prev > 0)
return;
cs_field_t *fx = cs_field_by_name("x");
cs_field_t *fy = cs_field_by_name("y");
cs_field_t *fz = cs_field_by_name("z");
if ((fptrx = fopen("fx.txt","r")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
if ((fptry = fopen("fy.txt","r")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
if ((fptrz = fopen("fz.txt","r")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
if (fx != NULL) {
for (cs_lnum_t cell_id = 0; cell_id < m->n_cells; cell_id++){
fgets(linex, sizeof(linex), fptrx);
sscanf(linex,"%f",&num_x);
fx->val[cell_id] = num_x;
fgets(liney, sizeof(liney), fptry);
sscanf(liney,"%f",&num_y);
fy->val[cell_id] = num_y;
fgets(linez, sizeof(linez), fptrz);
sscanf(linez,"%f",&num_z);
fz->val[cell_id] = num_z;
}
}
fclose(fptrx);
fclose(fptry);
fclose(fptrz);
printf("Initialization Done!!! \n");
}