def test_distribcomp_list_feature(self): size = 15 model = om.Group() model.add_subsystem("indep", om.IndepVarComp('x', np.zeros(size))) model.add_subsystem("C2", DistribComp(size=size)) model.add_subsystem("C3", Summer(size=size)) model.connect('indep.x', 'C2.invec') model.connect('C2.outvec', 'C3.invec', src_indices=om.slicer[:]) prob = om.Problem(model) prob.setup() # prior to model execution, the global shape of a distributed variable is not available # and only the local portion of the value is available model.C2.list_inputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True) model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True) prob['indep.x'] = np.ones(size) prob.run_model() # after model execution, the global shape of a distributed variable is available # and the complete global value is available model.C2.list_inputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True) model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True) # note that the shape of the input variable for the non-distributed Summer component # is different on each processor, use the all_procs argument to display on all processors model.C3.list_inputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, all_procs=True) assert_near_equal(prob['C3.sum'], -5.)
def test_distribcomp_feature(self): size = 15 model = om.Group() # Distributed component "C2" requires an IndepVarComp to supply inputs. model.add_subsystem("indep", om.IndepVarComp('x', np.zeros(size))) model.add_subsystem("C2", DistribComp(size=size)) model.add_subsystem("C3", Summer(size=size)) model.connect('indep.x', 'C2.invec') # to copy the full distributed output C2.outvec into C3.invec on all procs, we need # to specify src_indices=om.slicer[:] model.connect('C2.outvec', 'C3.invec', src_indices=om.slicer[:]) prob = om.Problem(model) prob.setup() prob.set_val('indep.x', np.ones(size)) prob.run_model() assert_near_equal(prob.get_val('C2.invec'), np.ones((8,)) if model.comm.rank == 0 else np.ones((7,))) assert_near_equal(prob.get_val('C2.outvec'), 2*np.ones((8,)) if model.comm.rank == 0 else -3*np.ones((7,))) assert_near_equal(prob.get_val('C3.sum'), -5.)
def test_distribcomp_feature(self): import numpy as np import openmdao.api as om from openmdao.test_suite.components.distributed_components import DistribComp, Summer size = 15 model = om.Group() model.add_subsystem("indep", om.IndepVarComp('x', np.zeros(size))) model.add_subsystem("C2", DistribComp(size=size)) model.add_subsystem("C3", Summer(size=size)) model.connect('indep.x', 'C2.invec') model.connect('C2.outvec', 'C3.invec') prob = om.Problem(model) prob.setup() prob['indep.x'] = np.ones(size) prob.run_model() assert_near_equal(prob['C2.invec'], np.ones((8,)) if model.comm.rank == 0 else np.ones((7,))) assert_near_equal(prob['C2.outvec'], 2*np.ones((8,)) if model.comm.rank == 0 else -3*np.ones((7,))) assert_near_equal(prob['C3.sum'], -5.)
def test_distributed_array_list_vars(self): size = 100 # how many items in the array prob = om.Problem() prob.model.add_subsystem('des_vars', om.IndepVarComp('x', np.ones(size)), promotes=['x']) prob.model.add_subsystem('plus', DistributedAdder(size=size), promotes=['x', 'y']) prob.model.add_subsystem('summer', Summer(size=size), promotes_outputs=['sum']) prob.model.promotes('summer', inputs=[('invec', 'y')], src_indices=om.slicer[:]) prob.setup(force_alloc_complex=True) # force complex array storage to detect mpi bug prob['x'] = np.arange(size) prob.run_driver() stream = StringIO() with multi_proc_exception_check(prob.comm): inputs = sorted(prob.model.list_inputs(values=True, print_arrays=True, out_stream=stream)) if prob.comm.rank: self.assertEqual(inputs, []) else: self.assertEqual(inputs[0][0], 'plus.x') self.assertEqual(inputs[1][0], 'summer.invec') self.assertEqual(inputs[0][1]['val'].size, 100) self.assertEqual(inputs[1][1]['val'].size, 100) text = stream.getvalue() if prob.comm.rank: # Only rank 0 prints self.assertEqual(len(text), 0) else: self.assertEqual(text.count('val'), 3) self.assertEqual(text.count('\nplus'), 1) self.assertEqual(text.count('\n x'), 1) self.assertEqual(text.count('\nsummer'), 1) self.assertEqual(text.count('\n invec'), 1) # make sure all the arrays written have 100 elements in them self.assertEqual(len(text.split('[')[1].split(']')[0].split()), 100) self.assertEqual(len(text.split('[')[2].split(']')[0].split()), 100) stream = StringIO() with multi_proc_exception_check(prob.comm): outputs = sorted(prob.model.list_outputs(values=True, units=True, shape=True, bounds=True, residuals=True, scaling=True, hierarchical=True, print_arrays=True, out_stream=stream)) if prob.comm.rank: self.assertEqual(outputs, []) else: self.assertEqual(outputs[0][0], 'des_vars.x') self.assertEqual(outputs[1][0], 'plus.y') self.assertEqual(outputs[2][0], 'summer.sum') self.assertEqual(outputs[0][1]['val'].size, 100) self.assertEqual(outputs[1][1]['val'].size, 100) self.assertEqual(outputs[2][1]['val'].size, 1) text = stream.getvalue() if prob.comm.rank: # Only rank 0 prints self.assertEqual(len(text), 0) else: self.assertEqual(text.count('val'), 3) self.assertEqual(text.count('\ndes_vars'), 1) self.assertEqual(text.count('\n x'), 1) self.assertEqual(text.count('\nplus'), 1) self.assertEqual(text.count('\n y'), 1) self.assertEqual(text.count('\nsummer'), 1) self.assertEqual(text.count('\n sum'), 1) # make sure all the arrays written have 100 elements in them self.assertEqual(len(text.split('[')[1].split(']')[0].split()), 100) self.assertEqual(len(text.split('[')[2].split(']')[0].split()), 100) self.assertEqual(len(text.split('[')[3].split(']')[0].split()), 100) self.assertEqual(len(text.split('[')[4].split(']')[0].split()), 100)
def test_distribcomp_list_vars(self): print_opts = {'linewidth': 1024} if LooseVersion(np.__version__) >= LooseVersion("1.14"): print_opts['legacy'] = '1.13' size = 15 model = om.Group() model.add_subsystem("indep", om.IndepVarComp('x', np.zeros(size))) model.add_subsystem("C2", DistribComp(size=size)) model.add_subsystem("C3", Summer(size=size)) model.connect('indep.x', 'C2.invec') model.connect('C2.outvec', 'C3.invec', src_indices=om.slicer[:]) prob = om.Problem(model) prob.setup() # prior to model execution, the global shape of a distributed variable is not available # and only the local portion of the value is available stream = StringIO() with printoptions(**print_opts): model.C2.list_inputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "1 Input(s) in 'C2'", '', 'varname val shape global_shape', '------- --------------- ----- ------------', 'invec |3.87298334621| (8,) (15,)', ' val:', ' array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) stream = StringIO() with printoptions(**print_opts): model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "1 Explicit Output(s) in 'C2'", '', 'varname val shape global_shape', '------- --------------- ----- ------------', 'outvec |3.87298334621| (8,) (15,)', ' val:', ' array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) # run the model prob['indep.x'] = np.ones(size) prob.run_model() # after model execution, the global shape of a distributed variable is available # and the complete global value is available stream = StringIO() with printoptions(**print_opts): model.C2.list_inputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "1 Input(s) in 'C2'", '', 'varname val shape global_shape', '------- --------------- ----- ------------', 'invec |3.87298334621| (8,) (15,)', ' val:', ' array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) stream = StringIO() with printoptions(**print_opts): model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "1 Explicit Output(s) in 'C2'", '', 'varname val shape global_shape', '------- -------------- ----- ------------', 'outvec |9.74679434481| (8,) (15,)', ' val:', ' array([ 2., 2., 2., 2., 2., 2., 2., 2., -3., -3., -3., -3., -3., -3., -3.])' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) stream = StringIO() with printoptions(**print_opts): model.C3.list_inputs(hierarchical=False, shape=True, global_shape=True, all_procs=True, print_arrays=True, out_stream=stream) text = stream.getvalue().split('\n') print('\n'.join(text)) norm = '|9.74679434481|' shape = (15,) value = '[2., 2., 2., 2., 2., 2., 2., 2., -3., -3., -3., -3., -3., -3., -3.]' expected = [ "1 Input(s) in 'C3'", '', 'varname val shape global_shape', '------- ------------------- ----- ------------', 'invec {} {} {} '.format(norm, shape, shape), ' val:', ' array({})'.format(value), ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual(remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) assert_near_equal(prob['C3.sum'], -5.)
def test_distributed_array_list_vars(self): size = 100 # how many items in the array prob = om.Problem() prob.model.add_subsystem('des_vars', om.IndepVarComp('x', np.ones(size)), promotes=['x']) prob.model.add_subsystem('plus', DistributedAdder(size=size), promotes=['x', 'y']) prob.model.add_subsystem('summer', Summer(size=size), promotes=['y', 'sum']) prob.setup() prob['x'] = np.arange(size) prob.run_driver() stream = cStringIO() inputs = sorted( prob.model.list_inputs(values=True, print_arrays=True, out_stream=stream)) self.assertEqual(inputs[0][0], 'plus.x') self.assertEqual(inputs[1][0], 'summer.y') self.assertEqual(inputs[0][1]['value'].size, 50) # should only return half that is local self.assertEqual(inputs[1][1]['value'].size, 100) text = stream.getvalue() if prob.comm.rank: # Only rank 0 prints self.assertEqual(len(text), 0) else: self.assertEqual(text.count('value'), 3) self.assertEqual(text.count('\nmodel'), 1) self.assertEqual(text.count('\n plus'), 1) self.assertEqual(text.count('\n x'), 1) self.assertEqual(text.count('\n summer'), 1) self.assertEqual(text.count('\n y'), 1) # make sure all the arrays written have 100 elements in them self.assertEqual(len(text.split('[')[1].split(']')[0].split()), 100) self.assertEqual(len(text.split('[')[2].split(']')[0].split()), 100) stream = cStringIO() outputs = sorted( prob.model.list_outputs(values=True, units=True, shape=True, bounds=True, residuals=True, scaling=True, hierarchical=True, print_arrays=True, out_stream=stream)) self.assertEqual(outputs[0][0], 'des_vars.x') self.assertEqual(outputs[1][0], 'plus.y') self.assertEqual(outputs[2][0], 'summer.sum') self.assertEqual(outputs[0][1]['value'].size, 100) self.assertEqual(outputs[1][1]['value'].size, 50) self.assertEqual(outputs[2][1]['value'].size, 1) text = stream.getvalue() if prob.comm.rank: # Only rank 0 prints self.assertEqual(len(text), 0) else: self.assertEqual(text.count('value'), 3) self.assertEqual(text.count('\n des_vars'), 1) self.assertEqual(text.count('\n x'), 1) self.assertEqual(text.count('\n plus'), 1) self.assertEqual(text.count('\n y'), 1) self.assertEqual(text.count('\n summer'), 1) self.assertEqual(text.count('\n sum'), 1) # make sure all the arrays written have 100 elements in them self.assertEqual(len(text.split('[')[1].split(']')[0].split()), 100) self.assertEqual(len(text.split('[')[2].split(']')[0].split()), 100) self.assertEqual(len(text.split('[')[3].split(']')[0].split()), 100) self.assertEqual(len(text.split('[')[4].split(']')[0].split()), 100)
def test_distribcomp_list_vars(self): from openmdao.test_suite.components.distributed_components import DistribComp, Summer print_opts = {'linewidth': 1024} from distutils.version import LooseVersion if LooseVersion(np.__version__) >= LooseVersion("1.14"): print_opts['legacy'] = '1.13' size = 15 model = om.Group() model.add_subsystem("indep", om.IndepVarComp('x', np.zeros(size))) model.add_subsystem("C2", DistribComp(size=size)) model.add_subsystem("C3", Summer(size=size)) model.connect('indep.x', 'C2.invec') model.connect('C2.outvec', 'C3.invec') prob = om.Problem(model) prob.setup() # prior to model execution, the global shape of a distributed variable is not available # and only the local portion of the value is available stream = cStringIO() with printoptions(**print_opts): model.C2.list_inputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "1 Input(s) in 'C2'", '------------------', '', 'varname value shape global_shape', '------- --------------- ----- ------------', 'invec |2.82842712475| (8,) Unavailable ', ' value:', ' array([1., 1., 1., 1., 1., 1., 1., 1.])' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual( remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) stream = cStringIO() with printoptions(**print_opts): model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "1 Explicit Output(s) in 'C2'", '----------------------------', '', 'varname value shape global_shape', '------- --------------- ----- ------------', 'outvec |2.82842712475| (8,) Unavailable ', ' value:', ' array([1., 1., 1., 1., 1., 1., 1., 1.])' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual( remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) # run the model prob['indep.x'] = np.ones(size) prob.run_model() # after model execution, the global shape of a distributed variable is available # and the complete global value is available stream = cStringIO() with printoptions(**print_opts): model.C2.list_inputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "1 Input(s) in 'C2'", '------------------', '', 'varname value shape global_shape', '-------- --------------- ----- ------------', 'C2.invec |3.87298334621| (8,) (15,) ', ' value:', ' array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual( remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) stream = cStringIO() with printoptions(**print_opts): model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, out_stream=stream) if prob.comm.rank == 0: text = stream.getvalue().split('\n') expected = [ "1 Explicit Output(s) in 'C2'", '----------------------------', '', 'varname value shape global_shape', '--------- -------------- ----- ------------', 'C2.outvec |9.74679434481| (8,) (15,) ', ' value:', ' array([ 2., 2., 2., 2., 2., 2., 2., 2., -3., -3., -3., -3., -3., -3., -3.])' ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual( remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) # note that the shape of the input variable for the non-distributed Summer component # is different on each processor, use the all_procs argument to display on all processors stream = cStringIO() with printoptions(**print_opts): model.C3.list_inputs(hierarchical=False, shape=True, global_shape=True, all_procs=True, print_arrays=True, out_stream=stream) text = stream.getvalue().split('\n') if prob.comm.rank == 0: norm = '|5.65685424949|' shape = (8, ) value = '[2., 2., 2., 2., 2., 2., 2., 2.]' else: norm = '|7.93725393319|' shape = (7, ) value = '[-3., -3., -3., -3., -3., -3., -3.]' expected = [ "1 Input(s) in 'C3'", '------------------', '', 'varname value shape global_shape', '-------- ------------------- ----- ------------', 'C3.invec {} {} {} '.format(norm, shape, shape), ' value:', ' array({})'.format(value), ] for i, line in enumerate(expected): if line and not line.startswith('-'): self.assertEqual( remove_whitespace(text[i]), remove_whitespace(line), '\nExpected: %s\nReceived: %s\n' % (line, text[i])) assert_rel_error(self, prob['C3.out'], -5.)