Data Generation

make_microstructure

pymks.datasets.make_microstructure(n_samples=10, size=(101, 101), n_phases=2, grain_size=None, seed=10, volume_fraction=None, percent_variance=None)

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
  • volume_fraction (tuple, optional) – specify the volume fraction of each phase
  • percent_variance (int, optional) – varies the volume fraction of the microstructure up to this percentage
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_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_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_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).

Data Generation that Requires SfePy

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:
  • n_samples (int, optional) – number of samples
  • 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

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

make_elastic_stiffness

pymks.datasets.make_elastic_stiffness(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, volume_fraction=None, percent_variance=None)

Generates microstructures and their effective stiffness 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
  • volume_fraction (tuple, optional) – specify the volume fraction of each phase
  • percent_variance (int, optional) – Only used if volume_fraction is specified. Randomly varies the volume fraction of the microstructure.
Returns:

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

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, volume_fraction=None, percent_variance=None)

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
  • volume_fraction (tuple, optional) – specify the volume fraction of each phase
  • percent_variance (int, optional) – Only used if volume_fraction is specified. Randomly varies the volume fraction of the microstructure.
Returns:

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

Simulations

Cahn-Hilliard

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, ...)

Finite Element Elasticity