text: # -*- coding: utf-8 -*-
  """
    run_screening.py generated by WhatsOpt 1.10.4
  """
  # DO NOT EDIT unless you know what you are doing
  # analysis_id: 942
  
  import sys
  import numpy as np
  import matplotlib.pyplot as plt
  
  from openmdao.api import Problem, SqliteRecorder, CaseReader
  from openmdao_extensions.salib_doe_driver import SalibDOEDriver
  
  from SALib.analyze import morris
  from SALib.analyze import sobol
  from SALib.plotting import morris as mp
  from SALib.plotting.bar import plot as barplot
  from nested_sellar import NestedSellar 
  
  
  from optparse import OptionParser
  parser = OptionParser()
  parser.add_option("-b", "--batch",
                    action="store_true", dest="batch", default=False,
                    help="do not plot anything")
  parser.add_option("-s", "--sobol",
                    action="store_true", dest="sobol", default=False,
                    help="do not plot anything")
  parser.add_option("-p", "--parallel", 
                    action="store_true", default=False,
                    help="run doe in parallel")
  (options, args) = parser.parse_args()
  
  pb = Problem(NestedSellar())
  sa_method_name='Morris'
  sa_doe_options={'n_trajs': 10, 'n_levels': 4}
  if options.sobol:
      sa_method_name='Sobol'
      sa_doe_options={'n_samples': 500, 'calc_second_order': False}
  
  pb.driver = SalibDOEDriver(sa_method_name=sa_method_name, sa_doe_options=sa_doe_options)
  pb.driver.options['run_parallel'] = options.parallel
  
  case_recorder_filename = 'nested_sellar_screening.sqlite'        
  recorder = SqliteRecorder(case_recorder_filename)
  pb.driver.add_recorder(recorder)
  
  pb.model.nonlinear_solver.options['err_on_non_converge'] = True
  
  
  pb.model.add_design_var('x', lower=0, upper=10)
  pb.model.add_design_var('z', lower=0, upper=10)
  
  
  pb.model.add_objective('f')
  
  
  pb.model.add_constraint('g1', upper=0.)
  pb.model.add_constraint('g2', upper=0.)
  
  
  
  pb.setup()  
  pb.run_driver()        
  
  
  if options.batch  or options.parallel:
      exit(0)
  reader = CaseReader(case_recorder_filename)
  cases = reader.list_cases('driver', recurse=False)
  n = len(cases)
  data = {'inputs': {}, 'outputs': {} }
  data['inputs']['x'] = np.zeros((n,)+(1,))
  data['inputs']['z'] = np.zeros((n,)+(2,))
  data['outputs']['f'] = np.zeros((n,)+(1,))
  data['outputs']['g1'] = np.zeros((n,)+(1,))
  data['outputs']['g2'] = np.zeros((n,)+(1,))
  data['outputs']['y1'] = np.zeros((n,)+(1,))
  data['outputs']['y2'] = np.zeros((n,)+(1,))
  
  for i in range(len(cases)):
      case = reader.get_case(cases[i])
      data['inputs']['x'][i,:] = case.outputs['x']
      data['inputs']['z'][i,:] = case.outputs['z']
      data['outputs']['f'][i,:] = case.outputs['f']
      data['outputs']['g1'][i,:] = case.outputs['g1']
      data['outputs']['g2'][i,:] = case.outputs['g2']
      data['outputs']['y1'][i,:] = case.outputs['y1']
      data['outputs']['y2'][i,:] = case.outputs['y2']
  
  salib_pb = pb.driver.get_salib_problem()
  inputs = pb.driver.get_cases()
  
  print('')
  print('*** Output: f')
  output = data['outputs']['f'].reshape((-1,))
  
  if options.sobol:  
      Si = sobol.analyze(salib_pb, output, 
                         calc_second_order=sa_doe_options['calc_second_order'], 
                         print_to_console=True)
      Si_df = Si.to_df()
      fig, axes = plt.subplots(1, len(Si_df))
      for idx, f in enumerate(Si_df):
          axes[idx] = barplot(f, ax=axes[idx])
      fig.suptitle('f '+'sensitivity')
  
  else:
      Si = morris.analyze(salib_pb, inputs, output, print_to_console=True)
  
      fig, (ax1, ax2) = plt.subplots(1,2)
      fig.suptitle('f '+'sensitivity')
  
      mp.horizontal_bar_plot(ax1, Si, {})
      mp.covariance_plot(ax2, Si, {})
  
  print('')
  print('*** Output: g1')
  output = data['outputs']['g1'].reshape((-1,))
  
  if options.sobol:  
      Si = sobol.analyze(salib_pb, output, 
                         calc_second_order=sa_doe_options['calc_second_order'], 
                         print_to_console=True)
      Si_df = Si.to_df()
      fig, axes = plt.subplots(1, len(Si_df))
      for idx, f in enumerate(Si_df):
          axes[idx] = barplot(f, ax=axes[idx])
      fig.suptitle('g1 '+'sensitivity')
  
  else:
      Si = morris.analyze(salib_pb, inputs, output, print_to_console=True)
  
      fig, (ax1, ax2) = plt.subplots(1,2)
      fig.suptitle('g1 '+'sensitivity')
  
      mp.horizontal_bar_plot(ax1, Si, {})
      mp.covariance_plot(ax2, Si, {})
  
  print('')
  print('*** Output: g2')
  output = data['outputs']['g2'].reshape((-1,))
  
  if options.sobol:  
      Si = sobol.analyze(salib_pb, output, 
                         calc_second_order=sa_doe_options['calc_second_order'], 
                         print_to_console=True)
      Si_df = Si.to_df()
      fig, axes = plt.subplots(1, len(Si_df))
      for idx, f in enumerate(Si_df):
          axes[idx] = barplot(f, ax=axes[idx])
      fig.suptitle('g2 '+'sensitivity')
  
  else:
      Si = morris.analyze(salib_pb, inputs, output, print_to_console=True)
  
      fig, (ax1, ax2) = plt.subplots(1,2)
      fig.suptitle('g2 '+'sensitivity')
  
      mp.horizontal_bar_plot(ax1, Si, {})
      mp.covariance_plot(ax2, Si, {})
  
  print('')
  print('*** Output: y1')
  output = data['outputs']['y1'].reshape((-1,))
  
  if options.sobol:  
      Si = sobol.analyze(salib_pb, output, 
                         calc_second_order=sa_doe_options['calc_second_order'], 
                         print_to_console=True)
      Si_df = Si.to_df()
      fig, axes = plt.subplots(1, len(Si_df))
      for idx, f in enumerate(Si_df):
          axes[idx] = barplot(f, ax=axes[idx])
      fig.suptitle('y1 '+'sensitivity')
  
  else:
      Si = morris.analyze(salib_pb, inputs, output, print_to_console=True)
  
      fig, (ax1, ax2) = plt.subplots(1,2)
      fig.suptitle('y1 '+'sensitivity')
  
      mp.horizontal_bar_plot(ax1, Si, {})
      mp.covariance_plot(ax2, Si, {})
  
  print('')
  print('*** Output: y2')
  output = data['outputs']['y2'].reshape((-1,))
  
  if options.sobol:  
      Si = sobol.analyze(salib_pb, output, 
                         calc_second_order=sa_doe_options['calc_second_order'], 
                         print_to_console=True)
      Si_df = Si.to_df()
      fig, axes = plt.subplots(1, len(Si_df))
      for idx, f in enumerate(Si_df):
          axes[idx] = barplot(f, ax=axes[idx])
      fig.suptitle('y2 '+'sensitivity')
  
  else:
      Si = morris.analyze(salib_pb, inputs, output, print_to_console=True)
  
      fig, (ax1, ax2) = plt.subplots(1,2)
      fig.suptitle('y2 '+'sensitivity')
  
      mp.horizontal_bar_plot(ax1, Si, {})
      mp.covariance_plot(ax2, Si, {})
  
    
  plt.show()
  
  