mesh.c 90 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
/*
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
2
 * MigFlow - Copyright (C) <2010-2018>
3
4
 * <Universite catholique de Louvain (UCL), Belgium
 *  Universite de Montpellier, France>
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
5
 * 	
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
6
 * List of the contributors to the development of MigFlow: see AUTHORS file.
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
7
8
 * Description and complete License: see LICENSE file.
 * 	
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
9
 * This program (MigFlow) is free software: 
10
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General 
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
11
12
13
14
15
16
 * Public License as published by the Free Software Foundation, either version
 * 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Lesser General Public License for more details.
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
18
 * 
19
20
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program (see COPYING and COPYING.LESSER files).  If not, 
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
21
22
23
 * see <http://www.gnu.org/licenses/>.
 */

24
25
26
27
#include "mesh.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Margaux Boxho's avatar
wip    
Margaux Boxho committed
28
#include <math.h>
29
#include "tools.h"
Margaux Boxho's avatar
wip    
Margaux Boxho committed
30
#define N_N (DIMENSION+1)
31

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
32
33
34
35
36
37
38
39
40
typedef struct {
  int n[DIMENSION];
  int element;
  int pos; // or tag if element == -1
}HalfEdge;

int hedgecmp(const void *pe0, const void *pe1) {
  int *e0 = ((HalfEdge*)pe0)->n;
  int *e1 = ((HalfEdge*)pe1)->n;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
41
  if (e0[0] != e1[0]) return e0[0]-e1[0];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
42
#if DIMENSION==2
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
43
  return e0[1]-e1[1];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
44
45
46
47
#else
  if (e0[1] != e1[1]) return e0[1]-e1[1];
  return e0[2]-e1[2];
#endif
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
48
49
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
50
51
52
53
54
55
static void swapint(int *a, int *b) {
  int c = *a;
  *a = *b;
  *b = c;
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
56
57
58
59
60
61
62
63
64
65
66
static void sort_edge_nodes(int *n) {
  if(n[0]>n[1])
    swapint(n,n+1);
#if DIMENSION == 3
  if(n[0]>n[2])
    swapint(n,n+2);
  if(n[1]>n[2])
    swapint(n+1,n+2);
#endif
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
67
void mesh_gen_edges(Mesh *m, int n_bnd_elements, int *bnd_elements, int *bnd_tags) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
68
69
70
71
72
73
74
75
  /*
  for (int i= 0; i < n_bnd_elements; ++i) {
    printf("%i %i : %i\n",bnd_elements[i*2+0],bnd_elements[i*2+1], bnd_tags[i]);
  }
  for (int i = 0; i < m->n_elements; ++i) {
    printf(" el %i : %i %i %i\n",i, m->elements[i*3+0], m->elements[i*3+1], m->elements[i*3+2]);
  }
  */
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
76
77
78
  int n_half_edges = m->n_elements*(DIMENSION+1)+n_bnd_elements;
  HalfEdge *halfedges = malloc(sizeof(HalfEdge)*n_half_edges);
  HalfEdge *he = halfedges; // current
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
79
  int id = 0;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
80
  for (int i=0; i<m->n_elements; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
81
82
83
    for (int j = 0; j < DIMENSION+1; ++j) {
      for (int k = 0; k < DIMENSION; ++k) {
        he->n[k] = m->elements[i*(DIMENSION+1)+elbnd[j][k]];
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
84
      }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
85
86
87
88
      sort_edge_nodes(he->n);
      he->element = i;
      he->pos = j;
      he++;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
89
90
91
    }
  }
  for (int i=0; i<n_bnd_elements; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
92
    for (int k = 0; k < DIMENSION; ++k){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
93
      he->n[k] = bnd_elements[i*DIMENSION+k];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
94
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
95
96
    sort_edge_nodes(he->n);
    he->element = -1;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
97
    he->pos = bnd_tags[i];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
98
    he ++;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
99
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
100
  qsort(halfedges,n_half_edges,sizeof(HalfEdge),hedgecmp);
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
101
102
  int n_edges = 0;
  for (int i = 0; i < n_half_edges; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
103
    if (i == 0 || hedgecmp(halfedges+i-1,halfedges+i)) n_edges++;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
104
105
106
107
  }
  int *edges = malloc(n_edges*4*sizeof(int));
  n_edges = 0;
  for (int i = 0; i < n_half_edges; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
108
    if (i == 0 || hedgecmp(halfedges+i-1,halfedges+i)){
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
109
      n_edges += 1;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
110
111
      edges[(n_edges-1)*4+0] = halfedges[i].element;
      edges[(n_edges-1)*4+1] = halfedges[i].pos;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
112
113
114
115
      edges[(n_edges-1)*4+2] = -1;
      edges[(n_edges-1)*4+3] = -1;
    }
    else {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
116
117
      edges[(n_edges-1)*4+2] = halfedges[i].element;
      edges[(n_edges-1)*4+3] = halfedges[i].pos;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
118
119
120
121
122
    }
  }
  m->n_interfaces = n_edges;
  m->interfaces = edges;
  free(halfedges);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
123
  for (int i = 0; i < n_edges; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
124
    int *e = edges + i*4;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
125
    // ensure boundary tag is on the second part of the closure
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
126
127
128
    if (e[0] < 0) {
      swapint(&e[0],&e[2]);
      swapint(&e[1],&e[3]);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
129
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
130
131
132
133
    // compute the closure id of the second element
    if (e[2] >= 0) {
#if DIMENSION==2
      e[3] += 3;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
134
#else
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
135
136
137
138
139
      int k = 0;
      const int *e0 = &m->elements[e[0]*4];
      const int *e1 = &m->elements[e[2]*4];
      e[3] += 12;
      while (e0[elbnd[e[1]][0]] != e1[elbnd[e[3]][k]]) e[3] += 4;
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
140
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
141
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
142
143
144
145
146
147
148
    else {
      if (e[3] == -1){
        const int *e0 = &m->elements[e[0]*(DIMENSION+1)];
        printf("boundary edge %i %i with tag -1 !!!\n",e0[elbnd[e[1]][0]],e0[elbnd[e[1]][1]]);
        exit(1);
      }
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
149
  }
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
150
151
}

Margaux Boxho's avatar
Margaux Boxho committed
152

153
154
155
static int check_word_in_file(FILE *f, const char *w) {
  char word[256];
  if(fscanf(f,"%255s", word) != 1){
156
    printf("Invalid mesh file (\"%s\" expected).\n", w);
157
158
159
    return -1;
  }
  if(strcmp(word, w) != 0){
160
    printf("Invalid mesh file (\"%s\" expected).\n", w);
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    return -1;
  }
  return 0;
}

static int read_quoted_string(FILE *f, char *w, size_t maxl) {
  char c;
  size_t pos = 0;
  c = fgetc(f);
  while (c != '\"' && !feof(f)) {
    c = fgetc(f);
  }
  do {
    c = fgetc(f);
    if (c != '\"')
      w[pos++] = c;
  }while (c != '\"' && !feof(f) && pos < maxl);
  if (pos == maxl)
    return printf("string too long in mesh file\n");
  if(feof(f))
    return printf("string not terminated in mesh file\n");
  w[pos] = '\0';
  return 0;
}

static int int_cmp(const void *p0, const void *p1)
{
  int i0 = *(int*)p0;
  int i1 = *(int*)p1;
  if (i0 == i1) return 0;
  if (i0 < i1) return -1;
  return 1;
}

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
static void sortDi(int i[DIMENSION]) {
#if DIMENSION == 2
   if (i[0]>i[1]) {
     int n = i[0];
     i[0] = i[1];
     i[1] = n;
   }
#else
   int n[3] = {i[0],i[1],i[2]};
   if (n[0]>n[1]) {
      i[1]=n[0];    
      i[0]=n[1];   
   } else {
      i[1]=n[1];  
      i[0]=n[0];  
   } 
   if (i[1]>n[2]) { 
      i[2]=i[1];    
      if(i[0]>n[2]){         
         i[1]=i[0];                
         i[0]=n[2];
      } else i[1]=n[2];      
   } else i[2]=n[2]; 
#endif
}

221
222
223
Mesh *mesh_load_msh(const char *filename)
{
  Mesh *m = malloc(sizeof(Mesh));
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
224
  m->interfaces = NULL;
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  FILE *f = fopen(filename, "r");
  if (!f){
    printf("Cannot open file \"%s\".\n", filename);
    return NULL;
  }
  check_word_in_file(f,"$MeshFormat");
  float v0;
  int v1, v2;
  if(fscanf(f, "%g %d %d", &v0, &v1, &v2) != 3){
    printf("Cannot parse mesh file version\n");
    return NULL;
  }
  if ((int)10*v0 != 22){
    printf("Mesh file format is %g and not msh 2.2\n", v0);
    return NULL;
  }
  check_word_in_file(f,"$EndMeshFormat");
  check_word_in_file(f,"$PhysicalNames");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
243
244
  int nphysfile;
  if (fscanf(f, "%d", &nphysfile) != 1){
245
246
247
    printf("Cannot read physical names\n");
    return NULL;
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
248
249
250
251
252
253
  m->boundary_names = malloc(sizeof(char*)*nphysfile);
  int *physid = malloc(sizeof(int)*nphysfile);
  int nphys = 0;
  for (int i = 0; i < nphysfile; ++i) {
    int physdim, pid;
    if (fscanf(f, "%i %i", &physdim, &pid) != 2){
254
255
256
257
258
      printf("Cannot read physical names\n");
      return NULL;
    }
    char pname[256];
    if(read_quoted_string(f, pname, 256) < 0) return NULL;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
259
260
261
262
263
    if (physdim == DIMENSION-1) {
      physid[nphys] = pid;
      m->boundary_names[nphys] = strdup(pname);
      nphys++;
    }
264
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
265
  m->n_boundaries = nphys;
266
267
268
269
270
271
272
  check_word_in_file(f,"$EndPhysicalNames");
  check_word_in_file(f,"$Nodes");
  if (fscanf(f, "%d", &m->n_nodes) != 1){
      printf("Cannot read physical nodes.\n");
      return NULL;
  }
  m->x = malloc(sizeof(double)*m->n_nodes*3);
Jonathan Lambrechts's avatar
weak bc    
Jonathan Lambrechts committed
273

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  for (int i = 0; i < m->n_nodes; ++i) {
    int nid;
    if (fscanf(f, "%d %le %le %le", &nid, &m->x[i*3], &m->x[i*3+1], &m->x[i*3+2]) != 4){
      printf("Cannot read nodes.\n");
      return NULL;
    }
    if (nid != i+1){
      printf("Nodes are not sequentialy numbered.\n");
      return NULL;
    }
  }
  check_word_in_file(f,"$EndNodes");
  check_word_in_file(f,"$Elements");
  int n_el;
  if (fscanf(f, "%d", &n_el) != 1){
    printf("Cannot read elements.\n");
    return NULL;
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
292
293
  int *bnd_elements = malloc(sizeof(int)*n_el*DIMENSION);
  int *bnd_tags = malloc(sizeof(int)*n_el);
294
  size_t n_bnd_elements = 0;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
295
296
  m->elements = malloc(sizeof(int)*(DIMENSION+1)*n_el);
  m->n_elements = 0;
297
298
  for (int i = 0; i < n_el; ++i) {
    int eid, etype, nflags, flag;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
299
    int physfile=0;
300
301
302
303
304
305
306
307
308
309
    if (fscanf(f, "%d %d %d", &eid, &etype, &nflags) != 3){
      printf("Cannot read elements.\n");
      return NULL;
    }
    for (int j = 0; j < nflags; ++j){
      if (fscanf(f, "%d", &flag) != 1){
        printf("Cannot read elements.\n");
        return NULL;
      }
      if (j == 0)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
310
        physfile = flag;
311
    }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
312
    int n[DIMENSION+1];
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
313
    int phys = -1;
314
315
    switch (etype) {
      case(1):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
316
        if (fscanf(f, "%d %d", &n[0], &n[1]) != 2){
317
318
319
          printf("Cannot read elements.\n");
          return NULL;
        }
320
#if DIMENSION==2
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
321
322
323
324
325
        phys = -1;
        for (int in = 0; in < m->n_boundaries; ++in) {
          if (physid[in] == physfile) phys = in;
        }
        if (phys != -1) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
326
327
328
          bnd_tags[n_bnd_elements] = phys;
          for (int iD = 0; iD < 2; ++iD) bnd_elements[n_bnd_elements*2+iD] = n[iD]-1;
          sortDi(&bnd_elements[n_bnd_elements*2]);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
329
330
          n_bnd_elements++;
        }
331
#endif
332
333
        break;
      case(15):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
334
        if (fscanf(f, "%d", &n[0]) != 1){
335
336
337
338
339
          printf("Cannot read elements.\n");
          return NULL;
        }
        break;
      case(2):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
340
        if (fscanf(f, "%d %d %d", &n[0], &n[1], &n[2]) != 3){
341
342
343
          printf("Cannot read elements.\n");
          return NULL;
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
344
345
346
347
348
#if DIMENSION == 2
        m->elements[m->n_elements*3+0] = n[0]-1;
        m->elements[m->n_elements*3+1] = n[1]-1;
        m->elements[m->n_elements*3+2] = n[2]-1;
        m->n_elements++;
349
        break;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
350
#else
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
351
352
353
354
355
        phys = -1;
        for (int in = 0; in < m->n_boundaries; ++in) {
          if (physid[in] == physfile) phys = in;
        }
        if (phys != -1) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
356
357
358
          bnd_tags[n_bnd_elements] = phys;
          for (int iD = 0; iD < 3; ++iD) bnd_elements[n_bnd_elements*3+iD] = n[iD]-1;
          sortDi(&bnd_elements[n_bnd_elements*3]);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
359
360
          n_bnd_elements++;
        }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
361
362
363
364
365
366
367
368
369
370
371
372
373
        break;
      case(4):
        if (fscanf(f, "%d %d %d %d", &n[0], &n[1], &n[2], &n[3]) != 4){
          printf("Cannot read elements.\n");
          return NULL;
        }
        m->elements[m->n_elements*4+0] = n[0]-1;
        m->elements[m->n_elements*4+1] = n[1]-1;
        m->elements[m->n_elements*4+2] = n[2]-1;
        m->elements[m->n_elements*4+3] = n[3]-1;
        m->n_elements++;
        break;
#endif
374
375
376
377
      default:
        break;
    }
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
378
  m->elements = realloc(m->elements, (DIMENSION+1)*sizeof(int)*m->n_elements);
379
380
  check_word_in_file(f,"$EndElements");
  fclose(f);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
381
  free(physid);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
382
  mesh_gen_edges(m,n_bnd_elements,bnd_elements,bnd_tags);
383
  free(bnd_elements);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
384
  free(bnd_tags);
385
386
387
  return m;
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
388
389
int mesh_write_msh(Mesh *mesh, FILE *f)
{
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
390
391
392
393
394
  int nbnd = 0;
  for (int i = 0; i< mesh->n_interfaces; ++i) {
    if (mesh->interfaces[i*4+2] == -1) nbnd++;
  }

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
395
  fprintf(f,"$MeshFormat\n2.2 0 0\n$EndMeshFormat\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
396
397
398
399
400
401
  fprintf(f,"$PhysicalNames\n");
  fprintf(f,"%i\n", mesh->n_boundaries);
  for (int i = 0; i < mesh->n_boundaries; ++i) {
    fprintf(f,"%i %i \"%s\"\n", DIMENSION-1,i+1,mesh->boundary_names[i]);
  }
  fprintf(f,"$EndPhysicalNames\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
402
403
404
405
406
407
408
  fprintf(f,"$Nodes\n");
  fprintf(f, "%d\n", mesh->n_nodes);
  for (int i = 0; i < mesh->n_nodes; ++i) {
    fprintf(f, "%i %.16g %.16g %.16g\n", i+1, mesh->x[i*3], mesh->x[i*3+1], mesh->x[i*3+2]);
  }
  fprintf(f, "$EndNodes\n");
  fprintf(f, "$Elements\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
409
  fprintf(f, "%i\n", mesh->n_elements+nbnd);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
410
411
412
  for (int i= 0; i < mesh->n_elements; ++i) {
#if DIMENSION==2
    int *tri = mesh->elements+i*3;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
413
    fprintf(f, "%i 2 2 1 1 %i %i %i\n", i+1, tri[0]+1, tri[1]+1, tri[2]+1);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
414
415
416
417
#else
    int *tet = mesh->elements+i*4;
    fprintf(f, "%i 4 2 1 1 %i %i %i %i\n", i+1, tet[0]+1, tet[1]+1, tet[2]+1, tet[3]+1);
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
418
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
  nbnd = 0;
  for (int i = 0; i< mesh->n_interfaces; ++i) {
    if (mesh->interfaces[i*4+2] != -1) continue;
    int *cl = elbnd[mesh->interfaces[i*4+1]];
    int iel = mesh->interfaces[i*4+0];
    int phys = mesh->interfaces[i*4+3];
    int imsh = nbnd+mesh->n_elements;
#if DIMENSION==2
    int *tri = mesh->elements+iel*3;
    fprintf(f, "%i 1 2 %i %i %i %i\n", imsh+1, phys+1,phys+1,tri[cl[0]]+1, tri[cl[1]]+1);
#else
    int *tet = mesh->elements+iel*4;
    fprintf(f, "%i 2 2 %i %i %i %i %i\n", imsh+1, phys+1,phys+1,tet[cl[0]]+1, tet[cl[1]]+1, tet[cl[2]]+1);
#endif
    nbnd++;
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
435
436
437
438
  fprintf(f, "$EndElements\n");
  return 0;
}

439
440
void mesh_free(Mesh *m)
{
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
441
  free(m->elements);
442
  free(m->x);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
443
444
  for (int i = 0; i < m->n_boundaries; ++i) {
    free(m->boundary_names[i]);
445
  }
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
446
  free(m->boundary_names);
Jonathan Lambrechts's avatar
broken    
Jonathan Lambrechts committed
447
  free(m->interfaces);
448
449
450
  free(m);
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
451
int mesh_write_msh_scalar(const Mesh *mesh, FILE *f, const char *field_name, double t, int iter, const double *solution, int n_fields, int field_id)
452
453
454
455
456
457
458
459
460
461
462
463
{
  fprintf(f,"$NodeData\n");
  fprintf(f, "1\n\"%s\"\n",field_name);
  fprintf(f, "1\n%.16g\n",t);
  fprintf(f, "3\n%i\n%i\n%i\n", iter, 1, mesh->n_nodes);
  for (int i = 0; i < mesh->n_nodes; ++i) {
    fprintf(f, "%i %.16g\n", i+1, solution[i*n_fields+field_id]);
  }
  fprintf(f, "$EndNodeData\n");
  return 0;
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
464
int mesh_write_msh_vector(const Mesh *mesh, FILE *f, const char *field_name, double t, int iter, const double *solution, int n_fields, int field_id[DIMENSION])
465
466
467
468
469
470
{
  fprintf(f,"$NodeData\n");
  fprintf(f, "1\n\"%s\"\n",field_name);
  fprintf(f, "1\n%.16g\n",t);
  fprintf(f, "3\n%i\n%i\n%i\n", iter, 3, mesh->n_nodes);
  for (int i = 0; i < mesh->n_nodes; ++i) {
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
471
472
473
474
475
#if DIMENSION==2
    fprintf(f, "%i %.16g %.16g 0.\n", i+1, solution[i*n_fields+field_id[0]], solution[i*n_fields+field_id[1]]);
#else
    fprintf(f, "%i %.16g %.16g %.16g\n", i+1, solution[i*n_fields+field_id[0]], solution[i*n_fields+field_id[1]], solution[i*n_fields+field_id[2]]);
#endif
476
477
  }
  fprintf(f, "$EndNodeData\n");
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
478
479
480
481
482
483
484
485
486
487
488
  return 0;
}

int mesh_write_pos_scalar(const Mesh *mesh, const char *dir_name, const char *field_name, double t, int iter, const double *solution, int n_fields, int field_id)
{
  char file_name[1024];
  sprintf(file_name,"%s/%s_%05i.pos",dir_name, field_name, iter);
  FILE *f = fopen(file_name, "w");
  if (!f)
    return printf("Cannot open file \"%s\" for writing.\n", file_name);
  fprintf(f,"View\"%s\"{\nTIME {%g};\n", field_name, t);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
489
490
491
  for (int iel = 0; iel < mesh->n_elements; ++iel) {
#if DIMENSION==2
    int *tri = mesh->elements+iel*3;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
492
493
494
495
496
497
498
499
    const double x[3][2] = {
      {mesh->x[tri[0]*3+0],mesh->x[tri[0]*3+1]},
      {mesh->x[tri[1]*3+0],mesh->x[tri[1]*3+1]},
      {mesh->x[tri[2]*3+0],mesh->x[tri[2]*3+1]}};
    double V[3] = {solution[tri[0]*n_fields+field_id], solution[tri[1]*n_fields+field_id], solution[tri[2]*n_fields+field_id]};
    fprintf(f, "ST(%.8g, %.8g, 0, %.8g, %.8g, 0, %.8g, %.8g, 0){%.8g, %.8g, %.8g};\n",
        x[0][0],x[0][1],x[1][0],x[1][1],x[2][0],x[2][1],
        V[0],V[1],V[2]);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
500
501
502
503
504
505
506
507
508
509
510
511
512
#else
    int *tet = mesh->elements+iel*4;
    const double x[4][3] = {
      {mesh->x[tet[0]*3+0],mesh->x[tet[0]*3+1],mesh->x[tet[0]*3+2]},
      {mesh->x[tet[1]*3+0],mesh->x[tet[1]*3+1],mesh->x[tet[1]*3+2]},
      {mesh->x[tet[2]*3+0],mesh->x[tet[2]*3+1],mesh->x[tet[2]*3+2]},
      {mesh->x[tet[3]*3+0],mesh->x[tet[3]*3+1],mesh->x[tet[3]*3+2]},
    };
    double V[4] = {solution[tet[0]*n_fields+field_id], solution[tet[1]*n_fields+field_id], solution[tet[2]*n_fields+field_id], solution[tet[3]*n_fields+field_id]};
    fprintf(f, "ST(%.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g){%.8g, %.8g, %.8g, %.8g};\n",
        x[0][0],x[0][1],x[0][2],x[1][0],x[1][1],x[1][2],x[2][0],x[2][1],x[2][2],x[3][0],x[3][1],x[3][2],
        V[0],V[1],V[2],V[3]);
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
513
514
515
516
517
518
  }
  fprintf(f, "};\n");
  fclose(f);
  return 0;
}

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
519
int mesh_write_pos_vector(const Mesh *mesh, const char *dir_name, const char *field_name, double t, int iter, const double *solution, int n_fields, int field_id[DIMENSION])
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
520
521
522
523
524
525
526
{
  char file_name[1024];
  sprintf(file_name,"%s/%s_%05i.pos",dir_name, field_name, iter);
  FILE *f = fopen(file_name, "w");
  if (!f)
    return printf("Cannot open file \"%s\" for writing.\n", file_name);
  fprintf(f,"View\"%s\"{\nTIME {%g};\n", field_name, t);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
527
528
529
  for (int iel = 0; iel < mesh->n_elements; ++iel) {
#if DIMENSION ==2
    int *tri = mesh->elements+iel*3;
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
530
531
532
533
    const double x[3][2] = {
      {mesh->x[tri[0]*3+0],mesh->x[tri[0]*3+1]},
      {mesh->x[tri[1]*3+0],mesh->x[tri[1]*3+1]},
      {mesh->x[tri[2]*3+0],mesh->x[tri[2]*3+1]}};
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
534
535
    double V0[3] = {solution[tri[0]*n_fields+field_id[0]], solution[tri[1]*n_fields+field_id[0]], solution[tri[2]*n_fields+field_id[0]]};
    double V1[3] = {solution[tri[0]*n_fields+field_id[1]], solution[tri[1]*n_fields+field_id[1]], solution[tri[2]*n_fields+field_id[1]]};
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
536
537
538
    fprintf(f, "VT(%.8g, %.8g, 0, %.8g, %.8g, 0, %.8g, %.8g, 0){%.8g, %.8g, 0, %.8g, %.8g, 0, %.8g, %.8g, 0};\n",
        x[0][0],x[0][1],x[1][0],x[1][1],x[2][0],x[2][1],
        V0[0],V1[0], V0[1], V1[1], V0[2], V1[2]);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#else
    int *tet = mesh->elements+iel*4;
    const double x[4][3] = {
      {mesh->x[tet[0]*3+0],mesh->x[tet[0]*3+1],mesh->x[tet[0]*3+2]},
      {mesh->x[tet[1]*3+0],mesh->x[tet[1]*3+1],mesh->x[tet[1]*3+2]},
      {mesh->x[tet[2]*3+0],mesh->x[tet[2]*3+1],mesh->x[tet[2]*3+2]},
      {mesh->x[tet[3]*3+0],mesh->x[tet[3]*3+1],mesh->x[tet[3]*3+2]},
    };
    double V0[4] = {solution[tet[0]*n_fields+field_id[0]], solution[tet[1]*n_fields+field_id[0]], solution[tet[2]*n_fields+field_id[0]], solution[tet[3]*n_fields+field_id[0]]};
    double V1[4] = {solution[tet[0]*n_fields+field_id[1]], solution[tet[1]*n_fields+field_id[1]], solution[tet[2]*n_fields+field_id[1]], solution[tet[3]*n_fields+field_id[1]]};
    double V2[4] = {solution[tet[0]*n_fields+field_id[2]], solution[tet[1]*n_fields+field_id[2]], solution[tet[2]*n_fields+field_id[2]], solution[tet[3]*n_fields+field_id[2]]};
    fprintf(f, "VT(%.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g){%.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g, %.8g};\n",
        x[0][0],x[0][1],x[0][2],x[1][0],x[1][1],x[1][2],x[2][0],x[2][1],x[2][2],x[3][0],x[3][1],x[3][2],
        V0[0],V1[0],V2[0], V0[1], V1[1],V2[1], V0[2], V1[2],V2[2], V0[3], V1[3],V2[3]);
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
554
555
  }
  fprintf(f, "};\n");
556
557
558
  fclose(f);
  return 0;
}
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617

static int intcmp(const void *p0, const void *p1) {
  return *(int*)p0 - *(int*)p1;
}

MeshBoundary *mesh_boundaries_create(const Mesh *m) {
  MeshBoundary *b = malloc(sizeof(MeshBoundary)*m->n_boundaries);
  for (int i = 0; i < m->n_boundaries; ++i) {
    b[i].n_nodes = 0;
    b[i].n_interfaces = 0;
  }
  for (int i = 0; i < m->n_interfaces; ++i) {
    int *interface = m->interfaces + i*4;
    if (interface[2] == -1) {
      b[interface[3]].n_interfaces++;
    }
  }
  for (int i = 0; i < m->n_boundaries; ++i) {
    b[i].interfaces = malloc(b[i].n_interfaces*sizeof(int));
    b[i].nodes = malloc(b[i].n_interfaces*sizeof(int)*DIMENSION);
    b[i].n_interfaces = 0;
  }
  for (int i = 0; i < m->n_interfaces; ++i) {
    int *interface = m->interfaces + i*4;
    if (interface[2] == -1) {
      int tag = interface[3];
      int *el = m->elements + interface[0]*(DIMENSION+1);
      int *cl = elbnd[interface[1]];
      b[tag].interfaces[b[tag].n_interfaces++] = i;
      for (int i = 0; i < DIMENSION; ++i) {
        b[tag].nodes[b[tag].n_nodes++] = el[cl[i]];
      }
    }
  }
  for (int i = 0; i < m->n_boundaries; ++i) {
    qsort(b[i].nodes,b[i].n_nodes,sizeof(int),intcmp);
    int end = b[i].n_nodes;
    b[i].n_nodes = 0;
    int p = -1;
    for (int j = 0; j < end; ++j) {
      if (b[i].nodes[j] == p){
        continue;
      }
      b[i].nodes[b[i].n_nodes++] = b[i].nodes[j];
      p = b[i].nodes[j];
    }
    b[i].nodes = realloc(b[i].nodes, b[i].n_nodes*sizeof(int));
  }
  return b;
}

void mesh_boundaries_free(MeshBoundary *bs, int n)
{
  for (int i =0; i < n; ++i) {
    free(bs[i].nodes);
    free(bs[i].interfaces);
  }
  free(bs);
}
Margaux Boxho's avatar
wip    
Margaux Boxho committed
618
619
620

// Adding function to handle edges to create a gradient recovery estimator
void initialized_edges_array(Mesh *mesh, Edge *edges_vec, int n_edges){
621
    
Margaux Boxho's avatar
wip    
Margaux Boxho committed
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
    int tag_edge = 0;
    
    for (int iel=0; iel<mesh->n_elements; iel++) {
        const unsigned int *el = &mesh->elements[iel*N_N];
        for (int jj=0; jj<3; jj++){
            int tag_node1 = el[jj];
            int tag_node2 = el[(jj+1)%3];
            
            int ii = 0; int bool_stop = 0;
            
            while (ii<tag_edge && bool_stop==0) {
                
                int tag_node1_test = edges_vec[ii].tag_Node[0];
                int tag_node2_test = edges_vec[ii].tag_Node[1];
                
                if ((tag_node1_test==tag_node1 && tag_node2_test==tag_node2) || (tag_node1_test==tag_node2 && tag_node2_test==tag_node1)){
                    edges_vec[ii].tag_Tri[1]=iel+1; // the edge exits and we add its reference to the edge triangle
                    bool_stop=1; // break the loop
                }
                ii++;
Margaux Boxho's avatar
Margaux Boxho committed
642
            }
643
            
Margaux Boxho's avatar
wip    
Margaux Boxho committed
644
645
646
647
648
649
650
651
652
653
654
655
656
657
            if (ii==tag_edge && bool_stop==0) {
                edges_vec[ii].tag_Node[0]=el[jj];
                edges_vec[ii].tag_Node[1]=el[(jj+1)%3];
                edges_vec[ii].tag_Tri[0]=iel+1; // such that 0 is a triangle that does not exist
                edges_vec[ii].tag=tag_edge+1;
                tag_edge++;
            }
        }
    }
}

/*
 This function aims to initialize the attribut min_h_target of the structure Mesh.
 */
658
void mesh_initialized_min_h_target(Mesh *mesh){ 
Margaux Boxho's avatar
wip    
Margaux Boxho committed
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
    double X1, Y1, X2, Y2, X3, Y3;
    double a, b, c;
    double J, h_k, vol;

    mesh->min_h_target = (double*) calloc(mesh->n_elements, sizeof(double));
    for (int iel=0; iel<mesh->n_elements; iel++) {
        const unsigned int *el = &mesh->elements[iel*N_N];
        X1 = mesh->x[el[0]*3+0]; Y1 = mesh->x[el[0]*3+1];
        X2 = mesh->x[el[1]*3+0]; Y2 = mesh->x[el[1]*3+1];
        X3 = mesh->x[el[2]*3+0]; Y3 = mesh->x[el[2]*3+1];
        J = (X2-X1)*(Y3-Y1) - (X3-X1)*(Y2-Y1);
        vol = J/2.0;
        a = sqrt((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2));
        b = sqrt((X2-X3)*(X2-X3)+(Y2-Y3)*(Y2-Y3));
        c = sqrt((X3-X1)*(X3-X1)+(Y3-Y1)*(Y3-Y1));
        h_k = 0.5*a*b*c/vol;
675
676
        //double rrr = (rand() % 200)/100.0;
        //mesh->min_h_target[iel] = h_k*rrr;
677
        mesh->min_h_target[iel] = h_k;
678
        //mesh->min_h_target[iel] = 0.5*h_k*(iel==0) + 0.5*h_k*(iel==1) + 0.5*h_k*(iel==2) + 0.5*h_k*(iel==3);
Margaux Boxho's avatar
wip    
Margaux Boxho committed
679
680
    }
}
Margaux Boxho's avatar
Margaux Boxho committed
681

Margaux Boxho's avatar
Margaux Boxho committed
682
// ****************************************************************************************************************************************************************
683
// LOCAL MESH ADAPTATION USING RECURSION ==> KEEP AND MAINTAIN THE TREE STRUCTURE ITERATION AFTER ITERATION
Margaux Boxho's avatar
Margaux Boxho committed
684
// ****************************************************************************************************************************************************************
Margaux Boxho's avatar
Margaux Boxho committed
685

686
687
688
689
690
691
692
693
// INITIALIZATION OF THE TREE
void deep_tree_initialized(Mesh *mesh, Tree_cell *Tree, int size){
    for (int iel=0; iel<size; iel++) {
        Tree_cell *current = &Tree[iel];
        current->children = NULL;
        current->parent = NULL;
        current->num_children = 0;
        current->tag = iel;
Margaux Boxho's avatar
Margaux Boxho committed
694
        for (int ii=0; ii<3; ii++) {
695
696
697
            current->sub_nodes[ii]=-1;
            current->phys[ii]=-1;
            current->neighbours[ii]=-1;
Margaux Boxho's avatar
Margaux Boxho committed
698
        }
699
        
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
700
        const int *el = &mesh->elements[iel*N_N];
701
702
        for (int ii=0; ii<3; ii++) {
            current->nodes[ii] = el[ii];
703
        }
Margaux Boxho's avatar
Margaux Boxho committed
704
    }
705
}
706

707
708
709
void deep_tree_get_mapping(Tree_cell *Tree, Tree_cell **map, int size){
    for (int iel=0; iel<size; iel++) {
        deep_tree_go_to_leaves(&Tree[iel], map);
710
711
712
    }
}

713
714
715
716
717
718
void deep_tree_go_to_leaves(Tree_cell *current, Tree_cell **map){
    if (current->children==NULL) {
        map[current->tag] = current;
    }else{
        for (int ii=0; ii<current->num_children; ii++) {
            deep_tree_go_to_leaves(&current->children[ii], map);
719
        }
Margaux Boxho's avatar
Margaux Boxho committed
720
    }
Margaux Boxho's avatar
Margaux Boxho committed
721
722
}

723
724
void initialize_children(Tree_cell *current, int excess){
    for (int ii=0; ii<current->num_children+excess; ii++) {
725
        current->children[ii].parent=current;
726
        current->children[ii].children=NULL;
727
728
        current->children[ii].num_children = 0;
        current->children[ii].bool_ref = 0;
729
        current->children[ii].bool_def = 0;
730
731
732
        for (int jj=0; jj<3; jj++) {
            current->children[ii].neighbours[jj]=-1;
            current->children[ii].phys[jj]=-1;
733
            current->children[ii].sub_nodes[jj]=-1;
734
            current->children[ii].bdies[jj]=-1;
735
736
737
738
        }
    }
}

739
740
void deep_tree_check_for_refinement(Mesh *mesh, Tree_cell *current, double h_target, double h_min){
    double x[6] = {mesh->x[current->nodes[0]*3+0], mesh->x[current->nodes[0]*3+1], mesh->x[current->nodes[1]*3+0], mesh->x[current->nodes[1]*3+1], mesh->x[current->nodes[2]*3+0], mesh->x[current->nodes[2]*3+1]};
741
    double alpha = 1.5;
742
743
744
745
746
747
    double J = (x[2]-x[0])*(x[5]-x[1]) - (x[4]-x[0])*(x[3]-x[1]);
    double vol = J/2.0;
    double a = sqrt((x[0]-x[2])*(x[0]-x[2])+(x[1]-x[3])*(x[1]-x[3]));
    double b = sqrt((x[2]-x[4])*(x[2]-x[4])+(x[3]-x[5])*(x[3]-x[5]));
    double c = sqrt((x[4]-x[0])*(x[4]-x[0])+(x[5]-x[1])*(x[5]-x[1]));
    double h_k = 0.5*a*b*c/vol;
748
    if (h_k/1.4>h_target) {
749
        current->bool_ref = 1;
750
751
752
753
        current->bool_def = 0;
    }else if(h_k*alpha<h_target){
        current->bool_ref = 0;
        current->bool_def = 0; //1;
754
    }else{
755
        current->bool_def = 0;
756
        current->bool_ref = 0;
757
758
759
    }
}

760
void deep_tree_refinement(Mesh *mesh, Tree_cell *Tree, Tree_cell **map, int size, int h_min, int *n_nodes_new, int *n_elements_new, int *n_boundaries_new, int len_node, int *tab_node, int size_init_node, int len_elt, int *tab_elt, int size_init_elt, int *n_nodes_removed, int *n_elements_removed){
761
762
    int index = mesh->n_nodes;
    int elt = mesh->n_elements;
763
764
765
    int index_remove = 0;
    int elt_remove = 0;
    
766
    int ibd = 0;
767
    // NEED TO CARRY ABOUT THE NEIGHBOURS
768
    for (int kk=0; kk<mesh->n_interfaces; kk++) {
769
770
771
772
773
774
775
        int *interface = &mesh->interfaces[kk*4];
        int el0 = interface[0];
        int cl0 = interface[1];
        int el1 = interface[2];
        int cl1 = interface[3];
        
        if (el1==-1) {
776
777
            map[el0]->phys[cl0%3] = cl1;
            map[el0]->neighbours[cl0%3] = -1;
778
            map[el0]->bdies[cl0%3] = ibd;
779
            ibd++;
780
781
        }
        else {
782
            map[el0]->neighbours[cl0%3] = el1;
783
            map[el0]->bdies[cl0%3] = -1;
784
            map[el1]->neighbours[cl1%3] = el0;
785
            map[el1]->bdies[cl1%3] = -1;
786
        }
Margaux Boxho's avatar
Margaux Boxho committed
787
    }
788
    // ASK FOR REFINEMENT TO THE ESTIMATOR
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
789
    for (int iel=0; iel<mesh->n_elements; iel++) {
790
        deep_tree_check_for_refinement(mesh, map[iel], mesh->min_h_target[iel], h_min);
791
792
793
794
795
796
        Tree_cell *current = map[iel];
        if (current->children!=NULL && current->num_children==0) {
            free(current->children);
            current->children=NULL;
        }
        //printf("TAG %4d : (%4d, %4d, %4d)\n", current->tag, current->nodes[0], current->nodes[1], current->nodes[2]);
797
    }
798
    
799
800
801
    // CUT INTO 4 ELEMENTS
    for (int iel=0; iel<mesh->n_elements; iel++) {
        Tree_cell *current = map[iel];
802
        int bool_boundary = 0;
803
        if (current->parent!=NULL) {
804
            if (current->parent->num_children==4 && current->bool_ref==1 && bool_boundary==0) {
805
806
807
                //printf("====>CUT INTO 4\n");
                current->children = (Tree_cell *) malloc(4*sizeof(Tree_cell));
                current->num_children=4;
808
                initialize_children(current, 0);
809
                deep_tree_cut_into_4(current, map, &index, &elt, &ibd);
810
            }
811
        }else if(current->bool_ref==1 && bool_boundary==0){
812
813
814
            //printf("====>CUT INTO 4\n");
            current->children = (Tree_cell *) malloc(4*sizeof(Tree_cell));
            current->num_children=4;
815
            initialize_children(current, 0);
816
            deep_tree_cut_into_4(current, map, &index, &elt, &ibd);
817
        }
818
    }
Margaux Boxho's avatar
Margaux Boxho committed
819
820
821
    
    // TRANSFORM ELEMENT CUT INTO 2 OR 3 INTO ELEMENT CUT INTO 4
    printf("=============================> STARTING TRANSFORM ELEMENT <=============================\n");
822
    for (int iel=0; iel<mesh->n_elements; iel++) {
Margaux Boxho's avatar
Margaux Boxho committed
823
824
825
826
827
828
829
830
        Tree_cell *current = map[iel];
        if (current->children==NULL) {
            // WE ONLY LOOK FOR ELEMENT THAT HAVE A PARENT AND 1 OR 2 BROTHERS
            if (current->parent!=NULL) {
                // DEFINE BOOLEAN
                int bool_one_node = (current->sub_nodes[0]!=-1 && current->sub_nodes[1]==-1 && current->sub_nodes[2]==-1)||(current->sub_nodes[0]==-1 && current->sub_nodes[1]!=-1 && current->sub_nodes[2]==-1)||(current->sub_nodes[0]==-1 && current->sub_nodes[1]==-1 && current->sub_nodes[2]!=-1);
                int bool_two_nodes = (current->sub_nodes[0]!=-1 && current->sub_nodes[1]!=-1 && current->sub_nodes[2]==-1)||(current->sub_nodes[0]==-1 && current->sub_nodes[1]!=-1 && current->sub_nodes[2]!=-1)||(current->sub_nodes[0]!=-1 && current->sub_nodes[1]==-1 && current->sub_nodes[2]!=-1);
                
831
                if (current->parent->num_children==2 ) {
Margaux Boxho's avatar
Margaux Boxho committed
832
833
                    // TRANSFORM A 2-CUT INTO A 4-CUT
                    if (current->bool_ref || bool_one_node || bool_two_nodes) {
834
                        //printf("====>Transform 2 into 4\n");
Margaux Boxho's avatar
Margaux Boxho committed
835
836
837
838
839
                        deep_tree_transform2_into_4(current, map, &index, &elt, &ibd);
                    }
                }else if (current->parent->num_children==3){
                    // TRANSFORM A 3-CUT INTO A 4-CUT
                    if (current->bool_ref || bool_one_node || bool_two_nodes) {
840
                        //printf("====>Transform 3 into 4\n");
Margaux Boxho's avatar
Margaux Boxho committed
841
842
                        deep_tree_transform3_into_4(current, map, &index, &elt, &ibd);
                    }
843
844
845
846
847
                }
            }
        }
    }
    
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
    printf("=============================> STARTING COARSENING THE MESH <=============================\n");
    FILE *file1 = fopen("/Users/margauxboxho/migflow/testcases/my_local_drop/Mesh_Remove.dat", "w");
    if(file1 == NULL){
        printf("Error at opening file \n");
        exit(1);
    }
    for (int iel=0; iel<mesh->n_elements; iel++) {
        Tree_cell *current = map[iel];
        if (current->bool_def==1 && current->tag!=-1) {
            if (current->parent!=NULL) {
                for (int ii=0; ii<3; ii++) {
                    int tag_node_remove = current->parent->nodes[ii];
                    fprintf(file1, "%f, %f, ", mesh->x[tag_node_remove*3+0], mesh->x[tag_node_remove*3+1]);
                }
                fprintf(file1, "\n");
            }
            deep_tree_remove_elt(current, map, len_node, tab_node, size_init_node, len_elt, tab_elt, size_init_elt);
        }
    }
    fclose(file1);
    
    FILE *file2 = fopen("/Users/margauxboxho/migflow/testcases/my_local_drop/tab_node_before.dat", "w");
    if(file2 == NULL){
        printf("Error at opening file \n");
        exit(1);
    }
    for (int ii=0; ii<len_node; ii++) {
        fprintf(file2, "%4d, ", tab_node[ii]);
    }
    fclose(file2);
    
    printf("=============================> STARTING SMOOTHING THE REFINEMENT (0)<=============================\n");
    for (int iel=0; iel<mesh->n_elements; iel++) {
        Tree_cell *current = map[iel];
        if (current->parent!=NULL && current->tag!=-1 && current->children==NULL) {
            Tree_cell *parent = current->parent;
            int count_sub_nodes = 0;
            for (int ii=0; ii<3; ii++) {
                if (parent->sub_nodes[ii]!=-1) {
                    count_sub_nodes++;
                }
            }
            if (count_sub_nodes != parent->num_children-1 && len_node!=0) {
                if (count_sub_nodes==2 && parent->num_children==4) {
                    //printing_current(parent);
                    //printf("Transform 4->3\n");
                    deep_tree_transform4_into_3(parent, len_elt, tab_elt, size_init_elt);
                    //printing_current(parent);
                }else if (count_sub_nodes==1 && parent->num_children==4){
                    //printing_current(parent);
                    //printf("Transform 4->2\n");
                    deep_tree_transform4_into_2(parent, len_elt, tab_elt, size_init_elt);
                    //printing_current(parent);
                }else if (count_sub_nodes==0 && parent->num_children==4){
                    //printing_current(parent);
                    //printf("Transform 4->1\n");
                    deep_tree_transform4_into_1(parent, len_elt, tab_elt, size_init_elt);
                    //printing_current(parent);
                }else if (count_sub_nodes==1 && parent->num_children==3){
                    //printf("Transform 3->2\n");
                    deep_tree_transform3_into_2(parent, len_elt, tab_elt, size_init_elt);
                    //printing_current(parent);
                }else if (count_sub_nodes==0 && parent->num_children==3){
                    //printf("Transform 3->1\n");
                    deep_tree_transform3_into_1(parent, len_elt, tab_elt, size_init_elt);
                    //printing_current(parent);
                }else if (count_sub_nodes==0 && parent->num_children==2){
                    //printf("Transform 2->1\n");
                    deep_tree_transform2_into_1(parent, len_elt, tab_elt, size_init_elt);
                    //printing_current(parent);
                }else{
                    printf("ERROR IN SMOOTHING PROCEDURE (0)\n");
                    printing_current(parent);
                    exit(1);
                }
            }
        }
    }
    
Margaux Boxho's avatar
Margaux Boxho committed
927
    printf("=============================> STARTING SMOOTHING THE REFINEMENT (1)<=============================\n");
928
    // COMPLETE THE REFINEMENT BY CUTTING INTO 2 OR 3 DEENDING ON THE NUMBER OF SUB_NODES CONTAINED ON THE ELEMENT EDGES
Margaux Boxho's avatar
Margaux Boxho committed
929
930
931
    for (int iel=0; iel<mesh->n_elements; iel++) {
        Tree_cell *current = map[iel];
        if (current->children==NULL && current->parent!=NULL) {
932
            if (current->parent->num_children==2) {
Margaux Boxho's avatar
Margaux Boxho committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
                int tag = 0;
                for(int ii=0; ii<3; ii++){
                    if(current->parent->sub_nodes[ii]==-1 && current->parent->sub_nodes[(ii+1)%3]==-1){
                        tag = (ii+2)%3;
                    }
                }
                if (tag == 0) {
                    current->parent->sub_nodes[1] = current->parent->children[1].sub_nodes[1];
                    current->parent->sub_nodes[2] = current->parent->children[0].sub_nodes[2];
                }else if(tag == 1){
                    current->parent->sub_nodes[2] = current->parent->children[1].sub_nodes[1];
                    current->parent->sub_nodes[0] = current->parent->children[0].sub_nodes[2];
                }else{
                    current->parent->sub_nodes[0] = current->parent->children[1].sub_nodes[1];
                    current->parent->sub_nodes[1] = current->parent->children[0].sub_nodes[2];
                }
                int bool_two_nodes = (current->parent->sub_nodes[0]!=-1 && current->parent->sub_nodes[1]!=-1 && current->parent->sub_nodes[2]==-1)||(current->parent->sub_nodes[0]==-1 && current->parent->sub_nodes[1]!=-1 && current->parent->sub_nodes[2]!=-1)||(current->parent->sub_nodes[0]!=-1 && current->parent->sub_nodes[1]==-1 && current->parent->sub_nodes[2]!=-1);
                int bool_three_nodes = (current->parent->sub_nodes[0]!=-1 && current->parent->sub_nodes[1]!=-1 && current->parent->sub_nodes[2]!=-1);
                int save_tag[2] = {current->parent->children[0].tag, current->parent->children[1].tag};
                
                if (bool_two_nodes) {
                    current->parent->num_children = 3;
                    initialize_children(current->parent, 1);
                    deep_tree_cut_into_3(current->parent, &elt);
                    elt-=2;
                    for (int ii=0; ii<2; ii++) {
                        current->parent->children[ii].tag = save_tag[ii];
                    }
                    current->parent->children[2].tag = elt;
                    elt+=1;
                    
                }else if(bool_three_nodes){
                    current->parent->num_children = 4;
                    initialize_children(current->parent, 0);
                    deep_tree_cut_into_4(current->parent, map, &index, &elt, &ibd);
                    elt-=3;
                    for (int ii=0; ii<2; ii++) {
                        current->parent->children[ii].tag = save_tag[ii];
                    }
                    current->parent->children[2].tag = elt; elt+=1;
                    current->parent->children[3].tag = elt; elt+=1;
                    
975
976
                }
            }else if (current->parent->num_children==3){
Margaux Boxho's avatar
Margaux Boxho committed
977
978
979
980
981
982
983
                int tag = 0;
                for(int ii=0; ii<3; ii++){
                    if(current->parent->sub_nodes[ii]==-1){
                        tag = ii;
                    }
                }
                current->parent->sub_nodes[tag] = current->parent->children[0].sub_nodes[2];
984
                
Margaux Boxho's avatar
Margaux Boxho committed
985
986
987
988
989
990
991
992
993
994
995
                int bool_three_nodes = (current->parent->sub_nodes[0]!=-1 && current->parent->sub_nodes[1]!=-1 && current->parent->sub_nodes[2]!=-1);
                int save_tag[3] = {current->parent->children[0].tag, current->parent->children[1].tag, current->parent->children[2].tag};
                if (bool_three_nodes) {
                    current->parent->num_children = 4;
                    initialize_children(current->parent, 0);
                    deep_tree_cut_into_4(current->parent, map, &index, &elt, &ibd);
                    elt-=3;
                    for (int ii=0; ii<3; ii++) {
                        current->parent->children[ii].tag = save_tag[ii];
                    }
                    current->parent->children[3].tag = elt; elt+=1;
996
                }
997
998
999
            }
        }
    }
1000
    
Margaux Boxho's avatar
Margaux Boxho committed
1001
1002
1003
1004
    printf("=============================> STARTING SMOOTHING THE REFINEMENT (2)<=============================\n");
    // COMPLETE THE REFINEMENT BY CUTTING INTO 2 OR 3 DEENDING ON THE NUMBER OF SUB_NODES CONTAINED ON THE ELEMENT EDGES
    for (int iel=0; iel<mesh->n_elements; iel++) {
        Tree_cell *current = map[iel];
1005
1006
1007
1008
1009
        int num_child = 0;
        if (current->parent!=NULL) {
            num_child = current->parent->num_children;
        }
        if ((current->children==NULL && current->parent==NULL) || (current->children==NULL && num_child==4)) {
Margaux Boxho's avatar
Margaux Boxho committed
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
            int bool_one_node = (current->sub_nodes[0]!=-1 && current->sub_nodes[1]==-1 && current->sub_nodes[2]==-1)||(current->sub_nodes[0]==-1 && current->sub_nodes[1]!=-1 && current->sub_nodes[2]==-1)||(current->sub_nodes[0]==-1 && current->sub_nodes[1]==-1 && current->sub_nodes[2]!=-1);
            int bool_two_nodes = (current->sub_nodes[0]!=-1 && current->sub_nodes[1]!=-1 && current->sub_nodes[2]==-1)||(current->sub_nodes[0]==-1 && current->sub_nodes[1]!=-1 && current->sub_nodes[2]!=-1)||(current->sub_nodes[0]!=-1 && current->sub_nodes[1]==-1 && current->sub_nodes[2]!=-1);
            int bool_three_nodes = (current->sub_nodes[0]!=-1 && current->sub_nodes[1]!=-1 && current->sub_nodes[2]!=-1);
            
            if (bool_one_node) {
                //printf("====>CUT INTO 2\n");
                current->children = (Tree_cell*) malloc(4*sizeof(Tree_cell));
                current->num_children = 2;
                initialize_children(current, 2);
                deep_tree_cut_into_2(current, &elt);
            }else if(bool_two_nodes){
                //printf("====>CUT INTO 3\n");
                current->children = (Tree_cell*) malloc(4*sizeof(Tree_cell));
                current->num_children = 3;
                initialize_children(current, 1);
                deep_tree_cut_into_3(current, &elt);
            }else if(bool_three_nodes){
                //printf("====>CUT INTO 4\n");
                current->children = (Tree_cell*) malloc(4*sizeof(Tree_cell));
                current->num_children = 4;
                initialize_children(current, 0);
                deep_tree_cut_into_4(current, map, &index, &elt, &ibd);
            }
1033
1034
        }
    }
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
    
    
    /*FILE *file3 = fopen("/Users/margauxboxho/migflow/testcases/my_local_drop/tab_node_after.dat", "w");
    if(file3 == NULL){
        printf("Error at opening file \n");
        exit(1);
    }
    for (int ii=0; ii<len_node; ii++) {
        fprintf(file3, "%4d, ", tab_node[ii]);
    }
    fclose(file3);*/
    
    deep_tree_correspondence(tab_node, len_node, size_init_node, tab_elt, len_elt, size_init_elt, &index_remove, &elt_remove);
    
    /*FILE *file4 = fopen("/Users/margauxboxho/migflow/testcases/my_local_drop/tab_node_after_after.dat", "w");
    if(file4 == NULL){
        printf("Error at opening file \n");
        exit(1);
    }
    for (int ii=0; ii<len_node; ii++) {
        fprintf(file4, "%4d, ", tab_node[ii]);
    }
    fclose(file4);*/
    
    *n_nodes_new = index-index_remove;
    *n_elements_new = elt-elt_remove;
    *n_boundaries_new = ibd; // we do not touch to the boundaries
    
    printf("Number of nodes to be removed %4d\n", index_remove);
    printf("Number of elements to be removed %4d\n", elt_remove);

    *n_nodes_removed = index_remove;
    *n_elements_removed = elt_remove;
1068
}
Margaux Boxho's avatar
Margaux Boxho committed
1069

1070
void deep_tree_travel(Mesh *mesh, Tree_cell *Tree, Tree_cell **map, Tree_cell **map_new, double *x_new, int *elements_new, int *boundaries_new, int *boundary_tags_new, int n_nodes_new, int n_elements_new, int n_boundaries_new, int size, int len_node, int *tab_node, int size_init_node, int len_elt, int *tab_elt, int size_init_elt, int n_nodes_removed, int n_elements_removed){
1071
1072
    printf("====> CREATE THE DATA STRUCTURE <====\n");
    int ibd = 0;
1073
    for (int iel=0; iel<size; iel++) {
1074
        Tree_cell *current = &Tree[iel];
1075
        deep_tree_create_data_structure(mesh, current, map_new, x_new, elements_new, boundaries_new, boundary_tags_new, n_nodes_new, n_elements_new, len_node, tab_node, size_init_node, len_elt, tab_elt, size_init_elt, n_nodes_removed, n_elements_removed);
1076
1077
1078
    }
     for (int iel=0; iel<mesh->n_elements; iel++) {
         map[iel]->bool_boundary=1;
1079
    }
1080
    
1081
    printf("====> CREATE THE BOUNDARY STRUCTURE <====\n");
1082
    int count = 0;
1083
1084
1085
1086
1087
1088
1089
1090
1091
    for (int iel=0; iel<n_elements_new; iel++) {
        Tree_cell *current = map_new[iel];
        for (int ii=0; ii<3; ii++) {
            int ibd = current->bdies[ii];
            if (ibd>-1) {
                count++;
                boundaries_new[2*ibd+0] = current->nodes[ii];
                boundaries_new[2*ibd+1] = current->nodes[(ii+1)%3];
                boundary_tags_new[ibd] = current->phys[ii];
1092
1093
1094
            }
        }
    }
1095
    
1096
    printf("n_boundaries_new = %d \n", n_boundaries_new);
1097
1098
1099
1100
1101
1102
1103
    
    if (n_nodes_removed!=0 && n_elements_removed!=0) {
        for (int iel=0; iel<size; iel++) {
            Tree_cell *current = &Tree[iel];
            deep_tree_travel_to_get_new_correspondence(current, len_node, tab_node, size_init_node, len_elt, tab_elt, size_init_elt, n_nodes_removed, n_elements_removed);
        }
    }
1104
1105
}

1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
void deep_tree_create_data_structure(Mesh *mesh, Tree_cell *current, Tree_cell **map_new, double *x_new, int *elements_new, int *boundaries_new, int *boundary_tags_new, int n_nodes_new, int n_elements_new, int len_node, int *tab_node, int size_init_node, int len_elt, int *tab_elt, int size_init_elt, int n_nodes_removed, int n_elements_removed){
    
    if (current->children==NULL || current->num_children==0) { // the second statement will be used when elements are removed
        
        int save_old_nodes[3];
        if (current->parent!=NULL) {
            if (current->parent->num_children==4){
                for (int ii=0; ii<3; ii++) {
                    save_old_nodes[ii] = current->parent->nodes[ii];
                }
            }
        }
        
        // Get the new tag for the nodes, sub_nodes, neighbours and the current element
        if (len_node!=0 && len_elt!=0 && n_nodes_removed!=0 && n_elements_removed!=0) {
            deep_tree_map_for_remove(current, len_node, tab_node, size_init_node, len_elt, tab_elt, size_init_elt, n_nodes_removed, n_elements_removed, 1);
        }
        
1124
        if (current->tag>n_elements_new) {
Margaux Boxho's avatar
Margaux Boxho committed
1125
            printf("ERROR IN THE NODE TAG\n");
1126
            printing_current(current);
1127
            exit(1);
1128
        }
Margaux Boxho's avatar
Margaux Boxho committed
1129
1130
1131
1132
1133
1134
        
        if (current->tag == -1){
            printf("ERROR THE CURRENT ELEMENT HAS A TAG -1 !!!!\n");
            printing_current(current);
            printing_current(current->parent);
            exit(1);
1135
        }
1136
        
Margaux Boxho's avatar
Margaux Boxho committed
1137
        current->bool_boundary = 1;
1138
        map_new[current->tag] = current;
1139
        
1140
1141
1142
        elements_new[current->tag*3+0] = current->nodes[0];
        elements_new[current->tag*3+1] = current->nodes[1];
        elements_new[current->tag*3+2] = current->nodes[2];