diff --git a/LoopStructural/modelling/core/_model_exporter.py b/LoopStructural/modelling/core/_model_exporter.py index 2796d88c7..901f8a107 100644 --- a/LoopStructural/modelling/core/_model_exporter.py +++ b/LoopStructural/modelling/core/_model_exporter.py @@ -39,16 +39,15 @@ def get_fault_surfaces(model: GeologicalModel, faults: list[str] | None = None) def get_stratigraphic_surfaces( model: GeologicalModel, units: list[str] | None = None, bottoms: bool = True ) -> surface_list: - if units is None: - units = [] ## TODO change the stratigraphic column to its own class and have methods to get the relevant surfaces surfaces = [] - units = [] if model.stratigraphic_column is None: return [] - units = model.stratigraphic_column.get_isovalues(where='bottom' if bottoms else 'top') + isovalues = model.stratigraphic_column.get_isovalues(where='bottom' if bottoms else 'top') + if units: + isovalues = {name: u for name, u in isovalues.items() if name in units} units_for_group = {} - for name, u in units.items(): + for name, u in isovalues.items(): if u['group'] not in model: logger.warning(f"Group {u['group']} not found in model") continue @@ -92,7 +91,6 @@ def save( extension = path.suffix parent = path.parent name = path.stem - stratigraphic_surfaces = model.get_stratigraphic_surfaces() if fault_surfaces: for s in model.get_fault_surfaces(): ## geoh5 can save everything into the same file @@ -113,19 +111,18 @@ def save( else: grid.save(f'{parent}/{name}_block_model{extension}') if stratigraphic_data and model.stratigraphic_column is not None: - for group in model.stratigraphic_column: - if group == "faults": + for g in model.stratigraphic_column.get_groups(): + if g.name not in model: continue - for data in model.__getitem__(group).get_data(): + for data in model[g.name].get_data(): if extension == ".geoh5" or extension == '.omf': data.save(filename) else: - data.save(f'{parent}/{name}_{group}_data{extension}') + data.save(f'{parent}/{name}_{data.name}{extension}') if fault_data: for f in model.fault_names(): - for d in model.__getitem__(f).get_data(): + for d in model[f].get_data(): if extension == ".geoh5" or extension == '.omf': - d.save(filename) else: - d.save(f'{parent}/{name}_{group}{extension}') + d.save(f'{parent}/{name}_{d.name}{extension}') diff --git a/LoopStructural/modelling/core/_model_relationships.py b/LoopStructural/modelling/core/_model_relationships.py index 9236b092a..64ac08fe7 100644 --- a/LoopStructural/modelling/core/_model_relationships.py +++ b/LoopStructural/modelling/core/_model_relationships.py @@ -323,7 +323,6 @@ def add_onlap_unconformity( unconformity_feature : GeologicalFeature the created unconformity """ - feature.regions = [] uc_feature = UnconformityFeature(feature, value, False, onlap=True) feature.add_region(uc_feature.inverse()) for f in reversed(model.features): diff --git a/LoopStructural/modelling/core/fault_topology.py b/LoopStructural/modelling/core/fault_topology.py index 6b38a0444..cf19ea2a9 100644 --- a/LoopStructural/modelling/core/fault_topology.py +++ b/LoopStructural/modelling/core/fault_topology.py @@ -135,7 +135,7 @@ def get_fault_stratigraphic_unit_relationships(self): units_group_pairs = self.stratigraphic_column.get_group_unit_pairs() matrix = np.zeros((len(self.faults), len(units_group_pairs)), dtype=int) for i, fault in enumerate(self.faults): - for j, (unit_name, _group) in enumerate(units_group_pairs): + for j, (_group, unit_name) in enumerate(units_group_pairs): if (unit_name, fault) in self.stratigraphy_fault_relationships: matrix[i, j] = 1 diff --git a/LoopStructural/modelling/core/geological_model.py b/LoopStructural/modelling/core/geological_model.py index 0dfaaec62..3caa9d2a4 100644 --- a/LoopStructural/modelling/core/geological_model.py +++ b/LoopStructural/modelling/core/geological_model.py @@ -482,7 +482,7 @@ def series(self): def intrusions(self): intrusions = [] for f in self.features: - if f.type == "intrusion": + if f.type == FeatureType.INTRUSION: intrusions.append(f) return intrusions diff --git a/LoopStructural/modelling/input/map2loop_processor.py b/LoopStructural/modelling/input/map2loop_processor.py index 623514ab7..cd3f42dc8 100644 --- a/LoopStructural/modelling/input/map2loop_processor.py +++ b/LoopStructural/modelling/input/map2loop_processor.py @@ -72,12 +72,9 @@ def __init__(self, m2l_directory, use_thickness=None): supergroups = {} with open(f"{m2l_directory}/tmp/super_groups.csv") as f: - for line in f: - - i = 0 + for i, line in enumerate(f): for g in line.strip(",\n").split(","): supergroups[g] = f"supergroup_{i}" - i += 1 if "supergroup" not in groups.columns: groups["supergroup"] = "none" for i in groups.index: diff --git a/tests/unit/input/test_data_processor.py b/tests/unit/input/test_data_processor.py index 19d1726e8..0c2d67cb5 100644 --- a/tests/unit/input/test_data_processor.py +++ b/tests/unit/input/test_data_processor.py @@ -16,6 +16,38 @@ def test_create_processor(): assert (processor.data["val"].unique() == np.array([0.5, 0])).all() +def test_map2loop_processor_gives_each_supergroup_its_own_name(tmp_path, monkeypatch): + import networkx + + from LoopStructural.modelling.input import map2loop_processor as m2lp + + (tmp_path / "tmp").mkdir() + (tmp_path / "output").mkdir() + files = { + "tmp/all_sorts_clean.csv": ",group,code,colour\n0,g1,u1,red\n1,g2,u2,blue\n", + "tmp/super_groups.csv": "g1,\ng2,\n", + "tmp/bbox.csv": "minx,miny,maxx,maxy,lower,upper\n0,0,1,1,0,1\n", + "output/orientations_clean.csv": "formation,polarity\nu1,1\n", + "output/formation_summary_thicknesses.csv": "formation,thickness median\nu1,1\n", + "output/contacts_clean.csv": "formation\nu1\n", + "output/fault_displacements3.csv": "fname,vertical_displacement,downthrow_dir\n", + "output/fault_orientations.csv": "formation,DipDirection\n", + "output/faults.csv": "formation\n", + "output/supergroup-fault-relationships.csv": "supergroup\n", + "output/fault_dimensions.csv": "Fault,InfluenceDistance,VerticalRadius,HorizontalRadius\n", + } + for path, text in files.items(): + (tmp_path / path).write_text(text) + networkx.write_gml(networkx.Graph(), tmp_path / "tmp" / "fault_network.gml") + + captured = {} + + def fake_init(self, contacts, orientations, stratigraphic_order, **kwargs): + captured["order"] = stratigraphic_order + + monkeypatch.setattr(ProcessInputData, "__init__", fake_init) + m2lp.Map2LoopProcessor(str(tmp_path)) + assert captured["order"] == [("supergroup_0", ["u1"]), ("supergroup_1", ["u2"])] def test_from_processor_unit_ranges_match_processor_values(): """ Regression test: the basal unit (min=-inf, max=0) gave an infinite diff --git a/tests/unit/modelling/test_fault_topology.py b/tests/unit/modelling/test_fault_topology.py index 2a9464a40..62bb8fa5f 100644 --- a/tests/unit/modelling/test_fault_topology.py +++ b/tests/unit/modelling/test_fault_topology.py @@ -150,3 +150,13 @@ def test_to_dict_from_dict_round_trip(topology): assert restored.get_fault_relationship("f1", "f2") == FaultRelationshipType.ABUTTING assert restored.get_fault_relationship("f2", "f3") == FaultRelationshipType.FAULTED assert restored.get_fault_stratigraphic_relationship("unitA", "f1") is True + + +def test_fault_stratigraphic_unit_relationships_matrix(): + sc = StratigraphicColumn() + sc.add_unit("u1", thickness=1.0) + topo = FaultTopology(sc) + topo.add_fault("f1") + topo.add_stratigraphy_fault_relationship("u1", "f1") + # columns follow get_group_unit_pairs(): (Group_0, u1), (Group_1, Basement) + assert topo.get_fault_stratigraphic_unit_relationships().tolist() == [[1, 0]] diff --git a/tests/unit/modelling/test_geological_model.py b/tests/unit/modelling/test_geological_model.py index 44f56806c..d9fd59b22 100644 --- a/tests/unit/modelling/test_geological_model.py +++ b/tests/unit/modelling/test_geological_model.py @@ -266,6 +266,58 @@ def test_from_file_allow_pickle_false_raises_without_loading(tmp_path): GeologicalModel.from_file(model_file, allow_pickle=False) +def test_intrusions_lists_intrusion_features(): + from LoopStructural.modelling.intrusions import IntrusionFeature + + model = GeologicalModel(np.zeros(3), np.ones(3)) + intrusion = IntrusionFeature(None, None, name="intrusion", model=model) + model.features.append(intrusion) + assert model.intrusions == [intrusion] + + +def test_save_with_default_arguments(tmp_path): + from tests.fixtures.fault_models import OLD, add_fault, add_layers + + model = GeologicalModel(np.zeros(3), np.ones(3)) + model.nsteps = [5, 5, 5] + fault = add_fault( + model, + "fault", + OLD["center"], + OLD["dip"], + OLD["dip_azimuth"], + OLD["displacement"], + nelements=500, + ) + add_layers(model, [fault], nelements=500) + model.stratigraphic_column.add_unit("a", thickness=0.3) + model.stratigraphic_column.add_unit("b", thickness=0.3) + model.stratigraphic_column.group_mapping["Group_0"] = "strat" + + model.save(str(tmp_path / "m.pkl")) + + names = {p.name for p in tmp_path.iterdir()} + assert "m_block_model.pkl" in names + # one data file per data set: the fault frame data do not overwrite each other + fault_data = [d.name for d in model["fault"].get_data()] + assert len(fault_data) > 1 + for d in fault_data: + assert f"m_{d}.pkl" in names + for d in model["strat"].get_data(): + assert f"m_{d.name}.pkl" in names + + +def test_get_stratigraphic_surfaces_returns_only_the_requested_units(): + data, bb = load_claudius() + model = GeologicalModel(bb[0, :], bb[1, :]) + model.data = data + model.nsteps = [5, 5, 5] + model.create_and_add_foliation("strati", nelements=500) + for name, value in (("a", 0.0), ("b", 60.0), ("c", 250.0)): + model.stratigraphic_column.add_unit(name, thickness=value or 60.0) + model.stratigraphic_column.group_mapping["Group_0"] = "strati" + surfaces = model.get_stratigraphic_surfaces(units=["b"]) + assert [s.name for s in surfaces] == ["b"] def test_set_stratigraphic_column_keeps_group_names(): """Regression test: with two groups, each unit got the name of the other group.""" model = GeologicalModel([0, 0, 0], [1, 1, 1]) diff --git a/tests/unit/modelling/test_unconformity_truncates_faults.py b/tests/unit/modelling/test_unconformity_truncates_faults.py index b1da5ad59..fa55599fc 100644 --- a/tests/unit/modelling/test_unconformity_truncates_faults.py +++ b/tests/unit/modelling/test_unconformity_truncates_faults.py @@ -304,3 +304,15 @@ def add_series(name, values, pts, faults): ) assert far.sum() > 500 np.testing.assert_allclose(value[far], basement_field(exact[far]), atol=5e-3) + + +def test_onlap_unconformity_keeps_the_regions_the_feature_already_has(): + """An onlap unconformity on a series must not remove the erosional region of an older one.""" + model = new_model() + old = series(model, "old", basement_field, faults=[]) + model.add_unconformity(old, 0.0) + young = series(model, "young", cover_field, faults=[], seed=6) + before = [r.name for r in young.regions] + assert before + model.add_onlap_unconformity(young, 0.0) + assert set(before) <= {r.name for r in young.regions}