INHOMOGENEOUS ELASTICITY · THE CODE EXPLAINED

Direct iterative perturbation, from equation to code.

We write the physics, then its Fourier discretisation, then the code that implements it. Every stress, strain and acoustic-tensor entry is written out as an explicit polynomial in the components, as in my Fortran solver. We keep the worked code blocks visible here, so we can follow the implementation step by step. The complete notebook is available on request.

Code use: teaching, learning and demonstration only. Any other use requires my written permission.

We keep the code examples visible below. For the complete notebook, email me and clearly state which code you want, why you need it and how you intend to use it. Request details .

What stress does a misfitting plate create?

Take a soft plate that wants to stretch along \(x\) and contract along \(y\), tilted at 30° inside a stiffer matrix. The matrix holds it back. Which regions end up in compression, and which in tension? This picture is the answer we compute on this page: the plate is compressed, and just above and below its long faces the matrix is pulled into tension.

Stress sigma 11 around a soft elliptical plate tilted at 30 degrees: compressive inside the plate and to its sides, tensile just above and below its long faces
Stiffness ratio 0.5, \(\epsilon_\eta=0.003\), tetragonality \(t=-0.5\), clamped 128 nm cell, \(\sigma_{11}\) in MPa. The black curve marks the plate.

Because the stiffness changes from place to place, we cannot write this field down in one step. So we start from a displacement, solve an easier problem with one uniform stiffness, put what that problem leaves out on the right-hand side, and repeat until the displacement stops changing.

This page explains the first of the two inhomogeneous elasticity solvers. The complete NumPy, pyFFTW and Matplotlib notebook is available on request. It is based on my older Fortran codes, adapted to a single elliptical inclusion in two dimensions: the same iteration, the same homogeneous-strain treatment and the same expanded component form.

Step 1: The physical problem

A coherent precipitate would like to undergo a transformation strain \(\epsilon^0(\mathbf x)\), but the surrounding solid constrains it. The precipitate and matrix have different stiffnesses, so \(C(\mathbf x)\) varies in space. We need the displacement \(\mathbf u\) that puts the solid in mechanical equilibrium:

\[\epsilon_{ij}=\bar E_{ij}+\tfrac12\left(u_{i,j}+u_{j,i}\right),\qquad \sigma_{ij}=C_{ijkl}(\mathbf x)\left(\epsilon_{kl}-\epsilon^0_{kl}\right),\qquad \sigma_{ij,j}=0.\]

The total strain has two parts: a periodic part from the displacement, and the homogeneous strain \(\bar E\), its spatial average. How \(\bar E\) is chosen sets the macroscopic boundary condition (Step 9). The cell is a periodic unit square in two dimensions.

Step 2: Voigt notation, written out in components

In Voigt notation stress and strain are written as six-component lists and the stiffness as a symmetric \(6\times6\) matrix with 21 independent constants. A cubic crystal needs only three, \(C_{11}, C_{12}, C_{44}\), and in two dimensions only the in-plane block is used. The strain list carries the engineering shear \(\gamma_{12}=2\epsilon_{12}\); this is what makes the matrix product and the energy come out right:

\[\begin{pmatrix}\sigma_{11}\\ \sigma_{22}\\ \sigma_{12}\end{pmatrix}=\begin{pmatrix}C_{11}&C_{12}&0\\ C_{12}&C_{11}&0\\ 0&0&C_{44}\end{pmatrix}\begin{pmatrix}e_{11}\\ e_{22}\\ \gamma_{12}\end{pmatrix},\qquad f=\tfrac12\left(e_{11}\sigma_{11}+e_{22}\sigma_{22}+\gamma_{12}\sigma_{12}\right),\]

where \(e=\epsilon-\epsilon^0\) is the elastic strain. Multiplying out gives the polynomials the code uses directly:

\[\sigma_{11}=C_{11}e_{11}+C_{12}e_{22},\qquad \sigma_{22}=C_{12}e_{11}+C_{11}e_{22},\qquad \sigma_{12}=C_{44}\gamma_{12} .\]

Each field is a separate two-dimensional array, so every line of the code is a short vectorised expression. This is several times faster than contracting full fourth-order tensors.

The three functions

FunctionInputsReturns
wavevectors(n)grid size n (64, 128, 256, …)wavevector arrays kx, ky
plate_fields(n, contrast, angle, misfit_ratio, matrix)grid size, stiffness ratio, plate angle in degrees, tetragonality, matrix constantsstiffness fields, eigenstrain fields, reference stiffness and the indicator h
solve_direct_voigt(stiffness, eigenstrain, reference, control, mean_strain, applied_stress, tolerance, max_iterations)the fields from plate_fields; control is 'strain' or 'stress'; the prescribed \(\bar E\) or applied \(\sigma^a\); tolerance on the relative displacement change (default \(10^{-8}\)); iteration limita dictionary with displacement, strain, stress, mean strain, energy, equilibrium residual and the convergence history

Step 3: The periodic grid and its wavevectors

We sample the unit square on an \(N\times N\) grid, \(x_n=n/N\), and represent each field by its discrete Fourier series with wavevector components \(k_m=2\pi m\), we use grids of \(N=2^n\) points (64, 128, 256), and number the modes as in my older Fortran codes: \(m=0,1,\dots,N/2,-N/2+1,\dots,-1\). The mode \(m=N/2\) is the Nyquist mode. We set \(k=0\) for that mode and transform with pyFFTW; here is why.

def wavevectors(n):
    """Fourier wavevector components on an n x n grid (n = 64, 128, 256, ...), x along axis 0.
    k = 2 pi m with m = 0, 1, ..., n/2, -n/2+1, ..., -1, as in my Fortran codes, except that k = 0 at m = n/2:
    every derivative here is a first derivative, whose Nyquist term vanishes (Johnson 2011, Algorithm 1)."""
    if n < 4 or n & (n - 1):
        raise ValueError('Choose a power of two for the grid size, at least 4: 64, 128 or 256.')
    m = np.arange(n)
    m = np.where(m > n//2, m - n, m)
    k = 2*np.pi*m
    k[n//2] = 0.0
    return np.meshgrid(k, k, indexing='ij')
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
n\(N\)Number of grid points along each side; an FFT-friendly size such as 64, 128 or 256.
mInteger mode numbers 0, 1, …, N/2, −N/2+1, …, −1, as in my older codes.
k\(k_m=2\pi m\)One-dimensional list of wavenumbers.
kx, ky\(k_1,\ k_2\)Arrays of the two wavevector components at every Fourier point; x is array axis 0.

Step 4: Units, the matrix and the prescribed plate

Units and the matrix. One grid cell is 1 nm, so an \(N\times N\) grid is a box \(N\) nm across. Stiffness, stress and energy density are in eV/nm³: 1 eV/nm³ is 160.2 MPa, so 1 GPa is 6.2415 eV/nm³. The matrix is a superalloy at high temperature, described as in the Elastic Stress Effects Laboratory by its average shear modulus \(\mu=80\) GPa, effective Poisson parameter \(\nu=1/3\) and Zener ratio \(A_Z=1\), and converted to cubic constants by the Schmidt–Gross formulas:

\[C_{11}=\mu\left[\frac{2(2+A_Z)}{1+A_Z}-\frac{1-4\nu}{1-2\nu}\right],\qquad C_{12}=\mu\left[\frac{2A_Z}{1+A_Z}-\frac{1-4\nu}{1-2\nu}\right],\qquad C_{44}=\mu\,\frac{2A_Z}{1+A_Z},\]

which at \(A_Z=1\) and \(\nu=1/3\) give \(C_{11}=4\mu\), \(C_{12}=2\mu\), \(C_{44}=\mu\): 1997.3, 998.6 and 499.3 eV/nm³, that is 320, 160 and 80 GPa. The code keeps these in cubic and MATRIX, and converts stresses to MPa with MPA when it prints or plots them.

A smooth indicator \(h(\mathbf x)\) is 1 inside an elliptical plate and 0 in the matrix, with a tanh interface of fixed physical width. Each stiffness constant and eigenstrain component interpolates separately:

\[C_{11}(\mathbf x)=C^{m}_{11}+h\left(C^{p}_{11}-C^{m}_{11}\right)\ \text{(likewise }C_{12},C_{44}\text{)},\qquad \epsilon^0_{11}=\epsilon_\eta\,h,\quad \epsilon^0_{22}=\epsilon_\eta\,t\,h,\quad \gamma^0_{12}=0,\quad \epsilon_\eta=0.003,\]

as in my PhD thesis (Eq. 3.2): \(\epsilon_\eta\) is the characteristic misfit strain and \(t=\epsilon^0_{22}/\epsilon^0_{11}\) is the tetragonality, between \(-1\) (pure shear) and \(+1\) (dilatation). The default \(t=-0.5\) (Case D of the thesis) means \(\epsilon^0_{11}=0.003\), \(\epsilon^0_{22}=-0.0015\). The plate's stiffness is the matrix stiffness times the stiffness ratio. The reference stiffness is the mean of matrix and precipitate, as in my Fortran code. Rotating the plate rotates only its shape; crystal axes, stiffness and eigenstrain stay fixed.

# Units: one grid cell is 1 nm, so lengths are in nm; stiffness, stress and energy density are in eV/nm^3.
EV_NM3 = 1.602176634e-19/1e-27            # one eV/nm^3 in pascal: 1.602e8 Pa = 160.2 MPa
GPA, MPA = 1e9/EV_NM3, 1e6/EV_NM3        # one GPa and one MPa in eV/nm^3: 6.2415 and 0.0062415

def cubic(mu, nu=1/3, az=1.0):
    """Cubic constants C11, C12, C44 from the Schmidt-Gross parameters: the average shear modulus mu,
    the effective Poisson parameter nu and the Zener ratio az (Schmidt and Gross 1997; our Elastic Lab).
    az = 1 is isotropic: C44 = mu, C12 = 2 mu nu/(1 - 2 nu), C11 = 2 mu (1 - nu)/(1 - 2 nu)."""
    p = (1 - 4*nu)/(1 - 2*nu)
    return mu*(2*(2 + az)/(1 + az) - p), mu*(2*az/(1 + az) - p), mu*2*az/(1 + az)

MATRIX = cubic(80*GPA)                    # a superalloy matrix at high temperature: mu = 80 GPa = 499.3 eV/nm^3, nu = 1/3, isotropic
MISFIT = 0.003                            # the characteristic misfit strain: a few tenths of a percent, as between gamma and gamma'

def plate_fields(n=64, contrast=0.5, angle=30.0, misfit_ratio=-0.5, matrix=MATRIX):
    """Smooth plate: stiffness fields c11, c12, c44 and eigenstrain (e011, e022, g012)."""
    x, y = np.meshgrid(np.arange(n)/n - .5, np.arange(n)/n - .5, indexing='ij')
    t = np.deg2rad(angle)
    along = x*np.cos(t) + y*np.sin(t)
    across = -x*np.sin(t) + y*np.cos(t)
    h = .5*(1 - np.tanh(((along/.23)**2 + (across/.075)**2 - 1)/.35))
    m11, m12, m44 = matrix
    p11, p12, p44 = (contrast*m11, contrast*m12, contrast*m44)
    c11 = m11 + h*(p11 - m11)
    c12 = m12 + h*(p12 - m12)
    c44 = m44 + h*(p44 - m44)
    e011 = MISFIT*h
    e022 = MISFIT*misfit_ratio*h
    g012 = np.zeros_like(h)
    reference = (.5*(m11 + p11), .5*(m12 + p12), .5*(m44 + p44))
    return (c11, c12, c44), (e011, e022, g012), reference, h
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
contrast\(C^{p}/C^{m}\)Precipitate stiffness as a fraction of the matrix stiffness (0.5 = soft plate, 2 = hard plate).
angle\(\theta\)Angle of the plate's long axis from x, in degrees.
misfit_ratio\(t\)Tetragonality \(t=\epsilon^0_{22}/\epsilon^0_{11}\).
matrix\(C^m_{11}, C^m_{12}, C^m_{44}\)Matrix stiffness constants.
x, y\(x,\ y\)Grid coordinates, centered on the plate.
t\(\theta\)The plate angle converted to radians.
along, acrossCoordinates along and across the rotated plate.
h\(h(\mathbf x)\)Smooth precipitate indicator: 1 inside the plate, 0 in the matrix.
m11, m12, m44\(C^m_{ij}\)Matrix constants unpacked from matrix.
p11, p12, p44\(C^p_{ij}\)Precipitate stiffness constants.
c11, c12, c44\(C_{11}(\mathbf x)\), …Stiffness fields interpolated with \(h\).
e011, e022, g012\(\epsilon^0_{11},\ \epsilon^0_{22},\ \gamma^0_{12}\)Eigenstrain fields in Voigt form; g012 is the engineering shear (zero here).
reference\(C^{\mathrm{ref}}\)Reference stiffness: the mean of matrix and precipitate constants.

Step 5: Strain from the current displacement

The unknown is the displacement. In Fourier space a derivative is a multiplication, \(\partial_j\to\mathrm i k_j\), so the periodic strain components follow directly from \(\widetilde u\):

\[\widetilde{\delta\epsilon}_{11}=\mathrm i k_1\widetilde u_1,\qquad \widetilde{\delta\epsilon}_{22}=\mathrm i k_2\widetilde u_2,\qquad \widetilde{\delta\gamma}_{12}=\mathrm i\left(k_2\widetilde u_1+k_1\widetilde u_2\right).\]

We start from zero displacement, as in my older codes.

# Start from zero displacement, as in my older codes.
u1_hat = np.zeros((n, n), complex)
u2_hat = np.zeros((n, n), complex)
# Periodic strain from the current displacement: eps_ij = (u_i,j + u_j,i)/2, g12 = u1,2 + u2,1.
de11 = ifft(1j*kx*u1_hat)
de22 = ifft(1j*ky*u2_hat)
dg12 = ifft(1j*(ky*u1_hat + kx*u2_hat))
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
u1_hat, u2_hat\(\widetilde u_1,\ \widetilde u_2\)Fourier transforms of the two displacement components; the iteration updates these.
fft, ifftTwo-dimensional forward and inverse FFT; ifft keeps the real part.
de11, de22, dg12\(\delta\epsilon_{11},\ \delta\epsilon_{22},\ \delta\gamma_{12}\)Periodic strain from the displacement; dg12 is the engineering shear \(u_{1,2}+u_{2,1}\).
E11, E22, G12\(\bar E_{11},\ \bar E_{22},\ \bar\Gamma_{12}\)Homogeneous (average) strain; G12 is its engineering shear.
e11, e22, g12\(\epsilon_{11},\ \epsilon_{22},\ \gamma_{12}\)Total strain = homogeneous + periodic.

Why a periodic box, and what the homogeneous strain does

Before the code of Step 6, here is the idea it serves, told by the matrix, the plate and the strain. The numbers are from the default plate of Step 12: a 64 × 64 box, stiffness ratio 0.5, \(\epsilon^0_{11}=0.003\), \(\epsilon^0_{22}=-0.0015\), plate at 30°, matrix \(\mu=80\) GPa (\(C_{11}=1997\), \(C_{12}=999\), \(C_{44}=499\) eV/nm³), one grid cell 1 nm, so the box is 64 nm across; stresses are in MPa.

1

One plate stands for millions

A real crystal holds millions of these plates. I cannot compute them all.

So take one box with one of me, and repeat it. Every copy sees the same neighbors.

2

Why periodic

Periodic copies have no outer surface, so no edge effects. And every derivative becomes a multiplication in Fourier space.

The price: I feel my own images one box away. A box much bigger than me keeps that small.

3

Homogenization: the box as one material

Stretch the whole box by Ē and every copy follows. The mean stress I answer with is the box’s own law: ⟨σ⟩ = C_eff : Ē.

With no misfit, Ē = (0.001, 0, 0) gives ⟨σₓₓ⟩ = 308 MPa. The box with its soft plate has C₁₁ = 308 GPa, not the matrix’s 320. That is what homogenization computes.

4

Case 1: hold the homogeneous strain

Clamp me: Ē = 0. The plate wants to stretch along x, I do not let it, and I answer with a mean stress of −29 MPa along x.

Any fixed Ē works the same way: with Ē = (0.001, 0, 0) and the misfit, the mean stress is 279 MPa along x. This is control = 'strain'.

5

Case 2: stress-free

Now let go: ⟨σ⟩ = 0. Each iteration finds the Ē that makes the mean stress vanish, so I change the box’s shape: Ē = (0.000127, −0.000063, −0.000020).

And the energy falls from 1.00 × 10⁻⁴ to 0.88 × 10⁻⁴ eV/nm³, as it must when a constraint is released. This is control = 'stress' with σᵃ = 0.

6

Pull on the box

With σᵃ ≠ 0 we pull on a box that has no surface to pull. The same Ē update does it.

How that works, how to hold a void or a rigid inclusion, and which solver to use: How do we pull on a periodic box?

CaseWhat is held\(\bar E\)\(\langle\sigma\rangle\)Energy \(F/V\) (eV/nm³)
No misfit, pulled\(\bar E\)(0.001, 0, 0)(308, 153, 0.7) MPa
Clamped\(\bar E\)(0, 0, 0)(−29.4, −0.02, 1.5) MPa\(1.001\times10^{-4}\)
Pulled, with misfit\(\bar E\)(0.001, 0, 0)(279, 153, 2.2) MPa\(8.786\times10^{-4}\)
Stress-free\(\langle\sigma\rangle\)(0.000127, −0.000063, −0.000020)(0, 0, 0) to \(10^{-8}\) MPa\(8.837\times10^{-5}\)

The first row is homogenization in one line: the box answers a pure stretch with a mean stress, and the ratio, 1924 eV/nm³ or 308 GPa, is the effective \(C_{11}\) of the box with its soft plate, against 320 GPa for the matrix alone. Each run converged in 15 iterations. The strain triples carry the engineering shear \(\gamma_{12}\), as everywhere on this page. Prescribing a mean stress other than zero, and the exact void and rigid limits that this solver cannot reach, are the subject of How do we pull on a periodic box?

Step 6: The homogeneous strain

The Fourier solve fixes only the periodic part of the strain. The average part, \(\bar E\), is set by the macroscopic boundary condition:

\[\bar E=\langle C\rangle^{-1}:\left(\sigma^{a}+\langle C:\epsilon^0\rangle-\langle C:\delta\epsilon\rangle\right).\]

Because \(\delta\epsilon\) changes with the displacement, \(\bar E\) is recomputed at every iteration, exactly as in my older codes.

Derivation of the homogeneous strain

Write the total strain as its average plus the periodic part from the displacement, \(\epsilon(\mathbf x)=\bar E+\delta\epsilon(\mathbf x)\). The periodic part has zero average, \(\langle\delta\epsilon\rangle=0\), because it is a gradient of a periodic displacement. Hooke's law gives

\[\sigma(\mathbf x)=C(\mathbf x):\big(\bar E+\delta\epsilon(\mathbf x)-\epsilon^0(\mathbf x)\big).\]

Average over the cell. \(\bar E\) is uniform, so it comes out of the average, but \(\delta\epsilon\) does not: \(C\) varies in space, so \(\langle C:\delta\epsilon\rangle\ne0\) even though \(\langle\delta\epsilon\rangle=0\):

\[\langle\sigma\rangle=\langle C\rangle:\bar E+\langle C:\delta\epsilon\rangle-\langle C:\epsilon^0\rangle .\]

The macroscopic boundary condition sets this average equal to the applied stress, \(\langle\sigma\rangle=\sigma^{a}\). Solving for \(\bar E\),

\[\bar E=\langle C\rangle^{-1}:\left(\sigma^{a}+\langle C:\epsilon^0\rangle-\langle C:\delta\epsilon\rangle\right)\equiv\langle C\rangle^{-1}:b .\]

For the in-plane cubic block, \(\langle C\rangle\) has the same form as \(C\), and its inverse in Voigt form (engineering shear) is written out:

\[\bar E_{11}=\frac{\langle C_{11}\rangle b_{11}-\langle C_{12}\rangle b_{22}}{\langle C_{11}\rangle^2-\langle C_{12}\rangle^2},\qquad \bar E_{22}=\frac{\langle C_{11}\rangle b_{22}-\langle C_{12}\rangle b_{11}}{\langle C_{11}\rangle^2-\langle C_{12}\rangle^2},\qquad \bar\Gamma_{12}=\frac{b_{12}}{\langle C_{44}\rangle} .\]

In my older Fortran code this is hom_strain_v = SS_v · (sigappl_v + avgeigstress_v − avgperstress_v), with SS_v the inverse of the averaged stiffness from FindInv.

# Averages used by the stress-controlled homogeneous strain.
a11, a12, a44 = c11.mean(), c12.mean(), c44.mean()
t011 = (c11*e011 + c12*e022).mean()
t022 = (c12*e011 + c11*e022).mean()
t012 = (c44*g012).mean()
sa11, sa22, sa12 = applied_stress
E11, E22, G12 = mean_strain
if control == 'stress':
    # <sigma> = <C>:E + <C:delta_eps> - <C:eps0> = sigma_applied, solved for E.
    b11 = sa11 + t011 - (c11*de11 + c12*de22).mean()
    b22 = sa22 + t022 - (c12*de11 + c11*de22).mean()
    b12 = sa12 + t012 - (c44*dg12).mean()
    E11 = (a11*b11 - a12*b22)/(a11**2 - a12**2)
    E22 = (a11*b22 - a12*b11)/(a11**2 - a12**2)
    G12 = b12/a44
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
control'strain' fixes the homogeneous strain; 'stress' makes the average stress equal the applied stress.
a11, a12, a44\(\langle C_{11}\rangle,\ \langle C_{12}\rangle,\ \langle C_{44}\rangle\)Spatial averages of the stiffness constants.
t011, t022, t012\(\langle C:\epsilon^0\rangle\)Average eigenstress, computed once before the loop.
sa11, sa22, sa12\(\sigma^{a}_{11},\ \sigma^{a}_{22},\ \sigma^{a}_{12}\)Applied stress (zero for a stress-free body).
b11, b22, b12\(b=\sigma^a+\langle C:\epsilon^0\rangle-\langle C:\delta\epsilon\rangle\)Right-hand side for the homogeneous strain, recomputed every iteration.

Step 7: Split the stiffness and form the right-hand side

This is the central idea. We split the stiffness into a homogeneous part and the remainder, \(C=C^{\mathrm{hom}}+\Delta C\), and write the stress with the total strain \(\epsilon=\bar E+\delta\epsilon\):

\[\sigma=C:(\bar E+\delta\epsilon-\epsilon^0)=C^{\mathrm{hom}}:\delta\epsilon-ts,\qquad ts=C:(\epsilon^0-\bar E)-\Delta C:\delta\epsilon .\]

Equilibrium, \(\partial_j\sigma_{ij}=0\), then reads \(\partial_j\big(C^{\mathrm{hom}}_{ijkl}\,\delta\epsilon_{kl}\big)=\partial_j\, ts_{ij}\): a problem for a homogeneous solid, with every heterogeneous term moved into \(ts\). In components,

\[\begin{aligned}ts_{11}&=C_{11}(\epsilon^0_{11}-\bar E_{11})+C_{12}(\epsilon^0_{22}-\bar E_{22})-\left(\Delta C_{11}\delta\epsilon_{11}+\Delta C_{12}\delta\epsilon_{22}\right),\\ ts_{22}&=C_{12}(\epsilon^0_{11}-\bar E_{11})+C_{11}(\epsilon^0_{22}-\bar E_{22})-\left(\Delta C_{12}\delta\epsilon_{11}+\Delta C_{11}\delta\epsilon_{22}\right),\\ ts_{12}&=C_{44}(\gamma^0_{12}-\bar\Gamma_{12})-\Delta C_{44}\,\delta\gamma_{12} .\end{aligned}\]
# Inhomogeneous part of the stiffness, as Cinhom in my older codes.
d11, d12, d44 = c11 - r11, c12 - r12, c44 - r44
# Right-hand side as in my older codes: ts = C:(eps0 - E) - Cinhom:delta_eps.
ts11 = c11*(e011 - E11) + c12*(e022 - E22) - (d11*de11 + d12*de22)
ts22 = c12*(e011 - E11) + c11*(e022 - E22) - (d12*de11 + d11*de22)
ts12 = c44*(g012 - G12) - d44*dg12
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
r11, r12, r44\(C^{\mathrm{hom}}_{ij}\)Homogeneous (reference) stiffness constants: the mean of matrix and precipitate, Chom in my older codes.
d11, d12, d44\(\Delta C_{ij}\)Inhomogeneous part \(C-C^{\mathrm{hom}}\) at every grid point, Cinhom in my older codes.
ts11, ts22, ts12\(ts_{11},\ ts_{22},\ ts_{12}\)Right-hand side \(C:(\epsilon^0-\bar E)-\Delta C:\delta\epsilon\), the array ts in my older codes.

Step 8: Solve the homogeneous problem for the new displacement

Substituting \(\partial_j\to\mathrm i k_j\) turns the homogeneous equilibrium equation into a \(2\times2\) linear system for \(\widetilde u\) at every wavevector, with the acoustic tensor \(Q\) and its inverse \(\Omega=Q^{-1}\):

\[Q_{11}=C^{\mathrm{hom}}_{11}k_1^2+C^{\mathrm{hom}}_{44}k_2^2,\qquad Q_{22}=C^{\mathrm{hom}}_{44}k_1^2+C^{\mathrm{hom}}_{11}k_2^2,\qquad Q_{12}=\left(C^{\mathrm{hom}}_{12}+C^{\mathrm{hom}}_{44}\right)k_1k_2,\]\[\Omega_{11}=\frac{Q_{22}}{\det Q},\qquad \Omega_{22}=\frac{Q_{11}}{\det Q},\qquad \Omega_{12}=-\frac{Q_{12}}{\det Q} .\]
# Acoustic tensor of the homogeneous solid, Q_ik = Chom_ijkl k_j k_l, and its inverse Omega.
q11 = r11*kx**2 + r44*ky**2
q22 = r44*kx**2 + r11*ky**2
q12 = (r12 + r44)*kx*ky
det = q11*q22 - q12**2
zero_k = (kx == 0) & (ky == 0)          # no displacement for a k = 0 mode
det[zero_k] = 1.0
o11, o22, o12 = q22/det, q11/det, -q12/det
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
q11, q22, q12\(Q_{11},\ Q_{22},\ Q_{12}\)Acoustic tensor of the homogeneous solid at every wavevector.
det\(Q_{11}Q_{22}-Q_{12}^2\)Its determinant; set to 1 at \(\mathbf k=\mathbf 0\) only to avoid dividing by zero.
o11, o22, o12\(\Omega_{11},\ \Omega_{22},\ \Omega_{12}\)Components of \(\Omega=Q^{-1}\), written out explicitly (omega in my older codes).

The new displacement is then

\[f_1=k_1\widetilde{ts}_{11}+k_2\widetilde{ts}_{12},\quad f_2=k_1\widetilde{ts}_{12}+k_2\widetilde{ts}_{22},\qquad \widetilde u_i^{\,\mathrm{new}}=-\mathrm i\,\Omega_{ij}f_j,\qquad \widetilde{\mathbf u}(\mathbf 0)=\mathbf 0 .\]

The \(\mathbf k=\mathbf 0\) mode only translates the body, so we set it to zero; the average strain is carried by \(\bar E\).

# Solve the homogeneous problem for the new displacement: u_new = -i Omega . (k . ts).
ts11_hat, ts22_hat, ts12_hat = fft(ts11), fft(ts22), fft(ts12)
f1 = kx*ts11_hat + ky*ts12_hat
f2 = kx*ts12_hat + ky*ts22_hat
u1_new = -1j*(o11*f1 + o12*f2)
u2_new = -1j*(o12*f1 + o22*f2)
u1_new[zero_k] = u2_new[zero_k] = 0
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
ts11_hat, ts22_hat, ts12_hat\(\widetilde{ts}_{ij}\)Fourier transforms of the right-hand side.
f1, f2\(f_1,\ f_2\)Force term \(k_j\widetilde{ts}_{ij}\) (fk1, fk2 in my older codes).
u1_new, u2_new\(\widetilde u_1,\ \widetilde u_2\)New displacement \(-\mathrm i\,\Omega\cdot\mathbf f\); the \(\mathbf k=\mathbf 0\) mode is set to 0 (unewkx, unewky).

Step 9: Repeat until the displacement stops changing

\(ts\) depends on the strain, and so on the displacement being solved for. We therefore repeat Steps 5–8, replacing the displacement by the new one each time, and stop when the relative change is below the tolerance:

\[\frac{\lVert \widetilde{\mathbf u}^{\,\mathrm{new}}-\widetilde{\mathbf u}\rVert}{\lVert\widetilde{\mathbf u}^{\,\mathrm{new}}\rVert}<\text{tolerance},\qquad \widetilde{\mathbf u}\leftarrow\widetilde{\mathbf u}^{\,\mathrm{new}} .\]

My older codes test the absolute change (disperror); here we divide by the size of the displacement so that the same tolerance works for any misfit.

# Convergence: how much did the displacement change? (disperror in my older codes)
change = np.sqrt(np.sum(np.abs(u1_new - u1_hat)**2 + np.abs(u2_new - u2_hat)**2))
size = max(np.sqrt(np.sum(np.abs(u1_new)**2 + np.abs(u2_new)**2)), 1e-300)
u1_hat, u2_hat = u1_new, u2_new
history.append((iteration, change/size))
if change/size < tolerance:
    break
if not np.isfinite(change) or change/size > 1e12:
    raise RuntimeError('Displacement iteration diverged; choose a reference stiffness closer to the phases.')
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
changeSize of the change in displacement since the previous iteration (disperror in my older codes).
sizeSize of the new displacement; the test uses the relative change change/size.
historyOne row per iteration: iteration number and relative displacement change. Used for the convergence plot.
toleranceStop when the relative change falls below this value (default \(10^{-8}\)).

Step 10: Stress, equilibrium check and energy

With the converged displacement, we build the strain, the elastic strain and the stress with the expanded Hooke's law of Step 2:

e11, e22, g12 = E11 + de11, E22 + de22, G12 + dg12
# Elastic strain and stress, written out in components.
el11, el22, elg = e11 - e011, e22 - e022, g12 - g012
s11 = c11*el11 + c12*el22
s22 = c12*el11 + c11*el22
s12 = c44*elg
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
el11, el22, elg\(e_{11},\ e_{22},\ \gamma^{\mathrm{el}}_{12}\)Elastic strain: total strain minus eigenstrain.
s11, s22, s12\(\sigma_{11},\ \sigma_{22},\ \sigma_{12}\)Stress components from the expanded Hooke's law.

As an independent test, the divergence of the stress must vanish, \(\widetilde{(\nabla\cdot\sigma)}_i=\mathrm i\,k_j\widetilde\sigma_{ij}=0\). We use the same first-derivative wavevectors as for the strain, so the Nyquist term drops out here too.

# Check equilibrium: div(sigma)_i = i k_j sigma_ij should vanish.
s11_hat, s22_hat, s12_hat = fft(s11), fft(s22), fft(s12)
div1_hat = 1j*(kx*s11_hat + ky*s12_hat)
div2_hat = 1j*(kx*s12_hat + ky*s22_hat)
div1, div2 = ifft(div1_hat), ifft(div2_hat)
stress_size = max(2*np.pi*np.sqrt(np.mean(s11**2 + s22**2 + 2*s12**2)), 1e-300)
residual = np.sqrt(np.mean(div1**2 + div2**2))/stress_size
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
s11_hat, s22_hat, s12_hat\(\widetilde\sigma_{ij}\)Fourier transforms of the stresses.
div1_hat, div2_hat, div1, div2\((\nabla\cdot\sigma)_1,\ (\nabla\cdot\sigma)_2\)The two components of the stress divergence; zero at equilibrium.
stress_sizeA typical stress size, used to make the residual dimensionless.
residual\(r\)Root-mean-square divergence divided by stress_size: an independent check, reported after convergence.
\[\frac{F}{V}=\frac12\left\langle e_{11}\sigma_{11}+e_{22}\sigma_{22}+\gamma^{\mathrm{el}}_{12}\sigma_{12}\right\rangle\;-\;\sigma^{a}:\bar E\quad\text{(the last term only under stress control)}.\]
# Elastic energy density; under stress control subtract the work of the applied load.
energy = 0.5*np.mean(el11*s11 + el22*s22 + elg*s12)
if control == 'stress':
    energy -= sa11*E11 + sa22*E22 + sa12*G12
Names in this code
Every name, in a table
In the codeSymbolMeaning
energy\(F/V\)Average elastic energy density; under stress control the work term \(\sigma^a:\bar E\) is subtracted.

Step 11: The complete solver

Putting Steps 5–10 together: start from zero displacement; compute the strain; update \(\bar E\) under stress control; form \(ts\); solve for the new displacement; repeat until it stops changing; then compute the stress, check equilibrium and evaluate the energy.

The complete solver function
def solve_direct_voigt(stiffness, eigenstrain, reference, control='strain',
                       mean_strain=(0.0, 0.0, 0.0), applied_stress=(0.0, 0.0, 0.0),
                       tolerance=1e-8, max_iterations=4000):
    c11, c12, c44 = stiffness
    e011, e022, g012 = eigenstrain
    r11, r12, r44 = reference
    if min(r44, r11 - r12, r11 + r12) <= 0:
        raise ValueError('The reference stiffness must be stable.')
    if control not in ('strain', 'stress'):
        raise ValueError("control must be 'strain' or 'stress'.")
    n = c11.shape[0]
    kx, ky = wavevectors(n)
    fft, ifft = fftw.fft2, lambda a: fftw.ifft2(a).real

    # Inhomogeneous part of the stiffness, as Cinhom in my older codes.
    d11, d12, d44 = c11 - r11, c12 - r12, c44 - r44

    # Acoustic tensor of the homogeneous solid, Q_ik = Chom_ijkl k_j k_l, and its inverse Omega.
    q11 = r11*kx**2 + r44*ky**2
    q22 = r44*kx**2 + r11*ky**2
    q12 = (r12 + r44)*kx*ky
    det = q11*q22 - q12**2
    zero_k = (kx == 0) & (ky == 0)          # no displacement for a k = 0 mode
    det[zero_k] = 1.0
    o11, o22, o12 = q22/det, q11/det, -q12/det

    # Averages used by the stress-controlled homogeneous strain.
    a11, a12, a44 = c11.mean(), c12.mean(), c44.mean()
    t011 = (c11*e011 + c12*e022).mean()
    t022 = (c12*e011 + c11*e022).mean()
    t012 = (c44*g012).mean()
    sa11, sa22, sa12 = applied_stress
    E11, E22, G12 = mean_strain

    # Start from zero displacement, as in my older codes.
    u1_hat = np.zeros((n, n), complex)
    u2_hat = np.zeros((n, n), complex)
    history = []
    for iteration in range(1, max_iterations + 1):
        # Periodic strain from the current displacement: eps_ij = (u_i,j + u_j,i)/2, g12 = u1,2 + u2,1.
        de11 = ifft(1j*kx*u1_hat)
        de22 = ifft(1j*ky*u2_hat)
        dg12 = ifft(1j*(ky*u1_hat + kx*u2_hat))

        if control == 'stress':
            # <sigma> = <C>:E + <C:delta_eps> - <C:eps0> = sigma_applied, solved for E.
            b11 = sa11 + t011 - (c11*de11 + c12*de22).mean()
            b22 = sa22 + t022 - (c12*de11 + c11*de22).mean()
            b12 = sa12 + t012 - (c44*dg12).mean()
            E11 = (a11*b11 - a12*b22)/(a11**2 - a12**2)
            E22 = (a11*b22 - a12*b11)/(a11**2 - a12**2)
            G12 = b12/a44

        # Right-hand side as in my older codes: ts = C:(eps0 - E) - Cinhom:delta_eps.
        ts11 = c11*(e011 - E11) + c12*(e022 - E22) - (d11*de11 + d12*de22)
        ts22 = c12*(e011 - E11) + c11*(e022 - E22) - (d12*de11 + d11*de22)
        ts12 = c44*(g012 - G12) - d44*dg12

        # Solve the homogeneous problem for the new displacement: u_new = -i Omega . (k . ts).
        ts11_hat, ts22_hat, ts12_hat = fft(ts11), fft(ts22), fft(ts12)
        f1 = kx*ts11_hat + ky*ts12_hat
        f2 = kx*ts12_hat + ky*ts22_hat
        u1_new = -1j*(o11*f1 + o12*f2)
        u2_new = -1j*(o12*f1 + o22*f2)
        u1_new[zero_k] = u2_new[zero_k] = 0

        # Convergence: how much did the displacement change? (disperror in my older codes)
        change = np.sqrt(np.sum(np.abs(u1_new - u1_hat)**2 + np.abs(u2_new - u2_hat)**2))
        size = max(np.sqrt(np.sum(np.abs(u1_new)**2 + np.abs(u2_new)**2)), 1e-300)
        u1_hat, u2_hat = u1_new, u2_new
        history.append((iteration, change/size))
        if change/size < tolerance:
            break
        if not np.isfinite(change) or change/size > 1e12:
            raise RuntimeError('Displacement iteration diverged; choose a reference stiffness closer to the phases.')
    else:
        raise RuntimeError(f'Displacement did not converge in {max_iterations} iterations.')

    # Fields at the converged displacement.
    de11 = ifft(1j*kx*u1_hat)
    de22 = ifft(1j*ky*u2_hat)
    dg12 = ifft(1j*(ky*u1_hat + kx*u2_hat))
    e11, e22, g12 = E11 + de11, E22 + de22, G12 + dg12

    # Elastic strain and stress, written out in components.
    el11, el22, elg = e11 - e011, e22 - e022, g12 - g012
    s11 = c11*el11 + c12*el22
    s22 = c12*el11 + c11*el22
    s12 = c44*elg

    # Check equilibrium: div(sigma)_i = i k_j sigma_ij should vanish.
    s11_hat, s22_hat, s12_hat = fft(s11), fft(s22), fft(s12)
    div1_hat = 1j*(kx*s11_hat + ky*s12_hat)
    div2_hat = 1j*(kx*s12_hat + ky*s22_hat)
    div1, div2 = ifft(div1_hat), ifft(div2_hat)
    stress_size = max(2*np.pi*np.sqrt(np.mean(s11**2 + s22**2 + 2*s12**2)), 1e-300)
    residual = np.sqrt(np.mean(div1**2 + div2**2))/stress_size

    # Elastic energy density; under stress control subtract the work of the applied load.
    energy = 0.5*np.mean(el11*s11 + el22*s22 + elg*s12)
    if control == 'stress':
        energy -= sa11*E11 + sa22*E22 + sa12*G12
    return dict(displacement=(ifft(u1_hat), ifft(u2_hat)), strain=(e11, e22, g12),
                stress=(s11, s22, s12), mean_strain=(E11, E22, G12), energy=energy,
                residual=residual, history=np.array(history))

Step 12: Run it and check the answer

n = 64
contrast = 0.5
angle = 30.0
misfit_ratio = -0.5
stiffness, eigenstrain, reference, h = plate_fields(n, contrast, angle, misfit_ratio)
clamped = solve_direct_voigt(stiffness, eigenstrain, reference)
free = solve_direct_voigt(stiffness, eigenstrain, reference, control='stress')
print(f"Clamped (E = 0):     energy = {clamped['energy']:.9g} eV/nm^3, iterations = {len(clamped['history'])}, equilibrium residual = {clamped['residual']:.1e}")
print(f"Stress-free (<s>=0): energy = {free['energy']:.9g} eV/nm^3, E = {tuple(round(float(x), 7) for x in free['mean_strain'])}")
print('Average stress, stress-free cell (MPa):', [f'{s.mean()/MPA:.1e}' for s in free['stress']])
print('Peak stress in the plate (MPa):', f"{clamped['stress'][0].min()/MPA:.1f} clamped, {free['stress'][0].min()/MPA:.1f} stress-free")
Names in this code

Arrows show what feeds what. Dashed blue boxes are what we give the code, green boxes are what it returns, and the orange box is the stopping rule. Every name, including the ones left out of the chart, is in the table.

Every name, in a table
In the codeSymbolMeaning
stiffness, eigenstrain, reference, hThe four results of plate_fields.
clampedResult with \(\bar E=0\) (strain control).
freeResult with \(\langle\sigma\rangle=0\) (stress control, no applied load).
result['strain']Three strain fields: 11, 22 and the engineering shear 12.
result['stress']Three stress fields: 11, 22 and 12.
result['mean_strain']\(\bar E\)Homogeneous strain at convergence.
result['energy']\(F/V\)Final elastic energy density.
result['displacement']\(u_1,\ u_2\)The converged displacement field: the quantity the solver iterates on.
result['residual'], result['history']Equilibrium check, and the record of displacement changes.

With the default plate (64 × 64 grid, stiffness ratio 0.5, \(\epsilon^0_{11}=0.003\) and \(\epsilon^0_{22}=-0.0015\), i.e. tetragonality \(t=-0.5\), plate at 30°):

Request the complete notebook

Reference: Saswata Bhattacharyya, Tae Wook Heo, Kunok Chang and Long-Qing Chen, A Spectral Iterative Method for the Computation of Effective Properties of Elastically Inhomogeneous Polycrystals, Communications in Computational Physics 11 (2012), 726–738. The iterative perturbation method was implemented in our group by Tushar Jogi.

← Back to the two inhomogeneous elasticity solvers · Elastic Stress Effects Laboratory