Coverage for steam_pysigma\sigma\pysigma.py: 77%
193 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-12-16 17:09 +0100
« prev ^ index » next coverage.py v7.4.3, created at 2024-12-16 17:09 +0100
1# STEAM PySigma is a python wrapper of STEAM-SIGMA written in Java.
2# Copyright (C) 2023, CERN, Switzerland. All rights reserved.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, version 3 of the License.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <https://www.gnu.org/licenses/>.
16import os
17from pathlib import Path
19import pandas as pd
20from py4j.java_gateway import launch_gateway, JavaGateway, GatewayParameters
23class SetupConfigSIGMA:
24 def __init__(self, g, model_java_file_path, COMSOL_batch_path, COMSOL_compile_path, funLibPath, run_study, numOfQH,
25 study_type, qh_positions, quench_init_HTs=None, quench_init_heat=None):
26 self.cfg = g.ConfigSigma()
27 self.cfg.setOutputModelPath(model_java_file_path)
28 self.cfg.setComsolBatchPath(COMSOL_batch_path)
29 self.cfg.setComsolCompilePath(COMSOL_compile_path)
30 self.cfg.setExternalCFunLibPath(funLibPath)
31 self.cfg.setRunStudy(run_study)
32 self.cfg.setNumOfQHs(numOfQH)
33 self.cfg.setStudyType(study_type)
34 QHPositions = g.gateway.jvm.java.util.ArrayList()
35 for qh in qh_positions:
36 temp = g.gateway.jvm.java.util.ArrayList()
37 for pos in qh:
38 temp.add(pos)
39 QHPositions.add(temp)
40 self.cfg.setQHPositions(QHPositions)
41 if quench_init_HTs != None and len(quench_init_HTs)>0:
42 array = ArraysSIGMA.convert_list_to_string_array(g.gateway, quench_init_HTs)
43 self.cfg.setQuenchInitHT(array)
44 self.cfg.setQuenchInitHeat(quench_init_heat)
47class GatewaySIGMA:
48 """
49 Python wrapper for SIGMA jars exposes java methods and attributes.
50 """
52 def __init__(self):
53 jars_path = os.path.join(Path(__file__).parent, '*')
54 print(
55 f"PySIGMA started and uses {[each for each in os.listdir(Path(__file__).parent) if each.endswith('.jar')][0]}")
56 self.port = launch_gateway(classpath=jars_path, die_on_exit=True)
57 self.gateway = JavaGateway(gateway_parameters=GatewayParameters(port=self.port))
58 self.Point = self.gateway.jvm.model.geometry.basic.Point
59 self.Line = self.gateway.jvm.model.geometry.basic.Line
60 self.Arc = self.gateway.jvm.model.geometry.basic.Arc
61 self.EllipticArc = self.gateway.jvm.model.geometry.basic.EllipticArc
62 self.Circumference = self.gateway.jvm.model.geometry.basic.Circumference
63 self.Area = self.gateway.jvm.model.geometry.basic.Area
64 self.HyperLine = self.gateway.jvm.model.geometry.basic.HyperLine
65 self.Element = self.gateway.jvm.model.geometry.Element
66 self.Domain = self.gateway.jvm.model.domains.Domain
67 self.AirFarFieldDomain = self.gateway.jvm.model.domains.database.AirFarFieldDomain
68 self.AirDomain = self.gateway.jvm.model.domains.database.AirDomain
69 self.IronDomain = self.gateway.jvm.model.domains.database.IronDomain
70 self.HoleDomain = self.gateway.jvm.model.domains.database.HoleDomain
71 self.CoilDomain = self.gateway.jvm.model.domains.database.CoilDomain
72 self.WedgeDomain = self.gateway.jvm.model.domains.database.WedgeDomain
73 self.MatDatabase = self.gateway.jvm.model.materials.database.MatDatabase
74 self.ConfigSigma = self.gateway.jvm.config.ConfigSigma
75 self.TxtSigmaServer = self.gateway.jvm.server.TxtSigmaServer
76 self.MagnetMPHBuilder = self.gateway.jvm.comsol.MagnetMPHBuilder
77 self.QuenchHeaterMPHBuilder = self.gateway.jvm.comsol.QuenchHeaterMPHBuilder
78 self.Cable = self.gateway.jvm.model.geometry.coil.Cable
79 self.Winding = self.gateway.jvm.model.geometry.coil.Winding
80 self.Pole = self.gateway.jvm.model.geometry.coil.Pole
81 self.Coil = self.gateway.jvm.model.geometry.coil.Coil
82 self.Magnet_T0_QH = self.gateway.jvm.input.OthersT0.Magnet_T0_QH
83 self.Magnet_T1_QH = self.gateway.jvm.input.OthersT0.Magnet_T1_QH
84 self.MPHC = self.gateway.jvm.comsol.constants.MPHC
85 self.StudyAPI = self.gateway.jvm.comsol.api.StudyAPI
86 self.ResultsAPI = self.gateway.jvm.comsol.api.ResultsAPI
87 self.constants = self.gateway.jvm.comsol.constants.MPHC
88 # def __del__(self):
89 # self.gateway.shutdown()
92class ArraysSIGMA:
94 @staticmethod
95 def create_hyper_line_array(gateway, args):
96 HyperLine = gateway.jvm.model.geometry.basic.HyperLine
97 if type(args) is tuple:
98 hl_array = gateway.new_array(HyperLine, len(args))
99 for i in range(len(args)):
100 hl_array[i] = args[i]
101 else:
102 hl_array = gateway.new_array(HyperLine, 1)
103 hl_array[0] = args
104 return hl_array
106 @staticmethod
107 def create_element_array(gateway, args):
108 Element = gateway.jvm.model.geometry.Element
109 if type(args) is tuple:
110 el_array = gateway.new_array(Element, len(args))
111 for i in range(len(args)):
112 el_array[i] = args[i]
113 else:
114 el_array = gateway.new_array(Element, 1)
115 el_array[0] = args
116 return el_array
118 @staticmethod
119 def create_string_array(gateway, args):
120 if any(var_type is type(args) for var_type in [tuple, list]):
121 el_array = gateway.new_array(gateway.jvm.String, len(args))
122 for i in range(len(args)):
123 el_array[i] = args[i]
124 else:
125 el_array = gateway.new_array(gateway.jvm.String, 1)
126 el_array[0] = args
127 return el_array
129 @staticmethod
130 def create_double_array(gateway, args):
131 if type(args) is tuple:
132 el_array = gateway.new_array(gateway.jvm.double, len(args))
133 for i in range(len(args)):
134 el_array[i] = args[i]
135 else:
136 el_array = gateway.new_array(gateway.jvm.double, 1)
137 el_array[0] = args
138 return el_array
140 @staticmethod
141 def create_int_array(gateway, args):
142 if type(args) is tuple:
143 el_array = gateway.new_array(gateway.jvm.int, len(args))
144 for i in range(len(args)):
145 el_array[i] = args[i]
146 else:
147 el_array = gateway.new_array(gateway.jvm.int, 1)
148 el_array[0] = args
149 return el_array
151 @staticmethod
152 def create_domain_array(gateway, args):
153 Domain = gateway.jvm.model.domains.Domain
154 if type(args) is tuple:
155 el_array = gateway.new_array(Domain, len(args))
156 for i in range(len(args)):
157 el_array[i] = args[i]
158 else:
159 el_array = gateway.new_array(Domain, 1)
160 el_array[0] = args
161 return el_array
163 @staticmethod
164 def create_area_array(gateway, args):
165 Area = gateway.jvm.model.geometry.basic.Area
166 if type(args) is tuple:
167 el_array = gateway.new_array(Area, len(args))
168 for i in range(len(args)):
169 el_array[i] = args[i]
170 else:
171 el_array = gateway.new_array(Area, 1)
172 el_array[0] = args
173 return el_array
175 @staticmethod
176 def convert_list_to_double_array(gateway, arg):
177 el_array = gateway.new_array(gateway.jvm.Double, len(arg))
178 for i in range(len(arg)):
179 el_array[i] = float(arg[i])
180 return el_array
182 @staticmethod
183 def convert_list_to_string_array(gateway, arg):
184 el_array = gateway.new_array(gateway.jvm.String, len(arg))
185 for i in range(len(arg)):
186 el_array[i] = arg[i]
187 return el_array
189 @staticmethod
190 def create_winding_array(gateway, args):
191 Winding = gateway.jvm.model.geometry.coil.Winding
192 if type(args) is tuple:
193 el_array = gateway.new_array(Winding, len(args))
194 for i in range(len(args)):
195 el_array[i] = args[i]
196 else:
197 el_array = gateway.new_array(Winding, 1)
198 el_array[0] = args
199 return el_array
201 @staticmethod
202 def create_pole_array(gateway, args):
203 Pole = gateway.jvm.model.geometry.coil.Pole
204 if type(args) is tuple:
205 el_array = gateway.new_array(Pole, len(args))
206 for i in range(len(args)):
207 el_array[i] = args[i]
208 else:
209 el_array = gateway.new_array(Pole, 1)
210 el_array[0] = args
211 return el_array
213 @staticmethod
214 def parse_java_array(java_array):
215 rows = len(java_array)
216 cols = len(java_array[0])
218 array = [[0 for x in range(rows)] for y in range(cols)]
220 for i in range(rows):
221 for j in range(cols):
222 array[i][j] = java_array[i][j]
223 return array
225 @staticmethod
226 def create_double_2D_array(gateway, input_array):
227 output_array = gateway.new_array(gateway.jvm.Double, len(input_array), len(input_array[0]))
228 for r in range(len(input_array)):
229 for c in range(len(input_array[r])):
230 output_array[r][c] = input_array[r][c]
231 return output_array
233 @staticmethod
234 def create_unboxed_double_2D_array(gateway, input_array):
235 output_array = gateway.new_array(gateway.jvm.double, len(input_array), len(input_array[0]))
236 for r in range(len(input_array)):
237 for c in range(len(input_array[r])):
238 output_array[r][c] = input_array[r][c]
239 return output_array
242class FunLibCSVReader:
243 def __init__(self):
244 pass
246 def read_csv_funclib_create_hashmap(self, gs: GatewaySIGMA = None):
247 df = pd.read_csv(os.path.join(Path(__file__).parent, '../utils/CFUN_mapping.csv'))
248 funlib_map = gs.gateway.jvm.java.util.HashMap()
249 for index, row in df.iterrows():
250 funlib_map.put(row['SIGMA_name'], row['File_name'])
251 return funlib_map