def test_multiphase_FE_strain(): from pymks import MKSRegressionModel from pymks.datasets import make_elastic_FE_strain_delta from pymks.datasets import make_elastic_FE_strain_random from pymks.bases import DiscreteIndicatorBasis L = 21 i = 3 elastic_modulus = (80, 100, 120) poissons_ratio = (0.3, 0.3, 0.3) macro_strain = 0.02 size = (L, L) X_delta, strains_delta = \ make_elastic_FE_strain_delta(elastic_modulus=elastic_modulus, poissons_ratio=poissons_ratio, size=size, macro_strain=macro_strain) basis = DiscreteIndicatorBasis(len(elastic_modulus)) MKSmodel = MKSRegressionModel(basis) MKSmodel.fit(X_delta, strains_delta) np.random.seed(99) X, strain = make_elastic_FE_strain_random(n_samples=1, elastic_modulus=elastic_modulus, poissons_ratio=poissons_ratio, size=size, macro_strain=macro_strain) strain_pred = MKSmodel.predict(X) assert np.allclose(strain_pred[0, i:-i], strain[0, i:-i], rtol=1e-2, atol=6.1e-3)
def test_cahn_hilliard(): from pymks.datasets.cahn_hilliard_simulation import CahnHilliardSimulation from pymks.datasets import make_cahn_hilliard from sklearn import metrics from pymks import MKSRegressionModel from pymks import ContinuousIndicatorBasis mse = metrics.mean_squared_error n_samples = 100 n_spaces = 20 dt = 1e-3 np.random.seed(0) X, y = make_cahn_hilliard(n_samples=n_samples, size=(n_spaces, n_spaces), dt=dt) basis = ContinuousIndicatorBasis(10, [-1, 1]) model = MKSRegressionModel(basis) model.fit(X, y) X_test = np.array( [np.random.random((n_spaces, n_spaces)) for i in range(1)]) CHSim = CahnHilliardSimulation(dt=dt) CHSim.run(X_test) y_test = CHSim.response y_pred = model.predict(X_test) assert mse(y_test[0], y_pred[0]) < 0.03
def test_resize_pred(): from pymks import MKSRegressionModel from pymks.bases import DiscreteIndicatorBasis nx, ny = 21, 21 resize = 3 X_delta, y_delta = get_delta_data(nx, ny) X_test, y_test = get_random_data(nx, ny) X_big_test, y_big_test = get_random_data(resize * nx, resize * ny) basis = DiscreteIndicatorBasis(n_states=2) model = MKSRegressionModel(basis) model.fit(X_delta, y_delta) y_pred = model.predict(X_test) assert np.allclose(y_pred, y_test, rtol=1e-2, atol=6.1e-3) model.resize_coeff((resize * nx, resize * ny)) y_big_pred = model.predict(X_big_test) assert np.allclose(y_big_pred, y_big_test, rtol=1e-2, atol=6.1e-2)
def test_MKS_elastic_delta(): from pymks import MKSRegressionModel from pymks.bases import DiscreteIndicatorBasis nx, ny = 21, 21 X, y_test = get_delta_data(nx, ny) basis = DiscreteIndicatorBasis(n_states=2) model = MKSRegressionModel(basis) model.fit(X, y_test) y_pred = model.predict(X) assert np.allclose(y_pred, y_test, rtol=1e-3, atol=1e-3)
def test_cahn_hilliard(): from pymks.datasets.cahn_hilliard_simulation import CahnHilliardSimulation from pymks.datasets import make_cahn_hilliard from sklearn import metrics from pymks import MKSRegressionModel from pymks import ContinuousIndicatorBasis mse = metrics.mean_squared_error n_samples = 100 n_spaces = 20 dt = 1e-3 np.random.seed(0) X, y = make_cahn_hilliard(n_samples=n_samples, size=(n_spaces, n_spaces), dt=dt) basis = ContinuousIndicatorBasis(10, [-1, 1]) model = MKSRegressionModel(basis) model.fit(X, y) X_test = np.array([np.random.random((n_spaces, n_spaces)) for i in range(1)]) CHSim = CahnHilliardSimulation(dt=dt) CHSim.run(X_test) y_test = CHSim.response y_pred = model.predict(X_test) assert mse(y_test, y_pred) < 0.03
def 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. Args: 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 """ if not isinstance(grain_size[0], (list, tuple, np.ndarray)): grain_size = (grain_size, ) if not isinstance(n_samples, (list, tuple, np.ndarray)): n_samples = (n_samples, ) if volume_fraction is None: volume_fraction = (None, ) * len(n_samples) vf_0 = volume_fraction[0] if not isinstance(vf_0, (list, tuple, np.ndarray)) and vf_0 is not None: volume_fraction = (volume_fraction, ) np.random.seed(seed) if not isinstance(size, (list, tuple, np.ndarray)) or len(size) > 3: raise RuntimeError('size must have length of 2 or 3') [ RuntimeError('dimensions of size and grain_size are not the same.') for grains in grain_size if len(size) != len(grains) ] if vf_0 is not None: [ RuntimeError('dimensions of size and grain_size are not the same.') for volume_frac in volume_fraction if len(elastic_modulus) != len(volume_frac) ] if len(elastic_modulus) != len(poissons_ratio): raise RuntimeError('length of elastic_modulus and poissons_ratio are' \ 'not the same.') np.random.seed(seed) seed = np.random.randint(100, size=(len(volume_fraction), )) X_cal, y_cal = make_elastic_FE_strain_delta(elastic_modulus, poissons_ratio, size, macro_strain) n_states = len(elastic_modulus) basis = DiscreteIndicatorBasis(n_states) model = MKSRegressionModel(basis=basis) model.fit(X_cal, y_cal) X = np.concatenate([ make_microstructure(n_samples=sample, size=size, n_phases=n_states, grain_size=gs, seed=s, volume_fraction=vf, percent_variance=percent_variance) for vf, s, gs, sample in zip(volume_fraction, seed, grain_size, n_samples) ]) X_ = basis.discretize(X) index = tuple([None for i in range(len(size) + 1)]) + (slice(None), ) modulus = np.sum(X_ * np.array(elastic_modulus)[index], axis=-1) y_stress = model.predict(X) * modulus return X, np.average(y_stress.reshape(len(y_stress), -1), axis=1)
def 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. Args: 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 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 """ if not isinstance(grain_size[0], (list, tuple, np.ndarray)): grain_size = (grain_size,) if not isinstance(n_samples, (list, tuple, np.ndarray)): n_samples = (n_samples,) if volume_fraction is None: volume_fraction = (None,) * len(n_samples) vf_0 = volume_fraction[0] if not isinstance(vf_0, (list, tuple, np.ndarray)) and vf_0 is not None: volume_fraction = (volume_fraction,) if not isinstance(size, (list, tuple, np.ndarray)) or len(size) > 3: raise RuntimeError('size must have length of 2 or 3') [RuntimeError('dimensions of size and grain_size are not the same.') for grains in grain_size if len(size) != len(grains)] if vf_0 is not None: [RuntimeError('dimensions of size and grain_size are not the same.') for volume_frac in volume_fraction if len(elastic_modulus) != len(volume_frac)] if len(elastic_modulus) != len(poissons_ratio): raise RuntimeError('length of elastic_modulus and poissons_ratio are \ not the same.') X_cal, y_cal = make_elastic_FE_strain_delta(elastic_modulus, poissons_ratio, size, macro_strain) n_states = len(elastic_modulus) basis = DiscreteIndicatorBasis(n_states) model = MKSRegressionModel(basis=basis) model.fit(X_cal, y_cal) X = np.concatenate([make_microstructure(n_samples=sample, size=size, n_phases=n_states, grain_size=gs, seed=seed, volume_fraction=vf, percent_variance=percent_variance) for vf, gs, sample in zip(volume_fraction, grain_size, n_samples)]) X_ = basis.discretize(X) index = tuple([None for i in range(len(size) + 1)]) + (slice(None),) modulus = np.sum(X_ * np.array(elastic_modulus)[index], axis=-1) y_stress = model.predict(X) * modulus return X, np.average(y_stress.reshape(len(y_stress), -1), axis=1)
def 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. Args: 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 """ if not isinstance(grain_size[0], (list, tuple, np.ndarray)): grain_size = (grain_size,) if not isinstance(n_samples, (list, tuple, np.ndarray)): n_samples = (n_samples,) if not isinstance(size, (list, tuple, np.ndarray)) or len(size) > 3: raise RuntimeError('size must have length of 2 or 3') [RuntimeError('dimensions of size and grain_size are not the same.') for grains in grain_size if len(size) != len(grains)] if len(elastic_modulus) != len(poissons_ratio): raise RuntimeError('length of elastic_modulus and poissons_ratio are \ not the same.') X_cal, y_cal = make_elastic_FE_strain_delta(elastic_modulus, poissons_ratio, size, macro_strain) n_states = len(elastic_modulus) basis = DiscreteIndicatorBasis(n_states) model = MKSRegressionModel(basis=basis) model.fit(X_cal, y_cal) X = np.concatenate([make_microstructure(n_samples=sample, size=size, n_phases=n_states, grain_size=gs, seed=seed) for gs, sample in zip(grain_size, n_samples)]) X_ = basis.discretize(X) index = tuple([None for i in range(len(size) + 1)]) + (slice(None),) modulus = np.sum(X_ * np.array(elastic_modulus)[index], axis=-1) y_stress = model.predict(X) * modulus return X, np.average(y_stress.reshape(np.sum(n_samples), y_stress[0].size), axis=1)