From 1b08f90d6c8911e1b2f171ff0319c687b7ca820a Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Sun, 13 Feb 2022 23:54:16 -0500 Subject: [PATCH] allow constant (non-label) value in cm.from_table() keys --- python/damask/_configmaterial.py | 22 +++++++++++++++++++--- python/tests/test_ConfigMaterial.py | 12 ++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/python/damask/_configmaterial.py b/python/damask/_configmaterial.py index 01d85f6a4..250b52e7e 100644 --- a/python/damask/_configmaterial.py +++ b/python/damask/_configmaterial.py @@ -178,8 +178,9 @@ class ConfigMaterial(Config): table : damask.Table Table that contains material information. **kwargs - Keyword arguments where the key is the name and the value specifies - the label of the data column in the table. + Keyword arguments where the key is the property name and + the value specifies either the label of the data column in the table + or a constant value. Returns ------- @@ -211,8 +212,23 @@ class ConfigMaterial(Config): homogenization: {} phase: {} + >>> cm.from_table(t,O='qu',phase='phase',homogenization='SX') + material: + - constituents: + - O: [0.19, 0.8, 0.24, -0.51] + v: 1.0 + phase: Aluminum + homogenization: SX + - constituents: + - O: [0.8, 0.19, 0.24, -0.51] + v: 1.0 + phase: Steel + homogenization: SX + homogenization: {} + phase: {} + """ - kwargs_ = {k:table.get(v) for k,v in kwargs.items()} + kwargs_ = {k:table.get(v) if v in table.labels else np.atleast_2d([v]*len(table)).T for k,v in kwargs.items()} _,idx = np.unique(np.hstack(list(kwargs_.values())),return_index=True,axis=0) idx = np.sort(idx) diff --git a/python/tests/test_ConfigMaterial.py b/python/tests/test_ConfigMaterial.py index a713b636f..b8b5df52a 100644 --- a/python/tests/test_ConfigMaterial.py +++ b/python/tests/test_ConfigMaterial.py @@ -96,6 +96,18 @@ class TestConfigMaterial: for i,m in enumerate(c['material']): assert m['homogenization'] == 1 and (m['constituents'][0]['O'] == [1,0,1,1]).all() + def test_from_table_with_constant(self): + N = np.random.randint(3,10) + a = np.vstack((np.hstack((np.arange(N),np.arange(N)[::-1])), + np.ones(N*2),np.zeros(N*2),np.ones(N*2),np.ones(N*2), + np.ones(N*2), + )).T + t = Table(a,{'varying':1,'constant':4,'ones':1}) + c = ConfigMaterial.from_table(t,**{'phase':'varying','O':'constant','homogenization':1}) + assert len(c['material']) == N + for i,m in enumerate(c['material']): + assert m['homogenization'] == 1 and (m['constituents'][0]['O'] == [1,0,1,1]).all() + @pytest.mark.parametrize('N,n,kw',[ (1,1,{'phase':'Gold', 'O':[1,0,0,0],