Skip to content

Commit

Permalink
Merge branch 'cmf2' into reaction_object
Browse files Browse the repository at this point in the history
# Conflicts:
#	cmf/__init__.py
#	cmf/cmf_core_src/cmf_wrap.cpp
#	cmf/cmf_core_src/docstrings.i
  • Loading branch information
philippkraft committed Jan 11, 2023
2 parents e4a72e2 + 522df14 commit 1c43885
Show file tree
Hide file tree
Showing 17 changed files with 434 additions and 464 deletions.
5 changes: 1 addition & 4 deletions cmf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
from .timetools import StopWatch, datetime_to_cmf, timerange


__version__ = '2.0.0b3.reaction_object'
__compiletime__ = 'Thu Sep 12 17:27:29 2019'
__version__ = '2.0.0b6'

from .cmf_core import connect_cells_with_flux as __ccwf

Expand All @@ -41,5 +40,3 @@ def connect_cells_with_flux(cells, connection, start_at_layer=0):
raise TypeError("flux_connection does not implement the cell_connector protocol")




357 changes: 170 additions & 187 deletions cmf/cmf_core.py

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion cmf/cmf_core_src/math/real.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ typedef double real;
const real REAL_MAX = std::numeric_limits<real>::max();
#endif

const std::string __compiledate__ = std::string("cmf compiled ") + std::string(__DATE__) + " - " + std::string(__TIME__);

// Some helper functions
/// Returns the minimum of two values
Expand Down
41 changes: 27 additions & 14 deletions cmf/cmf_core_src/upslope/surfacewater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ void DiffusiveSurfaceRunoff::connect_cells( cmf::upslope::Cell& cell1,cmf::upslo
new DiffusiveSurfaceRunoff(sw_upper,lower.get_surfacewater(),w);
}

}
/// Returns the square root of the gradient with singularity prevention
///
/// \param grad Gradient in m/m
/// \return sign(grad) * sqrt(grad) but with a flatted singularity at grad = 0
real sqrt_slope(real grad) {

const real s_sqrt = sign(grad) * sqrt(std::abs(grad));
// linear slope width is a value in which slope range the slope should be altered
// to prevent a singularity in dq/ds
if (cmf::options.diffusive_slope_singularity_protection > 0.0) {
// Only a shortcut for faster writing
const real &s0 = cmf::options.diffusive_slope_singularity_protection;
// Weight of linear part
real w_lin = exp(-square((grad / s0)));
// linear part using the slope at s0/4
real s_lin = grad / (2. * sqrt(s0 / 4));
// Weighted sum of sqrt(s) and a*s
return w_lin * s_lin + (1 - w_lin) * s_sqrt;
} else {
return s_sqrt;

}

}

real DiffusiveSurfaceRunoff::calc_q( cmf::math::Time t )
Expand Down Expand Up @@ -101,21 +125,9 @@ real DiffusiveSurfaceRunoff::calc_q( cmf::math::Time t )
// Get slope
real grad = dPsi/m_distance;

// Get signed square root for
real s_sqrt = sign(grad) * sqrt(std::abs(grad));
// Get signed square root for gradient
real s_sqrt = sqrt_slope(grad);

// linear slope width is a value in which slope range the slope should be altered
// to prevent a singularity in dq/ds
if (cmf::options.diffusive_slope_singularity_protection > 0.0) {
// Only a shortcut for faster writing
const real & s0 = cmf::options.diffusive_slope_singularity_protection;
// Weight of linear part
real w_lin = exp(-square((grad/s0)));
// linear part using the slope at s0/4
real s_lin =grad/(2.*sqrt(s0/4));
// Weighted sum of sqrt(s) and a*s
s_sqrt = w_lin * s_lin + (1-w_lin) * s_sqrt;
}

return prevent_negative_volume(m_flowwidth * pow(d,5/3.) * s_sqrt/left->get_nManning() * 86400.);
}
Expand All @@ -126,3 +138,4 @@ void DiffusiveSurfaceRunoff::NewNodes()
wright = SurfaceWater::cast(right_node());
owright=OpenWaterStorage::cast(right_node());
}

10 changes: 6 additions & 4 deletions cmf/cmf_core_src/upslope/surfacewater.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ namespace cmf {
private:
static void connect_cells(cmf::upslope::Cell& c1,cmf::upslope::Cell& c2,ptrdiff_t dummy);
std::weak_ptr<cmf::upslope::SurfaceWater> wleft;
real m_distance, m_flowwidth;
protected:
virtual real calc_q(cmf::math::Time t);
void NewNodes();
real m_distance, m_flowwidth;
public:
/// Creates a KinematicSurfaceRunoff between a SurfaceWater (left) with another (right) node.
///
Expand Down Expand Up @@ -187,10 +188,11 @@ namespace cmf {
std::weak_ptr<cmf::upslope::SurfaceWater> wleft;
std::weak_ptr<cmf::river::OpenWaterStorage> owright;
std::weak_ptr<cmf::upslope::SurfaceWater> wright;
real calc_q(cmf::math::Time t) override;
void NewNodes() override;
real m_distance, m_flowwidth;
public:
protected:
real calc_q(cmf::math::Time t) override;
void NewNodes() override;
public:
DiffusiveSurfaceRunoff(cmf::upslope::SurfaceWater::ptr left,cmf::water::flux_node::ptr right,real flowwidth,real distance=-1)
: flux_connection(left,right,"Diffusive surface runoff"), m_distance(distance), m_flowwidth(flowwidth) {

Expand Down
8 changes: 4 additions & 4 deletions demo/cmf2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
from argparse import ArgumentParser
from datetime import datetime, timedelta

from matplotlib import cm
try:
import pylab
import cmf.draw
Expand Down Expand Up @@ -176,7 +176,7 @@ def run(p, outlet, until, dt=cmf.day):
outflow = cmf.timeseries(solver.t, dt)
for t in solver.run(solver.t, solver.t + until, dt):
outflow.add(outlet(t))
print("%20s - %6.1f l/day" % (t, outlet(t) * 1e3))
print("%20s - %6.1f l/day - %6.2f Wetness" % (t, outlet(t) * 1e3, p.cells[0].layers[0].Wetness))
return outflow, solver.info


Expand All @@ -186,12 +186,12 @@ def animate(p, outlet, until, dt=cmf.day):

solver = cmf.CVodeKrylov(p, 1e-9)
solver.t = cmf.Time(1, 1, 1980)
hp = HillPlot(p, solver.t)
hp = HillPlot(p, solver.t, cmap=cm.viridis_r)
hp.scale = 1000

def integration():
for t in solver.run(solver.t, solver.t + until, dt):
print(t)
print("%20s - %6.1f l/day - %6.2f Wetness" % (t, outlet(t) * 1e3, p.cells[0].layers[0].wetness))
yield t

anim = hp.get_animator(integration)
Expand Down
4 changes: 2 additions & 2 deletions demo/parkinglot.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
c.add_layer(0.1, cmf.VanGenuchtenMualem(Ksat=0.005))
c.install_connection(cmf.GreenAmptInfiltration)

cmf.DiffusiveSurfaceRunoff.set_linear_slope(1e-4)
# cmf.DiffusiveSurfaceRunoff.set_linear_slope(1e-4)
cmf.connect_cells_with_flux(p, cmf.DiffusiveSurfaceRunoff)
outlet = p.NewOutlet('outlet', -length, 0, -slope * length)

Expand Down Expand Up @@ -67,7 +67,7 @@ def getpot():
# %%
fig, ax = plt.subplots()
img = ax.imshow(getdepth(), cmap=plt.cm.Blues, aspect='equal',
vmin=0.0, vmax=0.01, origin='bottom', interpolation='nearest',
vmin=0.0, vmax=0.01, origin='lower', interpolation='nearest',
extent=(-length / 2, (size[0] - .5) * length, -length / 2, length * (size[1] - .5)))
fluxdir = cmf.cell_flux_directions(p, solver.t)
pos = cmf.cell_positions(p.cells)
Expand Down
79 changes: 1 addition & 78 deletions documentation/installation/CmfInstallUbuntu.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,10 @@

# Install cmf on a Linux with sudo privileges

First you need to make sure that the dependencies of cmf are fullfilled.
It is good practise to get ppackages from your Linux distribution, if
they are available and to get only those packages, that you can't get
from your distribution to install directly in Python. Any package
installed by your OS package manager will be updated by the OS, while
other packages need to get updated by yourself.

I am working mostly with Ubuntu variants, hence i can't say anything
about other Linux package managers. However an internet search like
"apt-get fedora" will help.

## Python 2.7

You need sudo privileges to install the dependencies for the system. You
will need `python-dev` and `numpy`. Recommended are `matplotlib`,
`scipy`and `pandas`.

### Dependencies of cmf from the Ubuntu package manager

#!sh
sudo apt-get install python-dev python-numpy python-scipy python-pandas python-matplotlib

### Get dependencies from the Python Package Index (PyPI)

Follow this steps if you can't get the packages from your OS or if you
do not have sudo privileges. This text assumes you want everything only
installed for yourself and not for other users.

First check your python version:

#!sh
python --version

If the version is \<=2.7.8 then get the program `pip` to install more
packages.For higher versions, `pip` is already part of Python

#!sh
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py --user

The next steps are only needed if you did not used the `apt-get`
commands from above:

#!sh
pip install numpy --user
pip install matplotlib --user
pip install scipy --user
pip install pandas --user

### Compile cmf

Get the source code from CmfDownload and unzip it to /cmf-source

#!sh
cd cmf-source
python setup.py install --user
cd .. # Important! You cannot test your installation from the source folder

or, without multi core processor support (without OpenMP)

#!sh
cd cmf-source
python setup.py install noopenmp --user
cd .. # Important! You cannot test your installation from the source folder

## Adding geometric support to CMF

To added geometric features to cmf, you need to install the geos library
and shapely. For the geos library, you need sudo privileges

#!sh
wget http://download.osgeo.org/geos/geos-3.2.2.tar.bz2
tar -xjf geos-3.2.2.tar.bz2
cd geos-3.2.2
./configure
make
sudo make install
cd ..
pip install shapely --user

## Python 3.5
## Python 3.7 and higher

Work in progress..

Expand Down
Loading

0 comments on commit 1c43885

Please sign in to comment.