Data Generation

make_delta_microstructures

pymks.datasets.make_delta_microstructures(n_phases=2, size=(21, 21))

Constructs delta microstructures

Constructs delta microstructures for an arbitrary number of phases given the size of the domain.

Parameters:
  • n_phases (int, optional) – number of phases
  • size (tuple, optional) – dimension of microstructure
Returns:

delta microstructures for the system of shape (n_samples, n_x, ...)

Example

>>> X = np.array([[[[0, 0, 0],
...                 [0, 0, 0],
...                 [0, 0, 0]],
...                [[0, 0, 0],
...                 [0, 1, 0],
...                 [0, 0, 0]],
...                [[0, 0, 0],
...                 [0, 0, 0],
...                 [0, 0, 0]]],
...               [[[1, 1, 1],
...                 [1, 1, 1],
...                 [1, 1, 1]],
...                [[1, 1, 1],
...                 [1, 0, 1],
...                 [1, 1, 1]],
...                [[1, 1, 1],
...                 [1, 1, 1],
...                 [1, 1, 1]]]])
>>> assert(np.allclose(X, make_delta_microstructures(2, size=(3, 3, 3))))

make_elastic_FE_strain_delta

pymks.datasets.make_elastic_FE_strain_delta(elastic_modulus=(100, 150), poissons_ratio=(0.3, 0.3), size=(21, 21), macro_strain=0.01)

Generate delta microstructures and responses

Simple interface to generate delta microstructures and their strain response fields that can be used for the fit method in the MKSRegressionModel. The length of elastic_modulus and poissons_ratio indicates the number of phases in the microstructure. The following example is or a two phase microstructure with dimensions of (5, 5).

Parameters:
  • elastic_modulus (list, optional) – elastic moduli for the phases
  • poissons_ratio (list, optional) – Poisson’s ratios for the phases
  • size (tuple, optional) – size of the microstructure
  • macro_strain (float, optional) – Scalar for macroscopic strain applied
  • strain_index (int, optional) – interger value to return a particular strain field. 0 returns exx, 1 returns eyy, etc. To return all strain fields set strain_index equal to slice(None).
Returns:

tuple containing delta microstructures and their strain fields

Example

>>> elastic_modulus = (1., 2.)
>>> poissons_ratio = (0.3, 0.3)
>>> X, y = make_elastic_FE_strain_delta(elastic_modulus=elastic_modulus,
...                                     poissons_ratio=poissons_ratio,
...                                     size=(5, 5))

X is the delta microstructures, and y is the strain response fields.

make_elastic_FE_strain_random

pymks.datasets.make_elastic_FE_strain_random(n_samples=1, elastic_modulus=(100, 150), poissons_ratio=(0.3, 0.3), size=(21, 21), macro_strain=0.01)

Generate random microstructures and responses

Simple interface to generate random microstructures and their strain response fields that can be used for the fit method in the MKSRegressionModel. The following example is or a two phase microstructure with dimensions of (5, 5).

Parameters:
  • elastic_modulus (list, optional) – elastic moduli for the phases
  • poissons_ratio (list, optional) – Poisson’s ratios for the phases
  • size (tuple, optional) – size of the microstructure
  • macro_strain (float, optional) – Scalar for macroscopic strain applied
  • strain_index (int, optional) – interger value to return a particular strain field. 0 returns exx, 1 returns eyy, etc. To return all strain fields set strain_index equal to slice(None).
Returns:

tuple containing delta microstructures and their strain fields

Example

>>> elastic_modulus = (1., 2.)
>>> poissons_ratio = (0.3, 0.3)
>>> X, y = make_elastic_FE_strain_random(n_samples=1,
...                                      elastic_modulus=elastic_modulus,
...                                      poissons_ratio=poissons_ratio,
...                                      size=(5, 5))

X is the delta microstructures, and y is the strain response fields.

make_cahn_hilliard

pymks.datasets.make_cahn_hilliard(n_samples=1, size=(21, 21), dx=0.25, width=1.0, dt=0.001, n_steps=1)

Generate microstructures and responses for Cahn-Hilliard. Simple interface to generate random concentration fields and their evolution after one time step that can be used for the fit method in the MKSRegressionModel. The following example is or a two phase microstructure with dimensions of (6, 6).

Parameters:
  • n_samples (int, optional) – number of microstructure samples
  • size (tuple, optional) – size of the microstructure
  • dx (float, optional) – grid spacing
  • dt (float, optional) – timpe step size
  • width (float, optional) – interface width between phases.
  • n_steps (int, optional) – number of time steps used
Returns:

Array representing the microstructures at n_steps ahead of ‘X’

Example

>>> X, y = make_cahn_hilliard(n_samples=1, size=(6, 6))

X is the initial concentration fields, and y is the strain response fields (the concentration after one time step).

make_microstructure

pymks.datasets.make_microstructure(n_samples=10, size=(101, 101), n_phases=2, grain_size=(33, 14), seed=10)

Constructs microstructures for an arbitrary number of phases given the size of the domain, and relative grain size.

Parameters:
  • n_samples (int, optional) – number of samples
  • size (tuple, optional) – dimension of microstructure
  • n_phases (int, optional) – number of phases
  • grain_size (tuple, optional) – effective dimensions of grains
  • seed (int, optional) – seed for random number microstructureGenerator
Returns:

microstructures for the system of shape (n_samples, n_x, ...)

Example

>>> n_samples, n_phases = 1, 2
>>> size, grain_size = (3, 3), (1, 1)
>>> Xtest = np.array([[[0, 1, 0],
...                [0, 0, 0],
...                [0, 1, 1]]])
>>> X = make_microstructure(n_samples=n_samples, size=size,
...                         n_phases=n_phases, grain_size=grain_size,
...                         seed=0)
>>> assert(np.allclose(X, Xtest))

make_checkerboard_microstructure

pymks.datasets.make_checkerboard_microstructure(square_size, n_squares)

Constructs a checkerboard_microstructure with the square_size by square_size size squares and on a n_squares by n_squares

Parameters:
  • square_size (int) – length of the side of one square in the checkerboard.
  • n_squares (int) – number of squares along on size of the checkerboard.
Returns:

checkerboard microstructure with shape of (1, square_size * n_squares, square_size * n_squares)

Example

>>> square_size, n_squares = 2, 2
>>> Xtest = np.array([[[0, 0, 1, 1],
...                    [0, 0, 1, 1],
...                    [1, 1, 0, 0],
...                    [1, 1, 0, 0]]])
>>> X = make_checkerboard_microstructure(square_size, n_squares)
>>> assert(np.allclose(X, Xtest))

make_elastic_stress_random

pymks.datasets.make_elastic_stress_random(n_samples=[10, 10], elastic_modulus=(100, 150), poissons_ratio=(0.3, 0.3), size=(21, 21), macro_strain=0.01, grain_size=[(3, 3), (9, 9)], seed=10)

Generates microstructures and their macroscopic stress values for an applied macroscopic strain.

Parameters:
  • n_samples (int, optional) – number of samples
  • elastic_modulus (tuple, optional) – list of elastic moduli for the different phases.
  • poissons_ratio (tuple, optional) – list of poisson’s ratio values for the phases.
  • size (tuple, optional) – size of the microstructures
  • macro_strain (tuple, optional) – macroscopic strain applied to the sample.
  • grain_size (tuple, optional) – effective dimensions of grains
  • seed (int, optional) – seed for random number generator
Returns:

array of microstructures with dimensions (n_samples, n_x, ...) and effective stress values

Example

>>> X, y = make_elastic_stress_random(n_samples=1, elastic_modulus=(1, 1),
...                                   poissons_ratio=(1, 1),
...                                   grain_size=(3, 3), macro_strain=1.0)
>>> assert np.allclose(y, np.ones(y.shape))
>>> X, y = make_elastic_stress_random(n_samples=1, grain_size=(1, 1),
...                                   elastic_modulus=(100, 200),
...                                   size=(2, 2), poissons_ratio=(1, 3),
...                                   macro_strain=1., seed=3)
>>> X_result = np.array([[[1, 1],
...                       [0, 1]]])
>>> assert np.allclose(X, X_result)
>>> assert float(np.round(y, decimals=5)[0]) == 228.74696
>>> X, y = make_elastic_stress_random(n_samples=1, grain_size=(1, 1, 1),
...                                   elastic_modulus=(100, 200),
...                                   poissons_ratio=(1, 3),  seed=3,
...                                   macro_strain=1., size=(2, 2, 2))
>>> X_result = np.array([[[1, 1],
...                       [0, 0]],
...                      [[1, 1],
...                       [0, 0]]])
>>> assert np.allclose(X, X_result)
>>> assert np.round(y[0]).astype(int) == 150

Simulations

class pymks.datasets.cahn_hilliard_simulation.CahnHilliardSimulation(dx=0.25, gamma=1.0, dt=0.001)

Solve the Cahn-Hilliard equation for multiple samples in arbitrary dimensions. The concentration varies from -1 to 1. The equation is given by

\[\dot{\phi} = \nabla^2 \left( \phi^3 - \phi \right) - \gamma \nabla^4 \phi\]

The discretiztion scheme used here is from Chang and Rutenberg. The scheme is a semi-implicit discretization in time and is given by

\[\phi_{t+\Delta t} + \left(1 - a_1\right) \Delta t \nabla^2 \phi_{t+\Delta t} + \left(1 - a_2\right) \Delta t \gamma \nabla^4 \phi_{t+\Delta t} = \phi_t - \Delta t \nabla^2 \left(a_1 \phi_t + a_2 \gamma \nabla^2 \phi_t - \phi_t^3 \right)\]

where \(a_1=3\) and \(a_2=0\).

In 1D.

>>> np.random.seed(99)
>>> N = 100
>>> phi = 0.01 * (2 * np.random.random((2, N)) - 1)
>>> ch = CahnHilliardSimulation(gamma=4)
>>> for i in range(10000):
...     ch.run(phi)
...     phi[:] = ch.response
>>> assert (max(phi.flat) > 2e-3) and (min(phi.flat) < -2e-3)

In 2D.

>>> N = 101
>>> phi = 0.01 * (2 * np.random.random((2, N, N)) - 1)
>>> ch = CahnHilliardSimulation(gamma=4.)
>>> for i in range(100):
...     ch.run(phi)
...     phi[:] = ch.response
>>> assert (max(phi.flat) > 0.001) and (min(phi.flat) < -0.001)

In 3D.

>>> phi = 0.01 * (2 * np.random.random((2, N, N, N)) - 1)
>>> ch = CahnHilliardSimulation(gamma=4.)
>>> for i in range(10):
...     ch.run(phi)
...     phi[:] = ch.response
>>> assert (max(phi.flat) > 0.0005) and (min(phi.flat) < -0.0005)

Instanitate a CahnHilliardSimulation

Parameters:
  • dx (float, optional) – grid spacing
  • dt (float, optional) – time step size
  • gamma (float, optional) – paramater in CH equation
run(X)

Return the response field

Parameters:X (ND array) – Array representing the concentration field between -1 and 1 with shape (n_samples, n_x, ...)
class pymks.datasets.elastic_FE_simulation.ElasticFESimulation(elastic_modulus, poissons_ratio, macro_strain=1.0)

Use SfePy to solve a linear strain problem in 2D with a varying microstructure on a rectangular grid. The rectangle (cube) is held at the negative edge (plane) and displaced by 1 on the positive x edge (plane). Periodic boundary conditions are applied to the other boundaries.

The microstructure is of shape (n_samples, n_x, n_y) or (n_samples, n_x, n_y, n_z).

>>> X = np.zeros((1, 3, 3), dtype=int)
>>> X[0, :, 1] = 1
>>> sim = ElasticFESimulation(elastic_modulus=(1.0, 10.0),
...                           poissons_ratio=(0., 0.))
>>> sim.run(X)
>>> y = sim.strain

y is the strain with components as follows

>>> exx = y[..., 0]
>>> eyy = y[..., 1]
>>> exy = y[..., 2]

In this example, the strain is only in the x-direction and has a uniform value of 1 since the displacement is always 1 and the size of the domain is 1.

>>> assert np.allclose(exx, 1)
>>> assert np.allclose(eyy, 0)
>>> assert np.allclose(exy, 0)

The following example is for a system with contrast. It tests the left/right periodic offset and the top/bottom periodicity.

>>> X = np.array([[[1, 0, 0, 1],
...                [0, 1, 1, 1],
...                [0, 0, 1, 1],
...                [1, 0, 0, 1]]])
>>> n_samples, N, N = X.shape
>>> macro_strain = 0.1
>>> sim = ElasticFESimulation((10.0,1.0), (0.3,0.3), macro_strain=0.1)
>>> sim.run(X)
>>> u = sim.displacement[0]

Check that the offset for the left/right planes is N * macro_strain.

>>> assert np.allclose(u[-1,:,0] - u[0,:,0], N * macro_strain)

Check that the left/right side planes are periodic in y.

>>> assert np.allclose(u[0,:,1], u[-1,:,1])

Check that the top/bottom planes are periodic in both x and y.

>>> assert np.allclose(u[:,0], u[:,-1])

Instantiate a ElasticFESimulation.

Parameters:
  • elastic_modulus (1D array) – array of elastic moduli for phases
  • poissons_ratio (1D array) – array of Possion’s ratios for phases
  • macro_strain (float, optional) – Scalar for macroscopic strain
run(X)

Run the simulation.

Parameters:X (ND array) – microstructure with shape (n_samples, n_x, ...)