Salome HOME
Added new test for field creation and saving
[tools/solverlab.git] / CDMATH / tests / swig / testFieldCreationAndSave.py
1 #!/usr/bin/env python3
2 # -*-coding:utf-8 -*
3
4 from math import sqrt
5 import cdmath
6
7 print("Loading a triangular mesh of a 2D square")
8 filename = "./meshSquare"
9 M=cdmath.Mesh(filename+".med")
10
11 print("Checking boundary group names")
12 boundaryFaceGroupNames=M.getNameOfFaceGroups()
13 boundaryNodeGroupNames=M.getNameOfNodeGroups()
14 print(len(boundaryFaceGroupNames), " Boundary Face Group detected : ", boundaryFaceGroupNames)
15 print(len(boundaryNodeGroupNames), " Boundary Node Group detected : ", boundaryNodeGroupNames)
16
17 assert(len(boundaryFaceGroupNames)==5)
18 assert(len(boundaryNodeGroupNames)==5)
19
20 assert boundaryFaceGroupNames[0]=="Top"
21 assert boundaryFaceGroupNames[1]=="Right"
22 assert boundaryFaceGroupNames[2]=="Left"
23 assert boundaryFaceGroupNames[3]=="Bottom"
24 assert boundaryFaceGroupNames[4]=="Boundary"
25
26 assert boundaryNodeGroupNames[0]=="Top"
27 assert boundaryNodeGroupNames[1]=="Right"
28 assert boundaryNodeGroupNames[2]=="Left"
29 assert boundaryNodeGroupNames[3]=="Bottom"
30 assert boundaryNodeGroupNames[4]=="Boundary"
31
32 #Extract domain sizes
33 xmin = M.getXMin()
34 xmax = M.getXMax()
35 ymin = M.getYMin()
36 ymax = M.getYMax()
37
38 radius = min(xmax-xmin,ymax-ymin)/4
39 xcentre = (xmax+xmin)/2
40 ycentre = (ymax+ymin)/2
41
42 nbCells = M.getNumberOfCells()
43 nbNodes = M.getNumberOfNodes()
44
45 # Create scalar fields
46 temperature_field_cells = cdmath.Field("Temperature", cdmath.CELLS, M, 1)
47 temperature_field_nodes = cdmath.Field("Temperature", cdmath.NODES, M, 1)
48 Tin  = 300
49 Tout = 400
50
51 for i in range(nbCells):
52     x = M.getCell(i).x()
53     y = M.getCell(i).y()
54     distance = sqrt( (x - xcentre) * (x - xcentre) + (y - ycentre) * (y - ycentre) )
55     if distance < radius:
56         temperature_field_cells[i] = Tin
57     else:
58         temperature_field_cells[i] = Tout
59
60 temperature_field_cells.writeMED(filename, False)
61
62 for i in range(nbNodes):
63     x = M.getNode(i).x()
64     y = M.getNode(i).y()
65     distance = sqrt( (x - xcentre) * (x - xcentre) + (y - ycentre) * (y - ycentre) )
66     if distance < radius:
67         temperature_field_nodes[i] = Tin
68     else:
69         temperature_field_nodes[i] = Tout
70
71 temperature_field_nodes.writeMED(filename, False)