Skip to content

Commit

Permalink
refactor: allow solve_fe docs without sfepy
Browse files Browse the repository at this point in the history
Allow solve_fe to display its documentation without having to import
sfepy.
  • Loading branch information
wd15 committed Mar 12, 2021
1 parent 09263b9 commit 5911d2c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
16 changes: 2 additions & 14 deletions pymks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,8 @@
from .fmks.correlations import TwoPointCorrelation
from .fmks.data.checkerboard import generate_checkerboard
from .fmks.pair_correlations import paircorr_from_twopoint
from .fmks.data import solve_fe

try:
import sfepy # noqa: F401
except ImportError:

def solve_fe(*_, **__):
"""Dummy funcion when sfepy unavailable
"""
# pylint: disable=redefined-outer-name, import-outside-toplevel, unused-import
import sfepy # noqa: F401, F811


else:
from .fmks.data.elastic_fe import solve_fe

# the following will be deprecated
from .mks_localization_model import MKSLocalizationModel
Expand All @@ -51,7 +39,7 @@ def solve_fe(*_, **__):
MKSRegressionModel = MKSLocalizationModel
DiscreteIndicatorBasis = PrimitiveBasis
ContinuousIndicatorBasis = PrimitiveBasis
# the above will be deprecatec
# the above will be deprecated


def test():
Expand Down
31 changes: 31 additions & 0 deletions pymks/fmks/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Functions for generating data
"""
from ..func import curry


@curry
def solve_fe(x_data, elastic_modulus, poissons_ratio, macro_strain=1.0, delta_x=1.0):
"""Solve the elasticity problem
Args:
x_data: microstructure with shape (n_samples, n_x, ...)
elastic_modulus: the elastic modulus in each phase
poissons_ration: the poissons ratio for each phase
macro_strain: the macro strain
delta_x: the grid spacing
Returns:
a dictionary of strain, displacement and stress with stress and
strain of shape (n_samples, n_x, ..., 3) and displacement shape
of (n_samples, n_x + 1, ..., 2)
"""
from .elastic_fe import _solve_fe # pylint: disable=import-outside-toplevel

return _solve_fe(
x_data,
elastic_modulus,
poissons_ratio,
macro_strain=macro_strain,
delta_x=delta_x,
)
24 changes: 4 additions & 20 deletions pymks/fmks/data/elastic_fe.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
>>> X = np.zeros((1, 3, 3), dtype=int)
>>> X[0, :, 1] = 1
>>> strain = solve_fe(
>>> strain = _solve_fe(
... X,
... elastic_modulus=(1.0, 10.0),
... poissons_ratio=(0., 0.),
Expand Down Expand Up @@ -43,7 +43,7 @@
... [1, 0, 0, 1]]])
>>> n_samples, N, N = X.shape
>>> macro_strain = 0.1
>>> displacement = solve_fe(
>>> displacement = _solve_fe(
... X,
... elastic_modulus=(10.0, 1.0),
... poissons_ratio=(0.3, 0.3),
Expand Down Expand Up @@ -80,7 +80,7 @@
>>> print(X.shape)
(2, 4, 4)
>>> x_data = da.from_array(X, chunks=(1, 4, 4))
>>> out = solve_fe(x_data,
>>> out = _solve_fe(x_data,
... elastic_modulus=(10.0, 1.0),
... poissons_ratio=(0.3, 0.3),
... macro_strain=macro_strain)['displacement']
Expand Down Expand Up @@ -145,23 +145,7 @@


@curry
def solve_fe(x_data, elastic_modulus, poissons_ratio, macro_strain=1.0, delta_x=1.0):
"""Solve the elasticity problem
Args:
x_data: microstructure with shape (n_samples, n_x, ...)
elastic_modulus: the elastic modulus in each phase
poissons_ration: the poissons ratio for each phase
macro_strain: the macro strain
delta_x: the grid spacing
Returns:
a dictionary of strain, displacement and stress with stress and
strain of shape (n_samples, n_x, ..., 3) and displacement shape
of (n_samples, n_x + 1, ..., 2)
"""

def _solve_fe(x_data, elastic_modulus, poissons_ratio, macro_strain=1.0, delta_x=1.0):
def solve_one_sample(property_array):
return pipe(
get_fields(property_array.shape[:-1], delta_x),
Expand Down
2 changes: 1 addition & 1 deletion pymks/fmks/tests/test_fe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import numpy as np
from toolz.curried import pipe, first, get
from pymks.fmks.data.elastic_fe import solve_fe
from pymks.fmks.data import solve_fe


def test_3d():
Expand Down
2 changes: 1 addition & 1 deletion pymks/fmks/tests/test_localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pymks.fmks.bases.primitive import PrimitiveTransformer
from pymks.fmks.localization import LocalizationRegressor
from pymks.fmks.data.delta import generate_delta
from pymks.fmks.data.elastic_fe import solve_fe
from pymks.fmks.data import solve_fe


def _get_x():
Expand Down

0 comments on commit 5911d2c

Please sign in to comment.