def test_sri_falloff(self): ref_phi = self.store.phi_cp ref_Pr = self.store.ref_Pr ref_ans = self.store.ref_Sri.copy() args = { 'Pr': lambda x: np.array(ref_Pr, order=x, copy=True), 'phi': lambda x: np.array(ref_phi, order=x, copy=True), 'X': lambda x: np.zeros_like(self.store.ref_Sri, order=x), 'Fi': lambda x: np.zeros_like(ref_Pr, order=x) } # get SRI reaction mask sri_mask = np.where(np.in1d(self.store.fall_inds, self.store.sri_inds))[0] if not sri_mask.size: return # create the kernel call kc = kernel_call( 'fall_sri', ref_ans, out_mask=[0], compare_mask=[get_comparable((sri_mask, ), ref_ans)], ref_ans_compare_mask=[ get_comparable( (np.arange(self.store.sri_inds.size, dtype=kint_type), ), ref_ans) ], **args) self.__generic_rate_tester(get_sri_kernel, kc)
def test_troe_falloff(self): phi = self.store.phi_cp ref_Pr = self.store.ref_Pr ref_ans = self.store.ref_Troe.copy() args = { 'Pr': lambda x: np.array(ref_Pr, order=x, copy=True), 'phi': lambda x: np.array(phi, order=x, copy=True), 'Fi': lambda x: np.zeros_like(ref_Pr, order=x), 'Atroe': lambda x: np.zeros_like(self.store.ref_Troe, order=x), 'Btroe': lambda x: np.zeros_like(self.store.ref_Troe, order=x), 'Fcent': lambda x: np.zeros_like(self.store.ref_Troe, order=x) } # get Troe reaction mask troe_mask = np.where( np.in1d(self.store.fall_inds, self.store.troe_inds))[0] if not troe_mask.size: return # create the kernel call kc = kernel_call( 'fall_troe', ref_ans, out_mask=[0], compare_mask=[get_comparable((troe_mask, ), ref_ans)], ref_ans_compare_mask=[ get_comparable( (np.arange(self.store.troe_inds.size, dtype=kint_type), ), ref_ans) ], **args) self.__generic_rate_tester(get_troe_kernel, kc)
def test_get_extra_var_rates(self): dphi = np.zeros_like(self.store.dphi_cp) dphi[:, 0] = self.store.conp_temperature_rates[:] args = { 'phi': lambda x: np.array(self.store.phi_cp, order=x, copy=True), 'wdot': lambda x: np.array(self.store.species_rates, order=x, copy=True), 'P_arr': lambda x: np.array(self.store.P, order=x, copy=True), 'dphi': lambda x: np.array(dphi, order=x, copy=True) } kc = [ kernel_call('get_extra_var_rates', [self.store.dphi_cp], input_mask=['cv', 'u'], compare_mask=[ get_comparable((np.array([1], dtype=kint_type), ), self.store.dphi_cp) ], **args) ] # test conp self.__generic_rate_tester(get_extra_var_rates, kc, conp=True) dphi = np.zeros_like(self.store.dphi_cv) dphi[:, 0] = self.store.conv_temperature_rates[:] args = { 'phi': lambda x: np.array(self.store.phi_cv, order=x, copy=True), 'wdot': lambda x: np.array(self.store.species_rates, order=x, copy=True), 'dphi': lambda x: np.array(dphi, order=x, copy=True) } # test conv kc = [ kernel_call('get_extra_var_rates', [self.store.dphi_cv], input_mask=['cp', 'h'], compare_mask=[ get_comparable((np.array([1], dtype=kint_type), ), self.store.dphi_cv) ], **args) ] # test conv self.__generic_rate_tester(get_extra_var_rates, kc, conp=False)
def test_get_molar_rates(self): args = { 'phi': lambda x: np.array(self.store.phi_cp, order=x, copy=True), 'wdot': lambda x: np.array(self.store.species_rates, order=x, copy=True), 'dphi': lambda x: np.zeros_like(self.store.phi_cp, order=x) } kc = [ kernel_call( 'get_molar_rates', [self.store.dphi_cp], input_mask=['cv', 'u'], compare_mask=[ get_comparable( (2 + np.arange(self.store.gas.n_species - 1), ), self.store.dphi_cp) ], **args) ] # test conp self.__generic_rate_tester(get_molar_rates, kc, conp=True) args = { 'V_arr': lambda x: np.array(self.store.V, order=x, copy=True), 'wdot': lambda x: np.array(self.store.species_rates, order=x, copy=True), 'dphi': lambda x: np.zeros_like(self.store.phi_cp, order=x) } # test conv kc = [ kernel_call( 'get_molar_rates', [self.store.dphi_cv], input_mask=['cp', 'h'], compare_mask=[ get_comparable( (2 + np.arange(self.store.gas.n_species - 1), ), self.store.dphi_cv) ], **args) ] # test conv self.__generic_rate_tester(get_molar_rates, kc, conp=False)
def test_lind_falloff(self): ref_ans = self.store.ref_Lind.copy() # get lindeman reaction mask lind_mask = np.where( np.in1d(self.store.fall_inds, self.store.lind_inds))[0] if not lind_mask.size: return # need a seperate answer mask to deal with the shape difference # in split arrays ans_mask = np.arange(self.store.lind_inds.size, dtype=np.int32) # create the kernel call kc = kernel_call( 'fall_lind', ref_ans, compare_mask=[get_comparable((lind_mask, ), ref_ans)], ref_ans_compare_mask=[get_comparable((ans_mask, ), ref_ans)]) self.__generic_rate_tester(get_lind_kernel, kc)
def test_temperature_rates(self): args = { 'wdot': lambda x: np.array( self.store.species_rates.copy(), order=x, copy=True), 'conc': lambda x: np.array(self.store.concs, order=x, copy=True), 'cp': lambda x: np.array(self.store.spec_cp, order=x, copy=True), 'h': lambda x: np.array(self.store.spec_h, order=x, copy=True), 'cv': lambda x: np.array(self.store.spec_cv, order=x, copy=True), 'u': lambda x: np.array(self.store.spec_u, order=x, copy=True), 'dphi': lambda x: np.zeros_like(self.store.dphi_cp, order=x) } kc = [ kernel_call('temperature_rate', [self.store.dphi_cp], input_mask=['cv', 'u'], compare_mask=[ get_comparable((np.array([0], dtype=kint_type), ), self.store.dphi_cp) ], **args) ] # test conp self.__generic_rate_tester(get_temperature_rate, kc, conp=True) # test conv kc = [ kernel_call('temperature_rate', [self.store.dphi_cv], input_mask=['cp', 'h'], compare_mask=[ get_comparable((np.array([0], dtype=kint_type), ), self.store.dphi_cv) ], **args) ] # test conv self.__generic_rate_tester(get_temperature_rate, kc, conp=False)
def test_mass_to_mole_fractions(self): # create a hybrid input array Yphi = np.concatenate((self.store.T.reshape( -1, 1), self.store.V.reshape(-1, 1), self.store.Y[:, :-1]), axis=1) args = { 'phi': lambda x: np.array(Yphi, order=x, copy=True), 'mw_work': lambda x: np.zeros(self.store.test_size, order=x) } def __chainer(self, out_vals): self.kernel_args['mw_work'] = out_vals[-1][0] # first test w/o the splitting compare_mask = [ get_comparable((np.arange(self.store.test_size), ), 1. / self.store.mw, compare_axis=(0, )) ] kc = kernel_call('molecular_weight_inverse', [1. / self.store.mw], strict_name_match=True, compare_axis=(0, ), compare_mask=compare_mask, **args) mole_fractions = ( self.store.mw * (self.store.Y[:, :-1] / self.store.gas.molecular_weights[:-1]).T).T # create a reference answer of same shape just to simply comparison ref_answer = np.concatenate((self.store.T.reshape( -1, 1), self.store.V.reshape(-1, 1), mole_fractions), axis=1) compare_mask = [ get_comparable((np.arange(2, self.store.jac_dim), ), ref_answer) ] kc2 = kernel_call('mole_fraction', [ref_answer], strict_name_match=True, compare_mask=compare_mask, compare_axis=(1, ), chain=__chainer, **args) self.__generic_conversion_tester(mass_to_mole_factions, [kc, kc2])
def test_pressure_mod(self): ref_pres_mod = self.store.ref_pres_mod.copy() ref_Pr = self.store.ref_Pr.copy() ref_Fi = self.store.ref_Fall.copy() ref_thd = self.store.ref_thd.copy() args = { 'Fi': lambda x: np.array(ref_Fi, order=x, copy=True), 'thd_conc': lambda x: np.array(ref_thd, order=x, copy=True), 'Pr': lambda x: np.array(ref_Pr, order=x, copy=True), 'pres_mod': lambda x: np.zeros_like(ref_pres_mod, order=x) } thd_only_inds = np.where( np.logical_not(np.in1d(self.store.thd_inds, self.store.fall_inds)))[0] fall_only_inds = np.where( np.in1d(self.store.thd_inds, self.store.fall_inds))[0] # create the kernel call kc = [ kernel_call( 'ci_thd', [ref_pres_mod], out_mask=[0], compare_mask=[get_comparable((thd_only_inds, ), ref_pres_mod)], input_mask=['Fi', 'Pr'], strict_name_match=True, **args), kernel_call('ci_fall', [ref_pres_mod], out_mask=[0], compare_mask=[ get_comparable((fall_only_inds, ), ref_pres_mod) ], input_mask=['thd_conc'], strict_name_match=True, **args) ] self.__generic_rate_tester(get_rxn_pres_mod, kc)
def test_spec_rates(self): args = { 'rop_net': lambda x: np.array(self.store.rxn_rates, order=x, copy=True), 'wdot': lambda x: np.zeros_like(self.store.species_rates, order=x) } wdot = self.store.species_rates kc = kernel_call( 'spec_rates', [wdot], compare_mask=[ get_comparable( (np.arange(self.store.gas.n_species, dtype=kint_type), ), wdot) ], **args) # test regularly self.__generic_rate_tester(get_spec_rates, kc)
def __test_rateconst_type(self, rtype): """ Performs tests for a single reaction rate type Parameters ---------- rtype : {'simple', 'plog', 'cheb'} The reaction type to test """ phi = self.store.phi_cp P = self.store.P ref_const = self.store.fwd_rate_constants if rtype != 'fall' else \ self.store.fall_rate_constants reacs = self.store.reacs masks = { 'simple': (np.array([ i for i, x in enumerate(reacs) if x.match((reaction_type.elementary, reaction_type.fall, reaction_type.chem)) ]), get_simple_arrhenius_rates), 'plog': (np.array([ i for i, x in enumerate(reacs) if x.match((reaction_type.plog, )) ]), get_plog_arrhenius_rates), 'cheb': (np.array([ i for i, x in enumerate(reacs) if x.match((reaction_type.cheb, )) ]), get_cheb_arrhenius_rates), 'fall': (np.arange( len([ i for i, x in enumerate(reacs) if x.match((reaction_type.fall, reaction_type.chem)) ])), lambda *args, **kwargs: get_simple_arrhenius_rates( *args, falloff=True, **kwargs)) } args = {'phi': lambda x: np.array(phi, order=x, copy=True)} if rtype != 'fall': args['kf'] = lambda x: np.zeros_like(ref_const, order=x) else: args['kf_fall'] = lambda x: np.zeros_like(ref_const, order=x) if rtype not in ['simple', 'fall']: args['P_arr'] = P if not masks[rtype][0].size: # don't have this type of reaction raise SkipTest( 'Skipping reaction test for {} reactions: not present in' 'mechanism'.format(rtype)) kwargs = {} if rtype == 'plog': kwargs['maxP'] = np.max([ len(rxn.rates) for rxn in self.store.gas.reactions() if isinstance(rxn, ct.PlogReaction) ]) elif rtype == 'cheb': kwargs['maxP'] = np.max([ rxn.nPressure for rxn in self.store.gas.reactions() if isinstance(rxn, ct.ChebyshevReaction) ]) kwargs['maxT'] = np.max([ rxn.nTemperature for rxn in self.store.gas.reactions() if isinstance(rxn, ct.ChebyshevReaction) ]) def __simple_post(kc, out): if len(out[0].shape) == 3: # vectorized data order # get the new indicies _get_index = indexer(kc.current_split, ref_const.shape) inds = _get_index((self.store.thd_inds, ), (1, )) pmod_inds = _get_index((np.arange(self.store.thd_inds.size), ), (1, )) # split the pres mod pmod, = kc.current_split.split_numpy_arrays( self.store.ref_pres_mod.copy()) out[0][tuple(inds)] *= pmod[tuple(pmod_inds)] else: out[0][:, self.store.thd_inds] *= self.store.ref_pres_mod compare_mask, rate_func = masks[rtype] post = None if rtype not in 'simple' else __simple_post # see if mechanism has this type if not compare_mask[0].size: return compare_mask = [get_comparable((compare_mask, ), ref_const)] # create the kernel call kc = kernel_call(rtype, ref_const, compare_mask=compare_mask, post_process=post, **args) self.__generic_rate_tester(rate_func, kc, do_ratespec=rtype in ['simple', 'fall'], **kwargs)
def test_get_comparable_nosplit(ndim, sparse): axis_size = 10 # create array arr = np.arange(axis_size**ndim) arr = arr.reshape((axis_size, ) * ndim) if sparse: # set some array elements to zero to sparsify it choice = np.sort(np.random.choice(axis_size, 3, replace=False)) choice1 = np.sort(np.random.choice(axis_size, 3, replace=False)) for x1 in choice: for x2 in choice1: arr[:, x1, x2] = 0 # create comparable object for i1, (masks, axes, tiling) in enumerate(compare_patterns(arr.shape)): comparable = get_comparable([masks], [arr], compare_axis=axes, tiling=tiling) namestore = None for i2, opts in enumerate(opts_loop(sparse=sparse)): kc = kernel_call('', arr, axes, masks) outv = arr.copy() if sparse and opts.jac_format == JacobianFormat.sparse: if csc_matrix is None: raise SkipTest( 'Scipy required for sparse Jacobian testing') # get the appropriate matrix type matrix = csr_matrix if opts.order == 'C' else csc_matrix # get the sparse indicies matrix = matrix(arr[0, :, :]) row, col = (matrix.indptr, matrix.indices) if opts.order == 'C' \ else (matrix.indices, matrix.indptr) # and get the sparse indicies in flat form matrix = coo_matrix(arr[0, :, :]) flat_row, flat_col = matrix.row, matrix.col kc.input_args = {} kc.input_args['jac'] = arr.copy() namestore = type( '', (object, ), { 'jac_row_inds': dummy_init(row), 'jac_col_inds': dummy_init(col), 'flat_jac_row_inds': dummy_init(flat_row), 'flat_jac_col_inds': dummy_init(flat_col) }) # and finally, sparsify array outv = sparsify(outv, col, row, opts.order) asplit = array_splitter(opts) kc.set_state(asplit, order=opts.order, namestore=namestore, jac_format=opts.jac_format) outv = asplit.split_numpy_arrays(outv.copy())[0] outv = comparable(kc, outv, 0, False) ansv = comparable(kc, kc.transformed_ref_ans[0].copy(), 0, True) assert np.array_equal(outv, ansv)