-
Notifications
You must be signed in to change notification settings - Fork 0
/
topo_api.py
176 lines (151 loc) · 4.93 KB
/
topo_api.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from typing import Any, Dict
# third party
import numpy as np
import torch
from utils import DEFAULT_DEVICE, DEFAULT_DTYPE
def specified_task(problem, device=DEFAULT_DEVICE, dtype=DEFAULT_DTYPE):
"""
Given a problem, return parameters for
running a topology optimization.
NOTE: Based on what I have been learning about pytorch
we may need to update these inputs to be torch tensors.
NOTE: Nothing to check here
"""
fixdofs = np.flatnonzero(problem.normals.ravel().cpu().detach().clone())
alldofs = np.arange(2 * (problem.width + 1) * (problem.height + 1))
freedofs = np.sort(list(set(alldofs) - set(fixdofs)))
# Variables that will utilize GPU calculations
mask = torch.tensor(problem.mask).to(device=device, dtype=dtype)
freedofs = torch.tensor(freedofs).to(device=device, dtype=torch.long)
fixdofs = torch.tensor(fixdofs).to(device=device, dtype=torch.long)
params = {
# material properties
"young": 1.0,
"young_min": 1e-9,
"poisson": 0.3,
"g": 0.0,
# constraints
"volfrac": problem.density,
"xmin": 0.001,
"xmax": 1.0,
# input parameters
"nelx": torch.tensor(problem.width),
"nely": torch.tensor(problem.height),
"mask": mask,
"freedofs": freedofs,
"fixdofs": fixdofs,
"forces": problem.forces.ravel(),
"penal": 3.0,
"filter_width": 2,
"epsilon": problem.epsilon,
'ndof': len(alldofs),
'tounn_mask': problem.tounn_mask,
}
return params
def multi_material_tip_cantilever_task(
nelx: int,
nely: int,
e_materials: torch.Tensor,
material_density_weight: torch.Tensor,
combined_frac: float,
epsilon=1e-3,
device=DEFAULT_DEVICE,
dtype=DEFAULT_DTYPE,
) -> Dict[str, Any]:
"""
Function that will create the design space for the multi
material tip cantilever structure
"""
ndof = 2 * (nelx + 1) * (nely + 1)
# Forces on the system
forces = torch.zeros((ndof, 1))
forces[2 * (nelx + 1) * (nely + 1) - 2 * nely + 1, 0] = -1
# Degrees of freedom
alldofs_array = np.arange(ndof)
# Fixed dofs
fixdofs_array = alldofs_array[0 : 2 * (nely + 1) : 1]
# Free dofs
freedofs_array = np.sort(list(set(alldofs_array) - set(fixdofs_array)))
# Convert to torch tensorse)
freedofs = torch.tensor(freedofs_array).to(device=device, dtype=torch.long)
fixdofs = torch.tensor(fixdofs_array).to(device=device, dtype=torch.long)
params = {
# material properties
"young": 1.0,
"young_min": 1e-9,
"poisson": 0.3,
"g": 0.0,
# constraints
"combined_frac": combined_frac,
"xmin": 0.001,
"xmax": 1.0,
# input parameters
"nelx": torch.tensor(nelx),
"nely": torch.tensor(nely),
"freedofs": freedofs,
"fixdofs": fixdofs,
"forces": forces,
"penal": 3.0,
"filter_width": 2,
"epsilon": epsilon,
"ndof": len(alldofs_array),
"e_materials": e_materials,
"material_density_weight": material_density_weight,
}
return params
def multi_material_bridge_task(
nelx: int,
nely: int,
e_materials: torch.Tensor,
material_density_weight: torch.Tensor,
combined_frac: float,
epsilon=1e-3,
device=DEFAULT_DEVICE,
dtype=DEFAULT_DTYPE,
) -> Dict[str, Any]:
"""
Function that will create the design space for the multi
material tip cantilever structure
"""
ndof = 2 * (nelx + 1) * (nely + 1)
# Forces on the system
forces = torch.zeros((ndof, 1))
forces[2 * (nely + 1) * int(nelx // 4 + 1) - 1, 0] = -1
forces[2 * (nely + 1) * int(2 * nelx // 4 + 1) - 1, 0] = -2
forces[2 * (nely + 1) * int(3 * nelx // 4 + 1) - 1, 0] = -1
# Degrees of freedom
alldofs_array = np.arange(ndof)
# Fixed dofs
fixdofs_array = np.union1d(
np.array([2 * (nely + 1) - 1 - 1, 2 * (nely + 1) - 1]),
np.array([2 * (nelx + 1) * (nely + 1) - 1]),
)
# Free dofs
freedofs_array = np.sort(list(set(alldofs_array) - set(fixdofs_array)))
# Convert to torch tensorse)
freedofs = torch.tensor(freedofs_array).to(device=device, dtype=torch.long)
fixdofs = torch.tensor(fixdofs_array).to(device=device, dtype=torch.long)
params = {
# material properties
"young": 1.0,
"young_min": 1e-9,
"poisson": 0.3,
"g": 0.0,
# constraints
"combined_frac": combined_frac,
"xmin": 0.001,
"xmax": 1.0,
# input parameters
"nelx": torch.tensor(nelx),
"nely": torch.tensor(nely),
"freedofs": freedofs,
"fixdofs": fixdofs,
"forces": forces,
"penal": 3.0,
"filter_width": 2,
"epsilon": epsilon,
"ndof": len(alldofs_array),
"e_materials": e_materials,
"material_density_weight": material_density_weight,
}
return params