<s> """<STR_LIT>""" <EOL> import errno <EOL> import os <EOL> from shutil import rmtree <EOL> from tempfile import mkdtemp <EOL> import time <EOL> import numpy as np <EOL> from sqlitedict import SqliteDict <EOL> from openmdao . core . problem import Problem <EOL> from openmdao . core . group import Group <EOL> from openmdao . core . parallel_group import ParallelGroup <EOL> from openmdao . core . component import Component <EOL> from openmdao . core . mpi_wrap import MPI <EOL> from openmdao . components . indep_var_comp import IndepVarComp <EOL> from openmdao . recorders . sqlite_recorder import SqliteRecorder <EOL> from openmdao . recorders . test . test_sqlite import _assertMetadataRecorded , _assertIterationDataRecorded <EOL> from openmdao . test . mpi_util import MPITestCase <EOL> if MPI : <EOL> from openmdao . core . petsc_impl import PetscImpl as impl <EOL> coordinate = [ MPI . COMM_WORLD . rank , '<STR_LIT>' , ( <NUM_LIT:1> , ) ] <EOL> else : <EOL> from openmdao . core . basic_impl import BasicImpl as impl <EOL> coordinate = [ <NUM_LIT:0> , '<STR_LIT>' , ( <NUM_LIT:1> , ) ] <EOL> class ABCDArrayComp ( Component ) : <EOL> def __init__ ( self , arr_size = <NUM_LIT:9> , delay = <NUM_LIT> ) : <EOL> super ( ABCDArrayComp , self ) . __init__ ( ) <EOL> self . add_param ( '<STR_LIT:a>' , np . ones ( arr_size , float ) ) <EOL> self . add_param ( '<STR_LIT:b>' , np . ones ( arr_size , float ) ) <EOL> self . add_param ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . add_param ( '<STR_LIT>' , [ ] ) <EOL> self . add_output ( '<STR_LIT:c>' , np . ones ( arr_size , float ) ) <EOL> self . add_output ( '<STR_LIT:d>' , np . ones ( arr_size , float ) ) <EOL> self . add_output ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . add_output ( '<STR_LIT>' , [ ] ) <EOL> self . delay = delay <EOL> def solve_nonlinear ( self , params , unknowns , resids ) : <EOL> time . sleep ( self . delay ) <EOL> unknowns [ '<STR_LIT:c>' ] = params [ '<STR_LIT:a>' ] + params [ '<STR_LIT:b>' ] <EOL> unknowns [ '<STR_LIT:d>' ] = params [ '<STR_LIT:a>' ] - params [ '<STR_LIT:b>' ] <EOL> unknowns [ '<STR_LIT>' ] = params [ '<STR_LIT>' ] + '<STR_LIT:_>' + self . name <EOL> unknowns [ '<STR_LIT>' ] = params [ '<STR_LIT>' ] + [ <NUM_LIT> ] <EOL> def run ( problem ) : <EOL> t0 = time . time ( ) <EOL> problem . run ( ) <EOL> t1 = time . time ( ) <EOL> return t0 , t1 <EOL> class TestSqliteRecorder ( MPITestCase ) : <EOL> filename = "<STR_LIT>" <EOL> dir = "<STR_LIT>" <EOL> N_PROCS = <NUM_LIT:2> <EOL> def setUp ( self ) : <EOL> self . dir = mkdtemp ( ) <EOL> self . filename = os . path . join ( self . dir , "<STR_LIT>" ) <EOL> self . tablename = '<STR_LIT>' <EOL> self . recorder = SqliteRecorder ( self . filename ) <EOL> self . recorder . options [ '<STR_LIT>' ] = False <EOL> self . eps = <NUM_LIT> <EOL> def tearDown ( self ) : <EOL> try : <EOL> rmtree ( self . dir ) <EOL> except OSError as e : <EOL> if e . errno not in ( errno . ENOENT , errno . EACCES , errno . EPERM ) : <EOL> raise e <EOL> def assertMetadataRecorded ( self , expected ) : <EOL> if self . comm . rank != <NUM_LIT:0> : <EOL> return <EOL> db = SqliteDict ( self . filename , self . tablename ) <EOL> _assertMetadataRecorded ( self , db , expected ) <EOL> db . close ( ) <EOL> def assertIterationDataRecorded ( self , expected , tolerance , root ) : <EOL> if self . comm . rank != <NUM_LIT:0> : <EOL> return <EOL> db = SqliteDict ( self . filename , self . tablename ) <EOL> _assertIterationDataRecorded ( self , db , expected , tolerance ) <EOL> db . close ( ) <EOL> def test_basic ( self ) : <EOL> size = <NUM_LIT:3> <EOL> prob = Problem ( Group ( ) , impl = impl ) <EOL> G1 = prob . root . add ( '<STR_LIT>' , ParallelGroup ( ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT:1.0> ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT> ) ) <EOL> prob . root . add ( '<STR_LIT>' , ABCDArrayComp ( size ) ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . driver . add_recorder ( self . recorder ) <EOL> self . recorder . options [ '<STR_LIT>' ] = True <EOL> self . recorder . options [ '<STR_LIT>' ] = True <EOL> prob . setup ( check = False ) <EOL> t0 , t1 = run ( prob ) <EOL> prob . cleanup ( ) <EOL> expected_params = [ <EOL> ( "<STR_LIT>" , [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) , <EOL> ] <EOL> expected_unknowns = [ <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ - <NUM_LIT:1.0> , - <NUM_LIT:1.0> , - <NUM_LIT:1.0> ] ) ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT> ] ) , <EOL> ] <EOL> expected_resids = [ <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , [ ] ) , <EOL> ] <EOL> self . assertIterationDataRecorded ( ( ( coordinate , ( t0 , t1 ) , <EOL> expected_params , expected_unknowns , <EOL> expected_resids ) , ) , <EOL> self . eps , prob . root ) <EOL> def test_includes ( self ) : <EOL> size = <NUM_LIT:3> <EOL> prob = Problem ( Group ( ) , impl = impl ) <EOL> G1 = prob . root . add ( '<STR_LIT>' , ParallelGroup ( ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT:1.0> ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT> ) ) <EOL> prob . root . add ( '<STR_LIT>' , ABCDArrayComp ( size ) ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . driver . add_recorder ( self . recorder ) <EOL> self . recorder . options [ '<STR_LIT>' ] = True <EOL> self . recorder . options [ '<STR_LIT>' ] = True <EOL> self . recorder . options [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> prob . setup ( check = False ) <EOL> t0 , t1 = run ( prob ) <EOL> prob . cleanup ( ) <EOL> expected_params = [ <EOL> ( "<STR_LIT>" , [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) , <EOL> ] <EOL> expected_unknowns = [ <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ - <NUM_LIT:1.0> , - <NUM_LIT:1.0> , - <NUM_LIT:1.0> ] ) ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT> ] ) , <EOL> ] <EOL> expected_resids = [ <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , [ ] ) , <EOL> ] <EOL> self . assertIterationDataRecorded ( ( ( coordinate , ( t0 , t1 ) , expected_params , expected_unknowns , expected_resids ) , ) , self . eps , prob . root ) <EOL> def test_includes_and_excludes ( self ) : <EOL> size = <NUM_LIT:3> <EOL> prob = Problem ( Group ( ) , impl = impl ) <EOL> G1 = prob . root . add ( '<STR_LIT>' , ParallelGroup ( ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT:1.0> ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT> ) ) <EOL> prob . root . add ( '<STR_LIT>' , ABCDArrayComp ( size ) ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . driver . add_recorder ( self . recorder ) <EOL> self . recorder . options [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> self . recorder . options [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> self . recorder . options [ '<STR_LIT>' ] = True <EOL> self . recorder . options [ '<STR_LIT>' ] = True <EOL> prob . setup ( check = False ) <EOL> t0 , t1 = run ( prob ) <EOL> prob . cleanup ( ) <EOL> expected_params = [ <EOL> ( "<STR_LIT>" , [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) , <EOL> ] <EOL> expected_unknowns = [ <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ - <NUM_LIT:1.0> , - <NUM_LIT:1.0> , - <NUM_LIT:1.0> ] ) ) , <EOL> ] <EOL> expected_resids = [ <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ] <EOL> self . assertIterationDataRecorded ( ( ( coordinate , ( t0 , t1 ) , expected_params , expected_unknowns , expected_resids ) , ) , self . eps , prob . root ) <EOL> def test_solver_record ( self ) : <EOL> size = <NUM_LIT:3> <EOL> prob = Problem ( Group ( ) , impl = impl ) <EOL> G1 = prob . root . add ( '<STR_LIT>' , ParallelGroup ( ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT:1.0> ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT> ) ) <EOL> prob . root . add ( '<STR_LIT>' , ABCDArrayComp ( size ) ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . root . nl_solver . add_recorder ( self . recorder ) <EOL> self . recorder . options [ '<STR_LIT>' ] = True <EOL> self . recorder . options [ '<STR_LIT>' ] = True <EOL> prob . setup ( check = False ) <EOL> t0 , t1 = run ( prob ) <EOL> prob . cleanup ( ) <EOL> if MPI : <EOL> coord = [ MPI . COMM_WORLD . rank , '<STR_LIT>' , ( <NUM_LIT:1> , ) , "<STR_LIT:root>" , ( <NUM_LIT:1> , ) ] <EOL> else : <EOL> coord = [ <NUM_LIT:0> , '<STR_LIT>' , ( <NUM_LIT:1> , ) , "<STR_LIT:root>" , ( <NUM_LIT:1> , ) ] <EOL> expected_params = [ <EOL> ( "<STR_LIT>" , [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) , <EOL> ] <EOL> expected_unknowns = [ <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ - <NUM_LIT:1.0> , - <NUM_LIT:1.0> , - <NUM_LIT:1.0> ] ) ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , [ <NUM_LIT> ] ) , <EOL> ] <EOL> expected_resids = [ <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , np . array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , [ ] ) , <EOL> ] <EOL> self . assertIterationDataRecorded ( ( ( coord , ( t0 , t1 ) , expected_params , expected_unknowns , expected_resids ) , ) , self . eps , prob . root ) <EOL> def test_driver_records_metadata ( self ) : <EOL> size = <NUM_LIT:3> <EOL> prob = Problem ( Group ( ) , impl = impl ) <EOL> G1 = prob . root . add ( '<STR_LIT>' , ParallelGroup ( ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT:1.0> ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT> ) ) <EOL> prob . root . add ( '<STR_LIT>' , ABCDArrayComp ( size ) ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . driver . add_recorder ( self . recorder ) <EOL> self . recorder . options [ '<STR_LIT>' ] = True <EOL> prob . setup ( check = False ) <EOL> prob . cleanup ( ) <EOL> expected = ( <EOL> list ( prob . root . params . iteritems ( ) ) , <EOL> list ( prob . root . unknowns . iteritems ( ) ) , <EOL> list ( prob . root . resids . iteritems ( ) ) , <EOL> ) <EOL> self . assertMetadataRecorded ( expected ) <EOL> def test_driver_doesnt_records_metadata ( self ) : <EOL> size = <NUM_LIT:3> <EOL> prob = Problem ( Group ( ) , impl = impl ) <EOL> G1 = prob . root . add ( '<STR_LIT>' , ParallelGroup ( ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT:1.0> ) ) <EOL> G1 . add ( '<STR_LIT>' , IndepVarComp ( '<STR_LIT:x>' , np . ones ( size , float ) * <NUM_LIT> ) ) <EOL> prob . root . add ( '<STR_LIT>' , ABCDArrayComp ( size ) ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . root . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> prob . driver . add_recorder ( self . recorder ) <EOL> self . recorder . options [ '<STR_LIT>' ] = False <EOL> prob . setup ( check = False ) <EOL> prob . cleanup ( ) <EOL> self . assertMetadataRecorded ( None ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> from openmdao . test . mpi_util import mpirun_tests <EOL> mpirun_tests ( ) </s>
