def test_save_and_load_soil():
    # Set the void ratio and specific gravity
    sl = sm.Soil()
    sl.id = 1
    sl.e_curr = 0.7
    assert sl.unit_dry_weight is None
    sl.specific_gravity = 2.95
    assert np.isclose(sl.unit_dry_weight, 17000, rtol=0.01)

    # set modulus parameters
    g_mod_set = 40e6
    sl.g_mod = g_mod_set
    sl.poissons_ratio = 0.4
    ecp_output = sm.Output()
    ecp_output.add_to_dict(sl)

    ecp_output.name = "a single soil"
    ecp_output.units = "N, kg, m, s"
    ecp_output.comments = ""
    p_str = json.dumps(ecp_output.to_dict(), skipkeys=["__repr__"], indent=4)
    a = open("temp.json", "w")
    a.write(p_str)
    a.close()
    objs = sm.loads_json(p_str, verbose=0)
    loaded_soil = objs['soils'][1]
    assert np.isclose(loaded_soil.g_mod, sl.g_mod)
Exemple #2
0
def test_save_load_w_diff_wmd():
    sl = lq.num.models.ManzariDafaliasModel(liq_mass_density=1., p_atm=101.)
    sl.e_curr = 0.8
    sl.g0 = 125
    sl.poissons_ratio = 0.3

    sl.m_c = 1.214  # From Cubrinovski 1998
    sl.c_c = 0.712
    sl.lambda_c = 0.019
    sl.e_0 = 0.934
    sl.ksi = 0.7
    sl.p_atm = 101
    sl.m_yield = 0.01
    sl.h_0 = 7.05
    sl.c_h = 0.968
    sl.n_b = 1.1
    sl.a_0 = 0.704
    sl.n_d = 3.5
    sl.z_max = 4
    sl.c_z = 600
    sl.specific_gravity = 2.65
    print(sl.unit_dry_weight)
    sl.cohesion = 0

    eo = sm.Output()
    eo.add_to_dict(sl)
    p_str = json.dumps(eo.to_dict(), indent=4)
    mods = sm.loads_json(
        p_str,
        {'soil-manzaridafalias_model': lq.num.models.ManzariDafaliasModel})
def test_save_and_load_building():
    number_of_storeys = 6
    interstorey_height = 3.4  # m
    masses = 40.0e3  # kg
    n_bays = 3

    fb = models.FrameBuilding(number_of_storeys, n_bays)
    fb.id = 1
    fb.interstorey_heights = interstorey_height * np.ones(number_of_storeys)
    fb.floor_length = 18.0  # m
    fb.floor_width = 16.0  # m
    fb.storey_masses = masses * np.ones(number_of_storeys)  # kg

    fb.bay_lengths = [6., 6.0, 6.0]
    fb.set_beam_prop("depth", [0.5, 0.5, 0.5], repeat="up")
    fb.set_beam_prop("width", [0.4, 0.4, 0.4], repeat="up")
    fb.set_column_prop("width", [0.5, 0.5, 0.5, 0.5], repeat="up")
    fb.set_column_prop("depth", [0.5, 0.5, 0.5, 0.5], repeat="up")
    fb.n_seismic_frames = 3
    fb.n_gravity_frames = 0

    ecp_output = sm.Output()
    ecp_output.add_to_dict(fb)

    ecp_output.name = "a single soil"
    ecp_output.units = "N, kg, m, s"
    ecp_output.comments = ""
    p_d = ecp_output.to_dict()
    p_str = json.dumps(ecp_output.to_dict(), skipkeys=["__repr__"], indent=4)
    mods = sm.loads_json(p_str)
    bd2 = mods['building'][1]
    print(bd2)
def test_save_and_load_soil_w_diff_wmd():  # deprecated
    sl = models.Soil(wmd=1.0)
    sl.e_curr = 0.65
    sl.specific_gravity = 2.65
    ecp_output = sm.Output()
    ecp_output.add_to_dict(sl)
    p_str = json.dumps(ecp_output.to_dict(), indent=4)
    objs = sm.loads_json(p_str)

    sl = models.StressDependentSoil(wmd=1.0, p_atm=101.0)
    sl.e_curr = 0.65
    sl.specific_gravity = 2.65

    ecp_output = sm.Output()
    ecp_output.add_to_dict(sl)
    p_str = json.dumps(ecp_output.to_dict(), indent=4)
    objs = sm.loads_json(p_str)
def test_save_and_load_soil_w_diff_liq_mass_density():
    sl = models.Soil(liq_mass_density=1.0)
    sl.e_curr = 0.65
    sl.specific_gravity = 2.65
    ecp_output = sm.Output()
    ecp_output.add_to_dict(sl)
    p_str = json.dumps(ecp_output.to_dict(), indent=4)
    print(p_str)
    objs = sm.loads_json(p_str)
def test_can_set_more_than_two_positional_args():
    c3 = Custom3Req(1, 3, 5)
    c3.id = 1
    ecp_output = sm.Output()
    ecp_output.add_to_dict(c3)
    p_str = json.dumps(ecp_output.to_dict(), skipkeys=["__repr__"], indent=4)
    objs = sm.loads_json(p_str, custom={"custom_object-custom3": Custom3Req})
    cus = objs["custom_object"][1]
    assert cus.p2 == 3
    assert cus.p3 == 5
def test_save_and_load_an_element():
    ele = models.buildings.BeamColumnElement()
    ele.sections = [models.sections.RCBeamSection()]
    ele.set_section_prop('width', 0.5)
    ecp_output = sm.Output()
    ecp_output.add_to_dict(ele)
    p_str = json.dumps(ecp_output.to_dict(), skipkeys=["__repr__"], indent=4)
    mods = sm.loads_json(p_str)
    ele2 = mods['beam_column_element'][1]
    s2 = mods['section'][1]
    assert ele2.s[0].width == 0.5, ele2.s[0].width
    assert s2.width == 0.5
def test_save_and_load_soil_profile_w_and_wo_none():
    sl1 = models.Soil()
    sl1_gmod = 30e6
    sl1_unit_dry_weight = 16000
    sl1.g_mod = sl1_gmod
    sl1.unit_dry_weight = sl1_unit_dry_weight
    sl2 = models.Soil()
    sl2_cohesion = 20e3
    sl2.cohesion = sl2_cohesion
    sp = models.SoilProfile()
    sp.id = 0
    sp.add_layer(0, sl1)
    sp.add_layer(3, sl2)
    sp.set_soil_ids_to_layers()
    ecp_output_verby = sm.Output()
    ecp_output_verby.add_to_dict(sp, export_none=True)
    p_str = json.dumps(ecp_output_verby.to_dict(),
                       skipkeys=["__repr__"],
                       indent=4)
    assert 'e_min' in p_str
    ecp_output = sm.Output()
    ecp_output.add_to_dict(sp, export_none=False)

    ecp_output.name = "a single soil"
    ecp_output.units = "N, kg, m, s"
    ecp_output.comments = ""
    p_str = json.dumps(ecp_output.to_dict(), skipkeys=["__repr__"], indent=4)
    assert 'e_min' not in p_str
    a = open("temp.json", "w")
    a.write(p_str)
    a.close()
    objs = sm.loads_json(p_str, verbose=0)
    loaded_soil = objs['soils'][1]
    load_soil_from_profile = sp.layer(1)
    assert np.isclose(loaded_soil.g_mod, sl1.g_mod)
    assert np.isclose(load_soil_from_profile.g_mod, sl1.g_mod)
def test_save_and_load_two_soil_profiles():
    sl = models.Soil(liq_mass_density=1.0)
    sl.e_curr = 0.65
    sl.specific_gravity = 2.65
    sp = models.SoilProfile()
    sp.add_layer(0, sl)
    sp.add_layer(2, sl)
    sp2 = sp.deepcopy()
    sp2.move_layer(3, 2)
    ecp_output = sm.Output()
    ecp_output.add_to_dict(sp)
    ecp_output.add_to_dict(sp2)
    p_str = json.dumps(ecp_output.to_dict(), indent=4)
    objs = sm.loads_json(p_str)
    assert len(objs['soil_profile']) == 2
Exemple #10
0
def test_save_and_load_2d_system():

    vs = 250.0

    sl = sm.Soil()
    unit_mass = 1700.0
    sl.g_mod = vs**2 * unit_mass
    sl.poissons_ratio = 0.0
    # sl.cohesion = sl.g_mod / 1000
    sl.cohesion = 30.0e3
    sl.id = 1
    sl.phi = 0.0
    sl.unit_dry_weight = unit_mass * 9.8
    sl.specific_gravity = 2.65
    sl.xi = 0.03  # for linear analysis
    sl.sra_type = 'hyperbolic'
    sl.inputs += ['sra_type', 'xi']
    sp = sm.SoilProfile()
    sp.add_layer(0, sl)
    sp.height = 1.0e3
    sp.id = 1
    sp.x_angles = [0.0, 0.0]
    fd = sm.RaftFoundation()
    fd.width = 2
    fd.depth = 0.8
    fd.height = 1
    fd.length = 100
    fd.ip_axis = 'width'
    bd = sm.SDOFBuilding()
    bd.h_eff = 8.
    bd.set_foundation(fd, x=0.0)
    sys_width = 6 * fd.width
    tds = sm.TwoDSystem(width=sys_width, height=10)
    tds.add_sp(sp, 0)
    tds.add_bd(bd, sys_width / 2)
    tds.id = 1
    ecp_out = sm.Output()
    ecp_out.add_to_dict(tds)
    # ecp_out.add_to_dict(sp)
    p_str = json.dumps(ecp_out.to_dict(), skipkeys=["__repr__"], indent=4)
    objs = sm.loads_json(p_str)
    building = objs["building"][1]
    system = objs["system"][1]
    assert np.isclose(building.h_eff, 8)
    assert np.isclose(building.fd.height, 1)
    assert isinstance(system, sm.TwoDSystem)
    assert system.sps[0].layer(1).xi == 0.03
def test_save_and_load_2d_frame_building():
    number_of_storeys = 6
    interstorey_height = 3.4  # m
    masses = 40.0e3  # kg
    n_bays = 3

    fb2d = models.FrameBuilding2D(number_of_storeys, n_bays)
    fb2d.n_storeys = 6
    fb2d.id = 1
    fb2d.interstorey_heights = interstorey_height * np.ones(number_of_storeys)
    fb2d.floor_length = 18.0  # m
    fb2d.floor_width = 16.0  # m
    fb2d.storey_masses = masses * np.ones(number_of_storeys)  # kg

    fb2d.bay_lengths = [6., 6.0, 6.0]
    fb2d.set_beam_prop("depth", [0.5, 0.6, 0.5], repeat="up")
    fb2d.set_beam_prop("width", [0.4, 0.4, 0.4], repeat="up")
    fb2d.set_column_prop("width", [0.5, 0.5, 0.5, 0.5], repeat="up")
    fb2d.set_column_prop("depth", [0.5, 0.5, 0.5, 0.5], repeat="up")
    fb2d.beams[0][0].set_section_prop('a_custom_property', 11)
    fb2d.beams[0][0].add_inputs_to_section(["a_custom_property"])

    ecp_output = sm.Output()
    ecp_output.add_to_dict(fb2d)

    ecp_output.name = "a single building"
    ecp_output.units = "N, kg, m, s"
    ecp_output.comments = ""
    p_d = ecp_output.to_dict()
    p_str = json.dumps(ecp_output.to_dict(), skipkeys=["__repr__"], indent=4)
    a = open("temp.json", "w")
    a.write(p_str)
    a.close()

    objs = sm.loads_json(p_str)
    building = objs["buildings"][1]
    assert np.isclose(building.beams[0][0].sections[0].depth,
                      0.5), building.beams[0][0].sections[0].depth
    assert np.isclose(building.beams[0][1].sections[0].depth, 0.6)

    assert building.beams[0][0].s[0].a_custom_property == 11, building.beams[
        0][0].sections[0].a_custom_property
    assert np.isclose(building.columns[0][0].sections[0].depth, 0.5)
Exemple #12
0
def test_save_and_load_soil_profile():
    sl1 = models.Soil()
    sl1_gmod = 30e6
    sl1_unit_dry_weight = 16000
    sl1.g_mod = sl1_gmod
    sl1.unit_dry_weight = sl1_unit_dry_weight
    sl2 = models.Soil()
    sl2_cohesion = 20e3
    sl2.cohesion = sl2_cohesion
    sp = models.SoilProfile()
    sp.id = 0
    sp.add_layer(0, sl1)
    sp.add_layer(3, sl2)
    sp.set_soil_ids_to_layers()
    ecp_output = sm.Output()
    ecp_output.add_to_dict(sp, export_none=False)
    p_str = ecp_output.to_str()
    objs = sm.loads_json(p_str, verbose=0)
    loaded_soil = objs['soils'][1]
    load_soil_from_profile = sp.layer(1)
    assert np.isclose(loaded_soil.g_mod, sl1.g_mod)
    assert np.isclose(load_soil_from_profile.g_mod, sl1.g_mod)
Exemple #13
0
def test_save_and_load_w_linked_building_and_soil():
    number_of_storeys = 6
    interstorey_height = 3.4  # m

    wb = models.WallBuilding(number_of_storeys)
    wb.id = 1
    wb.interstorey_heights = interstorey_height * np.ones(number_of_storeys)
    wb.floor_length = 18.0  # m
    wb.floor_width = 16.0  # m

    fd = models.RaftFoundation()
    fd.length = 4
    fd.width = 6
    fd.height = 0.0
    fd.density = 3
    fd.id = 1

    # link building to foundation
    fd.set_building(wb, two_way=False)
    assert fd.building.n_storeys == number_of_storeys
    assert wb.foundation is None
    fd.set_building(wb, two_way=True)
    assert wb.foundation.length == 4

    ecp_output = sm.Output()
    ecp_output.add_to_dict(wb)
    ecp_output.add_to_dict(fd)

    ecp_output.name = "a single wall building"
    ecp_output.units = "N, kg, m, s"
    ecp_output.comments = ""
    p_str = json.dumps(ecp_output.to_dict(), skipkeys=["__repr__"], indent=4)
    objs = sm.loads_json(p_str)
    building = objs["building"][1]
    foundation = objs["foundation"][1]
    assert foundation.width == 6
    assert building.foundation.width == 6, building.fd
    assert np.isclose(building.floor_length, 18.0)
def test_save_and_load_wall_building():
    number_of_storeys = 6
    interstorey_height = 3.4  # m
    masses = 40.0e3  # kg
    n_bays = 3

    fb = models.WallBuilding(number_of_storeys)
    fb.id = 1
    fb.interstorey_heights = interstorey_height * np.ones(number_of_storeys)
    fb.floor_length = 18.0  # m
    fb.floor_width = 16.0  # m
    fb.storey_masses = masses * np.ones(number_of_storeys)  # kg

    ecp_output = sm.Output()
    ecp_output.add_to_dict(fb)

    ecp_output.name = "a single wall building"
    ecp_output.units = "N, kg, m, s"
    ecp_output.comments = ""
    p_str = json.dumps(ecp_output.to_dict(), skipkeys=["__repr__"], indent=4)
    objs = sm.loads_json(p_str)
    building = objs["buildings"][1]
    assert np.isclose(building.floor_length, 18.0)
def run(out_folder, dytime=None):

    xi = 0.05

    sl = sm.Soil()
    sl.type = 'pimy'
    vs = 160.
    unit_mass = 1700.0
    sl.cohesion = 58.0e3
    sl.phi = 0.0
    sl.g_mod = vs**2 * unit_mass
    sl.poissons_ratio = 0.0
    sl.phi = 0.0
    sl.unit_dry_weight = unit_mass * 9.8
    sl.specific_gravity = 2.65
    sl.peak_strain = 0.01  # set additional parameter required for PIMY model
    ref_press = 100.e3
    sl.xi = 0.03  # for linear analysis
    sl.sra_type = 'hyperbolic'
    o3soil.backbone.set_params_from_op_pimy_model(sl, ref_press)
    sl.inputs += [
        'strain_curvature', 'xi_min', 'sra_type', 'strain_ref', 'peak_strain'
    ]
    assert np.isclose(vs, sl.get_shear_vel(saturated=False))
    sp = sm.SoilProfile()
    sp.add_layer(0, sl)

    sl = sm.Soil()
    sl.type = 'pimy'
    vs = 350.
    unit_mass = 1700.0
    sl.g_mod = vs**2 * unit_mass
    sl.poissons_ratio = 0.0
    sl.cohesion = 395.0e3
    sl.phi = 0.0
    sl.unit_dry_weight = unit_mass * 9.8
    sl.specific_gravity = 2.65
    sl.peak_strain = 0.1  # set additional parameter required for PIMY model
    sl.xi = 0.03  # for linear analysis
    sl.sra_type = 'hyperbolic'
    o3soil.backbone.set_params_from_op_pimy_model(sl, ref_press)
    sl.inputs += [
        'strain_curvature', 'xi_min', 'sra_type', 'strain_ref', 'peak_strain'
    ]
    sp.add_layer(8.5, sl)
    sp.height = 14.0

    ecp_out = sm.Output()
    ecp_out.add_to_dict(sp)
    ecp_out.to_file(out_folder + 'ecp.json')

    asig = eqsig.load_asig(ap.MODULE_DATA_PATH + 'gms/short_motion_dt0p01.txt',
                           m=2.5)
    if dytime is None:
        ind = None
    else:
        ind = int(dytime / asig.dt)
    asig = eqsig.AccSignal(asig.values[:ind], asig.dt)

    outs = {'ACCX': 'all', 'TAU': 'all', 'STRS': 'all'}
    # cache = 0
    # if not cache:
    #     outputs = site_response(soil_profile, asig, xi=xi, out_folder=out_folder, outs=outs, rec_dt=asig.dt, analysis_time=dytime, fixed_base=1)
    # else:
    #     o3sra_outs = o3ptools.O3SRAOutputs()
    #     outputs = o3sra_outs.load_results_from_files()

    show = 1
    if show:
        import matplotlib.pyplot as plt
        bf, sps = plt.subplots(nrows=3)
        # TODO: Show loads on playback
        # TODO: add material to playback, and show legend with material type, set material.__str__ as basetype, params[2:]
        sra1d = o3soil.sra.run_sra(sp,
                                   asig,
                                   xi=xi,
                                   cache_path=out_folder,
                                   outs=outs,
                                   analysis_time=dytime,
                                   base_imp=-1,
                                   playback=True)
        outputs = sra1d.out_dict
        import pandas as pd
        df = pd.DataFrame.from_dict(outputs['TAU'])
        df.to_csv('tau.csv', index=False)
        ind_3m = sra1d.get_nearest_ele_layer_at_depth(3)
        ind_6m = sra1d.get_nearest_ele_layer_at_depth(6)
        ind_12m = sra1d.get_nearest_ele_layer_at_depth(12)
        sps[0].plot(outputs["time"], outputs["TAU"][ind_3m], ls='--')
        sps[0].plot(outputs["time"], outputs["TAU"][ind_6m], ls='--', c='r')
        sps[0].plot(outputs["time"], outputs["TAU"][ind_12m], ls='--')
        sps[2].plot(outputs["time"], outputs["STRS"][ind_6m], ls='--', c='r')
        sps[1].plot(outputs['STRS'][ind_6m], outputs['TAU'][ind_6m], c='r')
        # sps[1].plot(outputs['STRS'][3], sl.g_mod / 1e3 * outputs['STRS'][3], ls='--')
        # sps[2].plot(outputs["time"], outputs["ACCX"][5], ls='--')
        plt.show()
Exemple #16
0
def run(show=0, export=0):
    sl = sm.Soil()

    vs = 160.
    unit_mass = 1700.0
    sl.cohesion = 58.0e3
    sl.phi = 0.0
    sl.g_mod = vs ** 2 * unit_mass
    sl.poissons_ratio = 0.0
    sl.phi = 0.0
    sl.unit_dry_weight = unit_mass * 9.8
    sl.strain_peak = 0.1  # set additional parameter required for PIMY model
    sl.xi = 0.03  # for linear analysis
    assert np.isclose(vs, sl.get_shear_vel(saturated=False))
    soil_profile = sm.SoilProfile()
    soil_profile.add_layer(0, sl)

    sl = sm.Soil()
    vs = 400.
    unit_mass = 1700.0
    sl.g_mod = vs ** 2 * unit_mass
    sl.poissons_ratio = 0.0
    sl.cohesion = 395.0e3
    sl.phi = 0.0
    sl.unit_dry_weight = unit_mass * 9.8
    sl.strain_peak = 0.1  # set additional parameter required for PIMY model
    sl.xi = 0.03  # for linear analysis
    soil_profile.add_layer(9.5, sl)
    soil_profile.height = 20.0
    ecp_out = sm.Output()
    ecp_out.add_to_dict(soil_profile)
    if export:
        import json
        ofile = open('ecp.json', 'w')
        ofile.write(json.dumps(ecp_out.to_dict(), indent=4))
        ofile.close()
    from tests.conftest import TEST_DATA_DIR

    record_path = TEST_DATA_DIR
    record_filename = 'test_motion_dt0p01.txt'
    dt = 0.01
    rec = np.loadtxt(record_path + record_filename) / 2
    acc_signal = eqsig.AccSignal(rec, dt)

    outputs = site_response(soil_profile, acc_signal, linear=0)
    tot_acc = np.sum(abs(outputs['rel_accel']))
    assert np.isclose(tot_acc, 515.76262984), tot_acc  # v3.1.0.11
    resp_dt = outputs['time'][2] - outputs['time'][1]
    surf_sig = eqsig.AccSignal(outputs['rel_accel'], resp_dt)

    if show:
        lw = 0.7
        import matplotlib.pyplot as plt
        from bwplot import cbox
        bf, sps = plt.subplots(nrows=3)

        # linear analysis with pysra
        od = run_pysra(soil_profile, acc_signal, odepths=np.array([0.0, 2.0]))
        pysra_sig = eqsig.AccSignal(od['ACCX_d0'], acc_signal.dt)

        sps[0].plot(acc_signal.time, acc_signal.values, c='k', lw=lw)
        sps[0].plot(surf_sig.time, surf_sig.values, c=cbox(0), lw=lw)
        sps[0].plot(acc_signal.time, pysra_sig.values, c=cbox(1), lw=lw)

        sps[1].plot(acc_signal.fa_frequencies, abs(acc_signal.fa_spectrum), c='k', label='Input', lw=lw)
        sps[1].plot(surf_sig.fa_frequencies, abs(surf_sig.fa_spectrum), c=cbox(0), label='O3', lw=lw)
        sps[1].plot(pysra_sig.fa_frequencies, abs(pysra_sig.fa_spectrum), c=cbox(1), label='pysra', lw=lw)
        sps[1].set_xlim([0, 20])
        h = surf_sig.smooth_fa_spectrum / acc_signal.smooth_fa_spectrum
        sps[2].plot(surf_sig.smooth_fa_frequencies, h, c=cbox(0))
        pysra_h = pysra_sig.smooth_fa_spectrum / acc_signal.smooth_fa_spectrum
        sps[2].plot(pysra_sig.smooth_fa_frequencies, pysra_h, c=cbox(1))
        # o3_nl_h = nl_surf_sig.smooth_fa_spectrum / acc_signal.smooth_fa_spectrum
        # sps[2].plot(nl_surf_sig.smooth_fa_frequencies, o3_nl_h, c=cbox(2))
        sps[2].axhline(1, c='k', ls='--')
        sps[1].legend()
        plt.show()
        print(outputs)
def run():
    sl = sm.Soil()
    sl.type = 'pimy'
    vs = 160.
    unit_mass = 1700.0
    sl.cohesion = 58.0e3
    sl.phi = 0.0
    sl.g_mod = vs**2 * unit_mass
    sl.poissons_ratio = 0.0
    sl.phi = 0.0
    sl.unit_dry_weight = unit_mass * 9.8
    sl.specific_gravity = 2.65
    sl.strain_peak = 0.1  # set additional parameter required for PIMY model
    sl.xi = 0.03  # for linear analysis
    sl.sra_type = 'hyperbolic'
    assert np.isclose(vs, sl.get_shear_vel(saturated=False))
    soil_profile = sm.SoilProfile()
    soil_profile.add_layer(0, sl)

    sl = sm.Soil()
    sl.type = 'pimy'
    vs = 400.
    unit_mass = 1700.0
    sl.g_mod = vs**2 * unit_mass
    sl.poissons_ratio = 0.0
    sl.cohesion = 395.0e3
    sl.phi = 0.0
    sl.unit_dry_weight = unit_mass * 9.8
    sl.specific_gravity = 2.65
    sl.strain_peak = 0.1  # set additional parameter required for PIMY model
    sl.xi = 0.03  # for linear analysis
    sl.sra_type = 'hyperbolic'
    soil_profile.add_layer(9.5, sl)
    soil_profile.height = 20.0
    ecp_out = sm.Output()
    ecp_out.add_to_dict(soil_profile)
    ofile = open('ecp.json', 'w')
    ofile.write(json.dumps(ecp_out.to_dict(), indent=4))
    ofile.close()

    record_path = TEST_DATA_DIR
    record_filename = 'short_motion_dt0p01.txt'
    in_sig = eqsig.load_asig(record_filename)

    # linear analysis with pysra
    od = run_pysra(soil_profile, in_sig, odepths=np.array([0.0, 2.0]))
    pysra_sig = eqsig.AccSignal(od['ACCX_d0'], in_sig.dt)

    outputs = o3soil.sra.site_response(soil_profile, in_sig)
    resp_dt = outputs['time'][2] - outputs['time'][1]
    surf_sig = eqsig.AccSignal(outputs['ACCX'][0], resp_dt)

    o3_surf_vals = np.interp(pysra_sig.time, surf_sig.time, surf_sig.values)

    show = 1

    if show:
        import matplotlib.pyplot as plt
        from bwplot import cbox

        bf, sps = plt.subplots(nrows=3)

        sps[0].plot(in_sig.time, in_sig.values, c='k', label='Input')
        sps[0].plot(pysra_sig.time, o3_surf_vals, c=cbox(0), label='o3')
        sps[0].plot(pysra_sig.time, pysra_sig.values, c=cbox(1), label='pysra')

        sps[1].plot(in_sig.fa_frequencies, abs(in_sig.fa_spectrum), c='k')
        sps[1].plot(surf_sig.fa_frequencies,
                    abs(surf_sig.fa_spectrum),
                    c=cbox(0))
        sps[1].plot(pysra_sig.fa_frequencies,
                    abs(pysra_sig.fa_spectrum),
                    c=cbox(1))
        sps[1].set_xlim([0, 20])
        h = surf_sig.smooth_fa_spectrum / in_sig.smooth_fa_spectrum
        sps[2].plot(surf_sig.smooth_fa_frequencies, h, c=cbox(0))
        pysra_h = pysra_sig.smooth_fa_spectrum / in_sig.smooth_fa_spectrum
        sps[2].plot(pysra_sig.smooth_fa_frequencies, pysra_h, c=cbox(1))
        sps[2].axhline(1, c='k', ls='--')
        sps[0].plot(pysra_sig.time, (o3_surf_vals - pysra_sig.values) * 10,
                    c='r',
                    label='Error x10')
        sps[0].legend()
        plt.show()

    assert np.isclose(o3_surf_vals, pysra_sig.values, atol=0.01,
                      rtol=100).all()
def create():
    sl = sm.Soil()
    vs = 200.
    unit_mass = 1700.0
    sl.cohesion = 68.0e3
    # sl.cohesion = 0.0
    sl.phi = 0.0
    sl.g_mod = 68.0e6
    print('G_mod: ', sl.g_mod)
    sl.unit_dry_weight = unit_mass * 9.8
    sl.specific_gravity = 2.65
    k0 = 1.0
    # sl.poissons_ratio = k0 / (1 + k0) - 0.01
    sl.poissons_ratio = 0.3
    strain_max = 0.01
    strains = np.logspace(-4, -1.5, 40)
    esig_v0 = 100.0e3
    ref_press = 100.e3
    ndm = 2
    # TODO: phi and cohesion are not used as you would expect for user defined surfaces !!! recalculated: tau_max calculated the
    set_params_from_op_pimy_model(sl, strain_max, ref_press)
    sl.inputs += ['strain_curvature', 'xi_min', 'sra_type', 'strain_ref']

    sp = sm.SoilProfile()
    sp.id = 1
    sp.add_layer(0, sl)
    sp.height = 30.0
    ecp_out = sm.Output()
    ecp_out.add_to_dict(sp)
    ofile = open('ecp.json', 'w')
    # ofile.write(json.dumps(ecp_out.to_dict(), indent=4))
    ofile.close()

    compare_backbone = 1
    if compare_backbone:
        dss_eq = np.sqrt(3. / 2)
        pysra_sl = pysra.site.ModifiedHyperbolicSoilType(
            "",
            1,
            strain_ref=sl.strain_ref * dss_eq,
            curvature=sl.strain_curvature,
            damping_min=sl.xi_min,
            strains=strains)
        pysra_tau = pysra_sl.mod_reduc.values * sl.g_mod * pysra_sl.mod_reduc.strains
        taus = calc_backbone_op_pimy_model(sl,
                                           strain_max,
                                           strains,
                                           ref_press,
                                           ndm=ndm,
                                           esig_v0=esig_v0)
        osi = o3.OpenSeesInstance(ndm=2, ndf=2, state=3)
        # See example: https://opensees.berkeley.edu/wiki/index.php/PressureIndependMultiYield_Material
        base_mat = o3.nd_material.PressureIndependMultiYield(
            osi,
            nd=2,
            rho=sl.unit_sat_mass,
            g_mod_ref=sl.g_mod_ref,
            bulk_mod_ref=sl.bulk_mod_ref,
            peak_strain=strain_max,
            cohesion=sl.cohesion,
            phi=sl.phi,
            p_ref=sl.p_ref,
            d=0.0,
            n_surf=25)

        disps = np.array(
            [0.0, 0.00003, -0.00003, 0.0004, 0.0001, 0.0009, -0.0009]) * 10
        disps = np.linspace(0.0, 0.02, 100)
        k0_init = sl.poissons_ratio / (1 - sl.poissons_ratio)
        print(k0_init)
        stress, strain, v_eff, h_eff, exit_code = d2d.run_2d_strain_driver_iso(
            osi,
            base_mat,
            esig_v0=esig_v0,
            disps=disps,
            handle='warn',
            verbose=1,
            target_d_inc=0.00001)
        bf, sps = plt.subplots(nrows=2)
        sps[0].plot(strain, stress, c='r')
        sps[0].plot(strains, taus, label='approx PIMY')
        sps[0].plot(pysra_sl.mod_reduc.strains,
                    pysra_tau,
                    ls='--',
                    label='PySRA Hyperbolic')
        # sps[1].plot(stress)
        sps[1].plot(v_eff)
        sps[1].plot(h_eff)
        sps[0].axhline(sl.cohesion, c='k')
        plt.legend()
        plt.show()