Salome HOME
Adding test from srf2med conversion using MEDCoupling
[modules/hydrosolver.git] / doc / salome / examples / hs_003_converter_slf_med.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 Test for conversion of srf to med usgin medcoupling to rebuild boundary on
5 segment
6 """
7
8 # Modules Python
9 from os import path, environ
10 import tempfile
11 import subprocess as sp
12 import sys
13
14 import numpy as np
15
16 from data_manip.conversion.stbtel_converter import run_converter
17 from data_manip.extraction.telemac_file import TelemacFile
18
19
20 def create_test_file(file_name):
21     """
22     Create a triforce file and its boudary_file
23     """
24
25     if file_name.endswith("med"):
26         bnd_file = path.splitext(file_name)[0] + ".bnd"
27     else:
28         bnd_file = path.splitext(file_name)[0] + ".cli"
29
30     points = np.array(\
31             [[0., 0.],
32              [1., 0.],
33              [2., 0.],
34              [0.5, 1.],
35              [1.5, 1.],
36              [1., 2.]])
37     ikle = [[0, 1, 3],
38             [1, 4, 3],
39             [1, 2, 4],
40             [3, 4, 5],
41             ]
42
43     res = TelemacFile(file_name, bnd_file=bnd_file, access='w')
44     res.add_header('Zelda triforce', date=[1986, 2, 21, 0, 0, 0])
45     res.add_mesh(points[:, 0], points[:, 1], ikle)
46     res.add_variable('dummy', '')
47
48     if file_name.endswith("med"):
49         ikle_bnd = [[0, 3], [3, 5], [5, 4], [4, 2], [2, 1], [1, 0]]
50         lihbor = [2, 2, 2, 2, 4, 4]
51         liubor = [2, 2, 2, 2, 5, 5]
52         livbor = [2, 2, 2, 2, 5, 5]
53     else:
54         ikle_bnd = [0, 3, 5, 4, 2, 1]
55         lihbor = [4, 2, 2, 2, 4, 4]
56         liubor = [5, 2, 2, 2, 5, 5]
57         livbor = [5, 2, 2, 2, 5, 5]
58
59     res.add_bnd(ikle_bnd, lihbor=lihbor, liubor=liubor, livbor=livbor)
60
61     res.write()
62     res.close()
63
64     return bnd_file
65
66 #with tempfile.TemporaryDirectory() as tmp_dir:
67 tmp_dir = tempfile.mkdtemp()
68 ref_srf = path.join(tmp_dir, "ref.srf")
69 ref_med = path.join(tmp_dir, "ref.med")
70
71 ref_srf_bnd = create_test_file(ref_srf)
72 ref_med_bnd = create_test_file(ref_med)
73
74 output_file = path.join(tmp_dir, "test.med")
75 output_bnd_file = path.splitext(output_file)[0] + ".bnd"
76
77 print("ref_srf", ref_srf)
78 print("ref_srf_bnd", ref_srf_bnd)
79 print("ref_med", ref_med)
80 print("ref_med_bnd", ref_med_bnd)
81 print("output_file", output_file)
82 print("output_bndfile", output_bnd_file)
83
84 cmd = ["converter.py", "srf2med", ref_srf, "-b", ref_srf_bnd, output_file]
85
86 if sys.platform == "win32":
87     code = sp.call(cmd, shell=True)
88 else:
89     code = sp.call(cmd)
90
91 if code != 0:
92     sys.exit(code)
93
94 conv_res = TelemacFile(output_file, bnd_file=output_bnd_file)
95 ref_res = TelemacFile(ref_med, bnd_file=ref_med_bnd)
96
97 assert conv_res.typ_bnd_elem == ref_res.typ_bnd_elem, "type res/ref: {}/{}".format(conv_res.typ_bnd_elem, ref_res.typ_bnd_elem)
98 assert conv_res.nelebd == ref_res.nelebd, "nelebd res/ref: {}/{}".format(conv_res.nelebd, ref_res.nelebd)