Basic sampling techniques#
# This is only valid when the package is not installed
import sys
sys.path.append('../../') # two folders up
import DeepINN as dp
Using default backend: PyTorch
Using Pytorch: 2.0.1+cu117
X = dp.spaces.R2('x') # R2 space
R = dp.domains.Parallelogram(X, [0,0], [1,0], [0,1]) # unit square
C = dp.domains.Circle(X, [0,0], 1) # unit circle
random_R = dp.samplers.RandomUniformSampler(R, n_points=50) + dp.samplers.RandomUniformSampler(R.boundary, n_points=50) # 50 random points
grid_C = dp.samplers.GridSampler(C, density=10) + dp.samplers.GridSampler(C.boundary, density=10) # grid points with density 10
dp.utils.scatter(X, random_R)
dp.utils.scatter(X, grid_C)
![../../_images/4412b36c0dc39a4d6c65c9a6aad08baf8faca887dd58b21a17dd46efa1037249.png](../../_images/4412b36c0dc39a4d6c65c9a6aad08baf8faca887dd58b21a17dd46efa1037249.png)
![../../_images/f6bd08357ce4e93b2b7f2147c0be430e9f2b5daae716d285580075283b2f422a.png](../../_images/f6bd08357ce4e93b2b7f2147c0be430e9f2b5daae716d285580075283b2f422a.png)
X = dp.spaces.R2('x') # R2 space
R = dp.domains.Parallelogram(X, [-0.5,-0.5], [0.5,-0.5], [-0.5,0.5]) # unit square
C = dp.domains.Circle(X, [0,0], 0.25) # unit circle
intersect = R - C
random_R = dp.samplers.RandomUniformSampler(R, n_points=500) + dp.samplers.RandomUniformSampler(R.boundary, n_points=100) # 50 random points
grid_C = dp.samplers.GridSampler(C, n_points=100) + dp.samplers.GridSampler(C.boundary, n_points=100) # grid points with density 10
random_intersect = dp.samplers.LHSSampler(intersect, n_points=500) + dp.samplers.RandomUniformSampler(intersect.boundary, n_points=500)
dp.utils.scatter(X, random_R)
dp.utils.scatter(X, grid_C)
dp.utils.scatter(X, random_intersect)
/home/hell/Desktop/repos/DeepINN/Tutorials/1. Geometry/../../DeepINN/geometry/domains/domainoperations/sampler_helper.py:50: UserWarning: Will sample random points in the created domain operation, with
a for loop over all input parameters, in total: 1
This may slow down the training.
warnings.warn(f"""Will sample random points in the created domain operation, with
/home/hell/Desktop/repos/DeepINN/Tutorials/1. Geometry/../../DeepINN/geometry/domains/domainoperations/sampler_helper.py:163: UserWarning: Will sample random points in the created domain operation, with
a for loop over all input parameters, in total: 1
This may slow down the training.
warnings.warn(f"""Will sample random points in the created domain operation, with
/home/hell/Desktop/repos/DeepINN/Tutorials/1. Geometry/../../DeepINN/geometry/domains/domainoperations/cut.py:107: UserWarning: Exact volume of this domain boundary is not known,
will use the estimate:
volume = domain_a.volume + domain_b.volume.
If you need the exact volume for sampling,
use domain.set_volume().
warnings.warn("""Exact volume of this domain boundary is not known,
![../../_images/206b4796802fdd775aa1b3b9a1d4f364bd3ac3d218cd5e3a45de4d94a88230a8.png](../../_images/206b4796802fdd775aa1b3b9a1d4f364bd3ac3d218cd5e3a45de4d94a88230a8.png)
![../../_images/6fd2ace32477f771af6886a485c069316d1f5aa1d2c7b208781f2fec3da72a49.png](../../_images/6fd2ace32477f771af6886a485c069316d1f5aa1d2c7b208781f2fec3da72a49.png)
![../../_images/803c7c4e4fd945e1279b081367d874871218657940e31a56b970addb49aa8760.png](../../_images/803c7c4e4fd945e1279b081367d874871218657940e31a56b970addb49aa8760.png)
X = dp.spaces.R2('x') # R2 space
R = dp.domains.Parallelogram(X, [-0.5,-0.5], [0.5,-0.5], [-0.5,0.5]) # unit square
C = dp.domains.Circle(X, [0,0], 0.25) # unit circle
right_boundary = dp.samplers.RandomUniformSampler(R.boundary, n_points=100, filter_fn=lambda x: x[:,0]==0.5)
left_boundary = dp.samplers.RandomUniformSampler(R.boundary, n_points=100, filter_fn=lambda x: x[:,0]==-0.5)
grid_C = dp.samplers.GridSampler(C.boundary, n_points=100)
boundary = right_boundary + left_boundary + grid_C
dp.utils.scatter(X, boundary)
![../../_images/84ae33e8b02ff510bc2d03ff33d33dfcefbd219ff714f3c1c291e5ef2c9e975b.png](../../_images/84ae33e8b02ff510bc2d03ff33d33dfcefbd219ff714f3c1c291e5ef2c9e975b.png)
import torch
X = dp.spaces.R2('x') # R2 space
R = dp.domains.Parallelogram(X, [-0.5,-0.5], [0.5,-0.5], [-0.5,0.5]) # unit square
C = dp.domains.Circle(X, [0,0], 0.25) # unit circle
right_boundary = dp.samplers.RandomUniformSampler(R.boundary, n_points=100, filter_fn=lambda x: x[:,0]==0.5)
left_boundary = dp.samplers.RandomUniformSampler(R.boundary, n_points=100, filter_fn=lambda x: x[:,0]==-0.5)
grid_C = dp.samplers.GridSampler(C.boundary, n_points=100, filter_fn=lambda x: (torch.sign(x[:,0])*x[:,0]**2 + x[:,1]**2)==0.25**(2))
boundary = right_boundary + left_boundary + grid_C
dp.utils.scatter(X, boundary)
![../../_images/c07b835172869788e07467f333d4db96b538e6043a51f2a5a69f832c9d487052.png](../../_images/c07b835172869788e07467f333d4db96b538e6043a51f2a5a69f832c9d487052.png)
X = dp.spaces.R2('x') # R2 space
R = dp.domains.Parallelogram(X, [-0.5,-0.5], [0.5,-0.5], [-0.5,0.5]) # unit square
C = dp.domains.Circle(X, [0,0], 0.25) # unit circle
right_boundary = dp.samplers.RandomUniformSampler(R.boundary, n_points=100, filter_fn=lambda x: x[:,0]==0.5)
left_boundary = dp.samplers.RandomUniformSampler(R.boundary, n_points=100, filter_fn=lambda x: x[:,0]==-0.5)
grid_C = dp.samplers.GridSampler(C.boundary, n_points=100, filter_fn=lambda x: (torch.sign(x[:,0])*x[:,0]**2 + torch.sign(x[:,1])*x[:,1]**2)==0.25**(2))
boundary = right_boundary + left_boundary + grid_C
dp.utils.scatter(X, boundary)
![../../_images/96430ec0f6f71b45aee44a527faf89e202a3b915666556814e270c64f373b79f.png](../../_images/96430ec0f6f71b45aee44a527faf89e202a3b915666556814e270c64f373b79f.png)