~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to preprocessor/pre-post/ecs_pre_gmsh.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*============================================================================
 
2
 *  Définition de la fonction
 
3
 *   de lecture d'un fichier de maillage au format Gmsh
 
4
 *============================================================================*/
 
5
 
 
6
/*
 
7
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
8
 
 
9
  Copyright (C) 1998-2011 EDF S.A.
 
10
 
 
11
  This program is free software; you can redistribute it and/or modify it under
 
12
  the terms of the GNU General Public License as published by the Free Software
 
13
  Foundation; either version 2 of the License, or (at your option) any later
 
14
  version.
 
15
 
 
16
  This program is distributed in the hope that it will be useful, but WITHOUT
 
17
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
18
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
19
  details.
 
20
 
 
21
  You should have received a copy of the GNU General Public License along with
 
22
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
23
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
24
*/
 
25
 
 
26
/*----------------------------------------------------------------------------*/
 
27
 
 
28
/*----------------------------------------------------------------------------
 
29
 *  Fichiers `include' librairie standard C
 
30
 *----------------------------------------------------------------------------*/
 
31
 
 
32
#include <assert.h>
 
33
#include <errno.h>
 
34
#include <stdlib.h>
 
35
#include <string.h>
 
36
 
 
37
 
 
38
/*----------------------------------------------------------------------------
 
39
 *  Fichiers `include' visibles du  paquetage global "Utilitaire"
 
40
 *----------------------------------------------------------------------------*/
 
41
 
 
42
#include "ecs_def.h"
 
43
#include "ecs_elt_typ_liste.h"
 
44
#include "ecs_file.h"
 
45
#include "ecs_mem.h"
 
46
#include "ecs_tab.h"
 
47
 
 
48
 
 
49
/*----------------------------------------------------------------------------
 
50
 *  Fichiers `include' visibles des paquetages visibles
 
51
 *----------------------------------------------------------------------------*/
 
52
 
 
53
#include "ecs_descr.h"
 
54
#include "ecs_maillage.h"
 
55
#include "ecs_maillage_priv.h"
 
56
 
 
57
 
 
58
/*----------------------------------------------------------------------------
 
59
 *  Fichiers `include' visibles du  paquetage courant
 
60
 *----------------------------------------------------------------------------*/
 
61
 
 
62
#include "ecs_maillage_pre.h"
 
63
 
 
64
 
 
65
/*----------------------------------------------------------------------------
 
66
 *  Fichier  `include' du  paquetage courant associé au fichier courant
 
67
 *----------------------------------------------------------------------------*/
 
68
 
 
69
#include "ecs_pre_gmsh.h"
 
70
 
 
71
 
 
72
/*----------------------------------------------------------------------------
 
73
 *  Fichiers `include' privés   du  paquetage courant
 
74
 *----------------------------------------------------------------------------*/
 
75
 
 
76
 
 
77
/*============================================================================
 
78
 *  Définitions de paramètres-macros
 
79
 *============================================================================*/
 
80
 
 
81
#define ECS_GMSH_NBR_TYP_ELT        15 /* Nombre de types d'éléments Gmsh */
 
82
#define ECS_GMSH_NBR_MAX_SOM        27 /* Nombre max de noeuds par élément
 
83
                                          (27 pour hexaèdre parabolique) */
 
84
 
 
85
 
 
86
/* Valeurs associées aux dimensionnements pour la lecture des lignes */
 
87
 
 
88
#define ECS_LOC_LNG_MAX_CHAINE_GMSH 514 /* Dimension des chaînes de réception
 
89
                                           des lignes lues */
 
90
 
 
91
/*============================================================================
 
92
 *                          Définitions de types
 
93
 *============================================================================*/
 
94
 
 
95
typedef enum {
 
96
  GMSH_SEG2 = 1, /*  1 */
 
97
  GMSH_TRIA3,    /*  2 */
 
98
  GMSH_QUAD4,    /*  3 */
 
99
  GMSH_TETRA4,   /*  4 */
 
100
  GMSH_HEXA8,    /*  5 */
 
101
  GMSH_PENTA6,   /*  6 */
 
102
  GMSH_PYRA5,    /*  7 */
 
103
  GMSH_SEG3,     /*  8 */
 
104
  GMSH_TRIA6,    /*  9 */
 
105
  GMSH_QUAD9,    /* 10 */
 
106
  GMSH_TETRA10,  /* 11 */
 
107
  GMSH_HEXA27,   /* 12 */
 
108
  GMSH_PENTA18,  /* 13 */
 
109
  GMSH_PYRA14,   /* 14 */
 
110
  GMSH_POINT1    /* 15 */
 
111
} ecs_gmsh_elt_typ_t;
 
112
 
 
113
typedef struct {
 
114
 
 
115
  ecs_gmsh_elt_typ_t     gmsh_typ  ; /* Type Gmsh de l'élément */
 
116
  ecs_elt_typ_t          ecs_typ   ; /* Type ECS de l'élément */
 
117
  ecs_int_t              nbr_som   ; /* Nombre de sommets */
 
118
                                      /* Liste des numéros de sommet ECS */
 
119
  ecs_int_t              num_som[ECS_GMSH_NBR_MAX_SOM];
 
120
 
 
121
} ecs_gmsh_elt_t;
 
122
 
 
123
/*============================================================================
 
124
 *  Définitions de variables globales statiques
 
125
 *============================================================================*/
 
126
 
 
127
const ecs_gmsh_elt_t ecs_gmsh_elt_liste_c[ECS_GMSH_NBR_TYP_ELT] = {
 
128
  {                                /* 1 */
 
129
    GMSH_SEG2 ,
 
130
    ECS_ELT_TYP_NUL ,
 
131
    2 ,
 
132
    { 0 }
 
133
  } ,
 
134
  {                                /* 2 */
 
135
    GMSH_TRIA3 ,
 
136
    ECS_ELT_TYP_FAC_TRIA ,
 
137
    3 ,
 
138
    { 1, 2, 3 }
 
139
  } ,
 
140
  {                                /* 3 */
 
141
    GMSH_QUAD4 ,
 
142
    ECS_ELT_TYP_FAC_QUAD ,
 
143
    4 ,
 
144
    { 1, 2, 3, 4 }
 
145
  } ,
 
146
  {                                /* 4 */
 
147
    GMSH_TETRA4 ,
 
148
    ECS_ELT_TYP_CEL_TETRA ,
 
149
    4 ,
 
150
    { 1, 2, 3, 4 }
 
151
  } ,
 
152
  {                                /* 5 */
 
153
    GMSH_HEXA8 ,
 
154
    ECS_ELT_TYP_CEL_HEXA ,
 
155
    8 ,
 
156
    { 1, 2, 3, 4, 5, 6, 7, 8 } ,
 
157
  } ,
 
158
  {                                /* 6 */
 
159
    GMSH_PENTA6 ,
 
160
    ECS_ELT_TYP_CEL_PRISM ,
 
161
    6 ,
 
162
    { 1, 2, 3, 4, 5, 6 }
 
163
  } ,
 
164
  {                                /* 7 */
 
165
    GMSH_PYRA5 ,
 
166
    ECS_ELT_TYP_CEL_PYRAM ,
 
167
    5 ,
 
168
    { 1, 2, 3, 4, 5 }
 
169
  } ,
 
170
  {                                /* 8 */
 
171
    GMSH_SEG3 ,
 
172
    ECS_ELT_TYP_NUL ,
 
173
    3 ,
 
174
    { 0 }
 
175
  } ,
 
176
  {                                /* 9 */
 
177
    GMSH_TRIA6 ,
 
178
    ECS_ELT_TYP_FAC_TRIA ,
 
179
    6 ,
 
180
    { 1, 2, 3 }
 
181
  } ,
 
182
  {                               /* 10 */
 
183
    GMSH_QUAD9 ,
 
184
    ECS_ELT_TYP_FAC_QUAD ,
 
185
    9 ,
 
186
    { 1, 2, 3, 4 }
 
187
  } ,
 
188
  {                               /* 11 */
 
189
    GMSH_TETRA10 ,
 
190
    ECS_ELT_TYP_CEL_TETRA ,
 
191
    10 ,
 
192
    { 1, 2, 3, 4 }
 
193
  } ,
 
194
  {                               /* 12 */
 
195
    GMSH_HEXA27 ,
 
196
    ECS_ELT_TYP_CEL_HEXA ,
 
197
    27 ,
 
198
    { 1, 2, 3, 4, 5, 6, 7, 8 } ,
 
199
  } ,
 
200
  {                               /* 13 */
 
201
    GMSH_PENTA18 ,
 
202
    ECS_ELT_TYP_CEL_PRISM ,
 
203
    18 ,
 
204
    { 1, 2, 3, 4, 5, 6 }
 
205
  } ,
 
206
  {                               /* 14 */
 
207
    GMSH_PYRA14 ,
 
208
    ECS_ELT_TYP_CEL_PYRAM ,
 
209
    14 ,
 
210
    { 1, 2, 3, 4, 5 }
 
211
  } ,
 
212
  {                               /* 15 */
 
213
    GMSH_POINT1 ,
 
214
    ECS_ELT_TYP_NUL ,
 
215
    1 ,
 
216
    { 0 }
 
217
  }
 
218
};
 
219
 
 
220
/*============================================================================
 
221
 *  Fonctions privées
 
222
 *============================================================================*/
 
223
 
 
224
/*----------------------------------------------------------------------------
 
225
 *  Lecture et vérification de la version du format (pour version 2.0)
 
226
 *----------------------------------------------------------------------------*/
 
227
 
 
228
static void
 
229
ecs_loc_pre_gmsh__lit_vers_format(ecs_file_t     *fic_maillage,
 
230
                                  int            *version,
 
231
                                  int            *type,
 
232
                                  int            *num_ligne)
 
233
{
 
234
  char  chaine[ECS_LOC_LNG_MAX_CHAINE_GMSH];
 
235
  int   retour;
 
236
  int   taille_coo;
 
237
  float fversion;
 
238
 
 
239
  /* Décodage de la chaine de version */
 
240
 
 
241
  ecs_file_gets(chaine, ECS_LOC_LNG_MAX_CHAINE_GMSH, fic_maillage, num_ligne);
 
242
 
 
243
  retour = sscanf(chaine,"%f %d %d", &fversion, type, &taille_coo);
 
244
 
 
245
  if (retour != 3)
 
246
    ecs_error(__FILE__, __LINE__, 0,
 
247
              _("The Gmsh version specification of file\n"
 
248
                "\"%s\" is invalid or unreadable.)"));
 
249
 
 
250
  if (*type != 0 && *type != 1)
 
251
    ecs_error(__FILE__, __LINE__, 0,
 
252
              _("The Gmsh type specification of file\n"
 
253
                "\"%s\" is neither 0 (text file) nor 1 (binary file).\n"
 
254
                "This case is currently not handled."));
 
255
 
 
256
  printf(_("  Gmsh format version:     %2.1f\n"
 
257
           "  Size given for real numbers: %d\n\n"),
 
258
         fversion, taille_coo);
 
259
 
 
260
  *version = (int) fversion;
 
261
 
 
262
  if (*type == 1) {
 
263
 
 
264
    int   un;
 
265
 
 
266
    ecs_file_set_type(fic_maillage, ECS_FILE_TYPE_BINARY);
 
267
    ecs_file_read(&un, 4, 1, fic_maillage);
 
268
    if (un != 1)
 
269
      ecs_file_set_swap_endian(fic_maillage, 1);
 
270
 
 
271
    ecs_file_set_type(fic_maillage, ECS_FILE_TYPE_TEXT);
 
272
    ecs_file_gets(chaine, 2, fic_maillage, num_ligne);
 
273
 
 
274
  }
 
275
 
 
276
  /* Ligne de fin de rubrique */
 
277
 
 
278
  ecs_file_gets(chaine, ECS_LOC_LNG_MAX_CHAINE_GMSH, fic_maillage, num_ligne);
 
279
 
 
280
  if (strncmp(chaine, "$EndMeshFormat", strlen("$EndMeshFormat")) != 0)
 
281
    ecs_error(__FILE__, __LINE__, 0,
 
282
              _("The Gmsh format version specification end for file\n"
 
283
                "\"%s\" is not present at the expected place.)"));
 
284
}
 
285
 
 
286
/*----------------------------------------------------------------------------
 
287
 *  Lecture des coordonnées des noeuds
 
288
 *----------------------------------------------------------------------------*/
 
289
 
 
290
static void
 
291
ecs_loc_pre_gmsh__lit_nodes(ecs_maillage_t   *maillage,
 
292
                            ecs_file_t       *fic_maillage,
 
293
                            int               type_fmt_gmsh,
 
294
                            int              *num_ligne,
 
295
                            ecs_int_t       **som_val_label)
 
296
{
 
297
  char   chaine[ECS_LOC_LNG_MAX_CHAINE_GMSH];
 
298
  int    retour;
 
299
 
 
300
  /* Variables Gmsh lues */
 
301
 
 
302
  long         label;
 
303
  double       coord[3];
 
304
  ecs_int_t    nbr_nod;
 
305
 
 
306
  ecs_int_t    icoo;
 
307
 
 
308
  /* Stockage avant transfert */
 
309
 
 
310
  ecs_int_t    ind_nod = 0;
 
311
 
 
312
  ecs_coord_t * som_val_coord;
 
313
 
 
314
  /*xxxxxxxxxxxxxxxxxxxxxxxxxxx Instructions xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
 
315
 
 
316
  /* Initialisations */
 
317
  /*=================*/
 
318
 
 
319
  /* Décodage du nombre de noeuds */
 
320
 
 
321
  ecs_file_gets(chaine, ECS_LOC_LNG_MAX_CHAINE_GMSH, fic_maillage, num_ligne);
 
322
 
 
323
  nbr_nod = (ecs_int_t)(atol(chaine));
 
324
 
 
325
  /* Allocation mémoire associée */
 
326
 
 
327
  ECS_MALLOC(som_val_coord, nbr_nod * 3, ecs_coord_t);
 
328
  ECS_MALLOC(*som_val_label, nbr_nod, ecs_int_t);
 
329
 
 
330
  /* Boucle de lecture des noeuds */
 
331
  /*==============================*/
 
332
 
 
333
  if (type_fmt_gmsh == 0) {
 
334
 
 
335
    for (ind_nod = 0; ind_nod < nbr_nod; ind_nod++) {
 
336
 
 
337
      ecs_file_gets(chaine,
 
338
                    ECS_LOC_LNG_MAX_CHAINE_GMSH,
 
339
                    fic_maillage,
 
340
                    num_ligne);
 
341
 
 
342
      retour = sscanf(chaine,"%ld %lg %lg %lg",
 
343
                      &label, coord, coord+1, coord+2);
 
344
 
 
345
      if (retour != 4)
 
346
        ecs_error(__FILE__, __LINE__, 0,
 
347
                  _("Error decoding line %ld of file\n\"%s\" :\n"
 
348
                    "The description of point <%ld> was expected in the form "
 
349
                    "\"label x y z\"."),
 
350
                  (long)(*num_ligne), ecs_file_get_name(fic_maillage),
 
351
                  (long)(ind_nod+1));
 
352
 
 
353
      /* Étiquette du noeud lu */
 
354
 
 
355
      (*som_val_label)[ind_nod] = (ecs_int_t)label;
 
356
 
 
357
      /* Coordonnées du noeud lu */
 
358
 
 
359
      for (icoo = 0; icoo < 3; icoo++)
 
360
        som_val_coord[ind_nod * 3 + icoo] = (ecs_coord_t)(coord[icoo]);
 
361
 
 
362
    }
 
363
 
 
364
  }
 
365
  else {
 
366
 
 
367
    ecs_file_set_type(fic_maillage, ECS_FILE_TYPE_BINARY);
 
368
 
 
369
    for (ind_nod = 0; ind_nod < nbr_nod; ind_nod++) {
 
370
 
 
371
      double xyz[3];
 
372
 
 
373
      ecs_file_read(&((*som_val_label)[ind_nod]), sizeof(int), 1, fic_maillage);
 
374
      ecs_file_read(&xyz, sizeof(double), 3, fic_maillage);
 
375
 
 
376
      for (icoo = 0; icoo < 3; icoo++)
 
377
        som_val_coord[ind_nod * 3 + icoo] = xyz[icoo];
 
378
 
 
379
    }
 
380
 
 
381
    ecs_file_set_type(fic_maillage, ECS_FILE_TYPE_TEXT);
 
382
 
 
383
    ecs_file_gets(chaine, 2, fic_maillage, num_ligne);
 
384
 
 
385
  }
 
386
 
 
387
  /* Ligne de fin de rubrique */
 
388
 
 
389
  ecs_file_gets(chaine, ECS_LOC_LNG_MAX_CHAINE_GMSH, fic_maillage, num_ligne);
 
390
 
 
391
  if (   (strncmp(chaine, "$EndNodes", strlen("$EndNodes")) != 0)
 
392
      && (strncmp(chaine, "$ENDNOD", strlen("$ENDNOD")) != 0))
 
393
    ecs_error(__FILE__, __LINE__, 0,
 
394
              _("The end of the points list specification of file\n"
 
395
                "\"%s\" is not present at the expected place.)"),
 
396
              ecs_file_get_name(fic_maillage));
 
397
 
 
398
  /* Transfert des valeurs lues dans la structure d'entité de maillage */
 
399
  /*===================================================================*/
 
400
 
 
401
  ecs_maillage_pre__cree_som(maillage,
 
402
                            nbr_nod,
 
403
                            som_val_coord);
 
404
}
 
405
 
 
406
/*----------------------------------------------------------------------------
 
407
 *  Lecture de la table de connectivité
 
408
 *----------------------------------------------------------------------------*/
 
409
 
 
410
static void
 
411
ecs_loc_pre_gmsh__lit_elements(ecs_maillage_t  *maillage,
 
412
                               ecs_file_t      *fic_maillage,
 
413
                               int              version_fmt_gmsh,
 
414
                               int              type_fmt_gmsh,
 
415
                               int             *num_ligne,
 
416
                               ecs_int_t        nbr_som,
 
417
                               ecs_int_t      **som_val_label)
 
418
{
 
419
  char        chaine[ECS_LOC_LNG_MAX_CHAINE_GMSH];
 
420
  char       *ssch;
 
421
 
 
422
  /* Variables Gmsh lues */
 
423
 
 
424
  long        label;
 
425
  ecs_int_t   type_ecs;
 
426
  ecs_int_t   coul_elt;
 
427
  ecs_int_t   ind_elt;
 
428
  ecs_int_t   nbr_elt;
 
429
  ecs_int_t   nbr_elt_lus;
 
430
  ecs_int_t   nbr_elt_tot;
 
431
  ecs_int_t   ind_nod_elt;
 
432
  ecs_int_t   ind_som_elt;
 
433
  ecs_int_t   nbr_som_elt;
 
434
  ecs_int_t   ind_tag_elt;
 
435
  ecs_int_t   nbr_tag_elt;
 
436
 
 
437
  ecs_int_t   header[3];
 
438
  ecs_int_t   data[100];
 
439
 
 
440
  int         type_gmsh;
 
441
  ecs_int_t   nbr_nod_elt_gmsh;
 
442
  ecs_int_t   num_nod_elt_gmsh[ECS_GMSH_NBR_MAX_SOM];
 
443
  ecs_int_t   ent_num;
 
444
 
 
445
  ecs_int_t   icoul;
 
446
  ecs_int_t   ient;
 
447
 
 
448
  ecs_int_t    cpt_coul_ent[ECS_N_ENTMAIL]; /* Compteur de couleurs         */
 
449
  ecs_int_t   *val_coul_ent[ECS_N_ENTMAIL]; /* Tableau valeurs des couleurs */
 
450
  ecs_size_t  *cpt_elt_coul_ent[ECS_N_ENTMAIL];
 
451
 
 
452
  /* Stockage avant transfert */
 
453
  /*--------------------------*/
 
454
 
 
455
  size_t       cpt_elt_ent        [ECS_N_ENTMAIL]; /* Nombre d'elems/entite */
 
456
 
 
457
  ecs_size_t  *elt_pos_som_ent    [ECS_N_ENTMAIL]; /* Positions numeros som */
 
458
  ecs_int_t   *elt_val_som_ent    [ECS_N_ENTMAIL]; /* Numeros des sommets   */
 
459
  ecs_int_t   *elt_val_color_ent  [ECS_N_ENTMAIL]; /* Couleurs              */
 
460
 
 
461
  ecs_int_t cpt_are   = 0;
 
462
  ecs_int_t cpt_point = 0;
 
463
 
 
464
  bool       ligne_decodee = true;
 
465
 
 
466
  /*xxxxxxxxxxxxxxxxxxxxxxxxxxx Instructions xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
 
467
 
 
468
  /* Initialisations */
 
469
  /*=================*/
 
470
 
 
471
  /* Décodage du nombre d'éléments (toutes dimensions confondues */
 
472
 
 
473
  ecs_file_gets(chaine, ECS_LOC_LNG_MAX_CHAINE_GMSH, fic_maillage, num_ligne);
 
474
 
 
475
  nbr_elt = (ecs_int_t)(atol(chaine));
 
476
 
 
477
  /* Allocations des tableaux locaux */
 
478
  /*=================================*/
 
479
 
 
480
  /* Attention au decalage de `1' !!!         */
 
481
  /* On n'alloue pas les tableaux locaux pour */
 
482
  /* `ECS_ENTMAIL_DEB = ECS_ENTMAIL_SOM'      */
 
483
 
 
484
  for (ient = ECS_ENTMAIL_FAC; ient < ECS_N_ENTMAIL; ient++) {
 
485
 
 
486
    cpt_elt_ent        [ient] = 0;
 
487
 
 
488
    cpt_coul_ent       [ient] = 0   ;
 
489
    cpt_elt_coul_ent   [ient] = NULL;
 
490
    val_coul_ent       [ient] = NULL;
 
491
 
 
492
  }
 
493
 
 
494
  /*
 
495
    On surdimensionne les tableaux de lecture des éléments; on les
 
496
    redimensionnera à la fin.
 
497
  */
 
498
 
 
499
  for (ient = ECS_ENTMAIL_FAC; ient < ECS_N_ENTMAIL; ient++) {
 
500
 
 
501
    ECS_MALLOC(elt_val_color_ent[ient],   nbr_elt, ecs_int_t);
 
502
    ECS_MALLOC(elt_pos_som_ent[ient],     nbr_elt, ecs_size_t);
 
503
 
 
504
    elt_pos_som_ent[ient][0] = 1;
 
505
  }
 
506
 
 
507
  /*
 
508
    Plus "gros" élément linéaire disponible :
 
509
    - quadrangle (4 sommets) pour les faces
 
510
    - hexaèdre (8 sommets) pour les cellules.
 
511
  */
 
512
 
 
513
  ECS_MALLOC(elt_val_som_ent[ECS_ENTMAIL_FAC], nbr_elt * 4, ecs_int_t);
 
514
  ECS_MALLOC(elt_val_som_ent[ECS_ENTMAIL_CEL], nbr_elt * 8, ecs_int_t);
 
515
 
 
516
 
 
517
  /* Boucle de lecture des éléments */
 
518
  /*================================*/
 
519
 
 
520
  if (type_fmt_gmsh == 0) {
 
521
 
 
522
    for (ind_elt = 0; ind_elt < nbr_elt; ind_elt++) {
 
523
 
 
524
      if (ligne_decodee == false)
 
525
        break;
 
526
 
 
527
      ligne_decodee = false;
 
528
 
 
529
      ecs_file_gets(chaine, ECS_LOC_LNG_MAX_CHAINE_GMSH,
 
530
                    fic_maillage, num_ligne);
 
531
 
 
532
      /* Au format Gmsh 1.0, pour chaque élément, on a les entiers suivants :
 
533
         label type num_ent_phys num_ent_elem nb_sommets <liste_sommets> */
 
534
 
 
535
      /* Au format Gmsh 2.0, pour chaque élément, on a les entiers suivants :
 
536
         label type nb_tags <tag> <liste_sommets> */
 
537
 
 
538
      /* Lecture du label de l'élément courant */
 
539
 
 
540
      ssch = strtok(chaine, " ");
 
541
      if (ssch == NULL)
 
542
        break;
 
543
 
 
544
      label = atol(ssch);
 
545
 
 
546
      /* Lecture du type de l'élément courant */
 
547
 
 
548
      ssch   = strtok(NULL, " ");
 
549
      if (ssch == NULL)
 
550
        break;
 
551
 
 
552
      type_gmsh = atoi(ssch);
 
553
 
 
554
      if (type_gmsh < (int)GMSH_SEG2 || type_gmsh > (int) GMSH_POINT1)
 
555
        ecs_error(__FILE__, __LINE__, 0,
 
556
                  _("Error reading a Gmsh mesh file:\n"
 
557
                    "at line %ld of file \"%s\".\n"
 
558
                    "Type identifier <%d> for element <%ld> is not recognized."),
 
559
                  (long)(*num_ligne), ecs_file_get_name(fic_maillage),
 
560
                  (int)type_gmsh, (long)label);
 
561
 
 
562
      type_ecs         = ecs_gmsh_elt_liste_c[type_gmsh - 1].ecs_typ;
 
563
      nbr_nod_elt_gmsh = ecs_gmsh_elt_liste_c[type_gmsh - 1].nbr_som;
 
564
      nbr_som_elt      = ecs_fic_elt_typ_liste_c[type_ecs].nbr_som;
 
565
 
 
566
      if (type_gmsh == GMSH_POINT1) {
 
567
        cpt_point += 1;
 
568
        ligne_decodee = true;
 
569
        continue;
 
570
      }
 
571
      else if (type_gmsh == GMSH_SEG2 || type_gmsh == GMSH_SEG3) {
 
572
        cpt_are += 1;
 
573
        ligne_decodee = true;
 
574
        continue;
 
575
      }
 
576
 
 
577
      /* Lecture des "tags" de l'élément courant */
 
578
 
 
579
      coul_elt = 0;
 
580
 
 
581
      if (version_fmt_gmsh == 2) {
 
582
 
 
583
        ssch   = strtok(NULL, " ");
 
584
        if (ssch == NULL)
 
585
          break;
 
586
 
 
587
        nbr_tag_elt = (ecs_int_t)(atol(ssch));
 
588
 
 
589
        for (ind_tag_elt = 0; ind_tag_elt < nbr_tag_elt; ind_tag_elt++) {
 
590
 
 
591
          ssch   = strtok(NULL, " ");
 
592
          if (ssch == NULL)
 
593
            break;
 
594
 
 
595
          /* Par défaut, Gmsh écrit des fichiers avec 2 "tags",
 
596
             le premier correspondant à un numéro d'entité physique,
 
597
             le second à une entité géométrique élémentaire;
 
598
             on associe le premier à une couleur */
 
599
 
 
600
          if (ind_tag_elt == 0)
 
601
            coul_elt = (ecs_int_t)(atol(ssch));
 
602
 
 
603
        }
 
604
 
 
605
        if (ssch == NULL && ind_tag_elt < nbr_tag_elt)
 
606
          break;
 
607
 
 
608
      }
 
609
      else { /* Ancienne version 1 du format Gmsh */
 
610
 
 
611
        /* On interprête reg-phys (numéro d'entité physique) comme
 
612
           étant une couleur */
 
613
 
 
614
        ssch   = strtok(NULL, " ");
 
615
        if (ssch == NULL)
 
616
          break;
 
617
 
 
618
        coul_elt = (ecs_int_t)(atol(ssch));
 
619
 
 
620
        /* On saute la valeur reg-elem (numéro d'entité élémentaire) */
 
621
 
 
622
        ssch   = strtok(NULL, " ");
 
623
        if (ssch == NULL)
 
624
          break;
 
625
 
 
626
        /* On saute le nombre de noeuds, redondant */
 
627
 
 
628
        ssch   = strtok(NULL, " ");
 
629
        if (ssch == NULL)
 
630
          break;
 
631
 
 
632
      }
 
633
 
 
634
      /* Lecture des numéros des sommets de l'élément courant */
 
635
 
 
636
      for (ind_nod_elt = 0; ind_nod_elt < nbr_nod_elt_gmsh; ind_nod_elt++) {
 
637
 
 
638
        ssch   = strtok(NULL, " ");
 
639
        if (ssch == NULL)
 
640
          break;
 
641
 
 
642
        num_nod_elt_gmsh[ind_nod_elt] = (ecs_int_t)(atol(ssch));
 
643
      }
 
644
 
 
645
      if (ssch == NULL && ind_nod_elt < nbr_nod_elt_gmsh)
 
646
        break;
 
647
 
 
648
      ligne_decodee = true;
 
649
 
 
650
      /* Stockage des valeurs avant transfert dans la structure `maillage' */
 
651
      /*===================================================================*/
 
652
 
 
653
      /* Identification de l'entité concernée */
 
654
 
 
655
      ent_num = ecs_maillage_pre__ret_typ_geo(type_ecs);
 
656
 
 
657
      /* Position des numéros de sommets du prochain élément */
 
658
 
 
659
      elt_pos_som_ent[ent_num][cpt_elt_ent[ent_num] + 1] =
 
660
        elt_pos_som_ent[ent_num][cpt_elt_ent[ent_num]] + nbr_som_elt;
 
661
 
 
662
      /* Connectivité de l'élément par ses numéros de sommets */
 
663
 
 
664
      for (ind_som_elt = 0; ind_som_elt < nbr_som_elt; ind_som_elt++) {
 
665
 
 
666
        elt_val_som_ent
 
667
          [ent_num]
 
668
          [elt_pos_som_ent[ent_num][cpt_elt_ent[ent_num]] - 1 + ind_som_elt]
 
669
          = num_nod_elt_gmsh
 
670
          [ecs_gmsh_elt_liste_c[type_gmsh - 1].num_som[ind_som_elt] - 1];
 
671
 
 
672
      }
 
673
 
 
674
      /* Couleur (tag) de l'élément lu */
 
675
 
 
676
      icoul = 0;
 
677
      while (icoul < cpt_coul_ent[ent_num]           &&
 
678
             val_coul_ent[ent_num][icoul] != coul_elt)
 
679
        icoul++;
 
680
 
 
681
      if (icoul == cpt_coul_ent[ent_num]) {
 
682
 
 
683
        /* La valeur de la couleur n'a pas encore été stockée */
 
684
 
 
685
        ECS_REALLOC(val_coul_ent[ent_num]    , cpt_coul_ent[ent_num] + 1,
 
686
                    ecs_int_t);
 
687
        ECS_REALLOC(cpt_elt_coul_ent[ent_num], cpt_coul_ent[ent_num] + 1,
 
688
                    ecs_size_t);
 
689
        cpt_elt_coul_ent[ent_num][icoul] = 0;
 
690
        val_coul_ent[ent_num][icoul] = coul_elt;
 
691
        cpt_coul_ent[ent_num]++;
 
692
 
 
693
      }
 
694
 
 
695
      cpt_elt_coul_ent[ent_num][icoul]++;
 
696
      elt_val_color_ent[ent_num][cpt_elt_ent[ent_num]] = icoul + 1;
 
697
 
 
698
      /* Incrémentation du nombre d'éléments lus */
 
699
 
 
700
      cpt_elt_ent[ent_num]++;
 
701
 
 
702
    }
 
703
 
 
704
    /* Fin de la boucle de lecture sur les éléments
 
705
       (et sur les lignes du fichier) */
 
706
 
 
707
    if (ligne_decodee == false)
 
708
      ecs_error(__FILE__, __LINE__, 0,
 
709
                _("Error decoding line %ld "
 
710
                  "(corresponding to element <%ld>) of file\n"
 
711
                  "\"%s\"."),
 
712
                (long)(*num_ligne), (long)(ind_elt+1),
 
713
                ecs_file_get_name(fic_maillage));
 
714
 
 
715
  }
 
716
  else {
 
717
 
 
718
    ecs_file_set_type(fic_maillage, ECS_FILE_TYPE_BINARY);
 
719
 
 
720
    nbr_elt_lus = 0;
 
721
    nbr_elt_tot = nbr_elt;
 
722
 
 
723
    while (nbr_elt_lus != nbr_elt_tot) {
 
724
 
 
725
      ecs_file_read(&header, 4, 3, fic_maillage);
 
726
 
 
727
      type_gmsh   = header[0];
 
728
      nbr_elt     = header[1];
 
729
      nbr_tag_elt = header[2];
 
730
 
 
731
      if (type_gmsh < (int)GMSH_SEG2 || type_gmsh > (int) GMSH_POINT1)
 
732
        ecs_error(__FILE__, __LINE__, 0,
 
733
                  _("Error reading a Gmsh mesh file:\n"
 
734
                    "Type identifier <%d> is not recognized."),
 
735
                  ecs_file_get_name(fic_maillage), (int)type_gmsh);
 
736
 
 
737
      type_ecs         = ecs_gmsh_elt_liste_c[type_gmsh - 1].ecs_typ;
 
738
      nbr_nod_elt_gmsh = ecs_gmsh_elt_liste_c[type_gmsh - 1].nbr_som;
 
739
      nbr_som_elt      = ecs_fic_elt_typ_liste_c[type_ecs].nbr_som;
 
740
 
 
741
      for (ind_elt = 0; ind_elt < nbr_elt; ind_elt++) {
 
742
 
 
743
        ecs_file_read(&data, sizeof(int),
 
744
                      1 + nbr_tag_elt + nbr_nod_elt_gmsh, fic_maillage);
 
745
 
 
746
        if (type_gmsh == GMSH_POINT1) {
 
747
          cpt_point += 1;
 
748
          continue;
 
749
        }
 
750
        else if (type_gmsh == GMSH_SEG2 || type_gmsh == GMSH_SEG3) {
 
751
          cpt_are += 1;
 
752
          continue;
 
753
        }
 
754
 
 
755
        /* Par défaut, Gmsh écrit des fichiers avec 2 "tags",
 
756
           le premier correspondant à un numéro d'entité physique,
 
757
           le second à une entité géométrique élémentaire;
 
758
           on associe le premier à une couleur */
 
759
 
 
760
        label    = data[0];
 
761
        coul_elt = data[1];
 
762
 
 
763
        /* Lecture des numéros des sommets de l'élément courant */
 
764
 
 
765
        for (ind_nod_elt = 0; ind_nod_elt < nbr_nod_elt_gmsh; ind_nod_elt++)
 
766
          num_nod_elt_gmsh[ind_nod_elt] = data[1 + nbr_tag_elt + ind_nod_elt];
 
767
 
 
768
        /* Stockage des valeurs avant transfert dans la structure `maillage' */
 
769
        /*===================================================================*/
 
770
 
 
771
        /* Identification de l'entité concernée */
 
772
 
 
773
        ent_num = ecs_maillage_pre__ret_typ_geo(type_ecs);
 
774
 
 
775
        /* Position des numéros de sommets du prochain élément */
 
776
 
 
777
        elt_pos_som_ent[ent_num][cpt_elt_ent[ent_num] + 1] =
 
778
          elt_pos_som_ent[ent_num][cpt_elt_ent[ent_num]] + nbr_som_elt;
 
779
 
 
780
        /* Connectivité de l'élément par ses numéros de sommets */
 
781
 
 
782
        for (ind_som_elt = 0; ind_som_elt < nbr_som_elt; ind_som_elt++) {
 
783
 
 
784
          elt_val_som_ent
 
785
            [ent_num]
 
786
            [elt_pos_som_ent[ent_num][cpt_elt_ent[ent_num]] - 1 + ind_som_elt]
 
787
            = num_nod_elt_gmsh
 
788
            [ecs_gmsh_elt_liste_c[type_gmsh - 1].num_som[ind_som_elt] - 1];
 
789
 
 
790
        }
 
791
 
 
792
        /* Couleur (tag) de l'élément lu */
 
793
 
 
794
        icoul = 0;
 
795
        while (icoul < cpt_coul_ent[ent_num]           &&
 
796
               val_coul_ent[ent_num][icoul] != coul_elt)
 
797
          icoul++;
 
798
 
 
799
        if (icoul == cpt_coul_ent[ent_num]) {
 
800
 
 
801
          /* La valeur de la couleur n'a pas encore été stockée */
 
802
 
 
803
          ECS_REALLOC(val_coul_ent[ent_num]    , cpt_coul_ent[ent_num] + 1,
 
804
                      ecs_int_t);
 
805
          ECS_REALLOC(cpt_elt_coul_ent[ent_num], cpt_coul_ent[ent_num] + 1,
 
806
                      ecs_size_t);
 
807
          cpt_elt_coul_ent[ent_num][icoul] = 0;
 
808
          val_coul_ent[ent_num][icoul] = coul_elt;
 
809
          cpt_coul_ent[ent_num]++;
 
810
 
 
811
        }
 
812
 
 
813
        cpt_elt_coul_ent[ent_num][icoul]++;
 
814
        elt_val_color_ent[ent_num][cpt_elt_ent[ent_num]] = icoul + 1;
 
815
 
 
816
        /* Incrémentation du nombre d'éléments lus */
 
817
 
 
818
        cpt_elt_ent[ent_num]++;
 
819
 
 
820
      }
 
821
 
 
822
      nbr_elt_lus += nbr_elt;
 
823
    }
 
824
 
 
825
    ecs_file_set_type(fic_maillage, ECS_FILE_TYPE_TEXT);
 
826
 
 
827
    ecs_file_gets(chaine, 2, fic_maillage, num_ligne);
 
828
 
 
829
  }
 
830
 
 
831
  /* Indications */
 
832
 
 
833
  if (cpt_point > -1 || cpt_are > 0)
 
834
      printf("\n");
 
835
  if (cpt_point > -1)
 
836
      printf(_("  %ld elements of type \"point\" ignored\n"),
 
837
             (long)cpt_point);
 
838
  if (cpt_are > -1)
 
839
      printf(_("  %ld elements of type \"edge\" ignored\n"),
 
840
             (long)cpt_are);
 
841
  if (cpt_point > -1 || cpt_are > 0)
 
842
      printf("\n");
 
843
 
 
844
  /* Ligne de fin de rubrique */
 
845
 
 
846
  ecs_file_gets(chaine, ECS_LOC_LNG_MAX_CHAINE_GMSH, fic_maillage, num_ligne);
 
847
 
 
848
  if (   (strncmp(chaine, "$EndElements", strlen("$EndElements")) != 0)
 
849
      && (strncmp(chaine, "$ENDELM", strlen("$ENDELM")) != 0))
 
850
    ecs_error(__FILE__, __LINE__, 0,
 
851
              _("The end of the elements list specification of file\n"
 
852
                "\"%s\" is not present at the expected place.)"),
 
853
              ecs_file_get_name(fic_maillage));
 
854
 
 
855
  /* Réallocations des tableaux locaux */
 
856
  /*===================================*/
 
857
 
 
858
  /* Reallocations des tableaux locaux et mise à jour des références */
 
859
  /*=================================================================*/
 
860
 
 
861
  for (ient = ECS_ENTMAIL_FAC; ient < ECS_N_ENTMAIL; ient++) {
 
862
 
 
863
    if (cpt_elt_ent[ient] != 0) {
 
864
      ECS_REALLOC(elt_pos_som_ent[ient]    ,
 
865
                  cpt_elt_ent[ient] + 1    , ecs_size_t);
 
866
      ECS_REALLOC(elt_val_som_ent[ient]    ,
 
867
                  elt_pos_som_ent[ient][cpt_elt_ent[ient]] - 1, ecs_int_t);
 
868
      ECS_REALLOC(elt_val_color_ent[ient]  ,
 
869
                  cpt_elt_ent[ient]        , ecs_int_t);
 
870
    }
 
871
 
 
872
    ecs_maillage_pre__label_en_indice
 
873
      (nbr_som,
 
874
       elt_pos_som_ent[ient][cpt_elt_ent[ient]] - 1,
 
875
       *som_val_label,
 
876
       elt_val_som_ent[ient]);
 
877
  }
 
878
 
 
879
 
 
880
  ECS_FREE(*som_val_label);
 
881
 
 
882
 
 
883
  /* Transfert des valeurs lues dans les structures d'entités de maillage */
 
884
  /*======================================================================*/
 
885
 
 
886
  ecs_maillage_pre__cree_elt(maillage,
 
887
                             cpt_elt_ent,
 
888
                             elt_pos_som_ent,
 
889
                             elt_val_som_ent,
 
890
                             NULL,
 
891
                             elt_val_color_ent,
 
892
                             cpt_coul_ent,
 
893
                             val_coul_ent,
 
894
                             cpt_elt_coul_ent);
 
895
}
 
896
 
 
897
/*============================================================================
 
898
 *  Fonctions publiques
 
899
 *============================================================================*/
 
900
 
 
901
/*----------------------------------------------------------------------------
 
902
 *  Lecture d'un fichier de maillage au format Gmsh
 
903
 *   et affectation des donnees dans la structure de maillage
 
904
 *----------------------------------------------------------------------------*/
 
905
 
 
906
ecs_maillage_t  *
 
907
ecs_pre_gmsh__lit_maillage(const char  *nom_fic_maillage)
 
908
{
 
909
  ecs_file_t   *fic_maillage;          /* Descripteur du fichier */
 
910
  char          chaine[ECS_LOC_LNG_MAX_CHAINE_GMSH]; /* Ligne lue  */
 
911
  int           num_ligne;             /* Compteur des lignes lues */
 
912
  int           version_fmt_gmsh;
 
913
  int           type_fmt_gmsh;
 
914
  ecs_int_t    *som_val_label;
 
915
  int           dim_e;                 /* Dimension spatiale */
 
916
  bool          bool_elements = false;
 
917
  bool          bool_noeuds = false;
 
918
 
 
919
  /* Création d'un maillage initialement vide (valeur de retour) */
 
920
 
 
921
  ecs_maillage_t  *maillage = ecs_maillage__cree_nodal();
 
922
 
 
923
  /*xxxxxxxxxxxxxxxxxxxxxxxxxxx Instructions xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
 
924
 
 
925
  /* Affichage du titre */
 
926
  /*====================*/
 
927
 
 
928
  printf(_("\n\n"
 
929
           "Reading mesh from file in Gmsh format\n"
 
930
           "----------------------\n"));
 
931
 
 
932
  printf(_("  Mesh file: %s\n\n\n"),
 
933
         nom_fic_maillage);
 
934
 
 
935
  /* Initialisations */
 
936
  /*=================*/
 
937
 
 
938
  num_ligne = 0;
 
939
  dim_e     = 3;
 
940
 
 
941
  som_val_label = NULL;
 
942
 
 
943
  /* Par défaut, la version est 1.0 en ASCII */
 
944
 
 
945
  version_fmt_gmsh = 1;
 
946
  type_fmt_gmsh = 0;
 
947
 
 
948
  /* Ouverture du fichier Gmsh en lecture */
 
949
  /*---------------------------------------*/
 
950
 
 
951
  fic_maillage = ecs_file_open(nom_fic_maillage,
 
952
                               ECS_FILE_MODE_READ,
 
953
                               ECS_FILE_TYPE_TEXT);
 
954
 
 
955
  /*================================================*/
 
956
  /* Boucle sur les lignes du fichier de maillage : */
 
957
  /* tant qu'on n'a pa atteint la fin du fichier    */
 
958
  /*================================================*/
 
959
 
 
960
 
 
961
  while (ecs_file_gets_try(chaine, ECS_LOC_LNG_MAX_CHAINE_GMSH,
 
962
                           fic_maillage, &num_ligne) != NULL) {
 
963
 
 
964
    /* Si la chaine lue est un descripteur de format */
 
965
 
 
966
    if (strncmp(chaine, "$MeshFormat", strlen("$MeshFormat")) == 0)
 
967
 
 
968
      ecs_loc_pre_gmsh__lit_vers_format(fic_maillage,
 
969
                                        &version_fmt_gmsh,
 
970
                                        &type_fmt_gmsh,
 
971
                                        &num_ligne);
 
972
 
 
973
    /* Si la chaine lue marque le début de la section des noeuds */
 
974
 
 
975
    else if (strncmp(chaine,
 
976
                     "$ParametricNodes",
 
977
                     strlen("$ParametricNodes")) == 0)
 
978
      ecs_error
 
979
        (__FILE__, __LINE__, 0,
 
980
         _("Error reading file\n\"%s\" :\n"
 
981
           "Nodes are defined in the \"ParametricNodes\" variant, which\n"
 
982
           "is not handled (and not documented as of Gmsh 2.4.0)."),
 
983
         ecs_file_get_name(fic_maillage));
 
984
 
 
985
    /* Si la chaine lue marque le début de la section des noeuds */
 
986
 
 
987
    else if (   (strncmp(chaine, "$Nodes", strlen("$Nodes")) == 0)
 
988
             || (strncmp(chaine, "$NOD", strlen("$NOD")) == 0)) {
 
989
 
 
990
      ecs_loc_pre_gmsh__lit_nodes(maillage,
 
991
                                  fic_maillage,
 
992
                                  type_fmt_gmsh,
 
993
                                  &num_ligne,
 
994
                                  &som_val_label);
 
995
 
 
996
      bool_noeuds = true;
 
997
 
 
998
    }
 
999
 
 
1000
    /* Si la chaine lue marque le début de la section des éléments */
 
1001
 
 
1002
    else if (   (strncmp(chaine, "$Elements", strlen("$Elements")) == 0)
 
1003
             || (strncmp(chaine, "$ELM", strlen("$ELM")) == 0)) {
 
1004
 
 
1005
      ecs_loc_pre_gmsh__lit_elements(maillage,
 
1006
                                     fic_maillage,
 
1007
                                     version_fmt_gmsh,
 
1008
                                     type_fmt_gmsh,
 
1009
                                     &num_ligne,
 
1010
                                     maillage->n_vertices,
 
1011
                                     &som_val_label);
 
1012
 
 
1013
      bool_elements = true;
 
1014
 
 
1015
    }
 
1016
  }
 
1017
 
 
1018
  if (ecs_file_eof(fic_maillage) == 0)
 
1019
    ecs_error(__FILE__, __LINE__, errno,
 
1020
              _("Error reading line %ld of file \"%s\"."),
 
1021
              (long)num_ligne, ecs_file_get_name(fic_maillage));
 
1022
 
 
1023
  /* else : la fin de fichier a bien ete atteinte */
 
1024
 
 
1025
 
 
1026
  /* On verifie qu'on a bien lu des noeuds et des elements */
 
1027
 
 
1028
  if (bool_noeuds == false)
 
1029
    ecs_error(__FILE__, __LINE__, 0,
 
1030
              _("Error reading a Gmsh mesh file:\n"
 
1031
                "at line %ld of file \"%s\".\n"
 
1032
                "The points definition was not found."),
 
1033
              (long)num_ligne, ecs_file_get_name(fic_maillage));
 
1034
 
 
1035
  if (bool_elements == false)
 
1036
    ecs_error(__FILE__, __LINE__, 0,
 
1037
              _("Error reading a Gmsh mesh file:\n"
 
1038
                "at line %ld of file \"%s\".\n"
 
1039
                "The elements definition was not found."),
 
1040
              (long)num_ligne, ecs_file_get_name(fic_maillage));
 
1041
 
 
1042
 
 
1043
  /* Fermeture du fichier de maillage */
 
1044
  /*----------------------------------*/
 
1045
 
 
1046
  ecs_file_free(fic_maillage);
 
1047
 
 
1048
  /* Retour */
 
1049
  /*========*/
 
1050
 
 
1051
  return maillage;
 
1052
}
 
1053
 
 
1054
/*----------------------------------------------------------------------------*/
 
1055