Salome HOME
52a0158016010210be4c0f03a16fcdaaa119f6c9
[modules/med.git] / src / MEDMEM / MEDMEM_MEDMEMgaussEcr.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "MEDMEM_MEDMEMgaussEcr.hxx"
24 /*
25  * En attendant une correction de la gestion du mode d'accès au fichier dans MEDfichier
26  * on intègre la correction ici.
27  */
28
29 namespace med_2_3 {
30   extern "C" {
31 # define ICI                    {\
32                                         fflush(stdout);\
33                                         fprintf(stderr, "%s [%d] : " , __FILE__ , __LINE__ ) ;\
34                                         fflush(stderr) ;\
35                                 }
36
37 # define ISCRUTE_MED(entier)        {\
38                                         ICI ;\
39                                         fprintf(stderr,"%s = %d\n",#entier,entier) ;\
40                                         fflush(stderr) ;\
41                                 }
42
43 # define SSCRUTE_MED(chaine)        {\
44                                         ICI ;\
45                                         fprintf(stderr,"%s = \"%s\"\n",#chaine,chaine) ;\
46                                         fflush(stderr) ;\
47                                 }
48 # define MESSAGE_MED(chaine)        {\
49                                         ICI ;\
50                                         fprintf(stderr,"%s\n",chaine) ;\
51                                         fflush(stderr) ;\
52                                 }
53
54     extern void _MEDmodeErreurVerrouiller(void);
55
56     /*
57      * - Nom de la fonction : MEDgaussEcr
58      * - Description : Itérateur renvoyant (n°pdt,n°or), le nbre de point de GAUSS pour le type d'élément,
59      *                 et le  maillage par défaut avec son eventuel lien à un autre fichier.
60      * - Parametres :
61      *     - fid      (IN) : ID du fichier HDF courant
62      *     - type_geo (IN) : Le type géométrique de l'entité concerné {MED_POINT,MED_SEG2 ......}
63      *     - refcoo   (IN) : Les coordonnées des noeuds de l'élément de référence (tableau de taille(typegeo%100)*(typegeo/100) )
64      *     - mode_coo (IN) : Choix du type d'entrelacement utilisé en mémoire pour refcoo et gscoo
65      *                        { MED_FULL_INTERLACE(x1,y1,z1,x2,...)) , MED_NO_INTERLACE(x1,x2,y1,y2,z1,z2) }
66      *     - ngauss   (IN) : Nombre de points de Gauss pour l'élément de référence considéré
67      *     - gscoo    (IN) : Les coordonnées des points de Gauss pour l'élément de référence (tableau de taille ngauss*type_geo/100)
68      *     - wg       (IN) : Poids à appliquer aux points d'intégration (tableau de taille ngauss)
69      *     - locname (IN) : Nom à associer à la localisation (MED_TAILLe_NOM)
70
71      REM :
72      <locname> est à passer en paramètre de MEDchampEcrire.
73     */
74
75     med_err
76     MEDMEMgaussEcr(med_idt fid, med_geometrie_element type_geo, med_float *refcoo, med_mode_switch mode_coo,
77                    med_int ngauss, med_float *gscoo, med_float * wg, char * locname )
78     {
79       med_idt gid=0, chid=0;
80       med_size dimd[1];
81       med_err ret = -1;
82       med_int typegeo = -1;
83       char chemin[MED_TAILLE_GAUSS+1]="";
84
85       /*
86        * On inhibe le gestionnaire d'erreur HDF 5
87        */
88       _MEDmodeErreurVerrouiller();
89
90       /*
91        * Si le groupe "GAUSS" n'existe pas, on le cree
92        */
93       strncpy(chemin,MED_GAUSS,MED_TAILLE_GAUSS-1);
94       chemin[MED_TAILLE_GAUSS-1] = '\0';
95       if ((gid = _MEDdatagroupOuvrir(fid,chemin)) < 0)
96         if ((gid = _MEDdatagroupCreer(fid,chemin)) < 0) {
97           MESSAGE_MED("Impossible de creer le groupe MED_GAUSS : ");
98           SSCRUTE_MED(chemin); goto ERROR;
99         }
100
101       /*
102        * Si le groupe <locname> n'existe pas, on le cree
103        * Sinon => erreur
104        */
105       if ((chid = _MEDdatagroupOuvrir(gid,locname)) >= 0) {
106         if ( false )//MED_MODE_ACCES != MED_LECTURE_ECRITURE )
107   {
108           MESSAGE_MED("Le nom de localisation existe déjà : ");
109           SSCRUTE_MED(locname); goto ERROR;
110         }
111       } else
112         if ((chid = _MEDdatagroupCreer(gid,locname)) < 0)
113           goto ERROR;
114
115       /*
116        * On stocke <ngauss> sous forme d'attribut
117        */
118       if (_MEDattrEntierEcrire(chid,MED_NOM_NBR,&ngauss) < 0) {
119         MESSAGE_MED("Erreur à l'écriture de l'attribut MED_NOM_NBR : ");
120         ISCRUTE_MED(ngauss);goto ERROR;
121       };
122
123       /*
124        * On stocke <type_geo> sous forme d'attribut
125        */
126       typegeo = (med_int) type_geo;
127       /* sizeof(enum) tjrs = sizeof(int) en C, or
128          sur machines 64 bits par défaut med_int==long,
129          du coup sur  machines 64 bits _MEDattrEntierEcrire utilise
130          le type hdf NATIVE_LONG, ce qui pose un problème qd on passe
131          un enum.
132       */
133       if (_MEDattrEntierEcrire(chid,MED_NOM_GEO,&typegeo) < 0) {
134         MESSAGE_MED("Erreur à l'écriture de l'attribut MED_NOM_GEO : ");
135         ISCRUTE_MED(type_geo);goto ERROR;
136       };
137
138
139       /*
140        * On stocke les coordonnées de référence dans un dataset
141        */
142
143       dimd[0] = (type_geo%100)*(type_geo/100);
144       if ( _MEDdatasetNumEcrire(chid,MED_NOM_COO,MED_FLOAT64,mode_coo,(type_geo/100),MED_ALL,MED_NOPF,MED_NO_PFLMOD,0,MED_NOPG,dimd,
145                                 (unsigned char*) refcoo)  < 0 ) {
146         MESSAGE_MED("Impossible d'ecrire le dataset : ");SSCRUTE_MED(MED_NOM_COO);
147         ISCRUTE_MED(dimd); goto ERROR;
148       }
149
150       /*
151        * On stocke les points d'intégration dans un dataset
152        */
153
154       dimd[0] = ngauss*(type_geo/100);
155       if ( _MEDdatasetNumEcrire(chid,MED_NOM_GAU,MED_FLOAT64,mode_coo,(type_geo/100),MED_ALL,MED_NOPF,MED_NO_PFLMOD,0,MED_NOPG,dimd,
156                                 (unsigned char*) gscoo)  < 0 ) {
157         MESSAGE_MED("Impossible d'ecrire le dataset : ");SSCRUTE_MED(MED_NOM_GAU);
158         ISCRUTE_MED(dimd); goto ERROR;
159       }
160
161       /*
162        * On stocke les poids dans un dataset
163        */
164
165       dimd[0] = ngauss;
166       if ( _MEDdatasetNumEcrire(chid,MED_NOM_VAL,MED_FLOAT64,mode_coo,1,MED_ALL,MED_NOPF,MED_NO_PFLMOD,0,MED_NOPG,dimd,
167                                 (unsigned char*) wg)  < 0 ) {
168         MESSAGE_MED("Impossible d'ecrire le dataset : ");SSCRUTE_MED(MED_NOM_VAL);
169         ISCRUTE_MED(dimd); goto ERROR;
170       }
171
172
173       ret = 0;
174
175     ERROR:
176
177       /*
178        * On ferme tout
179        */
180
181       if (chid>0)     if (_MEDdatagroupFermer(chid) < 0) {
182         MESSAGE_MED("Impossible de fermer le datagroup : ");
183         ISCRUTE_MED(chid); ret = -1;
184       }
185
186       if (gid>0)     if (_MEDdatagroupFermer(gid) < 0) {
187         MESSAGE_MED("Impossible de fermer le datagroup : ");
188         ISCRUTE_MED(gid); ret = -1;
189       }
190
191       return ret;
192     }
193
194 #undef MESSAGE_MED
195 #undef SSCRUTE_MED
196 #undef ISCRUTE_MED
197
198   }
199 }