# Linear Elasticity in 2D for 3 Phases¶

## Introduction¶

This example provides a demonstration of using PyMKS to compute the linear strain field for a three-phase composite material. It demonstrates how to generate data for delta microstructures and then use this data to calibrate the first order MKS influence coefficients. The calibrated influence coefficients are used to predict the strain response for a random microstructure and the results are compared with those from finite element. Finally, the influence coefficients are scaled up and the MKS results are again compared with the finite element data for a large problem.

PyMKS uses the finite element tool SfePy to generate both the strain fields to fit the MKS model and the verification data to evaluate the MKS model’s accuracy.

### Elastostatics Equations and Boundary Conditions¶

The governing equations for linear elasticity and the boundary conditions used in this example are the same as those provided in the Linear Elastic in 2D example.

## Modeling with MKS¶

### Calibration Data and Delta Microstructures¶

The first order MKS influence coefficients are all that is needed to compute a strain field of a random microstructure as long as the ratio between the elastic moduli (also known as the contrast) is less than 1.5. If this condition is met we can expect a mean absolute error of 2% or less when comparing the MKS results with those computed using finite element methods .

Because we are using distinct phases and the contrast is low enough to only need the first-order coefficients, delta microstructures and their strain fields are all that we need to calibrate the first-order influence coefficients .

The generate_delta function can be used to create the two delta microstructures needed to calibrate the first-order influence coefficients for a two phase microstructure. This function uses the Python module SfePy to compute the strain fields using finite element methods.

:

%matplotlib inline

import numpy as np
from sklearn.pipeline import Pipeline

from pymks import (
generate_delta,
plot_microstructures,
solve_fe,
PrimitiveTransformer,
LocalizationRegressor,
coeff_to_real,

)

:

x_delta = generate_delta(n_phases=3, shape=(21, 21)).persist()

plot_microstructures(*x_delta[:3], titles=['', '', ''], cmap='gray') Using delta microstructures for the calibration of the first-order influence coefficients is essentially the same, as using a unit impulse response to find the kernel of a system in signal processing. Any given delta microstructure is composed of only two phases with the center cell having an alternative phase from the remainder of the domain. The number of delta microstructures that are needed to calibrated the first-order coefficients is $$N(N-1)$$ where $$N$$ is the number of phases, therefore in this example 6 delta microstructures are required.

### Generating Calibration Data¶

In this example, the microstructures have three phases with elastic moduli values of 80, 100 and 120 and Poisson’s ratio values all equal to 0.3. The macroscopic imposed strain is 0.02.

A helper function strain_xx is created to solve the finite element problem and return the $$\varepsilon_{xx}$$ component of the strain. The length of the values for the elastic_modulus and poissons_ratio parameteres indicate the number of phases.

:

strain_xx = lambda x: solve_fe(
x,
elastic_modulus=(80, 100, 120),
poissons_ratio=(0.3, 0.3, 0.3),
macro_strain=0.02
)['strain'][...,0]

y_delta = strain_xx(x_delta).persist()


Observe strain field.

:

plot_microstructures(y_delta, titles=[r'$\mathbf{\varepsilon_{xx}}$']) ### Calibrating First-Order Influence Coefficients¶

Calibrate the influence coefficients by creating a model pipeline using the PrimitiveTransformer and the LocalizationRegressor.

:

model = Pipeline(steps=[
('discretize', PrimitiveTransformer(n_state=3, min_=0.0, max_=2.0)),
('regressor', LocalizationRegressor())
])


Now, pass the delta microstructures and their strain fields into the fit method to calibrate the first-order influence coefficients.

:

model.fit(x_delta, y_delta);


Observe the influence coefficients.

:

to_real = lambda x: coeff_to_real(x.steps.coeff).real

coeff = to_real(model)
plot_microstructures(
coeff[..., 0],
coeff[..., 1],
coeff[..., 2],
titles=['Influence coeff ', 'Influence coeff ', 'Influence coeff ']
) ### Predict of the Strain Field for a Random Microstructure¶

Use the calibrated model to compute the strain field for a random two phase microstructure and compare it with the results from a finite element simulation. The strain_xx helper function is used to generate the strain field.

:

da.random.seed(99)

x_data = da.random.randint(2, size=(1,) + x_delta.shape[1:]).persist()
y_data = strain_xx(x_data).persist()

:

plot_microstructures(
y_data,
titles=[r'FE - $\mathbf{\varepsilon_{xx}}$']
) Note that the calibrated influence coefficients can only be used to reproduce the simulation with the same boundary conditions that they were calibrated with.

Now to get the strain field from the model, pass the same microstructure to the predict method.

:

y_predict = model.predict(x_data)


Finall, compare the results from finite element simulation and the MKS model.

:

plot_microstructures(
y_data,
y_predict,
titles=[
r'$\mathbf{\varepsilon_{xx}}$ - FE',
r'$\mathbf{\varepsilon_{xx}}$ - MKS'
]
) Plot the difference between the two strain fields.

:

plot_microstructures(
(y_data - y_predict),
titles=['FE - MKS']
) The MKS model is able to capture the strain field for the random microstructure after being calibrated with delta microstructures.

## Resizing the Coefficeints to use on Larger Microstructures¶

The influence coefficients that were calibrated on a smaller microstructure can be used to predict the strain field on a larger microstructure though spectral interpolation , but accuracy of the MKS model drops slightly. To demonstrate how this is done, generate a new larger random microstructure and its strain field.

:

new_shape = tuple(np.array(x_delta.shape[1:]) * 3)
x_large = da.random.randint(2, size=(1,) + new_shape).persist()
y_large = strain_xx(x_large).persist()

plot_microstructures(y_large, titles=[r'$\mathbf{\varepsilon_{xx}}$ - FE (large)']) The influence coefficients that have already been calibrated need to be resized to match the shape of the new larger microstructure that we want to compute the strain field for. This can be done by passing the shape of the new larger microstructure into the coeff_resize method.

:

model.steps.coeff_resize(x_large.shape);


Observe the resized influence coefficients.

:

coeff_large = to_real(model)
plot_microstructures(
coeff_large[..., 0],
coeff_large[..., 1],
coeff_large[..., 2],
titles=['Influence coeff ', 'Influence coeff ', 'Influence coeff ']
) The resized coefficients will only work with the large microstructure now.

:

y_large_predict = model.predict(x_large).persist()

plot_microstructures(
y_large,
y_large_predict,
titles=[
r'$\mathbf{\varepsilon_{xx}}$ - FE (large)',
r'$\mathbf{\varepsilon_{xx}}$ - MKS (large)'
]
) Plot the difference between the two strain fields.

:

plot_microstructures(
(y_large - y_large_predict),
titles=['FE - MKS']
) The results from the strain field computed with the resized influence coefficients are not as accurate as they were before they were resized. This decrease in accuracy is expected when using spectral interpolation .