-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtutorial.py
More file actions
325 lines (212 loc) · 11.1 KB
/
tutorial.py
File metadata and controls
325 lines (212 loc) · 11.1 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python
# coding: utf-8
# # Tutorial: Building domains with PINA's `BaseDomain` class
#
# [](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial6/tutorial.ipynb)
#
# In this tutorial, we explore how to use and visualize PINA’s built-in geometric domains and how to construct custom ones. We will cover:
# - Creating domains using `CartesianDomain`, `EllipsoidDomain`, and `SimplexDomain`
# - Combining domains through set operations
# - Defining custom domains
# - Sampling from domains
#
# We begin by importing the necessary modules.
# In[1]:
## routine needed to run the notebook on Google Colab
try:
import google.colab
IN_COLAB = True
except:
IN_COLAB = False
if IN_COLAB:
get_ipython().system('pip install "pina-mathlab[tutorial]"')
from copy import deepcopy
import torch
import matplotlib.pyplot as plt
from pina import LabelTensor
from pina.domain import (
CartesianDomain,
EllipsoidDomain,
SimplexDomain,
Union,
BaseDomain,
)
# ## Built-in Geometries
# We start with PINA’s built-in geometries. In particular, we define a Cartesian domain, an ellipsoid domain, and a simplex domain, all in two dimensions. Extending these constructions to higher dimensions follows the same principles.
# The Cartesian domain represents rectangular regions, the ellipsoid domain models circular or elliptical shapes, and the simplex domain corresponds to triangular regions, which can be combined to form general polygonal domains.
# In[2]:
# Carteisan, Ellipsoid, and Simplex domains
cartesian = CartesianDomain({"x": [0, 1], "y": [0, 1]})
ellipsoid = EllipsoidDomain({"x": [-0.5, 0.5], "y": [-0.5, 0.5]})
simplex = SimplexDomain(
[
LabelTensor(torch.tensor([[-0.5, 0]]), labels=["x", "y"]),
LabelTensor(torch.tensor([[0.5, 0]]), labels=["x", "y"]),
LabelTensor(torch.tensor([[-0.5, 1]]), labels=["x", "y"]),
]
)
# Example of a domain with fixed and variable dimensions
cartesian_fixed_variable = CartesianDomain({"x": [0, 2], "y": 1})
# Both Cartesian and ellipsoid domains are created by passing dictionaries that specify the bounds for each variable. If a lower and upper bound coincide, the variable can be fixed by providing a single numerical value.
# Since the concept of bounds does not apply to simplices, their initialization requires explicitly providing the vertices. The number of vertices must always be one more than the domain dimension.
# To visualize the shapes, we draw sample points from each domain using the `sample` method, available for all PINA domains. The argument `n` specifies how many points to generate. The optional `mode` argument selects the sampling strategy (e.g. "random"). The optional `variables` argument allows sampling over only a subset of variables; here, we sample all of them.
# In[3]:
cartesian_samples = cartesian.sample(n=1000, mode="random")
ellipsoid_samples = ellipsoid.sample(n=1000, mode="random")
simplex_samples = simplex.sample(n=1000, mode="random")
fixed_variable_samples = cartesian_fixed_variable.sample(n=1000, mode="random")
# We can inspect a few sampled points from each domain to get a better understanding of their structure.
# In[4]:
print(f"Cartesian samples: {cartesian_samples[:4]}\n")
print(f"Ellipsoid samples: {ellipsoid_samples[:4]}\n")
print(f"Simplex samples: {simplex_samples[:4]}\n")
print(f"Fixed variable samples: {fixed_variable_samples[:4]}\n")
# Now we are ready to visualize the sampled points!
# In[5]:
# Basic plotting function
def plot_scatter(ax, pts, title):
ax.title.set_text(title)
ax.scatter(pts.extract("x"), pts.extract("y"), color="blue", alpha=0.5)
ax.set_aspect("equal", adjustable="box")
fig, axs = plt.subplots(1, 3, figsize=(16, 4))
pts_list = [cartesian_samples, ellipsoid_samples, simplex_samples]
title_list = ["Cartesian Domain", "Ellipsoid Domain", "Simplex Domain"]
for ax, pts, title in zip(axs, pts_list, title_list):
plot_scatter(ax, pts, title)
# Similarly, we can sample and visualize boundary points by using the `partial` method. This method returns a new domain representing only the boundary of the original one, from which we can draw samples in exactly the same way.
# In[6]:
# Boundary definitions
cartesian_boundary = cartesian.partial()
ellipsoid_boundary = ellipsoid.partial()
simplex_boundary = simplex.partial()
# Boundary sampling
cartesian_bnd_samples = cartesian_boundary.sample(n=500, mode="random")
ellipsoid_bnd_samples = ellipsoid_boundary.sample(n=500, mode="random")
simplex_bnd_samples = simplex_boundary.sample(n=500, mode="random")
# Plot
fig, axs = plt.subplots(1, 3, figsize=(16, 4))
pts_list = [cartesian_bnd_samples, ellipsoid_bnd_samples, simplex_bnd_samples]
title_list = ["Cartesian Domain", "Ellipsoid Domain", "Simplex Domain"]
for ax, pts, title in zip(axs, pts_list, title_list):
plot_scatter(ax, pts, title)
# Great! We have created our first domains, sampled points from them, and visualized the results.
# ## Set Operations
# PINA’s built-in domains are powerful, but by themselves they cannot represent more complex shapes. To build richer geometries, we use set operations. PINA supports `Union`, `Intersection`, `Difference`, and `Exclusion` (symmetric difference) for all domain types.
# Here, we focus on `Union` for demonstration purposes; the remaining operations behave analogously.
# All set operations in PINA take a list of domains as input. For `Intersection`, `Difference`, and `Exclusion`, the operation is applied between the first two domains in the list. The resulting domain is then combined with the next one, and this process continues iteratively until all domains have been processed.
# Let’s build the union of:
# 1. `cartesian` and `simplex`
# 2. `cartesian` and `ellipsoid_boundary`
# 3. `ellipsoid` and `simplex_boundary`
# In[7]:
union_cart_sim = Union([cartesian, simplex])
union_cart_ell_bnd = Union([cartesian, ellipsoid_boundary])
union_ell_sim_bnd = Union([ellipsoid, simplex_boundary])
# And of course, we can sample points from these composite domains as well!
# In[8]:
cart_sim_samples = union_cart_sim.sample(n=1000, mode="random")
cart_ell_bnd_samples = union_cart_ell_bnd.sample(n=1000, mode="random")
ell_sim_bnd_samples = union_ell_sim_bnd.sample(n=1000, mode="random")
# We can now plot the samples to visualize each union.
# In[9]:
fig, axs = plt.subplots(1, 3, figsize=(16, 4))
pts_list = [cart_sim_samples, cart_ell_bnd_samples, ell_sim_bnd_samples]
title_list = [
"Cartesian and Simplex Union",
"Cartesian and Ellipsoid Border Union",
"Ellipsoid and Simplex Border Union",
]
for ax, pts, title in zip(axs, pts_list, title_list):
plot_scatter(ax, pts, title)
# ## Creating a Custom Domain
# Next, we explore how to create a custom domain. As an example, we consider a heart-shaped region defined by the inequality:
# $$(x^2+y^2-1)^3-x^2y^3 \le 0$$
# Custom domains in PINA can be created by inheriting from the `BaseDomain` class, which provides the general structure shared by all domains.
# We begin by defining the constructor: we specify the available sampling modes ("random", "grid", "chebyshev", "latin" or "lh"). Here, we default to random sampling. We also introduce the parameter `sample_surface`, which determines whether we sample the full heart or only its boundary.
# In[10]:
class Heart(BaseDomain):
"""
Implementation of the Heart Domain.
"""
def __init__(self, sample_surface=False):
"""
Initialization of the Heart Domain.
"""
super().__init__()
self._sample_modes = "random"
self.sample_surface = sample_surface
# Since the `Heart` domain inherits from BaseDomain, we must implement its abstract methods: `is_inside`, `sample`, and `partial`.
# The `is_inside` method checks whether a given point lies inside the domain. It receives the point to test and the boolean `check_border`, which indicates whether points on the boundary should be considered inside.
# In[11]:
def is_inside(self, point, check_border=False):
"""
Check if a point is inside the Heart domain.
"""
# Extract coordinates
x = point["x"]
y = point["y"]
# Define the quantity defining the heart shape
eqn = (x**2 + y**2 - 1) ** 3 - (x**2) * (y**3)
# If sampling on the surface, check for equality
if self.sample_surface:
return torch.allclose(eqn, torch.zeros_like(eqn))
# Check if point is inside the heart shape
return (eqn <= 0) if check_border else (eqn < 0)
# The `sample` method closely resembles those of PINA’s built-in domains. We specify the number of points `n` and the sampling strategy mode. Note that for illustration we implement a very naive sampling approach, which is inefficient and not suitable for sampling boundary points for the heart domain!
# In[12]:
def sample(self, n, mode="random"):
"""
Sampling routine for the Heart domain.
"""
# Create a list to store the sampled points
samples = []
# Random sampling
if mode == "random":
# Loop until we have n samples
while len(samples) < n:
# Generate random point in bounding box
pts = torch.rand(1, 2) * 3.0 - 1.5
pts = LabelTensor(pts, labels=["x", "y"])
# Check if the point is inside the heart, borders included
if self.is_inside(pts, True):
samples.append(pts)
return LabelTensor.cat(samples, dim=0)
# The `partial` method returns a new instance of the domain class that represents only its boundary. Implementing it is straightforward.
# In[13]:
def partial(self):
"""
Return the boundary of the Heart domain.
"""
# Copy the current instance and set sampling only on the surface
boundary = deepcopy(self)
boundary.sample_surface = True
return boundary
# We now have all the components needed to complete the `Heart` class.
# In[14]:
# Linking the methods to the Heart class
Heart.is_inside = is_inside
Heart.sample = sample
Heart.partial = partial
# Avoid complaints about abstract methods not being implemented
Heart.__abstractmethods__ = frozenset()
# Let’s generate the heart domain and draw sample points.
# In[15]:
# Generate the heart domain
heart = Heart()
# Draw samples from the heart domain
heart_samples = heart.sample(n=1000, mode="random")
# Finally, we visualize the samples.
# In[16]:
fig, ax = plt.subplots()
plot_scatter(ax, heart_samples, "Heart Domain")
# ## What's Next?
#
# In this tutorial, we introduced the construction of custom geometries and the use of domain operations to combine basic shapes. From here, you can experiment with a wide range of possibilities:
#
# 1. **Build More Complex Geometries**: Combine multiple simple shapes using set operations to design sophisticated domains.
#
# 2. **Optimize for Specific Applications**: Tailor domain definitions for tasks such as fluid flow, heat transfer, or structural mechanics.
#
# 3. **...and many more!**: Implement new geometries using DomainInterface and push PINA’s capabilities further.
#
# For more resources and tutorials, check out the [PINA Documentation](https://mathlab.github.io/PINA/).