Salome HOME
corrections to pass tests
[modules/hydro.git] / doc / salome / examples / g022_extensionSimpleComplete.py
1 # -*- coding: utf-8 -*-
2
3 import os
4 HYDRO_SAMPLES = os.path.join( os.environ["HYDRO_ROOT_DIR"], "bin/salome/test/HYDRO")
5 HYDRO_TEST_RESOURCES = os.path.join(os.environ["HYDRO_DIR"], "bin", "salome", "test", "tmp_test")
6 if not os.path.isdir(HYDRO_TEST_RESOURCES):
7     os.mkdir(HYDRO_TEST_RESOURCES)
8
9 import tempfile
10 tmpdir = tempfile.mkdtemp()
11 print("tmpdir=",tmpdir)
12
13 import sys
14 import salome
15
16 salome.salome_init()
17
18 #----------------------
19 # --- HYDRO
20 #----------------------
21
22 from HYDROPy import *
23 from PyQt5.QtCore import *
24 from PyQt5.QtGui import *
25
26 hydro_doc = HYDROData_Document.Document()
27
28 offsetX = 430000.
29 offsetY = 6350000.
30 hydro_doc.SetLocalCS( offsetX, offsetY )
31
32 name = "garonne"
33 shape = os.path.join(HYDRO_SAMPLES, name+".shp" )
34 HYDROData_PolylineXY.ImportShapesFromFile(shape)
35 garonne = hydro_doc.FindObjectByName(name + '_PolyXY_0')
36 for i in range(garonne.NbSections()):
37     garonne.SetSectionType(i, 1) # spline
38     garonne.Update()
39 garonne.SetZLevel( 2 )
40
41 name = "lit_majeur"
42 shape = os.path.join(HYDRO_SAMPLES, name+".shp" )
43 HYDROData_PolylineXY.ImportShapesFromFile(shape)
44 lit_majeur = hydro_doc.FindObjectByName(name + '_PolyXY_0')
45 for i in range(lit_majeur.NbSections()):
46     lit_majeur.SetSectionType(i, 1) # spline
47     lit_majeur.Update()
48 lit_majeur.SetZLevel( 3 )
49
50 name = "ile"
51 shape = os.path.join(HYDRO_SAMPLES, name+".shp" )
52 HYDROData_PolylineXY.ImportShapesFromFile(shape)
53 ile = hydro_doc.FindObjectByName(name + '_PolyXY_0')
54 for i in range(ile.NbSections()):
55     ile.SetSectionType(i, 1) # spline
56     ile.Update()
57 ile.SetZLevel( 4 )
58
59 name = "domaine"
60 shape = os.path.join(HYDRO_SAMPLES, name+".shp" )
61 HYDROData_PolylineXY.ImportShapesFromFile(shape)
62 domaine = hydro_doc.FindObjectByName(name + '_PolyXY_0')
63 for i in range(domaine.NbSections()):
64     domaine.SetSectionType(i, 0) # polyline
65     domaine.Update()
66 domaine.SetZLevel( 5 )
67
68 Cloud_02 = hydro_doc.CreateObject( KIND_BATHYMETRY )
69 Cloud_02.SetName( "Cloud_02" )
70
71 Cloud_02.SetAltitudesInverted( 0 )
72 if not(Cloud_02.ImportFromFile( os.path.join(HYDRO_SAMPLES, "Cloud_02.xyz" ))):
73   raise ValueError('problem while loading bathymetry')
74
75 Cloud_02.Update()
76
77
78 garonne_point_L93 = hydro_doc.CreateObject( KIND_BATHYMETRY )
79 garonne_point_L93.SetName( "garonne_point_L93" )
80
81 garonne_point_L93.SetAltitudesInverted( 0 )
82 if not(garonne_point_L93.ImportFromFile( os.path.join(HYDRO_SAMPLES, "garonne_point_L93.xyz" ))):
83   raise ValueError('problem while loading bathymetry')
84
85 garonne_point_L93.Update()
86
87
88 litMineur = hydro_doc.CreateObject( KIND_IMMERSIBLE_ZONE )
89 litMineur.SetName( "litMineur" )
90
91 litMineur.SetZLevel( 6 )
92
93 litMineur.SetAltitudeObject( garonne_point_L93 )
94 litMineur.SetPolyline( garonne )
95
96 litMineur.Update()
97
98
99 litMajeur = hydro_doc.CreateObject( KIND_IMMERSIBLE_ZONE )
100 litMajeur.SetName( "litMajeur" )
101
102 litMajeur.SetZLevel( 5 )
103
104 litMajeur.SetFillingColor( QColor( 0, 170, 127, 255 ) )
105
106 litMajeur.SetAltitudeObject( garonne_point_L93 )
107 litMajeur.SetPolyline( lit_majeur )
108
109 litMajeur.Update()
110
111 ilot = hydro_doc.CreateObject( KIND_IMMERSIBLE_ZONE )
112 ilot.SetName( "ilot" )
113
114 ilot.SetZLevel( 9 )
115
116 ilot.SetPolyline( ile )
117 ilot.SetIsSubmersible(False)
118
119 ilot.Update()
120
121 domaineEtendu = hydro_doc.CreateObject( KIND_IMMERSIBLE_ZONE )
122 domaineEtendu.SetName( "domaineEtendu" )
123
124 domaineEtendu.SetZLevel( 4 )
125
126 domaineEtendu.SetFillingColor( QColor( 201, 203, 55, 255 ) )
127
128 domaineEtendu.SetAltitudeObject( Cloud_02 )
129 domaineEtendu.SetPolyline( domaine )
130
131 domaineEtendu.Update()
132
133
134 # Calculation case
135 garonne_2 = hydro_doc.CreateObject( KIND_CALCULATION )
136 garonne_2.SetName( "garonne_2" )
137
138 garonne_2.SetAssignmentMode( HYDROData_CalculationCase.MANUAL )
139 garonne_2.AddGeometryObject( litMineur )
140 garonne_2.AddGeometryObject( domaineEtendu )
141 garonne_2.AddGeometryObject( litMajeur )
142 garonne_2.AddGeometryObject( ilot )
143
144 case_geom_group = domaineEtendu.GetGroup( 0 )
145 garonne_2.AddGeometryGroup( case_geom_group )
146 case_geom_group = litMineur.GetGroup( 0 )
147 garonne_2.AddGeometryGroup( case_geom_group )
148 case_geom_group = litMajeur.GetGroup( 0 )
149 garonne_2.AddGeometryGroup( case_geom_group )
150 garonne_2.SetBoundaryPolyline( domaine )
151
152 # Start the algorithm of the partition and assignment
153 garonne_2.Update()
154 garonne_2_litMineur = hydro_doc.FindObjectByName( "garonne_2_Reg_1" )
155 garonne_2_Zone_1 = hydro_doc.FindObjectByName( "garonne_2_Zone_1" )
156 garonne_2_Zone_1.SetMergeType( HYDROData_Zone.Merge_ZMIN )
157 garonne_2_Zone_1.SetColor( QColor( 192, 113, 64 ))
158 garonne_2_litMineur.AddZone( garonne_2_Zone_1 )
159
160 garonne_2_riveDroite = hydro_doc.FindObjectByName( "garonne_2_Reg_2" )
161 garonne_2_Zone_2 = hydro_doc.FindObjectByName( "garonne_2_Zone_2" )
162 garonne_2_Zone_2.SetColor( QColor( 141, 192, 64 ))
163 garonne_2_riveDroite.AddZone( garonne_2_Zone_2 )
164
165 garonne_2_Zone_3 = hydro_doc.FindObjectByName( "garonne_2_Zone_3" )
166 garonne_2_Zone_3.SetMergeType( HYDROData_Zone.Merge_Object )
167 Cloud_02 = hydro_doc.FindObjectByName( "Cloud_02" )
168 garonne_2_Zone_3.SetMergeObject( Cloud_02 )
169 garonne_2_Zone_3.SetColor( QColor( 64, 192, 77 ))
170 garonne_2_riveDroite.AddZone( garonne_2_Zone_3 )
171
172 garonne_2_riveGauche = hydro_doc.FindObjectByName( "garonne_2_Reg_3" )
173 garonne_2_Zone_4 = hydro_doc.FindObjectByName( "garonne_2_Zone_4" )
174 garonne_2_Zone_4.SetMergeType( HYDROData_Zone.Merge_Object )
175 Cloud_02 = hydro_doc.FindObjectByName( "Cloud_02" )
176 garonne_2_Zone_4.SetMergeObject( Cloud_02 )
177 garonne_2_Zone_4.SetColor( QColor( 64, 75, 192 ))
178 garonne_2_riveGauche.AddZone( garonne_2_Zone_4 )
179
180 garonne_2_Zone_5 = hydro_doc.FindObjectByName( "garonne_2_Zone_5" )
181 garonne_2_Zone_5.SetColor( QColor( 64, 192, 77 ))
182 garonne_2_riveGauche.AddZone( garonne_2_Zone_5 )
183
184 garonne_2_ile = hydro_doc.FindObjectByName( "garonne_2_Reg_4" )
185 garonne_2_Zone_6 = hydro_doc.FindObjectByName( "garonne_2_Zone_6" )
186 garonne_2_Zone_6.SetMergeType( HYDROData_Zone.Merge_ZMAX )
187 garonne_2_Zone_6.SetColor( QColor( 228, 145, 28 ))
188 garonne_2_ile.AddZone( garonne_2_Zone_6 )
189
190 garonne_2_litMineur.SetName("garonne_2_litMineur")
191 garonne_2_riveDroite.SetName("garonne_2_riveDroite")
192 garonne_2_riveGauche.SetName("garonne_2_riveGauche")
193 garonne_2_ile.SetName( "garonne_2_ile" )
194
195 # Export of the calculation case
196 garonne_2_entry = garonne_2.Export()
197
198 # --- add a new bathymetry for the test changeBathy
199
200 newBathy = os.path.join(tmpdir, 'newBathy.xyz')
201 fi=open(os.path.join(HYDRO_SAMPLES, "garonne_point_L93.xyz" ), 'r')
202 fo=open(newBathy, 'w')
203 for ligne in fi:
204     vals = ligne.split()
205     if len(vals) < 3:
206         continue
207     x = float(vals[0])
208     y = float(vals[1])
209     z = float(vals[2]) + 50
210     l = "%12.3f  %12.3f %12.3f\n" % (x, y, z)
211     fo.write(l)
212 fi.close()
213 fo.close()
214
215 #----------------------
216 # --- Geometry
217 #----------------------
218
219 # Get geometry shape and print debug information
220 import GEOM
221 from salome.geom import geomBuilder
222 import math
223 import SALOMEDS
224 from salome.hydrotools.controls import controlGeomProps
225
226 geompy = geomBuilder.New()
227
228 print("Entry:", garonne_2_entry)
229 HYDRO_garonne_2 = salome.IDToObject( str( garonne_2_entry ) )
230 print("Geom shape:", HYDRO_garonne_2)
231 print("Geom shape name:", HYDRO_garonne_2.GetName())
232
233 # --- manual definition: geometrical faces
234
235 [garonne_litMineur,garonne_riveDroite,garonne_riveGauche] = geompy.SubShapeAll(HYDRO_garonne_2, geompy.ShapeType["FACE"])
236
237 controlGeomProps(geompy, garonne_riveGauche,  33378.8047,   34595667.2876)
238 controlGeomProps(geompy, garonne_litMineur,   30337.548492,  3488480.304388)
239 controlGeomProps(geompy, garonne_riveDroite,  32012.343241, 25998769.23615)
240
241 # --- manual identification of all useful edge groups (boundary conditions)
242
243 allEdgesIds = geompy.SubShapeAllIDs(HYDRO_garonne_2, geompy.ShapeType["EDGE"])
244 print("allEdgesIds", allEdgesIds)
245
246 (isDone, ClosedFreeBoundary, OpenFreeBoundary) = geompy.GetFreeBoundary(HYDRO_garonne_2)
247 geompy.addToStudyInFather(HYDRO_garonne_2, ClosedFreeBoundary[0], "ClosedFreeBoundary")
248
249 freeBoundary = geompy.ExtractShapes(ClosedFreeBoundary[0], geompy.ShapeType["EDGE"], True)
250 freeBoundaryIds = [ geompy.GetSubShapeID(HYDRO_garonne_2, freeBoundary[i]) for i in range(len(freeBoundary)) ]
251 print("freeBoundaryIds", freeBoundaryIds)
252
253 [litMineur_droite] = geompy.GetSharedShapesMulti([garonne_riveDroite, garonne_litMineur], geompy.ShapeType["EDGE"], True)
254 [litMineur_gauche] = geompy.GetSharedShapesMulti([garonne_riveGauche, garonne_litMineur], geompy.ShapeType["EDGE"], True)
255 geompy.addToStudyInFather(HYDRO_garonne_2, litMineur_droite, "litMineur_droite")
256 geompy.addToStudyInFather(HYDRO_garonne_2, litMineur_gauche, "litMineur_gauche")
257 rives = [litMineur_droite, litMineur_gauche]
258 rivesIds = [ geompy.GetSubShapeID(HYDRO_garonne_2, rives[i]) for i in range(len(rives)) ]
259 print("rivesIds", rivesIds)
260
261 edges_litMineur = geompy.GetSharedShapesMulti([HYDRO_garonne_2, garonne_litMineur], geompy.ShapeType["EDGE"], True)
262 edges_riveGauche = geompy.GetSharedShapesMulti([HYDRO_garonne_2, garonne_riveGauche], geompy.ShapeType["EDGE"], True)
263 edges_riveDroite = geompy.GetSharedShapesMulti([HYDRO_garonne_2, garonne_riveDroite], geompy.ShapeType["EDGE"], True)
264 edges_litMineurIds = [ geompy.GetSubShapeID(HYDRO_garonne_2, edges_litMineur[i]) for i in range(len(edges_litMineur)) ]
265 edges_riveGaucheIds = [ geompy.GetSubShapeID(HYDRO_garonne_2, edges_riveGauche[i]) for i in range(len(edges_riveGauche)) ]
266 edges_riveDroiteIds = [ geompy.GetSubShapeID(HYDRO_garonne_2, edges_riveDroite[i]) for i in range(len(edges_riveDroite)) ]
267
268 print("edges_litMineurIds", edges_litMineurIds)
269 print("edges_riveGaucheIds", edges_riveGaucheIds)
270 print("edges_riveDroiteIds", edges_riveDroiteIds)
271
272 sectionsIds = [Id for Id in edges_litMineurIds if Id not in rivesIds]
273 print("sectionsIds", sectionsIds)
274 SectionsGaronne = geompy.CreateGroup(HYDRO_garonne_2, geompy.ShapeType["EDGE"])
275 geompy.UnionIDs(SectionsGaronne, sectionsIds)
276 geompy.addToStudyInFather(HYDRO_garonne_2, SectionsGaronne, "SectionsGaronne")
277
278 bordGaucheDomaineIds = [Id for Id in freeBoundaryIds if Id in edges_riveGaucheIds]
279 bordDroiteDomaineIds = [Id for Id in freeBoundaryIds if Id in edges_riveDroiteIds]
280 print("bordGaucheDomaineIds", bordGaucheDomaineIds)
281 print("bordDroiteDomaineIds", bordDroiteDomaineIds)
282 bordGaucheDomaine = geompy.CreateGroup(HYDRO_garonne_2, geompy.ShapeType["EDGE"])
283 geompy.UnionIDs(bordGaucheDomaine, bordGaucheDomaineIds)
284 geompy.addToStudyInFather(HYDRO_garonne_2, bordGaucheDomaine, "bordGaucheDomaine")
285 bordDroiteDomaine = geompy.CreateGroup(HYDRO_garonne_2, geompy.ShapeType["EDGE"])
286 geompy.UnionIDs(bordDroiteDomaine, bordDroiteDomaineIds)
287 geompy.addToStudyInFather(HYDRO_garonne_2, bordDroiteDomaine, "bordDroiteDomaine")
288
289 amont = geompy.GetEdgeNearPoint(HYDRO_garonne_2, geompy.MakeVertex(46757.861314, 25833.234752, 0))
290 aval = geompy.GetEdgeNearPoint(HYDRO_garonne_2, geompy.MakeVertex(39078.979127, 32588.627279, 0))
291 geompy.addToStudyInFather(HYDRO_garonne_2, amont, "amont")
292 geompy.addToStudyInFather(HYDRO_garonne_2, aval, "aval")
293
294 #----------------------
295 # --- Meshing
296 #----------------------
297
298 import  SMESH, SALOMEDS
299 from salome.smesh import smeshBuilder
300 from salome.hydrotools.controls import controlMeshStats, controlSubMeshStats
301 import tempfile
302
303 smesh = smeshBuilder.New()
304
305 # --- algorithms and hypothesis
306 garonne_2 = smesh.Mesh(HYDRO_garonne_2)
307
308 NETGEN_2D = garonne_2.Triangle(algo=smeshBuilder.NETGEN_1D2D)
309 NETGEN_2D_Parameters = NETGEN_2D.Parameters()
310 NETGEN_2D_Parameters.SetMaxSize( 200 )
311 NETGEN_2D_Parameters.SetSecondOrder( 0 )
312 NETGEN_2D_Parameters.SetOptimize( 1 )
313 NETGEN_2D_Parameters.SetFineness( 4 )
314 NETGEN_2D_Parameters.SetMinSize( 50 )
315 NETGEN_2D_Parameters.SetUseSurfaceCurvature( 1 )
316 NETGEN_2D_Parameters.SetFuseEdges( 1 )
317 NETGEN_2D_Parameters.SetQuadAllowed( 0 )
318
319 algo2D_litMineur = garonne_2.Quadrangle(algo=smeshBuilder.QUAD_MA_PROJ,geom=garonne_litMineur)
320 algo1D_litMineur = garonne_2.Segment(geom=garonne_litMineur)
321 hypo1D_litMineur = algo1D_litMineur.LocalLength(100,None,1e-07)
322 subMesh_litMineur = algo1D_litMineur.GetSubMesh()
323 smesh.SetName(subMesh_litMineur, "litMineur")
324
325 algo1D_SectionsGaronne = garonne_2.Segment(geom=SectionsGaronne)
326 hypo1D_SectionsGaronne = algo1D_SectionsGaronne.NumberOfSegments(8)
327 hypo1D_SectionsGaronne.SetDistrType( 0 )
328 subMesh_SectionsGaronne = algo1D_SectionsGaronne.GetSubMesh()
329 smesh.SetName(subMesh_SectionsGaronne, "SectionsGaronne")
330
331 isDone = garonne_2.SetMeshOrder( [ [ subMesh_SectionsGaronne, subMesh_litMineur ] ])
332
333 # --- compute mesh
334 isDone = garonne_2.Compute()
335 isDone = garonne_2.SplitQuadObject( garonne_2, 1 )
336 isDone = garonne_2.ReorientObject( garonne_2 )
337
338 # --- geometrical groups of faces
339 riveGauche_1 = garonne_2.GroupOnGeom(garonne_riveGauche,'riveGauche',SMESH.FACE)
340 litMineur_1 = garonne_2.GroupOnGeom(garonne_litMineur,'litMineur',SMESH.FACE)
341 riveDroite_1 = garonne_2.GroupOnGeom(garonne_riveDroite,'riveDroite',SMESH.FACE)
342
343 # --- geometrical groups of edges
344
345 ClosedFreeBoundary_1 = garonne_2.GroupOnGeom(ClosedFreeBoundary[0],'ClosedFreeBoundary',SMESH.EDGE)
346 litMineur_droite_1 = garonne_2.GroupOnGeom(litMineur_droite,'litMineur_droite',SMESH.EDGE)
347 litMineur_gauche_1 = garonne_2.GroupOnGeom(litMineur_gauche,'litMineur_gauche',SMESH.EDGE)
348 Sectionsgaronne_2 = garonne_2.GroupOnGeom(SectionsGaronne,'SectionsGaronne',SMESH.EDGE)
349 bordGaucheDomaine_1 = garonne_2.GroupOnGeom(bordGaucheDomaine,'bordGaucheDomaine',SMESH.EDGE)
350 bordDroiteDomaine_1 = garonne_2.GroupOnGeom(bordDroiteDomaine,'bordDroiteDomaine',SMESH.EDGE)
351 amont_1 = garonne_2.GroupOnGeom(amont,'amont',SMESH.EDGE)
352 aval_1 = garonne_2.GroupOnGeom(aval,'aval',SMESH.EDGE)
353
354 # --- geometrical groups of nodes
355
356 garonne_2_litMineur_2 = garonne_2.GroupOnGeom(garonne_litMineur,'garonne_2_litMineur',SMESH.NODE)
357 garonne_2_riveDroite_2 = garonne_2.GroupOnGeom(garonne_riveDroite,'garonne_2_riveDroite',SMESH.NODE)
358 garonne_2_riveGauche_2 = garonne_2.GroupOnGeom(garonne_riveGauche,'garonne_2_riveGauche',SMESH.NODE)
359 ClosedFreeBoundary_2 = garonne_2.GroupOnGeom(ClosedFreeBoundary[0],'ClosedFreeBoundary',SMESH.NODE)
360 litMineur_droite_2 = garonne_2.GroupOnGeom(litMineur_droite,'litMineur_droite',SMESH.NODE)
361 litMineur_gauche_2 = garonne_2.GroupOnGeom(litMineur_gauche,'litMineur_gauche',SMESH.NODE)
362 SectionsGaronne_2 = garonne_2.GroupOnGeom(SectionsGaronne,'SectionsGaronne',SMESH.NODE)
363 bordGaucheDomaine_2 = garonne_2.GroupOnGeom(bordGaucheDomaine,'bordGaucheDomaine',SMESH.NODE)
364 bordDroiteDomaine_2 = garonne_2.GroupOnGeom(bordDroiteDomaine,'bordDroiteDomaine',SMESH.NODE)
365 amont_2 = garonne_2.GroupOnGeom(amont,'amont',SMESH.NODE)
366 aval_2 = garonne_2.GroupOnGeom(aval,'aval',SMESH.NODE)
367
368 garonne_2.SetAutoColor( 1 )
369 fichierMaillage = os.path.join(HYDRO_TEST_RESOURCES, 'garonne_2.med')
370 garonne_2.ExportMED(fichierMaillage, 0, SMESH.MED_V2_2, 1, None ,1)
371
372 controlMeshStats(garonne_2, 3952, 512, 7690)
373 controlSubMeshStats(litMineur_1, 2384)
374 controlSubMeshStats(riveDroite_1, 2321)
375 controlSubMeshStats(riveGauche_1, 2985)
376
377 if salome.sg.hasDesktop():
378   salome.sg.updateObjBrowser()
379