def test_timecon_get_T(): """verify we get correct time frame""" # given con_tc = TimeConstraint(np.atleast_2d([0, 0, 0, 119, 0, 1]).T, index=['k', 'x', 'y', 'z', 0, 1], columns=['u_p0']) theo_T = 2 # pulls out last two rows # when T = con_tc.get_T() # then np.testing.assert_array_equal(theo_T, T)
def test_timecon_get_spat(): """verify we get correct spatial frame""" # given con_tc = TimeConstraint(np.atleast_2d([0, 0, 0, 119, 0, 1]).T, index=['k', 'x', 'y', 'z', 0, 1], columns=['u_p0']) theo_time = con_tc.loc[['k', 'x', 'y', 'z']] # pulls out last two rows # when time_vec = con_tc.get_spat() # then np.testing.assert_array_equal(theo_time, time_vec)
def test_data_spectrum_nolabels(): """test 1)can interpolate with no/incorrect column labels 2) float comparisons""" # given dt, z, T = 1, 65, 4 con_tc = TimeConstraint([[0, 0], [0, 0], [0, 0], [60, 70], [9, 17], [11, 20], [10, 18], [9, 16]], index=['k', 'x', 'y', 'z', 0, dt, 2 * dt, 3 * dt], columns=['dog', 'cat']) mags_theo = np.abs(np.fft.rfft(con_tc.get_time(), axis=0) / (T / dt)) spec_theo = np.mean(2 * mags_theo**2 * T, axis=1).reshape(-1, 1) f = np.arange(mags_theo.shape[0]) / T + 1e-12 # when spec = data_spectrum(f, 0, 0, z, con_tc=con_tc) # then np.testing.assert_array_almost_equal(spec, spec_theo)
def test_gen_turb_con(): """mean & std of iec turbulence, con'd turb is regen'd, correct columns """ # given -- constraining points con_spat_df = pd.DataFrame([[0, 0, 0, 70]], columns=_spat_rownames).T kwargs = {'u_ref': 10, 'turb_class': 'B', 'l_c': 340.2, 'z_ref': 70, 'T': 300, 'dt': 0.5, 'seed': 1337} coh_model = 'iec' con_turb_df = gen_turb(con_spat_df, coh_model=coh_model, **kwargs) con_tc = TimeConstraint().from_con_data(con_spat_df=con_spat_df.T, con_turb_df=con_turb_df) # old spat_df was T # given -- simulated, constrainted turbulence y, z = 0, [70, 72] spat_df = gen_spat_grid(y, z) wsp_func, sig_func, spec_func = power_profile, iec_sig, kaimal_spectrum sig_theo = np.tile([1.834, 1.4672, 0.917], 2) # sig_u, sig_v, sig_w u_theo = np.array([10, 0, 0, 10.05650077210035, 0, 0]) # U1, ... U2, ... theo_cols = [f'{"uvw"[ic]}_p{ip}' for ip in range(2)for ic in range(3)] # when sim_turb_df = gen_turb(spat_df, con_tc=con_tc, wsp_func=wsp_func, sig_func=sig_func, spec_func=spec_func, coh_model=coh_model, **kwargs) # then (std dev, mean, and regen'd time series should be close; right colnames) pd.testing.assert_index_equal(sim_turb_df.columns, pd.Index(theo_cols)) np.testing.assert_allclose(sig_theo, sim_turb_df.std(axis=0), atol=0.01, rtol=0.50) np.testing.assert_allclose(u_theo, sim_turb_df.mean(axis=0), atol=0.01) np.testing.assert_allclose(con_turb_df.u_p0, sim_turb_df.u_p0, atol=0.01)
def test_timecon_from_condata(): """verify correct converstion from old con_data format""" # given con_spat_df = pd.DataFrame([[0, 0, 0, 0, 119]], columns=['k', 'p_id', 'x', 'y', 'z']) con_turb_df = pd.DataFrame([[0], [1]], index=[0, 1], columns=['u_p0']) con_data = {'con_spat_df': con_spat_df, 'con_turb_df': con_turb_df} theo_tc = TimeConstraint(np.atleast_2d([0, 0, 0, 119, 0, 1]).T, index=['k', 'x', 'y', 'z', 0, 1], columns=['u_p0']) # when con_tc1 = TimeConstraint().from_con_data(con_data) con_tc2 = TimeConstraint().from_con_data(con_spat_df=con_spat_df, con_turb_df=con_turb_df) # then pd.testing.assert_frame_equal(theo_tc, con_tc1, check_dtype=False) pd.testing.assert_frame_equal(theo_tc, con_tc2, check_dtype=False)
def test_combine_spat_con_nonunique(): """should raise an error when we try to combine badly named columns""" # given spat_df = pd.DataFrame([[0], [0], [0], [60]], index=_spat_rownames, columns=['u_p0_con']) con_tc = TimeConstraint([[0], [0], [0], [70]], index=_spat_rownames, columns=['u_p0']) # when and then with pytest.raises(ValueError): utils.combine_spat_con(spat_df, con_tc)
def test_data_spectrum(): """verify 1) data interpolator implement in data_spectrum works, 2) dtype""" # given f, T, dt = [0, 0.5], 2, 1 k, y, z = np.repeat(range(3), 3), np.zeros(9, dtype=int), np.tile([40, 70, 100], 3) con_tc = TimeConstraint( [[0, 0, 1, 1, 2, 2], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [50, 90, 50, 90, 50, 90], [0, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 6]], index=['k', 'x', 'y', 'z', 0.0, 1.0]) spec_cols = 2 * np.abs(np.fft.rfft(con_tc.get_time(), axis=0) / (T / dt))**2 * T spec_theo = np.tile( np.interp([0, 0.5, 1, 2, 2.5, 3, 4, 4.5, 5], np.arange(6), spec_cols[0]), (2, 1)) # when spec_arr = data_spectrum(f, k, y, z, con_tc=con_tc) # then np.testing.assert_array_equal(spec_theo, spec_arr)
def test_gen_turb_bad_con_tc(): """verify the errors are thrown when con_tc dt doesn't match sim dt""" y, z = 0, [70, 80] spat_df = gen_spat_grid(y, z) kwargs = {'u_ref': 10, 'turb_class': 'B', 'l_c': 340.2, 'z_ref': 75, 'T': 1, 'dt': 0.5} con_spat_df = pd.DataFrame([[0, 0, 0, 0, 75]], columns=['k', 'p_id', 'x', 'y', 'z']) con_turb_df = pd.DataFrame([[9], [11]], index=[0, 5], columns=['u_p0']) con_tc = TimeConstraint().from_con_data(con_spat_df=con_spat_df, con_turb_df=con_turb_df) # when and then with pytest.raises(ValueError): # no con_tc given gen_turb(spat_df, con_tc=con_tc, **kwargs)
def test_combine_spat_con_tcinspat(): """some columns in TimeConstraint are in spat_df and float precision""" # given spat_df = pd.DataFrame([[0, 0], [0, 0], [0, 0], [50, 60.]], index=_spat_rownames, columns=['u_p0', 'u_p1']) con_tc = TimeConstraint([[0, 0], [0, 0], [0, 0], [60 + 1e-13, 70]], index=_spat_rownames, columns=['u_p0', 'u_p1']) theo_df = pd.DataFrame([[0, 0, 0], [0, 0, 0], [0, 0, 0], [60, 70, 50]], index=_spat_rownames, columns=['u_p0_con', 'u_p1_con', 'u_p0']) # when comb_df = utils.combine_spat_con(spat_df, con_tc) # then pd.testing.assert_frame_equal(theo_df, comb_df, check_dtype=False)
def test_gen_turb_sims_collocated(): """Should return None if all sim pts collocated with constraints""" # given -- constraining points con_spat_df = pd.DataFrame([[0, 0, 0, 70]], columns=_spat_rownames).T kwargs = {'u_ref': 10, 'turb_class': 'B', 'l_c': 340.2, 'z_ref': 70, 'T': 300, 'dt': 0.5, 'seed': 1337} coh_model = 'iec' con_turb_df = gen_turb(con_spat_df, coh_model=coh_model, **kwargs) con_tc = TimeConstraint().from_con_data(con_spat_df=con_spat_df.T, con_turb_df=con_turb_df) # given -- simulated, constrainted turbulence spat_df = con_spat_df # simulate same points as constraint # when sim_turb_df = gen_turb(spat_df, con_tc=con_tc, coh_model=coh_model, **kwargs) # then (std dev, mean, and regen'd time series should be close; right colnames) assert sim_turb_df is None
def test_check_sims_collocated(): """verify function works when no unique simulation points""" # given -- constraining points con_arr = np.array([[0, 0, 0, 70, 1], [1, 0, 0, 70, 1], [2, 0, 0, 70, 1]]).T kwargs = {'u_ref': 10, 'turb_class': 'B', 'l_c': 340.2, 'z_ref': 70, 'T': 300, 'dt': 0.5, 'seed': 1337} inp_out = [(0, 1, False), (0, 2, True)] # given -- points to simulate spat_df = pd.DataFrame([[0, 0, 0, 70], [1, 0, 0, 70]], columns=_spat_rownames).T for (start, stop, res_theo) in inp_out: # given -- constraint con_tc = TimeConstraint(con_arr[:, start:stop], index=_spat_rownames + [0]) # when res = utils.check_sims_collocated(spat_df, con_tc) # then assert res == res_theo
def test_gen_turb_bad_interp(): """verify the errors are thrown for bad interp_data options""" # given y, z = 0, [70, 80] spat_df = gen_spat_grid(y, z) kwargs = {'u_ref': 10, 'turb_class': 'B', 'l_c': 340.2, 'z_ref': 75, 'T': 2, 'dt': 1} con_spat_df = pd.DataFrame([[0, 0, 0, 0, 75]], columns=['k', 'p_id', 'x', 'y', 'z']) con_turb_df = pd.DataFrame([[0], [1]], index=[9, 11], columns=['u_p0']) con_tc = TimeConstraint().from_con_data(con_spat_df=con_spat_df, con_turb_df=con_turb_df) # when and then with pytest.raises(ValueError): # no con_tc given gen_turb(spat_df, interp_data='all', **kwargs) with pytest.raises(ValueError): # bad string gen_turb(spat_df, interp_data='dog', con_tc=con_tc, **kwargs) with pytest.raises(ValueError): # bad string in list gen_turb(spat_df, interp_data=['dog'], con_tc=con_tc, **kwargs)
def test_combine_spat_con_empty(): """verify we can combine empty/nonempty spat_df and con_tc""" # given empt_df = pd.DataFrame(index=_spat_rownames) df_sp = pd.DataFrame([[0], [0], [0], [60]], index=_spat_rownames, columns=['u_p0']) df_cn = pd.DataFrame([[0], [0], [0], [70]], index=_spat_rownames, columns=['u_p0']) theo_dfs = [empt_df, # empty+empt=empt df_sp, # empt con, full spat=spat df_cn.rename(index=str, columns={'u_p0': 'u_p0_con'}), # emp spt, ful cn pd.concat((df_cn.rename(index=str, columns={'u_p0': 'u_p0_con'}), df_sp), axis=1)] # full con, full spat=con/spat for i in range(4): # 4 diff tests spat_df = [empt_df, df_sp][i % 2] # spat_df empty on 0, 2 con_tc = TimeConstraint([empt_df, df_cn][i // 2]) # con_tc empty on 0, 1 # when comb_df = utils.combine_spat_con(spat_df, con_tc) # then pd.testing.assert_frame_equal(comb_df, theo_dfs[i])
Input = pd.read_csv( r'../InputsForConstrainedTurb/PyconturbInput/PythonWrapperInput.csv', index_col=None) #%% for i in range(0, Input.shape[0]): # Reading CSV File from Matlab an running PyConTurb Variables = pd.read_csv(Input.variables[i], index_col=None) GridY = pd.read_csv(Input.gridy[i], index_col=None) GridZ = pd.read_csv(Input.gridz[i], index_col=None) con_df = pd.read_csv(Input.contc[i], index_col=0) savename = Input.savename[i] print('running file ' + savename) # import pdb; pdb.set_trace() con_tc = TimeConstraint(con_df) # Constraining in time con_tc.index = con_tc.index.map(lambda x: float(x) if (x not in 'kxyz') else x) # Inputs to constrained Turbulence: spat_df = gen_spat_grid(GridY.iloc[0, :], GridZ.iloc[ 0, :]) # create our spatial pandas dataframe with our grid data. kwargs = { 'u_ref': Variables.iloc[0, 2], 'z_ref': Variables.iloc[0, 4], 'z_hub': Variables.iloc[0, 4], # necessary keyword arguments for IEC turbulence 'T': con_tc.get_T(), 'dt': con_tc.get_time().index[1], 'turb_class': Variables.iloc[0, -1], 'alpha': Variables.iloc[0, -2],
Plot=False # Plot=False GenerateBox=True # GenerateBox=False # --- Constants and derived params pkl_file = '{}{}.pkl'.format(Case,Suffix) pkl_space = '{}{}_space.pkl'.format(Case,Suffix) box_file = '{}{}{}.bts'.format(OUT_Folder,Case,Suffix) con_file = '35Hz_data/{}_pyConTurb_tc.csv'.format(Case) # --- Loading files print('Loading files...') sim_ts = pickle.load(open(pkl_file,'rb')) sim_sp = pickle.load(open(pkl_space,'rb')) con_tc = TimeConstraint(pd.read_csv(con_file, index_col=0)) # load data from csv directly into tc con_tc.index = con_tc.index.map(lambda x: float(x) if (x not in 'kxyz') else x) # index cleaning con_ts = con_tc.get_time() con_sp = con_tc.loc[['k','x','y','z']] RefPoints = con_sp.filter(regex='u_', axis=1).loc[['x','y','z']] print(RefPoints) print(sim_ts.columns) print(sim_sp.columns) if GenerateBox: print('Generating turbulence file:',box_file) ts = TurbSimFile() ts['y'] = np.unique(np.around(sim_sp.loc['y'].values,7)) ts['z'] = np.unique(np.around(sim_sp.loc['z'].values,7)) ts['t'] = np.around(sim_ts.index,10)
nz = 160 Suffix=Suffix+'_'+str(ny)'_'+str(nz) print('>>> Case: {}'.format(Case)) print('>>> Suffix: {}'.format(Suffix)) print('>>> Write: {} {}'.format(write_freq_data,combine_freq_data)) print('>>> y: {} {} {}'.format(ymin,ymax,ny)) print('>>> z: {} {} {}'.format(zmin,zmax,nz)) h_hub=57; pkl_file = '{}{}.pkl'.format(Case,Suffix) pkl_space = '{}{}_space.pkl'.format(Case,Suffix) con_file = '35Hz_data/{}_pyConTurb_tc.csv'.format(Case) # --- Constraint time series con_tc = TimeConstraint(pd.read_csv(con_file, index_col=0)) # load data from csv directly into tc con_tc.index = con_tc.index.map(lambda x: float(x) if (x not in 'kxyz') else x) # index cleaning time_df = con_tc.get_time() dt = con_tc.get_time().index[1] T = con_tc.get_T()-dt # TODO print('>>> Pkl : {}Mb'.format(0.07*ny*nz)) print('>>> T, dt:',T,dt) # --- Fitting power law and sigma # alpha,u_ref, my_wsp_func,veer_func = get_wsp_func(con_tc, h_hub, plot = False) # my_sig_func = get_sigma_func(con_tc, plot = False) if Case=='A1': def my_sig_func(k, y, z, **kwargs): sig = np.zeros(y.shape) sig[k==0] = 0.46577620264919134*np.exp(-0.009459702869284256*z[k==0])+0.5242096170226613 sig[k==1] = 0.32501065767141885*np.exp(-0.009059732365499604*z[k==1])+0.4664357892808409