UsesCase_MEDstructElement_1.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2011  EDF R&D, CEA/DEN
00004  *  MED is free software: you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published by
00006  *  the Free Software Foundation, either version 3 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  MED is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 /*
00019  * StructElement use case 1 : write struct elements model in a file
00020  * STEP 1 : suppport mesh creation
00021  * STEP 2 : struct element model creation
00022  * STEP 3 : computation mesh creation
00023  */
00024 
00025 #include <med.h>
00026 #define MESGERR 1
00027 #include <med_utils.h>
00028 
00029 #include <string.h>
00030 
00031 int main (int argc, char **argv) {
00032   med_idt fid;
00033   char elementname[MED_NAME_SIZE+1]="";
00034   const char ballsupportname[MED_NAME_SIZE+1]="BALL_SUPPORT_MESH";
00035   const char beamsupportname[MED_NAME_SIZE+1]="BEAM_SUPPORT_MESH";
00036   const char computmeshname[MED_NAME_SIZE+1]="COMPUT_MESH";
00037   const char beamsectionname[MED_NAME_SIZE+1]="BEAM_SECTION_MESH";
00038   const med_int elementdim = 3;
00039   med_int nnode;
00040   med_geometry_type geotype=MED_NONE;
00041   med_int ncomp;
00042   const med_float ballmeshnodescoo[3] = {0.0, 0.0, 0.0 };
00043   const med_float beammeshnodescoo[3*7] = {0.0,0.0,0.0,
00044                                            0.0,0.0,2.0,
00045                                            0.0,0.0,4.0,
00046                                            0.0,0.0,5.0,
00047                                            0.0,0.0,7.0,
00048                                            0.0,0.0,10.0,
00049                                            0.0,0.0,11.0 };
00050   const med_float beamsectioncoo[9*3] = {-0.2,-0.2,0.0,
00051                                          0.0,-0.2,0.0,
00052                                          0.2,-0.2,0.0,
00053                                          -0.2, 0.0,0.0,
00054                                          0.0, 0.0,0.0,
00055                                          0.2, 0.0,0.0,
00056                                          -0.2, 0.2,0.0,
00057                                          0.0, 0.2,0.0,
00058                                          0.2, 0.2,0.0 };
00059   const med_int seg2connectivity[2*6] = {1,2, 2,3, 3,4, 4,5, 5,6, 6,7};
00060   med_int spacedim, meshdim,nseg2;
00061   /*                                         123456789012345612345678901234561234567890123456 */
00062   const char axisname[3*MED_SNAME_SIZE+1] = "x               y               z               ";
00063   const char unitname[3*MED_SNAME_SIZE+1] = "cm              cm              cm              ";
00064   const med_float attvalue[6] = {0.2,0.3,0.4,0.4,0.3,0.2};
00065   /*                                            1234567890123456789012345678901234567890123456789012345678901234        */
00066   const char attprovalue[2*MED_NAME_SIZE+1] = {"EXTREMITY_1_____________________________________________________" \
00067                                                "EXTREMITY_2_____________________________________________________" };
00068   const char profilename[MED_NAME_SIZE+1] = "EXTREMITY_PROFILE_NAME";
00069   const med_int profilesize = 2;
00070   const med_int profile[2] = {1,6};
00071   const med_float meshcoo[3*12] = { 0.0, 0.0, 0.0,
00072                                     1.1, 1.1, 1.1,
00073                                     2.2, 2.2, 2.2,
00074                                     10., 10., 10.,
00075                                     12., 12., 12.,
00076                                     60., 20., 20.,
00077                                     70., 20., 20.,
00078                                     80., 20., 20.,
00079                                     90., 20., 20.,
00080                                     100., 20., 20.,
00081                                     110., 20., 20.,
00082                                     120., 20., 20.
00083   };
00084   const med_int meshseg2connectivity[12] = {  6,7,
00085                                               7,8,
00086                                               8,9,
00087                                               9,10,
00088                                               10,11,
00089                                               11,12 };
00090   med_int nentity;
00091   const med_int labels[3] = { 1, 2, 3 }; /* nodes numbers */
00092   const med_int ballconnectivity[2] = { 4, 5 }; /* nodes numbers */
00093   const med_int beamconnectivity[6] = { 1, 2, 3, 4, 5, 6}; /* cells numbers */
00094   const med_float balldiameter[2] = { 2.0, 5.8 };
00095   const med_int nquad4=4;
00096   const med_int beamsectionconnectivity[4*4] = { 4,5,2,1,
00097                                                  5,6,3,2,
00098                                                  7,8,5,4,
00099                                                  8,9,6,5};
00100   int ret=-1;
00101   
00102   /* file creation */
00103   fid = MEDfileOpen("UsesCase_MEDstructElement_1.med",MED_ACC_CREAT);
00104   if (fid < 0) {
00105     MESSAGE("ERROR : file creation");
00106     goto ERROR;
00107   }
00108 
00109   /* STEP 1 : support meshes creation */
00110   spacedim =  3;
00111   meshdim = 3;
00112 
00113   /* Mesh 1 : support mesh for ball model */
00114   if (MEDsupportMeshCr(fid, ballsupportname, spacedim, meshdim, "Support mesh for a ball model",
00115                        MED_CARTESIAN, axisname, unitname) < 0) {
00116     MESSAGE("ERROR : creating a support mesh ...");
00117     goto ERROR;
00118   }
00119   /* 1 node and no cell in the mesh */
00120   nnode = 1;
00121   if (MEDmeshNodeCoordinateWr(fid, ballsupportname, MED_NO_DT, MED_NO_IT, 0.0,
00122                               MED_FULL_INTERLACE, nnode, ballmeshnodescoo) < 0) {
00123     MESSAGE("ERROR : write nodes coordinates ...");
00124     goto ERROR;
00125   }
00126 
00127   /* Mesh 2 :support mesh for beam model */
00128   if (MEDsupportMeshCr(fid, beamsupportname, spacedim, meshdim, "Support mesh for a beam model",
00129                        MED_CARTESIAN, axisname, unitname) < 0) {
00130     MESSAGE("ERROR : creating a support mesh ...");
00131     goto ERROR;
00132   }
00133   /* 7 nodes and 6 MED_SEG2 */
00134   nnode = 7;
00135   if (MEDmeshNodeCoordinateWr(fid, beamsupportname, MED_NO_DT, MED_NO_IT, 0.0,
00136                               MED_FULL_INTERLACE, nnode,  beammeshnodescoo) < 0) {
00137     MESSAGE("ERROR : write nodes coordinates ...");
00138     goto ERROR;
00139   }
00140   nseg2 = 6;
00141   if (MEDmeshElementConnectivityWr(fid, beamsupportname, MED_NO_DT, MED_NO_IT, 0.0, MED_CELL, MED_SEG2,
00142                                    MED_NODAL, MED_FULL_INTERLACE, nseg2, seg2connectivity) < 0) {
00143     MESSAGE("ERROR : write cells connectivity ...");
00144     goto ERROR;
00145   }
00146 
00147   /* Mesh 3 : support mesh to define a section for integration points of
00148      a struct element */
00149   if (MEDsupportMeshCr(fid, beamsectionname, spacedim, meshdim, "Support mesh for a section of the beam model",
00150                        MED_CARTESIAN, axisname, unitname) < 0) {
00151     MESSAGE("ERROR : creating a support mesh ...");
00152     goto ERROR;
00153   }  
00154 
00155   nnode = 9;
00156   if (MEDmeshNodeCoordinateWr(fid, beamsectionname, MED_NO_DT, MED_NO_IT, 0.0,
00157                               MED_FULL_INTERLACE, nnode, beamsectioncoo) < 0) {
00158     MESSAGE("ERROR : write nodes coordinates ...");
00159     goto ERROR;
00160   }
00161 
00162   if (MEDmeshElementConnectivityWr(fid, beamsectionname, MED_NO_DT, MED_NO_IT, 0.0, MED_CELL, MED_QUAD4,
00163                                    MED_NODAL, MED_FULL_INTERLACE, nquad4, beamsectionconnectivity) < 0) {
00164     MESSAGE("ERROR : write cells connectivity ...");
00165     goto ERROR;
00166   }
00167 
00168   /* STEP 2 */
00169   /* particle model creation : no support mesh */
00170   strcpy(elementname,MED_PARTICLE_NAME);
00171   if ((geotype = MEDstructElementCr(fid, elementname, elementdim, MED_NO_MESHNAME,
00172                                     MED_NONE,MED_NONE)) < 0) {
00173     MESSAGE("ERROR : creating struct element");
00174     goto ERROR;
00175   }
00176   ncomp=1;
00177   if (MEDstructElementVarAttCr(fid, elementname,
00178                                MED_PARTICLE_LABEL, MED_ATT_INT, ncomp) < 0) {
00179     MESSAGE("ERROR : creating struct element");
00180     goto ERROR;
00181   }
00182 
00183   /* ball model creation */
00184   strcpy(elementname,MED_BALL_NAME);
00185   if ((geotype = MEDstructElementCr(fid, elementname, elementdim, ballsupportname,
00186                                     MED_NODE,MED_NONE)) < 0) {
00187     MESSAGE("ERROR : creating struct element");
00188     goto ERROR;
00189   }
00190   ncomp=1;
00191   if (MEDstructElementVarAttCr(fid, elementname,
00192                                MED_BALL_DIAMETER, MED_ATT_FLOAT64, ncomp) < 0) {
00193     MESSAGE("ERROR : creating struct element");
00194     goto ERROR;
00195   }
00196 
00197 
00198   /* a beam */
00199   strcpy(elementname,MED_BEAM_NAME);
00200   if ((geotype = MEDstructElementCr(fid, elementname, elementdim, beamsupportname,
00201                                     MED_CELL,MED_SEG2)) < 0) {
00202     MESSAGE("ERROR : creating struct element");
00203     goto ERROR;
00204   }
00205   ncomp=1;
00206   /* a first constant attribute */
00207   if (MEDstructElementConstAttWr(fid, elementname,
00208                                  MED_BEAM_THICKNESS, MED_ATT_FLOAT64, ncomp,
00209                                  MED_CELL,(void*) attvalue) < 0) {
00210     MESSAGE("ERROR : creating struct element");
00211     goto ERROR;
00212   }
00213   /* a second constant attribute defined with a profile for the first and the
00214      last segment */
00215   /* create the profile */
00216     if (MEDprofileWr(fid, profilename, profilesize, profile ) < 0) {
00217     MESSAGE("ERROR : create profile ...");
00218     goto ERROR; 
00219   }
00220   /* write the constant attribute */
00221   if (MEDstructElementConstAttWithProfileWr(fid, 
00222                                             elementname,
00223                                             "BEAM_EXTREMITIES_LABELS", 
00224                                             MED_ATT_NAME, 
00225                                             ncomp,
00226                                             MED_CELL,
00227                                             profilename,
00228                                             (void*) attprovalue) < 0) {
00229     MESSAGE("ERROR : creating struct element");
00230     goto ERROR;
00231   }
00232 
00233   /* STEP 3 : Computation mesh creation */
00234 
00235   /* mesh creation */
00236   if (MEDmeshCr(fid, computmeshname, spacedim, meshdim,  MED_UNSTRUCTURED_MESH,
00237                 "Computation mesh", "s", MED_SORT_DTIT,
00238                 MED_CARTESIAN, axisname, unitname) < 0) {
00239     MESSAGE("ERROR : creating computation mesh ...");
00240     goto ERROR;
00241   }
00242 
00243   /* mesh node creation */
00244   nnode = 12;
00245   if (MEDmeshNodeCoordinateWr(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_UNDEF_DT,
00246                               MED_FULL_INTERLACE, nnode, meshcoo) < 0) {
00247     MESSAGE("ERROR : writing nodes coordinates ...");
00248     goto ERROR;
00249   }
00250 
00251   /* mesh BEAM_MODEL struct element mesh support connectivity write */
00252   nentity = 1;
00253   SSCRUTE(elementname);
00254   geotype = MEDstructElementGeotype(fid,elementname);
00255   ISCRUTE(geotype);
00256   if (MEDmeshElementConnectivityWr(fid, computmeshname, MED_NO_DT, MED_NO_IT, 0.0, 
00257                                    MED_STRUCT_ELEMENT, geotype , MED_NODAL, MED_FULL_INTERLACE,
00258                                    nentity, meshseg2connectivity) < 0) {
00259     MESSAGE("ERROR : writing beams model mesh support connectivity ...");
00260     goto ERROR;
00261   }
00262 
00263   /* Get the dynamic geometry type of each struct element model.
00264      Then for each type, write the connectivity and variable(s) attribute(s) */
00265 
00266   /* 3 particles in the mesh */
00267   strcpy(elementname,MED_PARTICLE_NAME);
00268   geotype = MEDstructElementGeotype(fid,elementname);
00269   nentity = 3;
00270   ISCRUTE(geotype);
00271   if (MEDmeshElementConnectivityWr(fid, computmeshname, MED_NO_DT, MED_NO_IT, 0.0, 
00272                                    MED_STRUCT_ELEMENT, geotype , MED_NODAL, MED_FULL_INTERLACE,
00273                                    nentity, 0) < 0) {
00274     MESSAGE("ERROR : writing particles connectivity ...");
00275     goto ERROR;
00276   }
00277 
00278   /* no support mesh => no connectivity, the particles are localized with an association between
00279      the mesh nodes and the label attribute defined in the struct model */
00280   if (MEDmeshStructElementVarAttWr(fid, computmeshname, MED_NO_DT, MED_NO_IT,
00281                                    geotype, MED_PARTICLE_LABEL,
00282                                    nentity, labels) < 0 ) {
00283     MESSAGE("ERROR : writing variable attributes  ...");
00284     goto ERROR;
00285   }
00286 
00287 
00288   /* 2 balls */
00289   strcpy(elementname,MED_BALL_NAME);
00290   nentity = 2;
00291   geotype = MEDstructElementGeotype(fid,elementname);
00292   if (MEDmeshElementConnectivityWr(fid,computmeshname, MED_NO_DT, MED_NO_IT, MED_UNDEF_DT,
00293                                    MED_STRUCT_ELEMENT, geotype, MED_NODAL,
00294                                    MED_FULL_INTERLACE, nentity, ballconnectivity) < 0 ) {
00295     MESSAGE("ERROR : writing balls connectivity");
00296     goto ERROR;
00297   }
00298 
00299   /* variable attribute : write ball diameter */
00300   if (MEDmeshStructElementVarAttWr(fid, computmeshname, MED_NO_DT, MED_NO_IT,
00301                                    geotype, MED_BALL_DIAMETER,
00302                                    nentity, balldiameter) < 0 ) {
00303     MESSAGE("ERROR : writing variable attributes ...");
00304     goto ERROR;
00305   }
00306 
00307   /* 1 beam */
00308   strcpy(elementname,MED_BEAM_NAME);
00309   nentity = 1;
00310   geotype = MEDstructElementGeotype(fid,elementname);
00311 
00312   if (MEDmeshElementConnectivityWr(fid,computmeshname, MED_NO_DT, MED_NO_IT, MED_UNDEF_DT,
00313                                    MED_STRUCT_ELEMENT, geotype, MED_NODAL,
00314                                    MED_FULL_INTERLACE, nentity, beamconnectivity) < 0 ) {
00315     MESSAGE("ERROR : beam connectivity ...");
00316     goto ERROR;
00317   }
00318 
00319   ret=0;
00320  ERROR:
00321 
00322   /* close file */
00323   if (MEDfileClose(fid) < 0) {
00324     MESSAGE("ERROR : file closing");
00325     ret=-1;
00326   }
00327 
00328   return ret;
00329 }
00330 

Généré le Mon May 16 17:10:24 2011 pour MED fichier par  doxygen 1.6.1