1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2021-2022 CEA/DEN, EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author : Anthony GEAY (EDF R&D)
23 import medcoupling as mc
25 def __to_geomshape_3D(mcmesh):
27 Precondition mcmesh is a MEDCouplingUMesh containing exactly one linear 3D cell.
33 from salome.geom import geomBuilder
34 geompy = geomBuilder.New()
36 mcmesh2 = mcmesh.deepCopyConnectivityOnly()
37 vertices = [ geompy.MakeVertex(*list(elt)) for elt in mcmesh2.getCoords()]
39 mcfaces = mcmesh2.buildDescendingConnectivity()[0]
40 shell_1 = geompy.MakeShell(
44 geompy.MakePolyline([ vertices[nodeidx] for nodeidx in elt.getAllConn()[1:] ], True)
50 return geompy.MakeSolid([shell_1])
52 def to_geomshape(mcmesh):
54 Method converting a unique 3D linear cell in a MEDCoupling mesh to GEOM Shape solid
56 :param mcmesh: Mesh with single 3D linear cell.
57 :type mcmesh: mc.MEDCouplingUMesh
61 if not isinstance(mcmesh,mc.MEDCouplingUMesh):
62 raise RuntimeError("Input mesh is expected to be of type mc.MEDCouplingUMesh !")
63 if mcmesh.getNumberOfCells() != 1:
64 raise RuntimeError("Input mesh is expected to contain exactly one cell !")
65 if not mc.MEDCouplingUMesh.IsLinearGeometricType( mc.MEDCoupling1SGTUMesh(mcmesh).getCellModelEnum() ) :
66 raise RuntimeError("The unique cell in the mesh is expected to be linear !")
67 dico = { 3 : __to_geomshape_3D }
68 mdim = mcmesh.getMeshDimension()
70 raise RuntimeError( "Input mesh is expected to have mesh dimension in {}".format(list(dico.keys())) )
71 if mcmesh.getSpaceDimension() != 3:
72 mcmesh.changeSpaceDimension(3,0.0)
73 return (dico[mdim])(mcmesh)
75 def compute_interpolation_P0P0_matrix_with_geom(srcMesh,trgMesh):
77 Method computing interpolation matrix P0P0 using GEOM/OCCT engine.
78 This method is normaly slower than mc.MEDCouplingRemapper.prepare method but it may be used to check values.
80 :return: a matrix with the same format than mc.MEDCouplingRemapper.getCrudeMatrix (use mc.MEDCouplingRemapper.ToCSRMatrix to convert it into scipy sparse format)
82 mat_geom = [ {} for i in range(trgMesh.getNumberOfCells()) ]
83 for j in range(trgMesh.getNumberOfCells()):
84 for i in range(srcMesh.getNumberOfCells()):
85 mc_src_mesh = srcMesh[i]
86 src_geom = to_geomshape(mc_src_mesh)
87 mc_trg_mesh = trgMesh[j]
88 trg_geom = to_geomshape(mc_trg_mesh)
90 from salome.geom import geomBuilder
91 geompy = geomBuilder.New()
93 geompy.ExportBREP(src_geom, "src.brep")
94 geompy.ExportBREP(trg_geom, "trg.brep")
95 Common_1 = geompy.MakeCommonList([src_geom, trg_geom], True)
96 _,_,volume = geompy.BasicProperties(Common_1)
98 mat_geom[j][i] = volume