Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. def doctor(self):
  2. print "-- OpenMDAO Doctor --"
  3. print 30*"-"
  4. uncon_inputs = self.get_unconnected_inputs()
  5. n_uncon = len(uncon_inputs)
  6. if n_uncon > 0:
  7. print "- Assembly contains %i unconnected inputs:" % n_uncon
  8. for name in uncon_inputs:
  9. print " " + name
  10.  
  11. n_caserecorders = len(self.recorders)
  12. if n_caserecorders == 0:
  13. print "- No case recorders have been set"
  14.  
  15. name = self.__class__.__name__
  16. with open("%s_depgraph.html" % name, "wb") as f:
  17. f.write(self._repr_svg_())
  18.  
  19. from openmdao.util.dotgraph import plot_system_tree
  20.  
  21. #plot_system_tree(self._system)
  22.  
  23. def get_unconnected_inputs(self):
  24. unconnected_inputs = []
  25. connected_inputs = [i[1] for i in self.list_connections()]
  26. defaults = ['itername', 'force_execute', 'directory', 'exec_count',
  27. 'derivative_exec_count', 'fixed_external_vars',
  28. 'missing_deriv_policy', 'force_fd']
  29. for compname in self.list_components() + ['self']:
  30. if compname in ["self", "driver"]:
  31. continue
  32. comp = self.get(compname)
  33. for var in comp.list_inputs():
  34. if var in defaults:
  35. continue
  36. fullname = '.'.join([compname, var])
  37. if fullname not in connected_inputs:
  38. unconnected_inputs.append(fullname)
  39. return unconnected_inputs
  40.  
  41. def create(self, inst):
  42. name = inst.__class__.__name__
  43. self.add(name, inst)
  44. self.driver.workflow.add(name)
  45.  
  46. def auto_connect(self, print_only=False):
  47. """
  48. Collects the names of all input and output variables for all
  49. components within the assembly (drivers excluded).
  50. Then establishes connections between
  51. any output variable and input variable that has the same name so
  52. long as the variable name does not exist as an output to more than
  53. a single component (so excludes default outputs).
  54. """
  55.  
  56. inputs, outputs = {}, {}
  57.  
  58. # Gather all inputs and output from the components. Ignore all
  59. # framework vars.
  60. for compname in self.list_components():
  61. comp = self.get(compname)
  62.  
  63. comp_inputs = [inp for inp in comp.list_inputs() if \
  64. comp._trait_metadata[inp].get('framework_var') != True]
  65.  
  66. for input_name in comp_inputs:
  67. if input_name not in inputs:
  68. inputs[input_name] = [compname]
  69. else:
  70. inputs[input_name].append(compname)
  71.  
  72. comp_outputs = [inp for inp in comp.list_outputs() if \
  73. comp._trait_metadata[inp].get('framework_var') != True]
  74.  
  75. for output_name in comp_outputs:
  76. if output_name not in outputs:
  77. outputs[output_name] = [compname]
  78. else:
  79. outputs[output_name].append(compname)
  80.  
  81. # Automatically connect assembly boundary inputs too.
  82. assym_level = self.list_inputs()
  83. assym_level.remove('directory')
  84. for var in assym_level:
  85. if var in outputs:
  86. outputs[var].append('')
  87. else:
  88. outputs[var] = ['']
  89.  
  90. assym_level = self.list_outputs()
  91. assym_level.remove('derivative_exec_count')
  92. assym_level.remove('exec_count')
  93. assym_level.remove('itername')
  94. for var in assym_level:
  95. if var in inputs:
  96. inputs[var].append('')
  97. else:
  98. inputs[var] = ['']
  99.  
  100. # Do the connections
  101. if print_only:
  102. print(30*"-" + "\nConnections:\n" + 30*"-")
  103. connections = []
  104. for varname in outputs.keys():
  105. comps = outputs[varname]
  106. #if len(comps) > 1:
  107. # continue
  108. if comps[0]:
  109. frompath = '.'.join([comps[0], varname])
  110. else:
  111. frompath = varname
  112.  
  113. if varname in inputs:
  114. for compname in inputs[varname]:
  115. if compname == "":
  116. topath = varname
  117. else:
  118. topath = '.'.join([compname, varname])
  119. if print_only:
  120. connections.append("connect(%s, %s)" % (frompath, topath))
  121. else:
  122. self.connect(frompath, topath)
  123.  
  124. if print_only:
  125.  
  126. connections.sort()
  127. comp = connections[0].split(",")[0]
  128. for con in connections:
  129. this_comp = con.split(",")[0]
  130. if this_comp != comp:
  131. print
  132. print "self." + con
  133. comp = this_comp
  134.  
  135. print(30*"-")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement