UsesCase_MEDstructElement_1.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2015  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 beamconnectivity[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_float balldiameter[2] = { 2.0, 5.8 };
00094   const med_int nquad4=4;
00095   const med_int beamsectionconnectivity[4*4] = { 4,5,2,1,
00096                                                  5,6,3,2,
00097                                                  7,8,5,4,
00098                                                  8,9,6,5};
00099   int ret=-1;
00100   
00101   /* file creation */
00102   fid = MEDfileOpen("UsesCase_MEDstructElement_1.med",MED_ACC_CREAT);
00103   if (fid < 0) {
00104     MESSAGE("ERROR : file creation");
00105     goto ERROR;
00106   }
00107 
00108   /* STEP 1 : support meshes creation */
00109   spacedim =  3;
00110   meshdim = 3;
00111 
00112   /* Mesh 1 : support mesh for ball model */
00113   if (MEDsupportMeshCr(fid, ballsupportname, spacedim, meshdim, "Support mesh for a ball model",
00114                        MED_CARTESIAN, axisname, unitname) < 0) {
00115     MESSAGE("ERROR : creating a support mesh ...");
00116     goto ERROR;
00117   }
00118   /* 1 node and no cell in the mesh */
00119   nnode = 1;
00120   if (MEDmeshNodeCoordinateWr(fid, ballsupportname, MED_NO_DT, MED_NO_IT, 0.0,
00121                               MED_FULL_INTERLACE, nnode, ballmeshnodescoo) < 0) {
00122     MESSAGE("ERROR : write nodes coordinates ...");
00123     goto ERROR;
00124   }
00125 
00126   /* Mesh 2 :support mesh for beam model */
00127   if (MEDsupportMeshCr(fid, beamsupportname, spacedim, meshdim, "Support mesh for a beam model",
00128                        MED_CARTESIAN, axisname, unitname) < 0) {
00129     MESSAGE("ERROR : creating a support mesh ...");
00130     goto ERROR;
00131   }
00132   /* 7 nodes and 6 MED_SEG2 */
00133   nnode = 7;
00134   if (MEDmeshNodeCoordinateWr(fid, beamsupportname, MED_NO_DT, MED_NO_IT, 0.0,
00135                               MED_FULL_INTERLACE, nnode,  beammeshnodescoo) < 0) {
00136     MESSAGE("ERROR : write nodes coordinates ...");
00137     goto ERROR;
00138   }
00139   nseg2 = 6;
00140   if (MEDmeshElementConnectivityWr(fid, beamsupportname, MED_NO_DT, MED_NO_IT, 0.0, MED_CELL, MED_SEG2,
00141                                    MED_NODAL, MED_FULL_INTERLACE, nseg2, seg2connectivity) < 0) {
00142     MESSAGE("ERROR : write cells connectivity ...");
00143     goto ERROR;
00144   }
00145 
00146   /* Mesh 3 : support mesh to define a section for integration points of
00147      a struct element */
00148   if (MEDsupportMeshCr(fid, beamsectionname, spacedim, meshdim, "Support mesh for a section of the beam model",
00149                        MED_CARTESIAN, axisname, unitname) < 0) {
00150     MESSAGE("ERROR : creating a support mesh ...");
00151     goto ERROR;
00152   }
00153 
00154   nnode = 9;
00155   if (MEDmeshNodeCoordinateWr(fid, beamsectionname, MED_NO_DT, MED_NO_IT, 0.0,
00156                               MED_FULL_INTERLACE, nnode, beamsectioncoo) < 0) {
00157     MESSAGE("ERROR : write nodes coordinates ...");
00158     goto ERROR;
00159   }
00160 
00161   if (MEDmeshElementConnectivityWr(fid, beamsectionname, MED_NO_DT, MED_NO_IT, 0.0, MED_CELL, MED_QUAD4,
00162                                    MED_NODAL, MED_FULL_INTERLACE, nquad4, beamsectionconnectivity) < 0) {
00163     MESSAGE("ERROR : write cells connectivity ...");
00164     goto ERROR;
00165   }
00166 
00167   /* STEP 2 */
00168   /* particle model creation : no support mesh */
00169   strcpy(elementname,MED_PARTICLE_NAME);
00170   if ((geotype = MEDstructElementCr(fid, elementname, elementdim, MED_NO_MESHNAME,
00171                                     MED_NONE,MED_NONE)) < 0) {
00172     MESSAGE("ERROR : creating struct element");
00173     goto ERROR;
00174   }
00175   ncomp=1;
00176   if (MEDstructElementVarAttCr(fid, elementname,
00177                                MED_PARTICLE_LABEL, MED_ATT_INT, ncomp) < 0) {
00178     MESSAGE("ERROR : creating struct element");
00179     goto ERROR;
00180   }
00181 
00182   /* ball model creation */
00183   strcpy(elementname,MED_BALL_NAME);
00184   if ((geotype = MEDstructElementCr(fid, elementname, elementdim, ballsupportname,
00185                                     MED_NODE,MED_NONE)) < 0) {
00186     MESSAGE("ERROR : creating struct element");
00187     goto ERROR;
00188   }
00189   ncomp=1;
00190   if (MEDstructElementVarAttCr(fid, elementname,
00191                                MED_BALL_DIAMETER, MED_ATT_FLOAT64, ncomp) < 0) {
00192     MESSAGE("ERROR : creating struct element");
00193     goto ERROR;
00194   }
00195 
00196 
00197   /* A beam */
00198   strcpy(elementname,MED_BEAM_NAME);
00199   if ((geotype = MEDstructElementCr(fid, elementname, elementdim, beamsupportname,
00200                                     MED_CELL,MED_SEG2)) < 0) {
00201     MESSAGE("ERROR : creating struct element");
00202     goto ERROR;
00203   }
00204   ncomp=1;
00205   /* a first constant attribute */
00206   if (MEDstructElementConstAttWr(fid, elementname,
00207                                  MED_BEAM_THICKNESS, MED_ATT_FLOAT64, ncomp,
00208                                  MED_CELL,(void*) attvalue) < 0) {
00209     MESSAGE("ERROR : creating struct element");
00210     goto ERROR;
00211   }
00212   /* a second constant attribute defined with a profile for the first and the
00213      last segment */
00214   /* create the profile */
00215     if (MEDprofileWr(fid, profilename, profilesize, profile ) < 0) {
00216     MESSAGE("ERROR : create profile ...");
00217     goto ERROR;
00218   }
00219   /* write the constant attribute */
00220   if (MEDstructElementConstAttWithProfileWr(fid,
00221                                             elementname,
00222                                             "BEAM_EXTREMITIES_LABELS",
00223                                             MED_ATT_NAME,
00224                                             ncomp,
00225                                             MED_CELL,
00226                                             profilename,
00227                                             (void*) attprovalue) < 0) {
00228     MESSAGE("ERROR : creating struct element");
00229     goto ERROR;
00230   }
00231 
00232   /* STEP 3 : Computation mesh creation */
00233 
00234   /* mesh creation */
00235   if (MEDmeshCr(fid, computmeshname, spacedim, meshdim,  MED_UNSTRUCTURED_MESH,
00236                 "Computation mesh", "s", MED_SORT_DTIT,
00237                 MED_CARTESIAN, axisname, unitname) < 0) {
00238     MESSAGE("ERROR : creating computation mesh ...");
00239     goto ERROR;
00240   }
00241 
00242   /* mesh node creation */
00243   nnode = 12;
00244   if (MEDmeshNodeCoordinateWr(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_UNDEF_DT,
00245                               MED_FULL_INTERLACE, nnode, meshcoo) < 0) {
00246     MESSAGE("ERROR : writing nodes coordinates ...");
00247     goto ERROR;
00248   }
00249 
00250   /* 1 beam */
00251   nentity = 1;
00252   SSCRUTE(elementname);
00253   geotype = MEDstructElementGeotype(fid,elementname);
00254   ISCRUTE(geotype);
00255 
00256   if (MEDmeshElementConnectivityWr(fid,computmeshname, MED_NO_DT, MED_NO_IT, MED_UNDEF_DT,
00257                                    MED_STRUCT_ELEMENT, geotype, MED_NODAL,
00258                                    MED_FULL_INTERLACE, nentity, beamconnectivity) < 0 ) {
00259     MESSAGE("ERROR : beam 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 
00308   ret=0;
00309  ERROR:
00310 
00311   /* close file */
00312   if (MEDfileClose(fid) < 0) {
00313     MESSAGE("ERROR : file closing");
00314     ret=-1;
00315   }
00316 
00317   return ret;
00318 }
00319 

Généré le Thu Oct 8 14:26:17 2015 pour MED fichier par  doxygen 1.6.1