From fefb063d46bcc896b9a9cb89936997c1de5dffc7 Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Mon, 6 Jan 2025 11:37:38 +0100 Subject: [PATCH] Fix add_survey_RPORV looping forever on all zero input --- lib/resdata/rd_grav.cpp | 8 +-- python/tests/rd_tests/test_grav.py | 111 ++++++++++++++++++----------- 2 files changed, 74 insertions(+), 45 deletions(-) diff --git a/lib/resdata/rd_grav.cpp b/lib/resdata/rd_grav.cpp index dae92ab78..6193b48c5 100644 --- a/lib/resdata/rd_grav.cpp +++ b/lib/resdata/rd_grav.cpp @@ -373,7 +373,7 @@ static void rd_grav_survey_assert_RPORV(const rd_grav_survey_type *survey, int active_size = grid_cache.size(); const rd_kw_type *init_porv_kw = rd_file_iget_named_kw(init_file, PORV_KW, 0); - int check_points = 100; + int check_points = std::min(100, active_size); int check_nr = 0; const std::vector &global_index = grid_cache.global_index(); @@ -388,8 +388,8 @@ static void rd_grav_survey_assert_RPORV(const rd_grav_survey_type *survey, double log_pormod = log10(rporv / init_porv); if (fabs(log_pormod) > 1) { - /* Detected as error if the effective pore volume multiplier - is greater than 10 or less than 0.10. */ + // Detected as error if the effective pore volume multiplier + // is greater than 10 or less than 0.10. fprintf(stderr, "----------------------------------------------" "-------------------\n"); fprintf(stderr, "INIT PORV : %g \n", init_porv); @@ -406,8 +406,8 @@ static void rd_grav_survey_assert_RPORV(const rd_grav_survey_type *survey, "-------------------\n"); exit(1); } - check_nr++; } + check_nr++; } } diff --git a/python/tests/rd_tests/test_grav.py b/python/tests/rd_tests/test_grav.py index 25efef19f..a4b58caca 100644 --- a/python/tests/rd_tests/test_grav.py +++ b/python/tests/rd_tests/test_grav.py @@ -5,7 +5,46 @@ from resdata.gravimetry import ResdataGrav from resdata.util.test import TestAreaContext from tests import ResdataTest -from resdata.rd_util import Phase + + +def write_kws(filename, kws): + with openFortIO(filename + ".UNRST", mode=FortIO.WRITE_MODE) as f: + seq_hdr = ResdataKW("SEQNUM", 1, ResDataType.RD_INT) + seq_hdr[0] = 10 + + header = ResdataKW("INTEHEAD", 67, ResDataType.RD_INT) + header[64] = 1 + header[65] = 1 + header[66] = 2000 + + seq_hdr.fwrite(f) + header.fwrite(f) + for kw in kws: + kw.fwrite(f) + + seq_hdr[0] = 20 + header[66] = 2009 + + seq_hdr.fwrite(f) + header.fwrite(f) + for kw in kws: + kw.fwrite(f) + + seq_hdr[0] = 20 + header[66] = 2010 + + seq_hdr.fwrite(f) + header.fwrite(f) + for kw in kws: + kw.fwrite(f) + + header = ResdataKW("INTEHEAD", 95, ResDataType.RD_INT) + header[14] = 1 # sets phase to oil + header[94] = 100 # E100 + with openFortIO(filename + ".INIT", mode=FortIO.WRITE_MODE) as f: + header.fwrite(f) + for kw in kws: + kw.fwrite(f) class ResdataGravTest(ResdataTest): @@ -42,49 +81,13 @@ def test_create(self): kws += int_kws with TestAreaContext("grav_init"): - with openFortIO("TEST.UNRST", mode=FortIO.WRITE_MODE) as f: - seq_hdr = ResdataKW("SEQNUM", 1, ResDataType.RD_INT) - seq_hdr[0] = 10 - - header = ResdataKW("INTEHEAD", 67, ResDataType.RD_INT) - header[64] = 1 - header[65] = 1 - header[66] = 2000 - - seq_hdr.fwrite(f) - header.fwrite(f) - for kw in kws: - kw.fwrite(f) - - seq_hdr[0] = 20 - header[66] = 2009 - - seq_hdr.fwrite(f) - header.fwrite(f) - for kw in kws: - kw.fwrite(f) - - seq_hdr[0] = 20 - header[66] = 2010 - - seq_hdr.fwrite(f) - header.fwrite(f) - for kw in kws: - kw.fwrite(f) - + write_kws("TEST", kws) # The init file created here only contains a PORO field. More # properties must be added to this before it can be used for # any useful gravity calculations. - header = ResdataKW("INTEHEAD", 95, ResDataType.RD_INT) - header[14] = 1 # sets phase to oil - header[94] = 100 # E100 - with openFortIO("TEST.INIT", mode=FortIO.WRITE_MODE) as f: - header.fwrite(f) - for kw in kws: - kw.fwrite(f) - self.init = ResdataFile("TEST.INIT") + init = ResdataFile("TEST.INIT") - grav = ResdataGrav(self.grid, self.init) + grav = ResdataGrav(self.grid, init) restart_file = ResdataFile("TEST.UNRST") restart_view = restart_file.restartView(sim_time=datetime.date(2000, 1, 1)) @@ -100,6 +103,32 @@ def test_create(self): grav.eval("rporv", "pormod", (0, 0, 0), phase_mask=1) # Test that missing std_density raises - grav = ResdataGrav(self.grid, self.init) + grav = ResdataGrav(self.grid, init) with self.assertRaises(ValueError): grav.add_survey_FIP("fip", restart_view) + + def test_create_minimal(self): + kws = [ + ResdataKW(kw, self.grid.getGlobalSize(), ResDataType.RD_FLOAT) + for kw in [ + "PORO", + "PORV", + "SWAT", + "OIL_DEN", + "RPORV", + ] + ] + + with TestAreaContext("grav_init"): + write_kws("TEST", kws) + init = ResdataFile("TEST.INIT") + + grav = ResdataGrav(self.grid, init) + + restart_file = ResdataFile("TEST.UNRST") + restart_view = restart_file.restartView(sim_time=datetime.date(2000, 1, 1)) + + grav.new_std_density(1, 0.5) + grav.add_std_density(1, 0, 0.5) + + grav.add_survey_RPORV("rporv", restart_view)