Esempio n. 1
0
	def testFileReadWrite(self):
		cond_file_name = "test_cond.dat"
		cond_file = quakelib.EQSimConditionWriter()
		cond_file.open(cond_file_name)
		cond_file.set_stresses(1, 2, 3)
		cond_file.set_stresses(4, 5.1, 6.1)
		cond_file.set_rate_state(2, 3)
		cond_file.set_rate_state(7, 9.1)
		self.assertEqual(cond_file.num_elements(), 2)

		err = quakelib.EQSimErrors()
		cond_file.validate(err)
		self.assertEqual(err.count(), 0)

		cond_file.write()
		cond_file.close()

		cond_file_in = quakelib.EQSimConditionReader()
		cond_file_in.parse_file(cond_file_name)
		self.assertEqual(cond_file_in.get_shear_stress(1), 2)
		self.assertEqual(cond_file_in.get_normal_stress(1), 3)
		self.assertEqual(cond_file_in.get_shear_stress(4), 5.1)
		self.assertEqual(cond_file_in.get_normal_stress(4), 6.1)
		self.assertEqual(cond_file_in.get_rate_state(2), 3)
		self.assertEqual(cond_file_in.get_rate_state(7), 9.1)

		err = quakelib.EQSimErrors()
		cond_file_in.validate(err)
		self.assertEqual(err.count(), 0)

		os.remove(cond_file_name)
Esempio n. 2
0
    def testGetSetTriangle(self):
        ind_val = 123
        v_inds = [4, 5, 6]
        rake_val = 90.5
        slip_rate_val = 1.9
        aseis_factor_val = 0.5
        strike_val = 135.2
        dip_val = 14.9
        err = quakelib.EQSimErrors()

        # Set up the triangles
        t1 = quakelib.EQSimGeometryTriangle()
        t1.set_index(ind_val)
        for i, v in enumerate(v_inds):
            t1.set_vertex(i, v)
        t1.set_rake(rake_val)
        t1.set_slip_rate(slip_rate_val)
        t1.set_aseismic(aseis_factor_val)
        t1.set_strike(strike_val)
        t1.set_dip(dip_val)

        # Confirm that getting/setting a vertex out of bounds throws an exception
        self.assertRaises(IndexError, t1.vertex, 900)
        self.assertRaises(IndexError, t1.set_vertex, 900, 1)

        # Confirm that values read are those that were written
        self.assertEqual(t1.line_num(), -1)
        self.assertEqual(t1.index(), ind_val)
        for i, v in enumerate(v_inds):
            self.assertEqual(t1.vertex(i), v)
        self.assertEqual(t1.rake(), rake_val)
        self.assertEqual(t1.slip_rate(), slip_rate_val)
        self.assertEqual(t1.aseismic(), aseis_factor_val)
        self.assertEqual(t1.strike(), strike_val)
        self.assertEqual(t1.dip(), dip_val)

        # Confirm that t1 passes correctness checks
        t1.validate(err)
        self.assertEqual(err.count(), 0)

        # Confirm that changing aseismic to > 1 or < 0 generates errors
        t1.set_aseismic(-1)
        t1.validate(err)
        t1.set_aseismic(2)
        t1.validate(err)
        self.assertEqual(err.count(), 2)
Esempio n. 3
0
    def testAll(self):
        err = quakelib.EQSimErrors()
        fric_file_name = "test_fric.dat"
        fric_file = quakelib.EQSimFrictionWriter()
        fric_file.open(fric_file_name)

        # Set up initial values in the file
        fric_file.set_lame_lambda_mu(1e3, 1e4)
        fric_file.set_strengths(1, 2.1, 3.1)
        fric_file.set_strengths(7, 8.1, 9.1)
        rs1 = quakelib.EQSimFrictionRateState(2, 3, 4, 5, 6)
        rs2 = quakelib.EQSimFrictionRateState(2.1, 3.1, 4.1, 5.1, 6.1)
        fric_file.set_rs_param(1, rs1)
        fric_file.set_rs_param(7, rs2)
        fric_file.validate(err)
        self.assertEqual(err.count(), 0)
        fric_file.write()
        fric_file.close()

        fric_file_in = quakelib.EQSimFrictionReader()
        fric_file_in.parse_file(fric_file_name)

        # Confirm that Lame parameters are the same
        self.assertEqual(fric_file_in.get_lame_lambda(), 1e3)
        self.assertEqual(fric_file_in.get_lame_mu(), 1e4)

        # Confirm that strengths are the same
        self.assertEqual(fric_file_in.get_static_strength(1), 2.1)
        self.assertEqual(fric_file_in.get_dynamic_strength(1), 3.1)
        self.assertEqual(fric_file_in.get_static_strength(7), 8.1)
        self.assertEqual(fric_file_in.get_dynamic_strength(7), 9.1)

        # Confirm that rate-state parameters are the same
        self.assertEqual(fric_file_in.get_rs_param(1).A(), 2)
        self.assertEqual(fric_file_in.get_rs_param(1).B(), 3)
        self.assertEqual(fric_file_in.get_rs_param(1).L(), 4)
        self.assertEqual(fric_file_in.get_rs_param(1).f0(), 5)
        self.assertEqual(fric_file_in.get_rs_param(1).V0(), 6)
        self.assertEqual(fric_file_in.get_rs_param(7).A(), 2.1)
        self.assertEqual(fric_file_in.get_rs_param(7).B(), 3.1)
        self.assertEqual(fric_file_in.get_rs_param(7).L(), 4.1)
        self.assertEqual(fric_file_in.get_rs_param(7).f0(), 5.1)
        self.assertEqual(fric_file_in.get_rs_param(7).V0(), 6.1)

        os.remove(fric_file_name)
Esempio n. 4
0
    def testGetSetCompareVertex(self):
        ind_val = 123
        latlonval = quakelib.LatLonDepth(30, 40, 1000)
        das_val = 456.78
        trace_val = quakelib.MIDDLE_TRACE
        err = quakelib.EQSimErrors()

        # Set up v1
        v1 = quakelib.EQSimGeometryVertex()
        v1.set_index(ind_val)
        v1.set_loc(latlonval)
        v1.set_das(das_val)
        v1.set_trace_flag(trace_val)

        # Set up v2 in the same way
        v2 = quakelib.EQSimGeometryVertex()
        v2.set_index(ind_val)
        v2.set_loc(latlonval)
        v2.set_das(das_val)
        v2.set_trace_flag(trace_val)

        # Confirm that v1 retains the values assigned to it
        self.assertEqual(v1.line_num(), -1)
        self.assertEqual(v1.index(), ind_val)
        self.assertEqual(v1.loc(), latlonval)
        self.assertEqual(v1.das(), das_val)
        self.assertEqual(v1.trace_flag(), trace_val)

        # Confirm that v1 is equal to v2
        self.assertEqual(v1, v2)

        # Change an attribute of v2 and confirm it is no longer equal to v1
        v2.set_loc(quakelib.LatLonDepth(40, 40, 1000))
        self.assertNotEqual(v1, v2)

        # Confirm that v1 and v2 pass correctness checks
        v1.validate(err)
        self.assertEqual(err.count(), 0)
        v2.validate(err)
        self.assertEqual(err.count(), 0)

        # Confirm that changing trace_flag to invalid value breaks correctness
        v1.set_trace_flag(quakelib.UNDEFINED_TRACE_STATUS)
        v1.validate(err)
        self.assertEqual(err.count(), 1)
Esempio n. 5
0
    def testAll(self):
        num_sec = 3
        num_tri = 3
        num_rect = 3
        err_list = quakelib.EQSimErrors()
        geom_file_name = "test_geom.dat"
        geom_file = quakelib.EQSimGeometryWriter()
        geom_file.open(geom_file_name)
        # Create 3 new sections
        for i in range(num_sec):
            rect_vert_ids = []
            new_sec = geom_file.new_section()
            # In each section create rectangles, triangles, and vertices
            for n in range(num_tri):
                tri = new_sec.new_triangle()
                for p in range(3):
                    vert = new_sec.new_vertex()
                    vert.set_trace_flag(quakelib.NOT_ON_TRACE)
                    tri.set_vertex(p, vert.index())

            for n in range(num_rect):
                rect = new_sec.new_rectangle()
                rect.set_perfect_flag(0)
                for p in range(4):
                    vert = new_sec.new_vertex()
                    vert.set_trace_flag(quakelib.NOT_ON_TRACE)
                    rect.set_vertex(p, vert.index())

        geom_file.validate(err_list)
        #TODO: fix this
        #self.assertEqual(err_list.count(), 0)
        geom_file.write()
        geom_file.close()

        geom_in = quakelib.EQSimGeometryReader()
        geom_in.parse_file(geom_file_name)
        self.assertEqual(geom_in.num_sections(), num_sec)
        self.assertEqual(geom_in.num_vertices(),
                         num_sec * (num_tri * 3 + num_rect * 4))
        self.assertEqual(geom_in.num_triangles(), num_sec * num_tri)
        self.assertEqual(geom_in.num_rectangles(), num_sec * num_rect)

        os.remove(geom_file_name)
Esempio n. 6
0
    def testWriteRead(self):
        md_name = "./test_metadata.dat"

        # Test metadata record adding, setting, and counts
        md_file = quakelib.EQSimConditionWriter()
        md_file.open(md_name)
        md_file.meta_add_record(quakelib.META_COMMENT, "Comment 1")
        md_file.meta_add_record(quakelib.META_COMMENT, "Comment 2")
        md_file.meta_add_record(quakelib.META_COMMENT, "Comment 3")
        md_file.meta_set_record(quakelib.META_COMMENT, 1, "NewComment 2")
        self.assertEqual(md_file.meta_num_records(quakelib.META_COMMENT), 3)

        md_file.meta_add_record(quakelib.META_SIGNATURE, "Sig_1")
        md_file.meta_add_record(quakelib.META_SIGNATURE, "Sig_2")
        self.assertEqual(md_file.meta_get_record(quakelib.META_SIGNATURE, 0),
                         "Sig_2")
        md_file.meta_set_record(quakelib.META_SIGNATURE, 0, "Sig_3")
        self.assertEqual(md_file.meta_num_records(quakelib.META_SIGNATURE), 1)

        md_file.meta_add_record(quakelib.META_INFO, "Info 1")
        md_file.meta_add_record(quakelib.META_INFO, "Info 2")
        md_file.meta_add_record(quakelib.META_INFO, "Info 3")
        md_file.meta_set_record(quakelib.META_INFO, 1, "NewInfo 2")
        self.assertEqual(md_file.meta_num_records(quakelib.META_INFO), 3)

        md_file.meta_add_record(quakelib.META_TITLE, "Title 1")
        md_file.meta_add_record(quakelib.META_TITLE, "Title 2")
        self.assertEqual(md_file.meta_get_record(quakelib.META_TITLE, 0),
                         "Title 2")
        md_file.meta_set_record(quakelib.META_TITLE, 0, "Title 3")
        self.assertEqual(md_file.meta_num_records(quakelib.META_TITLE), 1)

        md_file.meta_add_record(quakelib.META_AUTHOR, "Author 1")
        md_file.meta_add_record(quakelib.META_AUTHOR, "Author 2")
        md_file.meta_add_record(quakelib.META_AUTHOR, "Author 3")
        md_file.meta_set_record(quakelib.META_AUTHOR, 1, "NewAuthor 2")
        self.assertEqual(md_file.meta_num_records(quakelib.META_AUTHOR), 3)

        md_file.meta_add_record(quakelib.META_DATE, "Date 1")
        md_file.meta_add_record(quakelib.META_DATE, "Date 2")
        self.assertEqual(md_file.meta_get_record(quakelib.META_DATE, 0),
                         "Date 2")
        md_file.meta_set_record(quakelib.META_DATE, 0, "Date 3")
        self.assertEqual(md_file.meta_num_records(quakelib.META_DATE), 1)

        err = quakelib.EQSimErrors()
        md_file.validate(err)
        self.assertEqual(err.count(), 0)

        md_file.write()
        md_file.close()

        # Read data back in to ensure validity, test erase function
        md_file_in = quakelib.EQSimConditionReader()
        md_file_in.parse_file(md_name)
        # TODO: check that there are no parse_errors
        #self.assertEqual(err.count(), 0)

        self.assertEqual(md_file_in.meta_get_record(quakelib.META_COMMENT, 0),
                         "Comment 1")
        self.assertEqual(md_file_in.meta_get_record(quakelib.META_COMMENT, 1),
                         "NewComment 2")
        self.assertEqual(md_file_in.meta_get_record(quakelib.META_COMMENT, 2),
                         "Comment 3")
        md_file_in.meta_erase_record(quakelib.META_COMMENT, 1)
        self.assertEqual(md_file_in.meta_get_record(quakelib.META_COMMENT, 1),
                         "Comment 3")
        self.assertEqual(md_file_in.meta_num_records(quakelib.META_COMMENT), 2)

        self.assertEqual(
            md_file_in.meta_get_record(quakelib.META_SIGNATURE, 0), "Sig_3")
        md_file_in.meta_erase_record(quakelib.META_SIGNATURE, 0)
        self.assertRaises(IndexError, md_file_in.meta_get_record,
                          quakelib.META_SIGNATURE, 0)
        self.assertEqual(md_file_in.meta_num_records(quakelib.META_SIGNATURE),
                         0)

        self.assertEqual(md_file_in.meta_get_record(quakelib.META_INFO, 0),
                         "Info 1")
        self.assertEqual(md_file_in.meta_get_record(quakelib.META_INFO, 1),
                         "NewInfo 2")
        self.assertEqual(md_file_in.meta_get_record(quakelib.META_INFO, 2),
                         "Info 3")
        md_file_in.meta_erase_record(quakelib.META_INFO, 1)
        self.assertEqual(md_file_in.meta_get_record(quakelib.META_INFO, 1),
                         "Info 3")
        self.assertEqual(md_file_in.meta_num_records(quakelib.META_INFO), 2)

        self.assertEqual(md_file_in.meta_get_record(quakelib.META_TITLE, 0),
                         "Title 3")
        md_file_in.meta_erase_record(quakelib.META_TITLE, 0)
        self.assertRaises(IndexError, md_file_in.meta_get_record,
                          quakelib.META_TITLE, 0)
        self.assertEqual(md_file_in.meta_num_records(quakelib.META_TITLE), 0)

        self.assertEqual(md_file_in.meta_get_record(quakelib.META_AUTHOR, 0),
                         "Author 1")
        self.assertEqual(md_file_in.meta_get_record(quakelib.META_AUTHOR, 1),
                         "NewAuthor 2")
        self.assertEqual(md_file_in.meta_get_record(quakelib.META_AUTHOR, 2),
                         "Author 3")
        md_file_in.meta_erase_record(quakelib.META_AUTHOR, 1)
        self.assertEqual(md_file_in.meta_get_record(quakelib.META_AUTHOR, 1),
                         "Author 3")
        self.assertEqual(md_file_in.meta_num_records(quakelib.META_AUTHOR), 2)

        self.assertEqual(md_file_in.meta_get_record(quakelib.META_DATE, 0),
                         "Date 3")
        md_file_in.meta_erase_record(quakelib.META_DATE, 0)
        self.assertRaises(IndexError, md_file_in.meta_get_record,
                          quakelib.META_DATE, 0)
        self.assertEqual(md_file_in.meta_num_records(quakelib.META_DATE), 0)

        err = quakelib.EQSimErrors()
        md_file_in.validate(err)
        self.assertEqual(err.count(), 0)

        os.remove(md_name)
Esempio n. 7
0
    def testPrep(self):
        #event definitions
        event_file = quakelib.EQSimEventWriter()
        es1 = quakelib.EQSimEventSet()
        s1 = quakelib.EQSimEventSummary()
        s2 = quakelib.EQSimEventSummary()
        sm1 = quakelib.EQSimEventSlipMap()
        sm2 = quakelib.EQSimEventSlipMap()
        sm3 = quakelib.EQSimEventSlipMap()
        sm4 = quakelib.EQSimEventSlipMap()
        element = []
        for i in range(8):
            element.append(quakelib.EQSimEventSlipElement(i + 1, -1))
            #Check for equality
            self.assertEqual(element[i].element_id(), i + 1)
        summarylist = [s1, s2]
        sliplist = [sm1, sm2, sm3, sm4]
        for i in range(4):
            sliplist[i].add_slip_entry(element[2 * i])
            sliplist[i].add_slip_entry(element[2 * i + 1])
        #structure
        es1.add_event_summary(s1)
        es1.add_event_summary(s2)
        s1.add_slip_map(sm1)
        s1.add_slip_map(sm2)
        s2.add_slip_map(sm3)
        s2.add_slip_map(sm4)
        #Population of EQSimEvent Summaries
        for i in range(2):
            j = i + 1
            summarylist[i].set_event_id(j)
            summarylist[i].set_magnitude(j)
            summarylist[i].set_time(j)
            summarylist[i].set_duration(j)
            summarylist[i].set_sid(j)
            summarylist[i].set_depth_lo(-2 * j)
            summarylist[i].set_depth_hi(-1 * j)
            summarylist[i].set_das_lo(j)
            summarylist[i].set_das_hi(2 * j)
            summarylist[i].set_hypo_depth(2 * j)
            summarylist[i].set_hypo_das(3 * j)
            summarylist[i].set_area(j)
            summarylist[i].set_mean_slip(4 * j)
            summarylist[i].set_moment(j)
            summarylist[i].set_shear_before(j)
            summarylist[i].set_shear_after(6 * j)
            summarylist[i].set_normal_before(j)
            summarylist[i].set_normal_after(2 * j)
            # Check for equality
            self.assertEqual(summarylist[i].event_id(), j)
            self.assertEqual(summarylist[i].magnitude(), j)
            self.assertEqual(summarylist[i].time(), j)
            self.assertEqual(summarylist[i].duration(), j)
            self.assertEqual(summarylist[i].sid(), j)
            self.assertEqual(summarylist[i].depth_lo(), -2 * j)
            self.assertEqual(summarylist[i].depth_hi(), -1 * j)
            self.assertEqual(summarylist[i].das_lo(), j)
            self.assertEqual(summarylist[i].das_hi(), 2 * j)
            self.assertEqual(summarylist[i].hypo_depth(), 2 * j)
            self.assertEqual(summarylist[i].hypo_das(), 3 * j)
            self.assertEqual(summarylist[i].area(), j)
            self.assertEqual(summarylist[i].mean_slip(), 4 * j)
            self.assertEqual(summarylist[i].moment(), j)
            self.assertEqual(summarylist[i].shear_before(), j)
            self.assertEqual(summarylist[i].shear_after(), 6 * j)
            self.assertEqual(summarylist[i].normal_before(), j)
            self.assertEqual(summarylist[i].normal_after(), 2 * j)

        #Population of EQSimEvent Slip Map
        for i in range(4):
            j = i + 1
            sliplist[i].set_depth_lo(-2 * j)
            sliplist[i].set_depth_hi(-1 * j)
            sliplist[i].set_das_lo(j)
            sliplist[i].set_das_hi(2 * j)
            sliplist[i].set_area(j)
            sliplist[i].set_mean_slip(4 * j)
            sliplist[i].set_moment(j)
            sliplist[i].set_shear_before(j)
            sliplist[i].set_shear_after(6 * j)
            sliplist[i].set_normal_before(j)
            sliplist[i].set_normal_after(2 * j)
            # Check for equality
            self.assertEqual(sliplist[i].depth_lo(), -2 * j)
            self.assertEqual(sliplist[i].depth_hi(), -1 * j)
            self.assertEqual(sliplist[i].das_lo(), j)
            self.assertEqual(sliplist[i].das_hi(), 2 * j)
            self.assertEqual(sliplist[i].area(), j)
            self.assertEqual(sliplist[i].mean_slip(), 4 * j)
            self.assertEqual(sliplist[i].moment(), j)
            self.assertEqual(sliplist[i].shear_before(), j)
            self.assertEqual(sliplist[i].shear_after(), 6 * j)
            self.assertEqual(sliplist[i].normal_before(), j)
            self.assertEqual(sliplist[i].normal_after(), 2 * j)

        err_list = quakelib.EQSimErrors()
        event_file.validate(err_list)
Esempio n. 8
0
    def testAll(self):
        #quakelib.EQSimEventWriter().flush()
        event_file = quakelib.EQSimEventWriter()
        err_list = quakelib.EQSimErrors()
        event_file_name = "test_event.dat"
        event_file.open(event_file_name)
        #event definitions
        s1 = quakelib.EQSimEventSummary()
        s2 = quakelib.EQSimEventSummary()
        es1 = quakelib.EQSimEventSet()
        sm1 = quakelib.EQSimEventSlipMap()
        sm2 = quakelib.EQSimEventSlipMap()
        sm3 = quakelib.EQSimEventSlipMap()
        sm4 = quakelib.EQSimEventSlipMap()
        element = []
        for i in range(8):
            element.append(quakelib.EQSimEventSlipElement(i + 1, -1))
        summarylist = [s1, s2]
        sliplist = [sm1, sm2, sm3, sm4]
        for i in range(4):
            sliplist[i].add_slip_entry(element[2 * i])
            sliplist[i].add_slip_entry(element[2 * i + 1])
        es1.add_event_summary(s1)
        es1.add_event_summary(s2)
        s1.add_slip_map(sm1)
        s1.add_slip_map(sm2)
        s2.add_slip_map(sm3)
        s2.add_slip_map(sm4)
        for i in range(2):
            j = i + 1
            summarylist[i].set_event_id(j)
            summarylist[i].set_magnitude(j)
            summarylist[i].set_time(j)
            summarylist[i].set_duration(j)
            summarylist[i].set_sid(j)
            summarylist[i].set_depth_lo(-2 * j)
            summarylist[i].set_depth_hi(-1 * j)
            summarylist[i].set_das_lo(j)
            summarylist[i].set_das_hi(2 * j)
            summarylist[i].set_hypo_depth(2 * j)
            summarylist[i].set_hypo_das(3 * j)
            summarylist[i].set_area(j)
            summarylist[i].set_mean_slip(4 * j)
            summarylist[i].set_moment(j)
            summarylist[i].set_shear_before(j)
            summarylist[i].set_shear_after(6 * j)
            summarylist[i].set_normal_before(j)
            summarylist[i].set_normal_after(2 * j)
        for i in range(4):
            j = i + 1
            sliplist[i].set_depth_lo(-2 * j)
            sliplist[i].set_depth_hi(-1 * j)
            sliplist[i].set_das_lo(j)
            sliplist[i].set_das_hi(2 * j)
            sliplist[i].set_area(j)
            sliplist[i].set_mean_slip(4 * j)
            sliplist[i].set_moment(j)
            sliplist[i].set_shear_before(j)
            sliplist[i].set_shear_after(6 * j)
            sliplist[i].set_normal_before(j)
            sliplist[i].set_normal_after(2 * j)

        #test

        event_file.add_event_summary(s1)
        event_file.add_event_summary(s2)

        event_file.validate(err_list)
        event_file.write()
        self.assertEqual(err_list.count(), 0)
        event_file.close()
        event_file.flush()
        event_file_in = quakelib.EQSimEventReader()
        event_file_in.parse_file(event_file_name)
        ies = event_file_in.event_summaries
        for i in ies:
            j = i + 1
            self.assertEqual(ies[i].event_id(), j)
            self.assertEqual(ies[i].magnitude(), j)
            self.assertEqual(ies[i].time(), j)
            self.assertEqual(ies[i].duration(), j)
            self.assertEqual(ies[i].sid(), j)
            self.assertEqual(ies[i].depth_lo(), -2 * j)
            self.assertEqual(ies[i].depth_hi(), -1 * j)
            self.assertEqual(ies[i].das_lo(), j)
            self.assertEqual(ies[i].das_hi(), 2 * j)
            self.assertEqual(ies[i].hypo_depth(), 2 * j)
            self.assertEqual(ies[i].hypo_das(), 3 * j)
            self.assertEqual(ies[i].area(), j)
            self.assertEqual(ies[i].mean_slip(), 4 * j)
            self.assertEqual(ies[i].moment(), j)
            self.assertEqual(ies[i].shear_before(), j)
            self.assertEqual(ies[i].shear_after(), 6 * j)
            self.assertEqual(ies[i].normal_before(), j)
            self.assertEqual(ies[i].normal_after(), 2 * j)

        eset = quakelib.EQSimEventSet()
        os.remove(event_file_name)