<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from openmdao . main . api import Assembly , Component <EOL> from openmdao . lib . datatypes . api import Float <EOL> from openmdao . lib . drivers . api import CaseIteratorDriver <EOL> from openmdao . lib . components . api import MultiFiMetaModel <EOL> from openmdao . lib . surrogatemodels . api import MultiFiCoKrigingSurrogate , KrigingSurrogate <EOL> class Model ( Component ) : <EOL> x = Float ( <NUM_LIT:0> , iotype = "<STR_LIT>" ) <EOL> f_x = Float ( <NUM_LIT:0.0> , iotype = "<STR_LIT>" ) <EOL> def execute ( self ) : <EOL> x = self . x <EOL> self . f_x = ( ( <NUM_LIT:6> * x - <NUM_LIT:2> ) ** <NUM_LIT:2> ) * np . sin ( ( <NUM_LIT:6> * x - <NUM_LIT:2> ) * <NUM_LIT:2> ) <EOL> class LowFidelityModel ( Component ) : <EOL> x = Float ( <NUM_LIT:0.0> , iotype = "<STR_LIT>" ) <EOL> f_x = Float ( <NUM_LIT:0.0> , iotype = "<STR_LIT>" ) <EOL> def execute ( self ) : <EOL> x = self . x <EOL> self . f_x = <NUM_LIT:0.5> * ( ( <NUM_LIT:6> * x - <NUM_LIT:2> ) ** <NUM_LIT:2> ) * np . sin ( ( <NUM_LIT:6> * x - <NUM_LIT:2> ) * <NUM_LIT:2> ) + ( x - <NUM_LIT:0.5> ) * <NUM_LIT> - <NUM_LIT:5> <EOL> class HighFidelityModel ( Model ) : <EOL> pass <EOL> class CasesBuilder ( Assembly ) : <EOL> def __init__ ( self , model , cases ) : <EOL> self . instance = model <EOL> self . cases = cases <EOL> super ( CasesBuilder , self ) . __init__ ( ) <EOL> def configure ( self ) : <EOL> self . add ( "<STR_LIT>" , self . instance ) <EOL> self . add ( "<STR_LIT>" , CaseIteratorDriver ( ) ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> self . driver . add_parameter ( "<STR_LIT>" , low = <NUM_LIT:0> , high = <NUM_LIT:1> ) <EOL> self . driver . add_response ( "<STR_LIT>" ) <EOL> self . driver . case_inputs . model . x = self . cases <EOL> self . create_passthrough ( '<STR_LIT>' ) <EOL> self . create_passthrough ( '<STR_LIT>' ) <EOL> class Simulation ( Assembly ) : <EOL> def __init__ ( self , surrogate , nfi = <NUM_LIT:1> ) : <EOL> self . surrogate = surrogate <EOL> self . nfi = nfi <EOL> super ( Simulation , self ) . __init__ ( ) <EOL> def configure ( self ) : <EOL> doe_e = [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.0> ] <EOL> doe_c = [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] + doe_e <EOL> self . add ( '<STR_LIT>' , CasesBuilder ( HighFidelityModel ( ) , doe_e ) ) <EOL> self . add ( '<STR_LIT>' , CasesBuilder ( LowFidelityModel ( ) , doe_c ) ) <EOL> self . add ( "<STR_LIT>" , MultiFiMetaModel ( params = ( '<STR_LIT:x>' , ) , <EOL> responses = ( '<STR_LIT>' , ) , nfi = self . nfi ) ) <EOL> self . meta_model . default_surrogate = self . surrogate <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . nfi > <NUM_LIT:1> : <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . add ( '<STR_LIT>' , CaseIteratorDriver ( ) ) <EOL> self . add ( '<STR_LIT>' , Model ( ) ) <EOL> self . mm_checker . add_parameter ( "<STR_LIT>" , low = <NUM_LIT:0> , high = <NUM_LIT:1> ) <EOL> self . mm_checker . add_parameter ( "<STR_LIT>" , low = <NUM_LIT:0> , high = <NUM_LIT:1> ) <EOL> self . mm_checker . add_response ( "<STR_LIT>" ) <EOL> self . mm_checker . add_response ( "<STR_LIT>" ) <EOL> ngrid = <NUM_LIT:100> <EOL> self . mm_checker . case_inputs . meta_model . x = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , ngrid ) <EOL> self . mm_checker . case_inputs . model . x = np . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , ngrid ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> if self . nfi > <NUM_LIT:1> : <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> self . driver . workflow . add ( '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> surrogate = MultiFiCoKrigingSurrogate ( ) <EOL> sim_cok = Simulation ( surrogate , nfi = <NUM_LIT:2> ) <EOL> sim_cok . run ( ) <EOL> predicted_cok = np . array ( [ d . mu for d in sim_cok . mm_checker . case_outputs . meta_model . f_x ] ) <EOL> sigma_cok = np . array ( [ d . sigma for d in sim_cok . mm_checker . case_outputs . meta_model . f_x ] ) <EOL> surrogate = KrigingSurrogate ( ) <EOL> sim_k = Simulation ( surrogate , nfi = <NUM_LIT:1> ) <EOL> sim_k . run ( ) <EOL> predicted_k = np . array ( [ d . mu for d in sim_k . mm_checker . case_outputs . meta_model . f_x ] ) <EOL> sigma_k = np . array ( [ d . sigma for d in sim_k . mm_checker . case_outputs . meta_model . f_x ] ) <EOL> actual = sim_k . mm_checker . case_outputs . model . f_x <EOL> check = sim_k . mm_checker . case_inputs . meta_model . x <EOL> import pylab as plt <EOL> plt . figure ( <NUM_LIT:2> ) <EOL> plt . ioff ( ) <EOL> plt . plot ( check , actual , '<STR_LIT:k>' , label = '<STR_LIT>' ) <EOL> plt . plot ( sim_cok . hifi_cases . x , sim_cok . hifi_cases . f_x , '<STR_LIT>' , label = "<STR_LIT>" ) <EOL> plt . plot ( sim_cok . lofi_cases . x , sim_cok . lofi_cases . f_x , '<STR_LIT>' , label = "<STR_LIT>" ) <EOL> plt . plot ( check , predicted_cok , '<STR_LIT:g>' , label = '<STR_LIT>' ) <EOL> plt . plot ( check , predicted_cok + <NUM_LIT:2> * sigma_cok , '<STR_LIT:g>' , alpha = <NUM_LIT:0.5> , label = '<STR_LIT>' ) <EOL> plt . plot ( check , predicted_cok - <NUM_LIT:2> * sigma_cok , '<STR_LIT:g>' , alpha = <NUM_LIT:0.5> ) <EOL> plt . fill_between ( check , predicted_cok + <NUM_LIT:2> * sigma_cok , <EOL> predicted_cok - <NUM_LIT:2> * sigma_cok , facecolor = '<STR_LIT:g>' , alpha = <NUM_LIT> ) <EOL> plt . plot ( check , predicted_k , '<STR_LIT:b>' , label = '<STR_LIT>' ) <EOL> plt . plot ( check , predicted_k + <NUM_LIT:2> * sigma_k , '<STR_LIT:b>' , alpha = <NUM_LIT:0.5> , label = '<STR_LIT>' ) <EOL> plt . plot ( check , predicted_k - <NUM_LIT:2> * sigma_k , '<STR_LIT:b>' , alpha = <NUM_LIT:0.5> ) <EOL> plt . fill_between ( check , predicted_k + <NUM_LIT:2> * sigma_k , <EOL> predicted_k - <NUM_LIT:2> * sigma_k , facecolor = '<STR_LIT:b>' , alpha = <NUM_LIT> ) <EOL> plt . legend ( loc = '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> error = <NUM_LIT:0.> <EOL> for a , p in zip ( actual , predicted_cok ) : <EOL> error += ( a - p ) ** <NUM_LIT:2> <EOL> error = ( error / len ( actual ) ) <EOL> print "<STR_LIT>" % error <EOL> error = <NUM_LIT:0.> <EOL> for a , p in zip ( actual , predicted_k ) : <EOL> error += ( a - p ) ** <NUM_LIT:2> <EOL> error = ( error / len ( actual ) ) <EOL> print "<STR_LIT>" % error </s>
