Removing mesh volume elements - Evolving (??) mesh

Questions and remarks about code_saturne usage
Forum rules
Please read the forum usage recommendations before posting.
Pisolino
Posts: 86
Joined: Thu Apr 26, 2012 1:55 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Pisolino »

Hi Yvan,
after some debugging the function is working, the boundary condition is transmitted to the i_face. Here the debugged code:

Code: Select all

static void
bounfunc(void        *input,
                cs_lnum_t   *n_cells,
                cs_lnum_t  **cell_ids)
{
 cs_lnum_t i, face_id;

  cs_lnum_t _n_cells = 0;
  cs_lnum_t *_cell_ids = NULL;
  cs_lnum_t n_b_faces = 0;
  //cs_lnum_t *b_face_ids = NULL;
 
 /* import the mesh */
 const cs_mesh_t *mesh = cs_glob_mesh;
 /* define variable and pointers*/
cs_lnum_t n_elts;
cs_lnum_t *elt_ids;
/*import all cells, also with halo - ghost */
const int n_cells_ext = mesh->n_cells_with_ghosts;

/* Initialize cell marker */
int *cell_marker;
/* malloc in cell marker the size of the cells, ghost included*/
BFT_MALLOC(cell_marker, n_cells_ext, int);

/* mark all cells as 0 */
for (i = 0; i < mesh->n_cells; i++)
  cell_marker[i] = 0;

/* Select boundary faces belonging to "heat" and save
*  their number and id  */
BFT_MALLOC(elt_ids, mesh->n_b_faces, cs_lnum_t);
cs_selector_get_b_face_list("heat", &n_elts, elt_ids);

/* Mark adjacent cells (with boundary face family) */
/* scroll all the selected element list   */
for (i = 0; i < n_elts; i++) {
  cs_lnum_t face_id = elt_ids[i];
/* marking cells cell_marker[cell_id]= Boundary face family  */
  cell_marker[mesh->b_face_cells[face_id]] = mesh->b_face_family[face_id];
}

/* Now propagate to interior faces */
for (i = 0; i < mesh->n_i_faces; i++) { //for all the i_faces
  for (cs_lnum_t j = 0; j < 2; j++) {
    cs_lnum_t cell_id = mesh->i_face_cells[i][j]; //pick the cell id of i_face
    if (cell_marker[cell_id] != 0) //if it is the cell to be removed
       mesh->i_face_family[i] = cell_marker[cell_id]; 
	
  }
}

/* now pick only the all - undesired cells to a new mesh*/
BFT_MALLOC(_cell_ids, mesh->n_cells, cs_lnum_t); /* Allocate selection list */
    for (i = 0; i < mesh->n_cells; i++) {
      if (cell_marker[i] == 0) {
	 _cell_ids[_n_cells] = i;
        _n_cells += 1;
      }
    }
	
    BFT_REALLOC(_cell_ids, _n_cells, cs_lnum_t); /* Adjust size(good practice,but not required) */
 
 /* Set return values */
  *n_cells = _n_cells;
  *cell_ids = _cell_ids;
 
BFT_FREE(cell_marker);
BFT_FREE(elt_ids);
} 


I have another question since it is not so clear to me: once the mesh is extracted after N iterations, how can i restart the simulation with the new mesh automatically to perform again the N iterations?

Thanks

Andrea
Andrea
CEO and R&D manager @ ARGO srl

mail: info@argosrl.eu
site: www.argosrl.eu
Yvan Fournier
Posts: 4077
Joined: Mon Feb 20, 2012 3:25 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Yvan Fournier »

Hello,

Since you cannot restart on a different mesh, the next calculation is a "new" calculation, for which you may use the extracted mesh (MED postprocessing output) as a new input mesh.

If you want to automate that part, it may be practical to use the use cs_user_scripts.py function, so as to search for the last med output and use it as input. The "auto restart" example near the top of that file should be a good example of hop to proceed (replacing the checkpoint/restart with postprocessing output/input mesh).

Regards,

Yvan
Pisolino
Posts: 86
Joined: Thu Apr 26, 2012 1:55 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Pisolino »

Hi yvan,
thank you, today i have done some experiments with the cs_user_scripts.py. So i wanted to start to define my condition for cell selecting, and again another error. I'm not sure how to use yplus value since it is a boundary value field. Below an extraction of the bounfunc defined in this post. Sorry i'm really bad at coding. I want to read in the selected cell the yplus value but i have allocation errors when i use ' f->val '

Code: Select all

/* mark all cells as 0 */
for (i = 0; i < mesh->n_cells; i++)
  cell_marker[i] = 0;
 
/* IMPORT THE YPLUS VALUES */ 
cs_field_t *f = cs_field_by_name("yplus"); 

/*CHECK IF TH FIELD EXISTS */
  if (f == NULL) {
    bft_error(__FILE__, __LINE__, 0,
              "No field with name \"yplus\" defined"); }


/* Select boundary faces belonging to "heat" and save
*  their number and id  */
BFT_MALLOC(elt_ids, mesh->n_b_faces, cs_lnum_t);
cs_selector_get_b_face_list("heat", &n_elts, elt_ids);



/* IF THERE ARE VALUES DIFFERENT FROM 0 */
if (f->val != NULL) {

ymin= 100000.0;
ymax= 0.0;
ymean= 0.0;
/*FIND MIN MAX AND MEAN OF YPLUS IN HEAT BOUNDARY DOMAIN*/
for (i = 0; i < elt_ids ; i++) {

ymean += f->val[i];
if (f->val[i] > ymax)
	ymax= f->val[i];

if (f->val[i] < ymin)
	ymin= f->val[i];
}
ymean= ymean/n_elts;
printf(" ymax= %f ", ymax);
/*------------------------------------------*/


/* Mark adjacent cells (with boundary face family) */
/* scroll all the selected element list   */
Andrea
CEO and R&D manager @ ARGO srl

mail: info@argosrl.eu
site: www.argosrl.eu
Yvan Fournier
Posts: 4077
Joined: Mon Feb 20, 2012 3:25 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Yvan Fournier »

Hello,

Your error is here:
for (i = 0; i < elt_ids ; i++) {
where you should have:
for (i = 0; i < n_elts ; i++) {
Regards,

Yvan
Pisolino
Posts: 86
Joined: Thu Apr 26, 2012 1:55 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Pisolino »

Hi Yvan, thanks ! now it works in single core as it is possible to see in the attached file.
Now i'm trying to extend the algorithm to a multi thread process. When i run the same algorithm i found :

Code: Select all

==9412== Conditional jump or move depends on uninitialised value(s)
==9412== at 0x401063: bounfunc (cs_user_postprocess.c:153)
==9412== by 0x4FE41B9: _define_mesh (cs_post.c:1580)
==9412== by 0x4FE4816: _redefine_mesh (cs_post.c:1711)
==9412== by 0x4FEAC76: cs_post_write_vars (cs_post.c:5185)
==9412== by 0x4FECCB5: pstvar_ (cs_post_default.c:358)
==9412== by 0x4ED0C33: caltri_ (caltri.f90:1118)
==9412== by 0x4E9E06F: cs_run (cs_solver.c:323)
==9412== by 0x4E9E2C7: main (cs_solver.c:512)
the line is

Code: Select all

 if (cell_marker[cell_id] != 0) //if it is the cell to be removed 
here below the entire function:

Code: Select all

static void
bounfunc(void        *input,
                cs_lnum_t   *n_cells,
                cs_lnum_t  **cell_ids)
{
 cs_lnum_t i, face_id, counter1;

 cs_real_t ymin, ymax, ymean;

  cs_lnum_t _n_cells = 0;
  cs_lnum_t *_cell_ids = NULL;
  cs_lnum_t n_b_faces = 0;
  //cs_lnum_t *b_face_ids = NULL;

 /* import the mesh */
 const cs_mesh_t *mesh = cs_glob_mesh;
 /* define variable and pointers*/
cs_lnum_t n_elts;
cs_lnum_t *elt_ids;
/*import all cells, also with halo - ghost */
const int n_cells_ext = mesh->n_cells_with_ghosts;

/* Initialize cell marker */
int *cell_marker;
/* malloc in cell marker the size of the cells, ghost included*/
BFT_MALLOC(cell_marker, n_cells_ext, int);

/* mark all cells as 0 */
for (i = 0; i < mesh->n_cells; i++)
  cell_marker[i] = 0;
 
/* IMPORT THE YPLUS VALUES */ 
cs_field_t *f = cs_field_by_name("yplus"); 

/*CHECK IF TH FIELD EXISTS */
  if (f == NULL) {
    bft_error(__FILE__, __LINE__, 0,
              "No field with name \"yplus\" defined"); }


/* Select boundary faces belonging to "heat" and save
*  their number and id  */
BFT_MALLOC(elt_ids, mesh->n_b_faces, cs_lnum_t);
cs_selector_get_b_face_list("heat", &n_elts, elt_ids);



/* IF THERE ARE VALUES DIFFERENT FROM 0 */
if (f->val != NULL) {

ymin= 100000.0;
ymax= 0.0;
ymean= 0.0;
/*FIND MIN MAX AND MEAN OF YPLUS IN HEAT BOUNDARY DOMAIN*/
for (i = 0; i < n_elts ; i++) {

ymean += f->val[i];
if (f->val[i] > ymax)
	ymax= f->val[i];

if (f->val[i] < ymin)
	ymin= f->val[i];
}
ymean= ymean/n_elts;

/*------------------------------------------*/


/* Mark adjacent cells (with boundary face family) */
/* scroll all the selected element list   */
counter1=0;

for (i = 0; i < n_elts; i++) {
if ( f->val[i] < (ymin+(ymean-ymin)*0.3) ) {
  cs_lnum_t face_id = elt_ids[i];
/* marking cells cell_marker[cell_id]= Boundary face family  */
  cell_marker[mesh->b_face_cells[face_id]] = mesh->b_face_family[face_id];
  counter1 +=1;
}
}

//printf(" counter= %f ", counter1);

/* Now propagate to interior faces */
for (i = 0; i < mesh->n_i_faces; i++) { //for all the i_faces
  for (cs_lnum_t j = 0; j < 2; j++) {
    cs_lnum_t cell_id = mesh->i_face_cells[i][j]; //pick the cell id of i_face
    if (cell_marker[cell_id] != 0) //if it is the cell to be removed
       mesh->i_face_family[i] = cell_marker[cell_id]; 
	
  }
}

/* now pick only the all - undesired cells to a new mesh*/
BFT_MALLOC(_cell_ids, mesh->n_cells, cs_lnum_t); /* Allocate selection list */
    for (i = 0; i < mesh->n_cells; i++) {
      if (cell_marker[i] == 0) {
	 _cell_ids[_n_cells] = i;
        _n_cells += 1;
      }
    }
	
    BFT_REALLOC(_cell_ids, _n_cells, cs_lnum_t); /* Adjust size(good practice,but not required) */
}
 
 /* Set return values */
  *n_cells = _n_cells;
  *cell_ids = _cell_ids;
 
BFT_FREE(cell_marker);
BFT_FREE(elt_ids);

} 
Moreover, after resolving this issue, i will surely have to find a way to code parallel sums (as it is possible to do with fortran) per the maxmimum, minimum, and mean value.
Attachments
heat_boundary_end.jpg
Andrea
CEO and R&D manager @ ARGO srl

mail: info@argosrl.eu
site: www.argosrl.eu
Yvan Fournier
Posts: 4077
Joined: Mon Feb 20, 2012 3:25 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Yvan Fournier »

Hello,

In parallel, you may also need to set initialize cell_marker using a loop of size n_cells_ext instead of mesh->n_cells (this should avoid the Valgrind warning which may be due to accessing values with cell_id > n_cells at faces on parallel boundaries / adjacent to ghost cells).

Best regards,

Yvan
Pisolino
Posts: 86
Joined: Thu Apr 26, 2012 1:55 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Pisolino »

Thank you Yvan, i was writing your solution while you posted. Yes i setted

Code: Select all

/* mark all cells as 0 */
for (i = 0; i < n_cells_ext ; i++)  // otherwise mesh->n_cells, essential for parallel solving
  cell_marker[i] = 0;
Now the simulation is running and i can see that some faces are not marked as "heat" boundary in the resulting mesh, maybe this happens because the face property is stored in the ghost domain and then it is lost ?

Code: Select all

/* Now propagate to interior faces */
for (i = 0; i < mesh->n_i_faces; i++) { //for all the i_faces
  for (cs_lnum_t j = 0; j < 2; j++) {
    cs_lnum_t cell_id = mesh->i_face_cells[i][j]; //pick the cell id of i_face
    if (cell_marker[cell_id] != 0) //if it is the cell to be removed
       mesh->i_face_family[i] = cell_marker[cell_id]; 
  }
}
n_i_faces takes in consideration also ghost faces ?

Thank you

Andrea
Andrea
CEO and R&D manager @ ARGO srl

mail: info@argosrl.eu
site: www.argosrl.eu
Yvan Fournier
Posts: 4077
Joined: Mon Feb 20, 2012 3:25 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Yvan Fournier »

Hello,

For this, synchronizing the cell marker may be an added precaution (before propagating to interior faces).

Code: Select all

cs_halo_sync_untyped(cs_glob_mesh->halo, CS_HALO_STANDARD, sizeof(int), cell_marker);
Best regards,

Yvan
Pisolino
Posts: 86
Joined: Thu Apr 26, 2012 1:55 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Pisolino »

Hi,
Thanks to your kind help now the simulation is working very good. I'm able to see the mesh evolving following the rules i have defined. however i don't know why some errors appears at a certain point.

Code: Select all

algebraic multigrid [Pressure]: divergence after 2 cycles:
  initial residual: 5.8255e-005; current residual: 1.#QNBe+000
At first i thought it was due to over-constrained cells so i edited the algorithm to remove also such cells but with no difference in divergence error. What i consider strange is the not-numerical error #QNB . Any suggestion ? i attach the modified mesh
Attachments
opti_cfd.zip
(417.66 KiB) Downloaded 208 times
Andrea
CEO and R&D manager @ ARGO srl

mail: info@argosrl.eu
site: www.argosrl.eu
Yvan Fournier
Posts: 4077
Joined: Mon Feb 20, 2012 3:25 pm

Re: Removing mesh volume elements - Evolving (??) mesh

Post by Yvan Fournier »

Hello,

Do you have this issue when restarting on the new mesh (with cells removed) ?

The crash may be due to boundary condition issues (in which case you may want to check the boundary groups were modified correctly using the mesh quality verification step, unless you have already done so), or to mesh quality issues (if the mesh becomes irregular due to removing some cells, convergence may be more difficult).

Could you try a conjugate gradient instead of multigrid ?

Best regards,

Yvan
Post Reply