def test_mask_two_samples(): from pymks import DiscreteIndicatorBasis from pymks.stats import correlate from pymks.datasets import make_microstructure X = make_microstructure(n_samples=2, n_phases=2, size=(3, 3), grain_size=(2, 2), seed=99) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) mask = np.ones(X.shape) mask[:, 0, 0] = 0.0 X_corr = correlate(X_, confidence_index=mask) X_result = np.array( [ [ [[1 / 3.0, 1 / 3.0, 1 / 3.0], [1 / 5.0, 1 / 5.0, 1 / 5.0], [1 / 4.0, 1 / 4.0, 0]], [[1 / 5.0, 1 / 5.0, 2 / 5.0], [1 / 2.0, 1 / 2.0, 0], [1 / 5.0, 1 / 5.0, 1 / 5.0]], [[1 / 4.0, 1 / 4.0, 1 / 2.0], [1 / 5.0, 1 / 5.0, 2 / 5.0], [1 / 3.0, 1 / 3.0, 0]], ], [ [[0.0, 0.0, 1 / 3.0], [2 / 5.0, 3 / 5.0, 0.0], [0.0, 0.0, 1 / 2.0]], [[0.0, 0.0, 2 / 5.0], [3 / 8.0, 5 / 8.0, 0], [0.0, 0.0, 3 / 5.0]], [[0.0, 0.0, 1 / 2.0], [2 / 5.0, 3 / 5.0, 0.0], [0.0, 0.0, 2 / 3.0]], ], ] ) print np.round(X_corr, decimals=4) print X_result assert np.allclose(X_corr, X_result)
def test_mask_two_samples(): from pymks import DiscreteIndicatorBasis from pymks.stats import correlate from pymks.datasets import make_microstructure X = make_microstructure(n_samples=2, n_phases=2, size=(3, 3), grain_size=(2, 2), seed=99) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) mask = np.ones(X.shape) mask[:, 0, 0] = 0. X_corr = correlate(X_, confidence_index=mask) X_result = np.array([[[[1 / 3., 1 / 3., 1 / 3.], [1 / 5., 1 / 5., 1 / 5.], [1 / 4., 1 / 4., 0]], [[1 / 5., 1 / 5., 2 / 5.], [1 / 2., 1 / 2., 0], [1 / 5., 1 / 5., 1 / 5.]], [[1 / 4., 1 / 4., 1 / 2.], [1 / 5., 1 / 5., 2 / 5.], [1 / 3., 1 / 3., 0]]], [[[0., 0., 1 / 3.], [2 / 5., 3 / 5., 0.], [0., 0., 1 / 2.]], [[0., 0., 2 / 5.], [3 / 8., 5 / 8., 0], [0., 0., 3 / 5.]], [[0., 0., 1 / 2.], [2 / 5., 3 / 5., 0.], [0., 0., 2 / 3.]]]]) print np.round(X_corr, decimals=4) print X_result assert np.allclose(X_corr, X_result)
def test_periodic_crosscorrelation(): """ test nonperiodic crosscorrelation """ from pymks import DiscreteIndicatorBasis from pymks.stats import crosscorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_cross = crosscorrelate(X_, periodic_axes=(0, 1)) X_result = np.array( [ [ [0.3, 0.3, 0.3, 0.3], [0.2, 0.2, 0.15, 0.2], [0.1, 0.1, 0.0, 0.1], [0.2, 0.2, 0.15, 0.2], [0.3, 0.3, 0.3, 0.3], ] ] ) assert np.allclose(X_result, X_cross[..., 0])
def test_nonperiodic_autocorrelation(): """ test nonperiodic autocorrelation for spatial statistics """ from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_auto = autocorrelate(X_) X_result = np.array( [ [ [0, 0, 0, 0], [1.0 / 8, 1.0 / 12, 3.0 / 16, 1.0 / 12], [0.2, 2.0 / 15, 0.3, 2.0 / 15], [1.0 / 8, 1.0 / 12, 3.0 / 16, 1.0 / 12], [0, 0, 0, 0], ] ] ) assert np.allclose(X_result, X_auto[..., 1])
def test_mixperdic_mask(): from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate from pymks.datasets import make_checkerboard_microstructure X = make_checkerboard_microstructure(1, 3) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) mask = np.ones((X.shape)) mask[0, 0, 0] = 0 X_auto_mixperiodic_mask = autocorrelate(X_, periodic_axes=[0], confidence_index=mask) X_result_0 = np.array([[[1 / 5.0, 1 / 7.0, 2 / 5.0], [0, 0.5, 0], [2 / 5.0, 1 / 7.0, 1 / 5.0]]]) X_result_1 = np.array([[[2 / 5.0, 1 / 7.0, 2 / 5.0], [0, 0.5, 0.0], [2 / 5.0, 1 / 7.0, 2 / 5.0]]]) X_result = np.concatenate((X_result_0[..., None], X_result_1[..., None]), axis=-1) assert np.allclose(X_auto_mixperiodic_mask, np.concatenate(X_result))
def test_n_components_change(): from pymks import MKSStructureAnalysis from pymks import DiscreteIndicatorBasis dbasis = DiscreteIndicatorBasis(n_states=2) model = MKSStructureAnalysis(basis=dbasis) model.n_components = 27 assert model.n_components == 27
def test_n_componets_from_reducer(): from pymks import MKSHomogenizationModel, DiscreteIndicatorBasis from sklearn.manifold import LocallyLinearEmbedding reducer = LocallyLinearEmbedding(n_components=7) dbasis = DiscreteIndicatorBasis(n_states=3, domain=[0, 2]) model = MKSHomogenizationModel(dimension_reducer=reducer, basis=dbasis) assert model.n_components == 7
def test_stress(): from pymks.datasets import make_elastic_stress_random from pymks import MKSHomogenizationModel, DiscreteIndicatorBasis sample_size = 200 grain_size = [(5, 5), (6, 4), (4, 6), (2, 2)] n_samples = [sample_size] * len(grain_size) elastic_modulus = (410, 200) poissons_ratio = (0.28, 0.3) macro_strain = 0.001 size = (21, 21) X, y = make_elastic_stress_random(n_samples=n_samples, size=size, grain_size=grain_size, elastic_modulus=elastic_modulus, poissons_ratio=poissons_ratio, macro_strain=macro_strain, seed=0) dbasis = DiscreteIndicatorBasis(n_states=2, domain=[0, 1]) model = MKSHomogenizationModel(basis=dbasis, n_components=3, degree=3) model.fit(X, y) test_sample_size = 1 n_samples = [test_sample_size] * len(grain_size) X_new, y_new = make_elastic_stress_random(n_samples=n_samples, size=size, grain_size=grain_size, elastic_modulus=elastic_modulus, poissons_ratio=poissons_ratio, macro_strain=macro_strain, seed=3) y_result = model.predict(X_new) assert np.allclose(np.round(y_new, decimals=2), np.round(y_result, decimals=2))
def test_periodic_crosscorrelation(): ''' test nonperiodic crosscorrelation ''' from pymks import DiscreteIndicatorBasis from pymks.stats import crosscorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_cross = crosscorrelate(X_, periodic_axes=(0, 1)) X_result = np.array([[[0.3, 0.3, 0.3, 0.3], [0.2, 0.2, 0.15, 0.2], [0.1, 0.1, 0., 0.1], [0.2, 0.2, 0.15, 0.2], [0.3, 0.3, 0.3, 0.3]]]) assert (np.allclose(X_result, X_cross[..., 0]))
def test_periodic_autocorrelation(): """ test periodic autocorrelation for spatial statistics """ from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_auto = autocorrelate(X_, periodic_axes=(0, 1)) X_result = np.array( [[[0, 0, 0, 0], [0.1, 0.1, 0.15, 0.1], [0.2, 0.2, 0.3, 0.2], [0.1, 0.1, 0.15, 0.1], [0, 0, 0, 0]]] ) assert np.allclose(X_result, X_auto[..., 1])
def test_periodic_autocorrelation(): ''' test periodic autocorrelation for spatial statistics ''' from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_auto = autocorrelate(X_, periodic_axes=(0, 1)) X_result = np.array([[[0, 0, 0, 0], [0.1, 0.1, 0.15, 0.1], [0.2, 0.2, 0.3, 0.2], [0.1, 0.1, 0.15, 0.1], [0, 0, 0, 0]]]) assert (np.allclose(X_result, X_auto[..., 1]))
def test_nonperiodic_autocorrelation(): ''' test nonperiodic autocorrelation for spatial statistics ''' from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_auto = autocorrelate(X_) X_result = np.array([[[0, 0, 0, 0], [1. / 8, 1. / 12, 3. / 16, 1. / 12], [0.2, 2. / 15, 0.3, 2. / 15], [1. / 8, 1. / 12, 3. / 16, 1. / 12], [0, 0, 0, 0]]]) assert (np.allclose(X_result, X_auto[..., 1]))
def test_periodic_correlate(): ''' test corrleate for non-periodic microstructures ''' from pymks import DiscreteIndicatorBasis from pymks.stats import correlate X = np.array([[[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 1, 0]], [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 1, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_corr = correlate(X_, periodic_axes=(0, 1)) X_result = [[0.6, 0.6, 0.75, 0.6], [0.6, 0.6, 0.75, 0.6], [0.6, 0.6, 0.8, 0.6], [0.6, 0.6, 0.75, 0.6], [0.6, 0.6, 0.75, 0.6]] assert (np.allclose(X_result, X_corr[0, ..., 0]))
def test_nonperiodic_correlate(): ''' test corrleate for non-periodic microstructures ''' from pymks import DiscreteIndicatorBasis from pymks.stats import correlate X = np.array([[[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 1, 0]], [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 1, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_corr = correlate(X_) X_result = [[2 / 3., 4 / 9., 0.75, 4 / 9.], [5 / 8., 0.5, 0.75, 0.5], [0.6, 7 / 15., 0.8, 7 / 15.], [5 / 8., 0.5, 0.75, 0.5], [0.5, 4 / 9., 0.75, 4 / 9.]] assert (np.allclose(X_result, X_corr[0, ..., 0]))
def test_n_components_with_reducer(): from pymks import MKSHomogenizationModel, DiscreteIndicatorBasis from sklearn.manifold import Isomap reducer = Isomap(n_components=7) dbasis = DiscreteIndicatorBasis(n_states=3, domain=[0, 2]) model = MKSHomogenizationModel(dimension_reducer=reducer, basis=dbasis, n_components=9) assert model.n_components == 9
def test_nonperiodic_mask(): ''' test uncertainty masks for nonperiodic axes. ''' from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate from pymks.datasets import make_checkerboard_microstructure X = make_checkerboard_microstructure(1, 3) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) mask = np.ones((X.shape)) mask[0, 0, 0] = 0 X_auto_nonperiodic_mask = autocorrelate(X_, confidence_index=mask) X_result_0 = np.array([[[1 / 3., 0, 0.5], [0, 0.5, 0.], [0.5, 0, 1 / 3.]]]) X_result_1 = np.array([[[2 / 3., 0, 0.5], [0, 0.5, 0.], [0.5, 0, 2 / 3.]]]) X_result = np.concatenate((X_result_0[..., None], X_result_1[..., None]), axis=-1) assert np.allclose(X_auto_nonperiodic_mask, np.concatenate(X_result))
def test_nonperiodic_crosscorrelation(): ''' test nonperiodic crosscorrelation ''' from pymks import DiscreteIndicatorBasis from pymks.stats import crosscorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_cross = crosscorrelate(X_) X_result = np.array([[[ 1 / 3., 4 / 9., 0.5, 4 / 9., ], [1 / 8., 0.25, 3 / 16., 0.25], [0., 2 / 15., 0., 2 / 15.], [0., 1 / 12., 0, 1 / 12.], [0, 0, 0, 0]]]) assert (np.allclose(X_result, X_cross[..., 0]))
def test_periodic_correlate(): """ test corrleate for non-periodic microstructures """ from pymks import DiscreteIndicatorBasis from pymks.stats import correlate X = np.array( [ [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 1, 0]], [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 1, 0, 0]], ] ) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_corr = correlate(X_, periodic_axes=(0, 1)) X_result = [ [0.6, 0.6, 0.75, 0.6], [0.6, 0.6, 0.75, 0.6], [0.6, 0.6, 0.8, 0.6], [0.6, 0.6, 0.75, 0.6], [0.6, 0.6, 0.75, 0.6], ] assert np.allclose(X_result, X_corr[0, ..., 0])
def test_nonperiodic_crosscorrelation(): """ test nonperiodic crosscorrelation """ from pymks import DiscreteIndicatorBasis from pymks.stats import crosscorrelate X = np.array([[[1, 0, 1, 1], [1, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]) basis = DiscreteIndicatorBasis(n_states=2) X_ = basis.discretize(X) X_cross = crosscorrelate(X_) X_result = np.array( [ [ [1 / 3.0, 4 / 9.0, 0.5, 4 / 9.0], [1 / 8.0, 0.25, 3 / 16.0, 0.25], [0.0, 2 / 15.0, 0.0, 2 / 15.0], [0.0, 1 / 12.0, 0, 1 / 12.0], [0, 0, 0, 0], ] ] ) assert np.allclose(X_result, X_cross[..., 0])
def test_mixperdic_mask(): from pymks import DiscreteIndicatorBasis from pymks.stats import autocorrelate from pymks.datasets import make_checkerboard_microstructure X = make_checkerboard_microstructure(1, 3) basis = DiscreteIndicatorBasis(n_states=2) mask = np.ones((X.shape)) mask[0, 0, 0] = 0 X_auto_mixperiodic_mask = autocorrelate(X, basis, periodic_axes=[0], confidence_index=mask) X_result_0 = np.array([[[1 / 5., 1 / 7., 2 / 5.], [0, 0.5, 0], [2 / 5., 1 / 7., 1 / 5.]]]) X_result_1 = np.array([[[2 / 5., 1 / 7., 2 / 5.], [0, 0.5, 0.], [2 / 5., 1 / 7., 2 / 5.]]]) X_result = np.concatenate((X_result_0[..., None], X_result_1[..., None]), axis=-1) assert np.allclose(X_auto_mixperiodic_mask, np.concatenate(X_result))
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)
def test_default_n_components(): from pymks import MKSHomogenizationModel, DiscreteIndicatorBasis dbasis = DiscreteIndicatorBasis(n_states=2) model = MKSHomogenizationModel(basis=dbasis) assert model.n_components == 5
def test_degree_change(): from pymks import MKSHomogenizationModel, DiscreteIndicatorBasis dbasis = DiscreteIndicatorBasis(n_states=2) model = MKSHomogenizationModel(basis=dbasis) model.degree = 4 assert model.degree == 4
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, 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)