def test_pump_it_up_1ddemocase(self):

        # run both models for a few minutes
        tstop = 3000
        with SubgridWrapper(mdu=self._mdu_path('1d-democase')) as subgrid:
            subgrid.initmodel()
            df = subgrid.get_nd('pumps')
            # Get the current capacity
            # Make sure you use item(0), otherwise you get a numpy 0d type
            capacity0 = df.capacity.item(0)
            pumpid = df.id.item(0)
            # Increase capacity of pump1 by a factor 10
            subgrid.set_structure_field("pumps", pumpid, "capacity",
                                        capacity0 * 10)
            df = subgrid.get_nd('pumps')
            self.assertEqual(df.id.item(0), pumpid)
            capacity1 = df.capacity.item(0)
            self.assertEqual(capacity1, capacity0 * 10)
            while subgrid.get_nd('t1') < tstop:
                subgrid.update(-1)
            s1pumps = subgrid.get_nd('s1').copy()

        with SubgridWrapper(mdu=self._mdu_path('1d-democase')) as subgrid:
            subgrid.initmodel()
            while subgrid.get_nd('t1') < tstop:
                subgrid.update(-1)
            s1nopumps = subgrid.get_nd('s1').copy()

        self.assertGreater(np.abs(s1pumps - s1nopumps).sum(), 0)
 def test_delfland_before_duip(self):
     """generate grid in delfland"""
     mdu = self._mdu_path('delfland_gebiedsbreed')
     with SubgridWrapper(mdu=mdu) as subgrid:
         subgrid.update(-1)
     with SubgridWrapper(mdu=self._mdu_path('duifp')) as subgrid:
         subgrid.update(-1)
Example #3
0
def main():
    """main program"""
    arguments = parse_args()

    if arguments.color:
        colorlogs()
        # redirect stdout to /dev/null under osx so we get only 1 output stream
        f = open(os.devnull, 'w')
        sys.stderr = f

    logger.info('Subgridpy')
    logger.setLevel(logging.DEBUG)

    if arguments.scenariodir:
        logger.info('Using scenario dir: %s' % arguments.scenariodir)

    scenario = EventContainer(arguments.scenariodir)
    # scenario events from arguments
    if arguments.bui:
        if str(arguments.bui) in AREA_WIDE_RAIN.keys():
            scenario.add(
                AreaWideGrid, sim_time_start=0, sim_time_end=None,
                rain_definition=str(arguments.bui), type=None)
    if arguments.radar:
        scenario.add(
            RadarGrid, sim_time_start=0, sim_time_end=None,
            radar_dt=arguments.radar, sync=1, multiplier=1, type=None)

    logger.info('---- Scenario summary ----')
    for line in scenario.summary():
        logger.info(line)


    subgrid = SubgridWrapper(mdu=arguments.mdu,
                             set_logger=arguments.verbose,
                             output_dir=arguments.outputdir)

    subgrid.start()
    # Should not be needed
    # subgrid.library.initmodel()

    rain_grid_container = RainGridContainer(subgrid)
    if arguments.radar:
        subgrid.subscribe_dataset(rain_grid_container.memcdf_name)

    if arguments.tend:
        t_end = arguments.tend
    else:
        # default
        t_end = subgrid.get_nd('tend')
    logger.info('End time (seconds): %r', t_end)

    t = subgrid.get_nd('t1')  # by reference
    while t < t_end:
        apply_events(subgrid, scenario, rain_grid_container)
        subgrid.update(-1)
        t = subgrid.get_nd('t1')  # by reference

    clean_events(scenario, rain_grid_container)
 def test_1ddemo_heerenveen(self):
     """load the heerenveen model after loading a 1d model"""
     with SubgridWrapper(mdu=self._mdu_path('1d-democase')) as subgrid:
         subgrid.initmodel()
         subgrid.update(-1)
     with SubgridWrapper(mdu=self._mdu_path('heerenveen')) as subgrid:
         subgrid.initmodel()
         subgrid.update(-1)
 def test_pump_it_up3(self):
     """Check if finalize also resets pumps"""
     with SubgridWrapper(mdu=self._mdu_path('1dpumps')) as subgrid:
         subgrid.initmodel()
     with SubgridWrapper(mdu=self._mdu_path('1dpumps')) as subgrid:
         subgrid.initmodel()
         df = subgrid.get_nd('pumps')
         self.assertEqual(len(df), 1)  # Fails ... we have 2 pumps now!
 def test_load_1ddemo_1ddemo(self):
     """load the model twice"""
     with SubgridWrapper(mdu=self._mdu_path('1d-democase')) as subgrid:
         subgrid.initmodel()
         subgrid.update(-1)
     with SubgridWrapper(mdu=self._mdu_path('1d-democase')) as subgrid:
         subgrid.initmodel()
         subgrid.update(-1)
 def test_000_load_delfland_then_duifp(self):
     """test load a 1d model twice"""
     mdu = self._mdu_path('delfland_gebiedsbreed')
     with SubgridWrapper(mdu=mdu) as subgrid:
         logger.info("loaded delfland")
         subgrid.update(-1)
     with SubgridWrapper(mdu=self._mdu_path('duifp')) as subgrid:
         logger.info("loaded duifpolder")
         subgrid.update(-1)
 def test_constant_grid(self):
     """Add grid to 1 model and then to another one"""
     with SubgridWrapper(mdu=self.mdu) as subgrid:
         rain_grid = RainGrid(subgrid, initial_value=9.)
         subgrid.subscribe_dataset(rain_grid.memcdf_name)
     with SubgridWrapper(mdu=self._mdu('duifp')) as subgrid:
         rain_grid2 = RainGrid(subgrid, initial_value=1.)
         subgrid.subscribe_dataset(rain_grid2.memcdf_name)
         subgrid.update(-1)
 def test_hhnk(self):
     """test if we can start 1d democase, followed by hhnk"""
     with SubgridWrapper(mdu=self._mdu_path('1d-democase')) as subgrid:
         subgrid.initmodel()
         for _ in range(10):
             subgrid.update(-1)
     with SubgridWrapper(mdu=self._mdu_path('hhnk')) as subgrid:
         subgrid.initmodel()
         for _ in range(10):
             subgrid.update(-1)
 def test_000_testcase_culvert(self):
     """test load and init testmodel with culvert"""
     with SubgridWrapper(mdu=self._mdu_path('testcase')) as subgrid:
         logger.info("loaded testcase")
         subgrid.initmodel()
         subgrid.update(-1)
     with SubgridWrapper(mdu=self._mdu_path('testcase')) as subgrid:
         logger.info("loaded testcase")
         subgrid.initmodel()
         subgrid.update(-1)
 def test_001_load_duifpolder_2d(self):
     """test load a model with groundwater twice"""
     with SubgridWrapper(mdu=self._mdu_path('duifpolder_2d')) as subgrid:
         logger.info("loaded duifpolder 2d: 1 of 2")
         subgrid.initmodel()
         subgrid.update(-1)
         npt.assert_allclose(subgrid.get_nd('sg'), -8)
     with SubgridWrapper(mdu=self._mdu_path('duifpolder_2d')) as subgrid:
         logger.info("loaded duifpolder 2d: 2 of 2")
         subgrid.initmodel()
 def test_load_1ddemo_wijder(self):
     """load the 1d demo model, then the wijdewormer"""
     with SubgridWrapper(mdu=self._mdu_path('1d-democase')) as subgrid:
         subgrid.initmodel()
         subgrid.update(-1)
     with SubgridWrapper(mdu=self._mdu_path('wijdewormer')) as subgrid:
         subgrid.initmodel()
         subgrid.update(-1)
         pumps = subgrid.get_nd('pumps')  # pandas DataFrame
         pumps.to_dict()
         self.assertEquals(pumps.to_dict()['id'].keys()[0], 0)
Example #13
0
 def test_draw_and_update_tables(self):
     geom_json = """
     { "type": "Polygon", "coordinates": [ [
         [ 81015.066359097298118, 440929.453467557614204 ],
         [ 81117.980358958055149, 440700.214556140999775 ],
         [ 81166.972951597010251, 440713.396766208577901 ],
         [ 81133.564320785750169, 440811.235749009530991 ],
         [ 81090.294539064285345, 440949.198693339480087 ],
         [ 81015.066359097298118, 440929.453467557614204 ]
     ] ] }
     """
     with SubgridWrapper(mdu=self._mdu_path('duifp')) as subgrid:
         subgrid.update(-1)
         x0 = subgrid.get_nd('x0p')
         x1 = subgrid.get_nd('x1p')
         y0 = subgrid.get_nd('y0p')
         y1 = subgrid.get_nd('y1p')
         raster = subgrid.get_nd('soiltype')
         if raster is None:
             raise ValueError("Make sure you are testing with a model " +
                              "with soiltype + numlayers > 0")
         rr, cc = draw_shape_on_raster(geom_json,
                                       raster,
                                       21,
                                       extent=(x0, y0, x1, y1))
         quad_grid = make_quad_grid(subgrid)
         quad_cells = set(quad_grid[row, col] for row, col in zip(rr, cc))
         subgrid.update_tables('soiltype', quad_cells)
         # TODO: create proper assert, what should we check here.
         self.assertGreater(len(quad_cells), 5)
 def test_001_run_hhnk(self):
     """test load"""
     mdu = self._mdu_path('hhnk_gebiedsbreed')
     with SubgridWrapper(mdu=mdu) as subgrid:
         logger.info("loaded hhnk gebiedsbreed")
         for i in xrange(600):
             subgrid.update(-1)
    def test_1d_levee_bathy(self):
        """test load"""
        mdu = self._mdu_path('testcase_1d_levee')
        with SubgridWrapper(mdu=mdu) as subgrid:
            logger.info("loaded 1d levee testcase")
            for i in xrange(100):
                subgrid.update(-1)
            mode = 1
            value = 30
            size = 45.0
            xc = 147790
            yc = 527080
            node = 231
            dps_pre = subgrid.get_nd('dps').copy()
            subgrid.changebathy(xc, yc, size, value, mode)
            dps_post = subgrid.get_nd('dps').copy()

            quad_grid = make_quad_grid(subgrid)
            nods = set(quad_grid[100:110, 100:110].ravel())
            dps_pre = subgrid.get_nd('dps')
            dps_pre[100:110, 100:110] = dps_pre[100:110, 100:110] + 5.0

            subgrid.update_tables('dps', list(nods))
            for i in xrange(100):
                subgrid.update(-1)
            dps_post = subgrid.get_nd('dps').copy()
            npt.assert_allclose(dps_post, dps_pre, rtol=0.01)
Example #16
0
    def test_draw_soiltype(self):
        geom_json = """
        { "type": "Polygon", "coordinates": [ [
            [ 81015.066359097298118, 440929.453467557614204 ],
            [ 81117.980358958055149, 440700.214556140999775 ],
            [ 81166.972951597010251, 440713.396766208577901 ],
            [ 81133.564320785750169, 440811.235749009530991 ],
            [ 81090.294539064285345, 440949.198693339480087 ],
            [ 81015.066359097298118, 440929.453467557614204 ]
        ] ] }
        """
        with SubgridWrapper(mdu=self._mdu_path('duifp')) as subgrid:
            subgrid.update(-1)
            x0 = subgrid.get_nd('x0p')
            x1 = subgrid.get_nd('x1p')
            y0 = subgrid.get_nd('y0p')
            y1 = subgrid.get_nd('y1p')
            raster = subgrid.get_nd('soiltype')
            if raster is None:
                raise ValueError("Make sure you are testing with a model " +
                                 "with soiltype + numlayers > 0")
            # Set value to 1
            raster0 = raster.copy()

            rr, cc = draw_shape_on_raster(geom_json,
                                          raster,
                                          21,
                                          extent=(x0, y0, x1, y1))
            #
            # assume there are values changed
            n = (raster != raster0).sum()
            logger.debug("%d values changed", n)
            self.assertTrue(n > 0)
            # assume all new values are 1
            npt.assert_equal(21, raster[raster != raster0])
Example #17
0
 def test_link_table(self):
     with SubgridWrapper(mdu=self._mdu_path('duifp')) as subgrid:
         data = dict(branch=subgrid.get_nd('link_branchid'),
                     chainage=subgrid.get_nd('link_chainage'),
                     idx=subgrid.get_nd('link_idx'))
         df = pandas.DataFrame(data)
         self.assertGreater(df['idx'].iloc[0], 0)
 def test_kaapstad_wkt(self):
     """load kaapstad and extract the well known text"""
     with SubgridWrapper(mdu=self._mdu_path('kaapstad_centrum')) as subgrid:
         subgrid.initmodel()
         wkt = subgrid.get_nd('wkt')
     # we should have some coordinate system
     self.assertTrue("GEOGCS" in wkt)
Example #19
0
    def test_get_sharedmem(self):
        import multiprocessing
        import time

        with SubgridWrapper(mdu=self._mdu_path('duifp'),
                            sharedmem=True) as subgrid:
            subgrid.initmodel()

            q = multiprocessing.Queue()

            def update_s1():
                q.put(subgrid.get_nd('s1'))

            p1 = multiprocessing.Process(target=update_s1)

            # Get initial water level
            s0 = subgrid.get_nd('s1')

            # Start subprocess
            p1.start()
            # Wait for process to put array in queue
            time.sleep(1)
            # Start updating
            subgrid.update(-1)
            # Wait for process to finish
            p1.join()
            # Get the array
            s1 = q.get()
            # Get the new array, which should now be changed
            s2 = subgrid.get_nd('s1')
            self.assertEqual(s0.sum(), s1.sum())
            # breaks if 1d is broken
            self.assertNotEqual(s1.sum(), s2.sum())
 def test_back_orifice_rain(self):
     """Orifice with rain"""
     with SubgridWrapper(mdu=self._mdu('brouwersdam')) as subgrid:
         rain_grid = RainGrid(subgrid, initial_value=9.)
         subgrid.subscribe_dataset(rain_grid.memcdf_name)
         for _ in range(5):
             subgrid.update(-1)
Example #21
0
 def test_update_tables(self):
     with SubgridWrapper(mdu=self._mdu_path('duifp')) as subgrid:
         subgrid.update(-1)
         quad_cells = np.array([1, 2, 3])
         subgrid.update_tables("soiltype", quad_cells)
         subgrid.update_tables("croptype", quad_cells)
         subgrid.update_tables("maxinterception", quad_cells)
         subgrid.update_tables("infiltrationrate", quad_cells)
 def test_001_load_duifpolder_default_raingrid(self):
     """test load"""
     with SubgridWrapper(mdu=self._mdu_path('duifp')) as subgrid:
         logger.info("loaded duifpolder default")
         rain_grid = RainGrid(subgrid, '', initial_value=0.)
         subgrid.subscribe_dataset(rain_grid.memcdf_name)
         logger.info('apply raingrid')
         subgrid.update(-1)
Example #23
0
def main():
    mdu_path = os.path.abspath(sys.argv[1])
    with SubgridWrapper(mdu=mdu_path, set_logger=False) as subgrid:
        tend = subgrid.get_nd('tend')
        time = subgrid.get_nd('t1')
        while (time < tend):
            subgrid.update(-1)
            time = subgrid.get_nd('t1')
    def test_pump_it_up_is_active(self):

        # run both models for a few minutes
        with SubgridWrapper(mdu=self._mdu_path('1d-democase')) as subgrid:
            subgrid.initmodel()
            df = subgrid.get_nd('pumps')
            import ipdb
            ipdb.set_trace()
            self.assertTrue(df['is_active'].all())
 def test_pump_it_up2(self):
     with SubgridWrapper(mdu=self._mdu_path('1dpumps')) as subgrid:
         subgrid.initmodel()
         df = subgrid.get_nd('pumps')
         self.assertEqual(len(df), 1)
         # Increase capacity of all pumps by a factor 10
         pumpid = df.id.item(0)
         subgrid.set_structure_field("pumps", pumpid, "capacity",
                                     df['capacity'] * 10)
         for i in range(10):
             subgrid.update(-1)
         s1_increase = subgrid.get_nd('s1').copy()
     with SubgridWrapper(mdu=self._mdu_path('1dpumps')) as subgrid:
         subgrid.initmodel()
         for i in range(10):
             subgrid.update(-1)
         s1_default = subgrid.get_nd('s1').copy()
     self.assertGreater(np.abs(s1_increase - s1_default).sum(), 0)
Example #26
0
 def test_discard_manhole(self):
     with SubgridWrapper(mdu=self.default_mdu) as subgrid:
         manhole_name = 'test_manhole'
         x = 85830.97071920538
         y = 448605.8983910042
         discharge_value = 100.0
         itype = 1
         subgrid.discharge(x, y, manhole_name, itype, discharge_value)
         subgrid.discard_manhole(x, y)
 def test_make_compound(self):
     with SubgridWrapper(mdu=self._mdu_path('1dpumps')) as subgrid:
         subgrid.initmodel()
         valtype = subgrid.make_compound_ctype("pumps")
         # check if the type is a pointer to the compound array type
         # types pointer->array->pumps->id
         self.assertTrue(
             valtype._type_.__name__.startswith('COMPOUND_Array'))
         self.assertEqual(valtype._type_._type_.__name__, 'COMPOUND')
Example #28
0
 def test_nd_t1(self):
     with SubgridWrapper(mdu=self.default_mdu) as subgrid:
         subgrid.initmodel()
         subgrid.update(-1)
         subgrid.update(-1)
         subgrid.update(-1)
         subgrid.update(-1)
         subgrid.update(-1)
         t1 = subgrid.get_nd('t1')
         logging.debug(t1)
         npt.assert_almost_equal(t1, 5 * 30)
Example #29
0
    def test_logging(self):
        subgrid = SubgridWrapper()
        foundmessage = False

        # Create a new handler
        stream = io.BytesIO()
        handler = logging.StreamHandler(stream)
        logger.addHandler(handler)

        # if the model logs it is stored in the handler
        subgrid.start()

        # flush
        handler.flush()
        foundmessage = stream.getvalue()

        # cleanup
        logger.removeHandler(handler)

        # we should have some messages
        self.assertTrue(foundmessage)
 def test_manhole_hhnk(self):
     """add a manhole to hhnk model"""
     with SubgridWrapper(mdu=self._mdu_path('hhnk')) as subgrid:
         subgrid.initmodel()
         manhole_name = 'test_manhole'
         x = 115741
         y = 517257
         discharge_value = 250.0
         itype = 1
         subgrid.discharge(x, y, manhole_name, itype, discharge_value)
         for _ in range(100):
             subgrid.update(-1)
 def test_manhole_mozambique(self):
     """load the mozambique model and add a discharge point"""
     mdu_filename = self._mdu_path('mozambique')
     with SubgridWrapper(mdu=mdu_filename) as subgrid:
         manhole_name = 'test_manhole'
         x = 695570
         y = 7806336
         discharge_value = 5000.0
         itype = 1
         subgrid.discharge(x, y, manhole_name, itype, discharge_value)
         for _ in range(10):
             subgrid.update(-1)
Example #32
0
    def test_logging(self):
        subgrid = SubgridWrapper()
        foundmessage = False

        # Create a new handler
        stream = io.BytesIO()
        handler = logging.StreamHandler(stream)
        logger.addHandler(handler)

        # if the model logs it is stored in the handler
        subgrid.start()

        # flush
        handler.flush()
        foundmessage = stream.getvalue()

        # cleanup
        logger.removeHandler(handler)

        # we should have some messages
        self.assertTrue(foundmessage)
Example #33
0
    def test_progress(self):
        """test progress handler"""
        subgrid = SubgridWrapper(mdu=self.default_mdu, set_progress=True)
        foundmessage = False

        # Create a new handler
        stream = io.BytesIO()
        handler = logging.StreamHandler(stream)
        progresslogger.addHandler(handler)

        # if the model logs it is stored in the handler
        subgrid.start()

        # flush
        handler.flush()
        foundmessage = stream.getvalue()

        # cleanup
        progresslogger.removeHandler(handler)
        subgrid.stop()

        # we should have some messages
        self.assertTrue(foundmessage)