-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
94 lines (86 loc) · 3.45 KB
/
Copy pathutils.py
File metadata and controls
94 lines (86 loc) · 3.45 KB
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
import mph
def get_current_units(processor):
"""Get the current units from COMSOL model."""
try:
# Try to get units from model parameters first
try:
params = processor.model.java.param()
for param in params.vars():
if 'unit' in param.lower():
return params.get(param)
except:
pass
# If we can't get units from parameters, try to infer from geometry
try:
# Get a reference length from geometry
bbox = processor.geom.getBoundingBox()
if bbox:
# If the geometry is in meters, typical values would be > 1
# If in mm, typical values would be < 1000
if max(abs(bbox[0]), abs(bbox[1])) > 1000:
return "mm"
elif max(abs(bbox[0]), abs(bbox[1])) > 1:
return "m"
else:
return "mm" # Default to mm for small values
except:
pass
# Try to get units from physics settings
try:
for comp_tag in processor.model.java.component().tags():
component = processor.model.java.component(comp_tag)
for phys_tag in component.physics().tags():
phys = component.physics(phys_tag)
# Try to get units from physics settings
try:
settings = phys.getSettings()
if settings and 'unit' in settings:
return settings['unit']
except:
pass
except:
pass
print("Warning: Could not determine units from COMSOL model, defaulting to meters", flush=True)
return "m" # Default to meters if we can't get the units
except Exception as e:
print(f"Warning: Error getting units from COMSOL model: {e}", flush=True)
return "m" # Default to meters if we can't get the units
def convert_to_mm(processor, value, unit_type='length'):
"""Convert a value to millimeters based on the current units."""
if processor.current_units == "mm":
return value
# Conversion factors to mm
conversion_factors = {
'length': {
'm': 1000.0, # meters to mm
'cm': 10.0, # centimeters to mm
'mm': 1.0, # mm to mm
'um': 0.001, # micrometers to mm
'nm': 0.000001, # nanometers to mm
'in': 25.4, # inches to mm
'ft': 304.8, # feet to mm
'yd': 914.4 # yards to mm
},
'velocity': {
'm/s': 1000.0, # m/s to mm/s
'cm/s': 10.0, # cm/s to mm/s
'mm/s': 1.0, # mm/s to mm/s
'um/s': 0.001, # um/s to mm/s
'nm/s': 0.000001, # nm/s to mm/s
'in/s': 25.4, # inches/s to mm/s
'ft/s': 304.8, # feet/s to mm/s
'yd/s': 914.4 # yards/s to mm/s
},
'pressure': {
'Pa': 0.001, # Pa to kPa
'kPa': 1.0, # kPa to kPa
'MPa': 1000.0, # MPa to kPa
'bar': 100.0, # bar to kPa
'atm': 101.325, # atm to kPa
'psi': 6.89476, # psi to kPa
'inHg': 3.38639 # inches of mercury to kPa
}
}
# Get the appropriate conversion factor
factor = conversion_factors[unit_type].get(processor.current_units, 1.0)
return value * factor