Coverage for steam_pysigma\postprocessing\postprocessing.py: 97%
35 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 pandas as pd
19def export_B_field_txt_to_map2d(path_map2d_roxie, path_result_txt_Bx, path_result_txt_By, path_new_file):
20 """
21 Copy content of reference map2d file and overwrites Bx and By values which are replaced values from
22 comsol output txt file and writes to a new map2d file.
23 :param path_map2d_roxie: Path to reference map2d from which all values apart from Bx and By is copied from
24 :param path_result_txt_Bx: Comsol output txt file with evaluated B-field x component
25 :param path_result_txt_By: Comsol output txt file with evaluated B-field y component
26 :param path_new_file: Path to new map2d file where new B-field is stored
27 :return:
28 """
29 df_reference = pd.read_csv(path_map2d_roxie, delim_whitespace=True)
30 with open(path_result_txt_Bx) as file: # opens a text file
31 lines = [line.strip().split() for line in file if not "%" in line] # loops over each line
33 df_txt_Bx = pd.DataFrame(lines, columns=["x", "y", "Bx"])
35 df_txt_Bx = df_txt_Bx.apply(pd.to_numeric)
37 with open(path_result_txt_By) as file: # opens a text file
38 lines = [line.strip().split() for line in file if not "%" in line] # loops over each line
40 df_txt_By = pd.DataFrame(lines, columns=["x", "y", "By"])
41 df_txt_By = df_txt_By.apply(pd.to_numeric)
43 # Verify all evaluate field at same coordinates!
45 x_tol, y_tol = 1e-10, 1e-10
46 x_ref, y_ref = df_reference['X-POS/MM'] / 1000, df_reference['Y-POS/MM'] / 1000
48 if ((x_ref - df_txt_Bx['x']).abs().max() < x_tol) and \
49 ((x_ref - df_txt_By['x']).abs().max() < x_tol) and \
50 ((y_ref - df_txt_Bx['y']).abs().max() < y_tol) and \
51 ((y_ref - df_txt_By['y']).abs().max() < y_tol):
52 print("All dataframes have the same x and y coordinates.")
53 else:
54 raise ValueError("Error: Not all dataframes have the same x and y coordinates. Can't compare map2ds!")
56 # Create new map2d
57 with open(path_new_file, 'w') as file:
58 file.write(" BL. COND. NO. X-POS/MM Y-POS/MM BX/T BY/T"
59 " AREA/MM**2 CURRENT FILL FAC.\n\n")
60 content = []
61 for index, row in df_reference.iterrows():
62 bl, cond, no, x, y, Bx, By, area, curr, fill, fac = row
63 bl = int(bl)
64 cond = int(cond)
65 no = int(no)
66 x = f"{x:.4f}"
67 y = f"{y:.4f}"
68 Bx = df_txt_Bx["Bx"].iloc[index]
69 Bx = f"{Bx:.4f}"
70 By = df_txt_By["By"].iloc[index]
71 By = f"{By:.4f}"
72 area = f"{area:.4f}"
73 curr = f"{curr:.2f}"
74 fill = f"{fill:.4f}"
75 content.append(
76 "{0:>6}{1:>6}{2:>7}{3:>13}{4:>13}{5:>11}{6:>11}{7:>11}{8:>9}{9:>8}\n".format(bl, cond, no, x, y, Bx,
77 By,
78 area, curr, fill))
79 file.writelines(content)