Skip to content

Commit

Permalink
Added new lumped demo file
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkraft committed Dec 20, 2018
1 parent f608e1e commit 1103c8f
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 22 deletions.
6 changes: 0 additions & 6 deletions cmf/cmf_core_src/docstrings.i
Original file line number Diff line number Diff line change
Expand Up @@ -19858,9 +19858,6 @@ ymax=1) ";
// File: _descriptor_8md.xml


// File: fluxogram__and__get__fluxes_8md.xml


// File: _cmf_tut1d_8md.xml


Expand Down Expand Up @@ -20056,9 +20053,6 @@ ymax=1) ";
// File: descriptor.xml


// File: fluxogram_and_get_fluxes.xml


// File: cmf_tut1d.xml


Expand Down
7 changes: 3 additions & 4 deletions demo/connector_matrix.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
Calculates a matrix from the connected states. Useful to explore numerical problems
"""

import numpy
import cmf

def connector_matrix(states, compression_factor=1):
"""
Expand All @@ -13,12 +14,10 @@ def connector_matrix(states, compression_factor=1):
unconnected states. Compressed matrices contain larger numbers for
the count of connection in the compressed field
"""
posdict = {}
l = len(states)
size = (l // compression_factor, l // compression_factor)
jac = numpy.zeros(size, dtype=int)
for i, a in enumerate(states):
posdict[a.node_id] = i
posdict = {a.node_id: i for i, a in enumerate(states)}
for i, a in enumerate(states):
for f, t in a.fluxes(cmf.Time()):
j = posdict.get(t.node_id)
Expand Down
2 changes: 1 addition & 1 deletion demo/data/climate.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
date,Prec [mm/day],T max [degC],T min [degC],rH mean [%], windspeed [m/s], sunshine [h/h]
date,Prec [mm/day],T max [degC],T min [degC],rH mean [%],windspeed [m/s],sunshine [h/h]
1980-01-03, 4.6, -0.4, -5.7, 81, 2.36457, 0.660309
1980-01-04, 4.3, 0.2, -5.7, 94, 4.34398, 0
1980-01-05, 1.7, 3.6, -0.6, 91, 1.85302, 0.0505279
Expand Down
37 changes: 37 additions & 0 deletions demo/load_climate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import cmf
import pandas as pd

def load_climate_data(project, climate_file):
"""
Loads climate data from 1980 to 2006 for Giessen from
a csv file and adds it to a cmf project as a meteorological station
and a rain station.
Data provided by DWD
:param project: A cmf project
:returns: meteo station, rainfall station
"""
data = pd.read_csv(climate_file, parse_dates=['date'], index_col=0)

meteo = project.meteo_stations.add_station(
'giessen', position=(0, 0), latitude=51
)
begin = data.index[0].to_pydatetime()

def col2ts(column_name):
return cmf.timeseries.from_array(begin, cmf.day, data[column_name])

meteo.Tmin = col2ts('T min [degC]')
meteo.Tmax = col2ts('T max [degC]')
meteo.rHmean = col2ts('rH mean [%]')
meteo.Sunshine = col2ts('sunshine [h/h]')
meteo.Windspeed = col2ts('windspeed [m/s]')

rainstation = project.rainfall_stations.add(
'Giessen',
col2ts('Prec [mm/day]'),
Position=(0, 0, 0)
)

return meteo, rainstation
66 changes: 66 additions & 0 deletions demo/lumped_2storages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
A simple lumped 2 stoarge model with ET, snow but no canopy interception
"""

import cmf

from .load_climate import load_climate_data

class Cmf2StorageModel:
def __init__(self, climate_file='data/climate.csv'):
self.project = cmf.project()
self.cell = self.project.NewCell(0, 0, 0, 1000)

self.soil = self.cell.add_layer(1.0)
self.gw = self.cell.add_layer(1.0)
self.snow = self.cell.add_storage('Snow', 'S')

self.outlet = self.project.NewOutlet('outlet')

self.meteo, self.rainstation = load_climate_data(self.project, climate_file)


def mm_to_m3(self, vol):
return vol * self.cell.area * 1e-3

def create_connections(self):
# Route snow melt to surface
cmf.SimpleTindexSnowMelt(self.cell.snow, self.cell.surfacewater, rate=7)
# Infiltration
cmf.SimpleInfiltration(self.soil, self.cell.surfacewater, W0=0.8)
# Route infiltration / saturation excess to outlet
cmf.WaterBalanceFlux(self.cell.surfacewater, self.outlet)
# Parameterize soil water capacity
self.soil.soil.porosity = 0.2
C = self.soil.get_capacity()
# Parameterize water stress function
self.cell.set_uptakestress(cmf.VolumeStress(0.2 * C, 0 * C))
cmf.TurcET(self.soil, self.cell.transpiration)

# Route water from soil to gw
cmf.PowerLawConnection(self.soil, self.gw,
Q0=self.mm_to_m3(50),
V0=0.5 * C,
beta=4
)
# Route water from gw to outlet
cmf.LinearStorageConnection(
self.gw, self.outlet,
residencetime=20,
residual=0 * C
)

def run(self, begin=None, end=None):
begin = begin or self.rainstation.data.begin
end = end or self.rainstation.data.end

solver = cmf.CVodeIntegrator(self.project, 1e-9)
outlet = cmf.timeseries(begin, cmf.day)
outlet.add(self.outlet(begin))
for t in solver.run(begin, end, cmf.day):
outlet.add(self.outlet.add)
print(t)

return outlet


10 changes: 0 additions & 10 deletions documentation/tutorial/CmfTutStart.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,10 @@

@subpage conceptual


## Physical Models

@subpage physical

## Tutorials under construction

The tutorials listed here are not ready to use. They may not work, need revisions
or use deprecated or experimental cmf methods

### Documenting the model

- @subpage fluxogram_and_get_fluxes "Fluxogram"
- Using `spotpy` for manual and automatic calibration



2 changes: 1 addition & 1 deletion documentation/tutorial/atmosphere/CmfTutET.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ et_pot_turc = cmf.TurcET(layer, cell.transpiration)
stress = cmf.VolumeStress(300, 100)
cell.set_uptakestress(stress)
# A solver
# A solver, any is fine, really
solver = cmf.HeunIntegrator(p)
solver.t = datetime(2018, 5, 1)
et_act = cmf.timeseries(solver.t, cmf.day)
Expand Down

0 comments on commit 1103c8f

Please sign in to comment.