Back to index

salome-med  6.5.0
med_opfield_test.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 ############################################################################
00025 # this Python script is testing all operations between FIELD(DOUBLE,INT)
00026 ############################################################################
00027 #
00028 from libMEDMEM_Swig import *
00029 from random import *
00030 import os
00031 #
00032 #befor running this script, please be sure about the path the file fileName
00033 #
00034 filePath=os.environ["MED_ROOT_DIR"]
00035 filePath=os.path.join(filePath, "share", "salome", "resources", "med")
00036 
00037 medFile = os.path.join( filePath, "pointe.med" )
00038 
00039 def add_one(i):
00040     return i+1
00041 
00042 def print_ord(i):
00043     if i == 0:
00044         return 'first'
00045     elif i == 1:
00046         return 'second'
00047     elif i == 2:
00048         return 'third'
00049     else:
00050         return `i`+'th'
00051 
00052 md = MEDFILEBROWSER(medFile)
00053 nbMeshes = md.getNumberOfMeshes()
00054 nbFields = md.getNumberOfFields()
00055 
00056 print "The med file", medFile, "contains", nbMeshes, "mesh(es) and", nbFields, "field(s)"
00057 
00058 if (nbMeshes>0):
00059     print "Mesh(es) Name(s) is(are) "
00060 
00061     for i in range(nbMeshes):
00062         mesh_name = md.getMeshName(i)
00063         print "   - ",mesh_name
00064 
00065 if (nbFields>0):
00066     print "Field(s) Name(s) is(are) "
00067 
00068     for i in range(nbFields):
00069         field_name = md.getFieldName(i)
00070         print "   - ",field_name
00071 
00072 print ""
00073 
00074 mesh_name = md.getMeshName(0)
00075 mesh = MESH(MED_DRIVER,md.getFileName(),mesh_name)
00076 spaceDim = mesh.getSpaceDimension()
00077 meshDim = mesh.getMeshDimension()
00078 nbNodes = mesh.getNumberOfNodes()
00079 print "The mesh",mesh_name,"is a",spaceDim,"D mesh on a",meshDim,"D geometry and has",nbNodes,"Nodes"
00080 
00081 if (nbFields>0):
00082     print ""
00083     print "Field(s) Analysis "
00084     for i in range(nbFields):
00085         print ""
00086         field_name = md.getFieldName(i)
00087         dtits = md.getFieldIteration(field_name)
00088         nbOfIt = len(dtits)
00089         print "The",print_ord(i),"field is",field_name,"with",nbOfIt,"iteration(s)"
00090         for dtitfield in dtits:
00091             print ""
00092             dt = dtitfield.getdt()
00093             it = dtitfield.getit()
00094             type = md.getFieldType(field_name)
00095             print "     * Iteration:",dt,"Order number:",it,"Type:",type
00096             if type == MED_INT32:
00097                 fieldint = FIELDINT(MED_DRIVER,md.getFileName(),field_name,dt,it,mesh)
00098                 name = fieldint.getName()
00099                 desc = fieldint.getDescription()
00100                 nbOfComp = fieldint.getNumberOfComponents()
00101                 print "     Field",name," : ",desc
00102                 print "     Number Of Components:",nbOfComp
00103                 iterationNb = fieldint.getIterationNumber()
00104                 orderNb = fieldint.getOrderNumber()
00105                 time = fieldint.getTime()
00106                 print "     Iteration Number",iterationNb
00107                 print "     Order Number",orderNb
00108                 print "     Time",time
00109               print "Norme  2  : ", fieldint.norm2()
00110               print "Norme Max : ", fieldint.normMax()
00111 
00112               if fielddouble.getSupport().getEntity()!=MED_NODE:
00113                   fieldint_vol=fieldint.getSupport().getMesh().getVolume(fieldint.getSupport())
00114                   print "Norme L1  : ", fieldint.normL1()
00115                   print "Norme L2  : ", fieldint.normL2()
00116                   print "Norme L2(vol) : ", fieldint.normL2(fieldint_vol)
00117 
00118                 for k in range(nbOfComp):
00119                     kp1 = k+1
00120                     compName = fieldint.getComponentName(kp1)
00121                     compDesc = fieldint.getComponentDescription(kp1)
00122                     compUnit = fieldint.getMEDComponentUnit(kp1)
00123                     print "      * Component:",kp1
00124                     print "          Name:",compName
00125                     print "          Description:",compDesc
00126                     print "          Unit:",compUnit
00127                   if fielddouble.getSupport().getEntity()!=MED_NODE:
00128                      print "          Norme L1  : ", fieldint.normL1(kp1)
00129                      print "          Norme L2  : ", fieldint.normL2(kp1)
00130                      print "          Norme L2(vol) : ", fieldint.normL2(kp1,fieldint_vol)
00131 
00132                 support = fieldint.getSupport()
00133                 nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00134                 print "     Values:",nbOf
00135                 for k in range(nbOf):
00136                     valueI = fieldint.getRow(k+1)
00137                     print "     *",valueI[:nbOfComp]
00138                 fieldint2 = FIELDINT(fieldint)
00139                 print ""
00140                 fieldintadd = fieldint + fieldint2
00141                 print "Test of the addition of two integer fields with creation a new one"
00142                 print ""
00143                 name = fieldintadd.getName()
00144                 desc = fieldintadd.getDescription()
00145                 nbOfComp = fieldintadd.getNumberOfComponents()
00146                 print "     Field",name," : ",desc
00147                 print "     Number Of Components:",nbOfComp
00148                 iterationNb = fieldintadd.getIterationNumber()
00149                 orderNb = fieldintadd.getOrderNumber()
00150                 time = fieldintadd.getTime()
00151                 print "     Iteration Number",iterationNb
00152                 print "     Order Number",orderNb
00153                 print "     Time",time
00154                 for k in range(nbOfComp):
00155                     kp1 = k+1
00156                     compName = fieldintadd.getComponentName(kp1)
00157                     compDesc = fieldintadd.getComponentDescription(kp1)
00158                     compUnit = fieldintadd.getMEDComponentUnit(kp1)
00159                     print "      * Component:",kp1
00160                     print "          Name:",compName
00161                     print "          Description:",compDesc
00162                     print "          Unit:",compUnit
00163 
00164                 support = fieldintadd.getSupport()
00165                 nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00166                 print "     Values:",nbOf
00167                 for k in range(nbOf):
00168                     valueI = fieldintadd.getRow(k+1)
00169                     print "     *",valueI[:nbOfComp]
00170                 print ""
00171                 fieldintsub = fieldint - fieldint2
00172                 print "Test of the substraction of two integer fields with creation a new one"
00173                 print ""
00174                 name = fieldintsub.getName()
00175                 desc = fieldintsub.getDescription()
00176                 nbOfComp = fieldintsub.getNumberOfComponents()
00177                 print "     Field",name," : ",desc
00178                 print "     Number Of Components:",nbOfComp
00179                 iterationNb = fieldintsub.getIterationNumber()
00180                 orderNb = fieldintsub.getOrderNumber()
00181                 time = fieldintsub.getTime()
00182                 print "     Iteration Number",iterationNb
00183                 print "     Order Number",orderNb
00184                 print "     Time",time
00185                 for k in range(nbOfComp):
00186                     kp1 = k+1
00187                     compName = fieldintsub.getComponentName(kp1)
00188                     compDesc = fieldintsub.getComponentDescription(kp1)
00189                     compUnit = fieldintsub.getMEDComponentUnit(kp1)
00190                     print "      * Component:",kp1
00191                     print "          Name:",compName
00192                     print "          Description:",compDesc
00193                     print "          Unit:",compUnit
00194 
00195                 support = fieldintsub.getSupport()
00196                 nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00197                 print "     Values:",nbOf
00198                 for k in range(nbOf):
00199                     valueI = fieldintsub.getRow(k+1)
00200                     print "     *",valueI[:nbOfComp]
00201                 print ""
00202                 fieldintmul = fieldint * fieldint2
00203                 print "Test of the multiplication of two integer fields with creation a new one"
00204                 print ""
00205                 name = fieldintmul.getName()
00206                 desc = fieldintmul.getDescription()
00207                 nbOfComp = fieldintmul.getNumberOfComponents()
00208                 print "     Field",name," : ",desc
00209                 print "     Number Of Components:",nbOfComp
00210                 iterationNb = fieldintmul.getIterationNumber()
00211                 orderNb = fieldintmul.getOrderNumber()
00212                 time = fieldintmul.getTime()
00213                 print "     Iteration Number",iterationNb
00214                 print "     Order Number",orderNb
00215                 print "     Time",time
00216                 for k in range(nbOfComp):
00217                     kp1 = k+1
00218                     compName = fieldintmul.getComponentName(kp1)
00219                     compDesc = fieldintmul.getComponentDescription(kp1)
00220                     compUnit = fieldintmul.getMEDComponentUnit(kp1)
00221                     print "      * Component:",kp1
00222                     print "          Name:",compName
00223                     print "          Description:",compDesc
00224                     print "          Unit:",compUnit
00225 
00226                 support = fieldintmul.getSupport()
00227                 nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00228                 print "     Values:",nbOf
00229                 for k in range(nbOf):
00230                     valueI = fieldintmul.getRow(k+1)
00231                     print "     *",valueI[:nbOfComp]
00232                 print ""
00233                 try:
00234                     fieldintdiv = fieldint / fieldint2
00235                     print "Test of the division of two integer fields with creation a new one"
00236                     print ""
00237                     name = fieldintdiv.getName()
00238                     desc = fieldintdiv.getDescription()
00239                     nbOfComp = fieldintdiv.getNumberOfComponents()
00240                     print "     Field",name," : ",desc
00241                     print "     Number Of Components:",nbOfComp
00242                     iterationNb = fieldintdiv.getIterationNumber()
00243                     orderNb = fieldintdiv.getOrderNumber()
00244                     time = fieldintdiv.getTime()
00245                     print "     Iteration Number",iterationNb
00246                     print "     Order Number",orderNb
00247                     print "     Time",time
00248                     for k in range(nbOfComp):
00249                         kp1 = k+1
00250                         compName = fieldintdiv.getComponentName(kp1)
00251                         compDesc = fieldintdiv.getComponentDescription(kp1)
00252                         compUnit = fieldintdiv.getMEDComponentUnit(kp1)
00253                         print "      * Component:",kp1
00254                         print "          Name:",compName
00255                         print "          Description:",compDesc
00256                         print "          Unit:",compUnit
00257 
00258                     support = fieldintdiv.getSupport()
00259                     nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00260                     print "     Values:",nbOf
00261                     for k in range(nbOf):
00262                         valueI = fieldintdiv.getRow(k+1)
00263                         print "     *",valueI[:nbOfComp]
00264 
00265                 except:
00266                     print "fieldintdiv = fieldint / fieldint2 catch/except error"
00267             elif type == MED_REEL64:
00268                 fielddouble = FIELDDOUBLE(MED_DRIVER,md.getFileName(),field_name,dt,it,mesh)
00269                 name = fielddouble.getName()
00270                 desc = fielddouble.getDescription()
00271                 nbOfComp = fielddouble.getNumberOfComponents()
00272                 print "     Field",name," : ",desc
00273                 print "     Number Of Components:",nbOfComp
00274                 iterationNb = fielddouble.getIterationNumber()
00275                 orderNb = fielddouble.getOrderNumber()
00276                 time = fielddouble.getTime()
00277                 print "     Iteration Number",iterationNb
00278                 print "     Order Number",orderNb
00279                 print "     Time",time
00280               print "Norme  2  : ", fielddouble.norm2()
00281               print "Norme Max : ", fielddouble.normMax()
00282               print "try sobolev",fielddouble.getSupport().getEntity()
00283               if fielddouble.getSupport().getEntity()!=MED_NODE:
00284                     if (spaceDim == 3) :
00285                         fielddouble_vol=fielddouble.getSupport().getMesh().getVolume(fielddouble.getSupport())
00286                     elif (spaceDim == 2):
00287                         fielddouble_vol=fielddouble.getSupport().getMesh().getArea(fielddouble.getSupport())
00288                   print "Norme L1  : ", fielddouble.normL1()
00289                   print "Norme L2  : ", fielddouble.normL2()
00290                   print "Norme L2(vol) : ", fielddouble.normL2(fielddouble_vol)
00291                   
00292                 for k in range(nbOfComp):
00293                     kp1 = k+1
00294                     compName = fielddouble.getComponentName(kp1)
00295                     compDesc = fielddouble.getComponentDescription(kp1)
00296                     compUnit = fielddouble.getMEDComponentUnit(kp1)
00297                     print "      * Component:",kp1
00298                     print "          Name:",compName
00299                     print "          Description:",compDesc
00300                     print "          Unit:",compUnit
00301                   if fielddouble.getSupport().getEntity()!=MED_NODE:
00302                      print "          Norme L1  : ", fielddouble.normL1(kp1)
00303                      print "          Norme L2  : ", fielddouble.normL2(kp1)
00304                      print "          Norme L2(vol) : ", fielddouble.normL2(kp1, fielddouble_vol)
00305 
00306                 support = fielddouble.getSupport()
00307                 nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00308                 print "     Values:",nbOf
00309                 for k in range(nbOf):
00310                     valueI = fielddouble.getRow(k+1)
00311                     print "     *",valueI[:nbOfComp]
00312                 fielddouble2 = FIELDDOUBLE(fielddouble)
00313                 print ""
00314                 fielddoubleadd = fielddouble + fielddouble2
00315                 print "Test of the addition of two double fields with creation a new one"
00316                 print ""
00317                 name = fielddoubleadd.getName()
00318                 desc = fielddoubleadd.getDescription()
00319                 nbOfComp = fielddoubleadd.getNumberOfComponents()
00320                 print "     Field",name," : ",desc
00321                 print "     Number Of Components:",nbOfComp
00322                 iterationNb = fielddoubleadd.getIterationNumber()
00323                 orderNb = fielddoubleadd.getOrderNumber()
00324                 time = fielddoubleadd.getTime()
00325                 print "     Iteration Number",iterationNb
00326                 print "     Order Number",orderNb
00327                 print "     Time",time
00328                 for k in range(nbOfComp):
00329                     kp1 = k+1
00330                     compName = fielddoubleadd.getComponentName(kp1)
00331                     compDesc = fielddoubleadd.getComponentDescription(kp1)
00332                     compUnit = fielddoubleadd.getMEDComponentUnit(kp1)
00333                     print "      * Component:",kp1
00334                     print "          Name:",compName
00335                     print "          Description:",compDesc
00336                     print "          Unit:",compUnit
00337 
00338                 support = fielddoubleadd.getSupport()
00339                 nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00340                 print "     Values:",nbOf
00341                 for k in range(nbOf):
00342                     valueI = fielddoubleadd.getRow(k+1)
00343                     print "     *",valueI[:nbOfComp]
00344                 print ""
00345                 fielddoublesub = fielddouble - fielddouble2
00346                 print "Test of the substraction of two double fields with creation a new one"
00347                 print ""
00348                 name = fielddoublesub.getName()
00349                 desc = fielddoublesub.getDescription()
00350                 nbOfComp = fielddoublesub.getNumberOfComponents()
00351                 print "     Field",name," : ",desc
00352                 print "     Number Of Components:",nbOfComp
00353                 iterationNb = fielddoublesub.getIterationNumber()
00354                 orderNb = fielddoublesub.getOrderNumber()
00355                 time = fielddoublesub.getTime()
00356                 print "     Iteration Number",iterationNb
00357                 print "     Order Number",orderNb
00358                 print "     Time",time
00359                 for k in range(nbOfComp):
00360                     kp1 = k+1
00361                     compName = fielddoublesub.getComponentName(kp1)
00362                     compDesc = fielddoublesub.getComponentDescription(kp1)
00363                     compUnit = fielddoublesub.getMEDComponentUnit(kp1)
00364                     print "      * Component:",kp1
00365                     print "          Name:",compName
00366                     print "          Description:",compDesc
00367                     print "          Unit:",compUnit
00368 
00369                 support = fielddoublesub.getSupport()
00370                 nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00371                 print "     Values:",nbOf
00372                 for k in range(nbOf):
00373                     valueI = fielddoublesub.getRow(k+1)
00374                     print "     *",valueI[:nbOfComp]
00375                 print ""
00376                 fielddoublemul = fielddouble * fielddouble2
00377                 print "Test of the multiplication of two double fields with creation a new one"
00378                 print ""
00379                 name = fielddoublemul.getName()
00380                 desc = fielddoublemul.getDescription()
00381                 nbOfComp = fielddoublemul.getNumberOfComponents()
00382                 print "     Field",name," : ",desc
00383                 print "     Number Of Components:",nbOfComp
00384                 iterationNb = fielddoublemul.getIterationNumber()
00385                 orderNb = fielddoublemul.getOrderNumber()
00386                 time = fielddoublemul.getTime()
00387                 print "     Iteration Number",iterationNb
00388                 print "     Order Number",orderNb
00389                 print "     Time",time
00390                 for k in range(nbOfComp):
00391                     kp1 = k+1
00392                     compName = fielddoublemul.getComponentName(kp1)
00393                     compDesc = fielddoublemul.getComponentDescription(kp1)
00394                     compUnit = fielddoublemul.getMEDComponentUnit(kp1)
00395                     print "      * Component:",kp1
00396                     print "          Name:",compName
00397                     print "          Description:",compDesc
00398                     print "          Unit:",compUnit
00399 
00400                 support = fielddoublemul.getSupport()
00401                 nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00402                 print "     Values:",nbOf
00403                 for k in range(nbOf):
00404                     valueI = fielddoublemul.getRow(k+1)
00405                     print "     *",valueI[:nbOfComp]
00406                 print ""
00407                 try:
00408                     fielddoublediv = fielddouble / fielddouble2
00409                     print "Test of the division of two double fields with creation a new one"
00410                     print ""
00411                     name = fielddoublediv.getName()
00412                     desc = fielddoublediv.getDescription()
00413                     nbOfComp = fielddoublediv.getNumberOfComponents()
00414                     print "     Field",name," : ",desc
00415                     print "     Number Of Components:",nbOfComp
00416                     iterationNb = fielddoublediv.getIterationNumber()
00417                     orderNb = fielddoublediv.getOrderNumber()
00418                     time = fielddoublediv.getTime()
00419                     print "     Iteration Number",iterationNb
00420                     print "     Order Number",orderNb
00421                     print "     Time",time
00422                     for k in range(nbOfComp):
00423                         kp1 = k+1
00424                         compName = fielddoublediv.getComponentName(kp1)
00425                         compDesc = fielddoublediv.getComponentDescription(kp1)
00426                         compUnit = fielddoublediv.getMEDComponentUnit(kp1)
00427                         print "      * Component:",kp1
00428                         print "          Name:",compName
00429                         print "          Description:",compDesc
00430                         print "          Unit:",compUnit
00431 
00432                     support = fielddoublediv.getSupport()
00433                     nbOf = support.getNumberOfElements(MED_ALL_ELEMENTS)
00434                     print "     Values:",nbOf
00435                     for k in range(nbOf):
00436                         valueI = fielddoublediv.getRow(k+1)
00437                         print "     *",valueI[:nbOfComp]
00438                 except:
00439                     print "fielddoublediv = fielddouble / fielddouble2 catch/except error"
00440             else:
00441                 print "  !!!! Bad type of Field !!!!"
00442 
00443 print ""
00444 print "TESTS OPERATIONS SUR FIELDDOUBLE : "
00445 
00446 
00447 fielddoublesub = fielddouble-fielddouble2
00448 fielddoublemul = fielddouble*fielddouble2
00449 
00450 try:
00451     fielddoublediv = fielddouble/fielddouble2
00452 except:
00453     pass
00454 
00455 fielddoubleasso = fielddouble+fielddouble2*fielddouble2
00456 fielddoubleSP=createFieldDoubleScalarProduct(fielddouble, fielddouble2)
00457 
00458 
00459 print " f1     : ",fielddouble.getValue()
00460 print " f2     : ",fielddouble2.getValue()
00461 print "--------------------------------------------------------------------------------------------------------------"
00462 print "  +     : ",fielddoubleadd.getValue()
00463 print "  -     : ",fielddoublesub.getValue()
00464 print "  *     : ",fielddoublemul.getValue()
00465 
00466 try:
00467     print "  /     : ",fielddoublediv.getValue()
00468 except:
00469     pass
00470 
00471 fielddouble+=fielddouble2;
00472 print "  +=    : ",fielddouble.getValue()
00473 fielddouble-=fielddouble2;
00474 print "  -=    : ",fielddouble.getValue()
00475 fielddouble*=fielddouble2;
00476 print "  *=    : ",fielddouble.getValue()
00477 fielddouble/=fielddouble2;
00478 print "  /=    : ",fielddouble.getValue()
00479 print "f1+f2*f2: ",fielddoubleasso.getValue()
00480 fielddouble.applyLin(4,1);
00481 print " 4f1+1  : ",fielddouble.getValue()
00482 print " f1.f2  : ",fielddoubleSP.getValue()
00483 fielddouble2.applyPyFunc(add_one)
00484 print " CB:f2+1: ",fielddouble2.getValue()
00485 
00486 print ""
00487 print "TESTS OPERATIONS SUR FIELDINT : "
00488 
00489 
00490 fieldintadd = fieldint+fieldint2
00491 fieldintsub = fieldint-fieldint2
00492 fieldintmul = fieldint*fieldint2
00493 
00494 try:
00495     fieldintdiv = fieldint/fieldint2
00496 except:
00497     pass
00498 
00499 fieldintasso = fieldint+fieldint*fieldint
00500 fieldintSP=createFieldIntScalarProduct(fieldint, fieldint2)
00501 
00502 print " f1     : ",fieldint.getValue()
00503 print " f2     : ",fieldint2.getValue()
00504 print "--------------------------------------------------------------------------------------------------------------"
00505 print "  +     : ",fieldintadd.getValue()
00506 print "  -     : ",fieldintsub.getValue()
00507 print "  *     : ",fieldintmul.getValue()
00508 
00509 try:
00510     print "  /     : ",fieldintdiv.getValue()
00511 except:
00512     pass
00513 
00514 fieldint+=fieldint2;
00515 print "  +=    : ",fieldint.getValue()
00516 fieldint-=fieldint2;
00517 print "  -=    : ",fieldint.getValue()
00518 fieldint*=fieldint2;
00519 print "  *=    : ",fieldint.getValue()
00520 fieldint/=fieldint2;
00521 print "  /=    : ",fieldint.getValue()
00522 print "f1+f2*f2: ",fieldintasso.getValue()
00523 fieldint.applyLin(4,1);
00524 print " 4f1+1  : ",fieldint.getValue()
00525 print " f1.f2  : ",fieldintSP.getValue()
00526 fieldint2.applyPyFunc(add_one)
00527 print " CB:f2+1: ",fieldint2.getValue()
00528 myname="toto"
00529 fieldint.setName(myname)
00530 name = fieldint.getName()
00531 print "test de setName : ",name
00532 print "END of the Pyhton script ..... Ctrl D to exit"