Skip to content

Instantly share code, notes, and snippets.

@bbrelje
Last active April 19, 2020 19:12
Show Gist options
  • Save bbrelje/01ed972b8ac0b4870a9983dc068c4fa8 to your computer and use it in GitHub Desktop.
Save bbrelje/01ed972b8ac0b4870a9983dc068c4fa8 to your computer and use it in GitHub Desktop.
Local transfer with out of bounds src_index
import openmdao.api as om
import mpi4py.MPI as MPI
SRC_INDICES = 1
class TestComp(om.ExplicitComponent):
def initialize(self):
self.options['distributed'] = False
def setup(self):
# src_indices = 1 will not raise an error, but reads a value of 1.0 on every proc
self.add_input('x', shape=1, src_indices=SRC_INDICES, val=-2038.0)
self.add_output('y', shape=1)
self.declare_partials('y', 'x')
def compute(self, inputs, outputs):
outputs['y'] = inputs['x'] ** 2
def compute_partials(self, inputs, J):
J['y', 'x'] = 2 * inputs[x]
prob = om.Problem()
model = prob.model
# nobody should ever do this but...
if MPI.COMM_WORLD.rank == 0:
setval = 2.0
else:
setval = -3.0
# no parallel or distributed comps, so default_vector is used (local xfer only)
model.add_subsystem('p1', om.IndepVarComp('x', setval))
model.add_subsystem('c3', TestComp())
model.connect("p1.x", "c3.x")
prob.setup(check=False, mode='fwd')
prob.run_model()
print('rank: ' + str(MPI.COMM_WORLD.rank) + ' val: ' + str(prob['c3.y']) + ' value on proc is: ' + str(setval ** 2))
# list_outputs only shows the value on the first proc (0th)
prob.model.list_outputs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment