From 539c38d894d68c1008699edab1473baa7edb8ab3 Mon Sep 17 00:00:00 2001 From: ageay Date: Thu, 12 Apr 2012 08:10:15 +0000 Subject: [PATCH] Python examples in doc. --- doc/doxygen/CMakeLists.txt | 7 +- doc/doxygen/Doxyfile_med_user.in | 4 +- ...gexamples.dox => medcouplingexamples.doxy} | 6 +- src/MEDCoupling/MEDCouplingFieldDouble.cxx | 2 + src/MEDCoupling_Swig/CMakeLists.txt | 1 + .../MEDCouplingExamplesTest.py | 140 ++++++++++++++++++ 6 files changed, 155 insertions(+), 5 deletions(-) rename doc/doxygen/{medcouplingexamples.dox => medcouplingexamples.doxy} (86%) create mode 100644 src/MEDCoupling_Swig/MEDCouplingExamplesTest.py diff --git a/doc/doxygen/CMakeLists.txt b/doc/doxygen/CMakeLists.txt index ae0a28b15..9a72b7e21 100644 --- a/doc/doxygen/CMakeLists.txt +++ b/doc/doxygen/CMakeLists.txt @@ -32,8 +32,13 @@ FOREACH(indoxfile ${indoxfiles}) CONFIGURE_FILE(${input} ${output}) MESSAGE(STATUS "Creation of ${output}") ENDFOREACH(indoxfile ${indoxfiles}) +FILE(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/medcouplingexamples.doxy" input) +FILE(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/BuildPyExamplesFromCPP.py" pythondocexamplesgenerator) +FILE(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}" output) -ADD_CUSTOM_TARGET(usr_docs ${DOXYGEN_EXECUTABLE} Doxyfile_med_user +ADD_CUSTOM_TARGET(usr_docs + COMMAND ${PYTHON_EXECUTABLE} ${pythondocexamplesgenerator} ${input} ${output} + COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile_med_user COMMAND ${PYTHON_EXECUTABLE} -c "import shutil, sys; shutil.rmtree(r'''${CMAKE_INSTALL_PREFIX}/share/doc/salome/gui/MED''', True); shutil.copytree(r'''${CMAKE_CURRENT_BINARY_DIR}/doc_ref_user/html''', r'''${CMAKE_INSTALL_PREFIX}/share/doc/salome/gui/MED'''); shutil.copy(r'''${CMAKE_CURRENT_SOURCE_DIR}/images/head.png''', r'''${CMAKE_INSTALL_PREFIX}/share/doc/salome/gui/MED''')" VERBATIM WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} diff --git a/doc/doxygen/Doxyfile_med_user.in b/doc/doxygen/Doxyfile_med_user.in index 8163cbdcd..33b8d5873 100644 --- a/doc/doxygen/Doxyfile_med_user.in +++ b/doc/doxygen/Doxyfile_med_user.in @@ -68,7 +68,8 @@ WARN_LOGFILE = log_user #--------------------------------------------------------------------------- # configuration options related to the input files #--------------------------------------------------------------------------- -INPUT = @srcdir@ \ +INPUT = @builddir@ \ + @srcdir@ \ @srcdir@/../../src/ParaMEDMEM \ @srcdir@/../../src/INTERP_KERNEL \ @srcdir@/../../src/INTERP_KERNEL/Bases \ @@ -136,6 +137,7 @@ EXCLUDE = CVS EXCLUDE_PATTERNS = *~ EXAMPLE_PATH = @srcdir@/../../src/ParaMEDMEM \ @srcdir@/../../src/MEDCoupling/Test \ + @srcdir@/../../src//MEDCoupling_Swig \ @srcdir@/../../doc/MEDMEM \ @srcdir@/../../src/MEDMEMBinTest \ @srcdir@/../../src/MEDMEM diff --git a/doc/doxygen/medcouplingexamples.dox b/doc/doxygen/medcouplingexamples.doxy similarity index 86% rename from doc/doxygen/medcouplingexamples.dox rename to doc/doxygen/medcouplingexamples.doxy index 5c542ae45..777ecf822 100644 --- a/doc/doxygen/medcouplingexamples.dox +++ b/doc/doxygen/medcouplingexamples.doxy @@ -33,9 +33,9 @@ Now let's create a subfield on nodes \b f2 from \b f1. \b f1 is a field on nodes, but \ref ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart "buildSubPart" method performs an extraction on \b cells. -After the call of \ref ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart "buildSubPart" on node field \b f1, f1 will be reduced on a -submesh of \b mesh1 containing cells whoses ids are in \b part2. -So contrary to fields on cells, it is normal for fields on nodes that number of tuples of the returned field of \ref ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart "buildSubPart" +After the call of \ref ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart "buildSubPart" on node field \b f1, \b f1 will be reduced on a +submesh of \b mesh1 containing cells whoses ids are in \b part2. So here the number of cells of \b f2 is 2 and the number of nodes is 4. +\nSo contrary to fields on cells, it is normal for fields on nodes that number of tuples of the returned field of \ref ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart "buildSubPart" method does not match the size of the input array (here \b part2). */ diff --git a/src/MEDCoupling/MEDCouplingFieldDouble.cxx b/src/MEDCoupling/MEDCouplingFieldDouble.cxx index 30500a0f4..070680bc3 100644 --- a/src/MEDCoupling/MEDCouplingFieldDouble.cxx +++ b/src/MEDCoupling/MEDCouplingFieldDouble.cxx @@ -398,6 +398,8 @@ MEDCouplingFieldDouble *MEDCouplingFieldDouble::buildSubPart(const DataArrayInt * will contain 6 tuples and this field will lie on this restricted mesh. * * \ref cpp_mcfielddouble_subpart1 "Here a C++ example." + * + * \ref py_mcfielddouble_subpart1 "Here a Python example." */ MEDCouplingFieldDouble *MEDCouplingFieldDouble::buildSubPart(const int *partBg, const int *partEnd) const throw(INTERP_KERNEL::Exception) { diff --git a/src/MEDCoupling_Swig/CMakeLists.txt b/src/MEDCoupling_Swig/CMakeLists.txt index 834976089..1490d516d 100644 --- a/src/MEDCoupling_Swig/CMakeLists.txt +++ b/src/MEDCoupling_Swig/CMakeLists.txt @@ -52,4 +52,5 @@ INSTALL(FILES MEDCoupling.i MEDCouplingRemapper.i MEDCouplingTypemaps.i DESTINAT INSTALL_AND_COMPILE_PYTHON_FILE("${PYFILES_TO_INSTALL}" ${MED_salomescript_PYTHON}) ADD_TEST(MEDCouplingBasicsTest ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/MEDCouplingBasicsTest.py) +ADD_TEST(MEDCouplingExamplesTest ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/MEDCouplingExamplesTest.py) ADD_TEST(MEDCouplingRemapperTest ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/MEDCouplingRemapperTest.py) diff --git a/src/MEDCoupling_Swig/MEDCouplingExamplesTest.py b/src/MEDCoupling_Swig/MEDCouplingExamplesTest.py new file mode 100644 index 000000000..dcbc4d262 --- /dev/null +++ b/src/MEDCoupling_Swig/MEDCouplingExamplesTest.py @@ -0,0 +1,140 @@ +# -*- coding: iso-8859-1 -*- +# Copyright (C) 2007-2011 CEA/DEN, EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +from MEDCoupling import * +import unittest + +class MEDCouplingBasicsTest(unittest.TestCase): + def testExampleFieldDoubleBuildSubPart1(self): + from MEDCouplingDataForTest import MEDCouplingDataForTest +#! [PySnippetFieldDoubleBuildSubPart1_1] + mesh1=MEDCouplingDataForTest.build2DTargetMesh_1(); + f1=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME); + f1.setTime(2.3,5,6); + f1.setMesh(mesh1); + array=DataArrayDouble.New(); + arr1=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.] + array.setValues(arr1,mesh1.getNumberOfCells(),2); + f1.setArray(array); +## ! [PySnippetFieldDoubleBuildSubPart1_1] +## ! [PySnippetFieldDoubleBuildSubPart1_2] + part1=[2,1,4] + f2=f1.buildSubPart(part1); +## ! [PySnippetFieldDoubleBuildSubPart1_2] + f2.zipCoords() + self.failUnlessEqual(3,f2.getNumberOfTuples()); + self.failUnlessEqual(2,f2.getNumberOfComponents()); + expected1=[5.,105.,4.,104.,7.,107.] + for i in xrange(6): + self.assertAlmostEqual(f2.getIJ(0,i),expected1[i],12); + pass + self.failUnlessEqual(3,f2.getMesh().getNumberOfCells()); + self.failUnlessEqual(6,f2.getMesh().getNumberOfNodes()); + self.failUnlessEqual(2,f2.getMesh().getSpaceDimension()); + self.failUnlessEqual(2,f2.getMesh().getMeshDimension()); + m2C=f2.getMesh(); + self.failUnlessEqual(13,m2C.getMeshLength()); + expected2=[0.2, -0.3, 0.7, -0.3, 0.2, 0.2, 0.7, 0.2, 0.2, 0.7, 0.7, 0.7] + for i in xrange(12): + self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12); + pass + expected3=[3,2,3,1,3,0,2,1,4,4,5,3,2] + self.failUnlessEqual(expected3,list(m2C.getNodalConnectivity().getValues())); + expected4=[0,4,8,13] + self.failUnlessEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues())); + # Test with field on nodes. +## ! [PySnippetFieldDoubleBuildSubPart1_3] + f1=MEDCouplingFieldDouble.New(ON_NODES,ONE_TIME); + f1.setTime(2.3,5,6); + f1.setMesh(mesh1); + array=DataArrayDouble.New(); + arr2=[3.,103.,4.,104.,5.,105.,6.,106.,7.,107.,8.,108.,9.,109.,10.,110.,11.,111.] + array.setValues(arr2,mesh1.getNumberOfNodes(),2); + f1.setArray(array); +## ! [PySnippetFieldDoubleBuildSubPart1_3] +## ! [PySnippetFieldDoubleBuildSubPart1_4] + part2=[1,2] + f2=f1.buildSubPart(part2); +## ! [PySnippetFieldDoubleBuildSubPart1_4] + self.failUnlessEqual(4,f2.getNumberOfTuples()); + self.failUnlessEqual(2,f2.getNumberOfComponents()); + expected5=[4.,104.,5.,105.,7.,107.,8.,108.] + for i in xrange(8): + self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12); + pass + self.failUnlessEqual(2,f2.getMesh().getNumberOfCells()); + self.failUnlessEqual(4,f2.getMesh().getNumberOfNodes()); + self.failUnlessEqual(2,f2.getMesh().getSpaceDimension()); + self.failUnlessEqual(2,f2.getMesh().getMeshDimension()); + m2C=f2.getMesh(); + self.failUnlessEqual(8,m2C.getMeshLength()); + for i in xrange(8):#8 is not an error + self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12); + pass + self.failUnlessEqual(expected3[:4],list(m2C.getNodalConnectivity().getValues())[4:]); + self.failUnlessEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[:4]); + self.failUnlessEqual(expected4[:3],list(m2C.getNodalConnectivityIndex().getValues())); + #idem previous because nodes of cell#4 are not fully present in part3 + part3=[1,2] + arrr=DataArrayInt.New(); + arrr.setValues(part3,2,1); + f2=f1.buildSubPart(arrr); + self.failUnlessEqual(4,f2.getNumberOfTuples()); + self.failUnlessEqual(2,f2.getNumberOfComponents()); + for i in xrange(8): + self.assertAlmostEqual(f2.getIJ(0,i),expected5[i],12); + pass + self.failUnlessEqual(2,f2.getMesh().getNumberOfCells()); + self.failUnlessEqual(4,f2.getMesh().getNumberOfNodes()); + self.failUnlessEqual(2,f2.getMesh().getSpaceDimension()); + self.failUnlessEqual(2,f2.getMesh().getMeshDimension()); + m2C=f2.getMesh(); + self.failUnlessEqual(8,m2C.getMeshLength()); + for i in xrange(8):#8 is not an error + self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12); + pass + self.failUnlessEqual(expected3[:4],list(m2C.getNodalConnectivity().getValues())[4:8]); + self.failUnlessEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[:4]); + self.failUnlessEqual(expected4[:3],list(m2C.getNodalConnectivityIndex().getValues())); + part4=[1,2,4] + f2=f1.buildSubPart(part4); + self.failUnlessEqual(6,f2.getNumberOfTuples()); + self.failUnlessEqual(2,f2.getNumberOfComponents()); + expected6=[4.,104.,5.,105.,7.,107.,8.,108.,10.,110.,11.,111.] + for i in xrange(12): + self.assertAlmostEqual(f2.getIJ(0,i),expected6[i],12); + pass + self.failUnlessEqual(3,f2.getMesh().getNumberOfCells()); + self.failUnlessEqual(6,f2.getMesh().getNumberOfNodes()); + self.failUnlessEqual(2,f2.getMesh().getSpaceDimension()); + self.failUnlessEqual(2,f2.getMesh().getMeshDimension()); + m2C=f2.getMesh(); + self.failUnlessEqual(13,m2C.getMeshLength()); + for i in xrange(12): + self.assertAlmostEqual(expected2[i],m2C.getCoords().getIJ(0,i),12); + pass + self.failUnlessEqual(expected3[0:4],list(m2C.getNodalConnectivity().getValues())[4:8]); + self.failUnlessEqual(expected3[4:8],list(m2C.getNodalConnectivity().getValues())[0:4]); + self.failUnlessEqual(expected3[8:13],list(m2C.getNodalConnectivity().getValues())[8:13]); + self.failUnlessEqual(expected4,list(m2C.getNodalConnectivityIndex().getValues())); + pass + pass + +unittest.main() -- 2.39.2