-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheat_transfer_materials.py
148 lines (121 loc) · 5.76 KB
/
heat_transfer_materials.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
This module is responsible for getting and storing material data.
"""
import os
import sys
import csv
from typing import List, Dict
def resource_path(file_name):
"""
Helper function allowing for bundling the material data file together
with the .exe application - so it does not need to be included
as a "physical" file, and we do not run the danger of somebody
deleting it.
NOTE: is not affecting the development process.
It is not perfect, however, as this file is then read-only, as the data
can be written there, but they will be erased as soon as the .exe
application finishes.
(The reason being the file is stored only in Temp folder, and new
Temp folder will be created in the next start of the .exe app)
Args:
file_name ... name of the file
"""
# Trying to locate the Temp folder, otherwise using the normal path
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except AttributeError:
base_path = os.path.dirname(os.path.realpath(__file__))
return os.path.join(base_path, file_name)
class MaterialService:
"""
Class holding information about all the available materials
and their properties
"""
materials_name_list: List[str] = []
materials_properties_dict: Dict[str, dict] = {}
def __init__(self):
# Defining file with default material data and getting data from there
default_materials_filename = "metals_properties.csv"
default_materials_filename = resource_path(default_materials_filename)
default_materials = self.get_materials_from_csv_file(
file_name=default_materials_filename, throw_exception=True)
# Defining filename with custom material data and retrieving its info
# As we want to access this filename later in saving method,
# make it an instance variable
self.custom_materials_filename = resource_path("custom_user_materials.csv")
custom_materials = self.get_materials_from_csv_file(
file_name=self.custom_materials_filename)
# Putting the two dictionaries together
# Because we supplied custom materials as last, the possible
# naming conflicts will be resolved in their favour, so users can
# even overwrite default materials
self.materials_properties_dict = {**default_materials, **custom_materials}
# Getting the list of all available materials
self.materials_name_list = [key for key in self.materials_properties_dict]
@staticmethod
def get_materials_from_csv_file(file_name: str,
throw_exception: bool = False) -> dict:
"""
Transfers material data from the CSV file into a python dictionary,
to be able to work with these data.
Args:
file_name ... name of the file
throw_exception ... whether to throw exception when the file
will not be found
Returns:
(dictionary) Dictionary with keys being material names and values
being material properties
"""
materials_dictionary = {}
try:
with open(file_name, "r") as materials_file:
csv_reader = csv.reader(materials_file)
next(csv_reader, None) # skip first line (headers)
for row in csv_reader:
if row:
name = row[0]
material_properties = {}
material_properties["rho"] = float(row[2])
material_properties["cp"] = float(row[3])
material_properties["lmbd"] = float(row[4])
materials_dictionary[name] = material_properties
except FileNotFoundError:
print("File not found - {}".format(file_name))
# Raising exception only when the data is really crucial
# and we cannot afford to miss them
if throw_exception:
raise
return materials_dictionary
def save_custom_user_material(self, material_dict: dict) -> None:
"""
Including custom user material into its dedicated file
"""
file_name = self.custom_materials_filename
# When the custom user file already exists, append it
# Otherwise create it from scratch
# Being consistent with the default file, so including the
# T_MELT column, and filling it with zero
if os.path.isfile(file_name):
with open(file_name, "a") as custom_materials_file:
csv_writer = csv.writer(custom_materials_file)
element_row = []
element_row.append(material_dict["name"])
element_row.append(0)
element_row.append(material_dict["properties"]["rho"])
element_row.append(material_dict["properties"]["cp"])
element_row.append(material_dict["properties"]["lmbd"])
csv_writer.writerow(element_row)
else:
with open(file_name, "w") as custom_materials_file:
csv_writer = csv.writer(custom_materials_file)
headers = ["NAME", "T_MELT [C]", "RHO [kg/m3]",
"C_P [J/(kg*K)]", "LAMBDA [W/(m*K)]"]
csv_writer.writerow(headers)
element_row = []
element_row.append(material_dict["name"])
element_row.append(0)
element_row.append(material_dict["properties"]["rho"])
element_row.append(material_dict["properties"]["cp"])
element_row.append(material_dict["properties"]["lmbd"])
csv_writer.writerow(element_row)