Skip to content

Commit

Permalink
Feature/vicexample (#7)
Browse files Browse the repository at this point in the history
* fix: fixed examples

* fix: fixed example runner

* chore: fixed pipelines

* fix: fixed example compilation

* chore: fixed pipeline

* chore: fixed pipeline 2

* fix: fixed C code of examples
  • Loading branch information
jlerat authored Nov 23, 2024
1 parent 2116867 commit 74d7786
Show file tree
Hide file tree
Showing 13 changed files with 418 additions and 40 deletions.
4 changes: 4 additions & 0 deletions .azure_devops/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ steps:
source activate $(CONDA_ENV_NAME)
cd $(BUILD_DIR)/pyquasoare
python examples/run_all_examples.py
cd examples/quadroute
./compile_unix.sh
cd ../vicmod
./compile_unix.sh
displayName: run_examples

- task: CopyFiles@2
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/python-package-conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ jobs:
cd $GITHUB_WORKSPACE
echo "--- pyquasoare examples : located in $PWD ---"
python examples/run_all_examples.py
cd examples/quadroute
./compile_unix.sh
cd ../vicmod
./compile_unix.sh
9 changes: 0 additions & 9 deletions examples/example_run_unix.sh

This file was deleted.

6 changes: 0 additions & 6 deletions examples/example_run_windows.bat

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,43 @@
import matplotlib.pyplot as plt
from pyquasoare import benchmarks, approx, models
from hydrodiy.io import csv

#
# Code to solve the reservoir equation associated
# with the GR4J production reservoir per Perring
# et al. (2006):
#
# dS/dt = P*[1-(S/X1)^2]-E*S/X1*(2-S/X1)-2.25^4*u^5
#
# where P is rain and E is PET and X1 is the
# reservoir capacity.
#
# In this implementation, the model is re-written as
#
# du/dt = P/X1
# - P/X1 * (1-u^2)
# - E/X1 * u*(2-u)
# - 2.25^4/X1 * u^5
#
# where u = S/X1
#
# Introducing the flux scaling factors, we finally obtain
#
# du/dt = s1*f1(u) -> f1(u) = 1 (constant) s1=P/X1
# + s2*f2(u) -> f2(u) = -u^2 s2=P/X1
# + s3*f4(u) -> f4(u) = -u*(2-u) s3=E/X1
# + s3*f3(u) -> f3(u) = -u^5 s4=2.25^4/X1
#
source_file = Path(__file__).resolve()
froot = source_file.parent.parent
froot = source_file.parent
basename = source_file.stem
fimg = froot / "examples" / "images"
fimg = froot / "images"
fimg.mkdir(exist_ok=True, parents=True)

# Get production store flux functions.
# The store capacity is X1 (mm) and its filling level is S.
# The equation is solved using normalised fluxes u = S/X1
#
# P is the daily rainfall, E is the daily evapotranspiration.
# infiltrated rainfall : fp(u) = P/X1.(1-u^2)
# actual evapotranspiration : fe(u) = E/X1.u(2-u)
# percolation : fr = -2.25^4/4 u^5
#
fluxes, _ = benchmarks.gr4jprod_fluxes_noscaling()
f1 = lambda u: 1
f2 = lambda u: -u**2
f3 = lambda u: -u*(2-u)
f4 = lambda u: -u**5
fluxes = [f1, f2, f3, f4]

# Definition of interpolation points
nalphas = 20
Expand All @@ -31,7 +51,7 @@

# Loading data from a test site
siteid = "203014"
fd = froot / "data" / "daily" / f"hydrodata_{siteid}.csv"
fd = froot.parent.parent / "data" / "daily" / f"hydrodata_{siteid}.csv"
df, _ = csv.read_csv(fd, index_col=0, parse_dates=True)
nval = len(df)
time = df.index
Expand All @@ -48,20 +68,21 @@
# the 3 flux functions.
X1 = 500
scalings = np.column_stack([rain_intercept/X1, \
rain_intercept/X1, \
evap_intercept/X1, \
np.ones(nval)])
2.25**4/X1*np.ones(nval)])

# Run the model using QuaSoare
s0 = 1./2
niter, s1, fx = models.quad_model(alphas, scalings, \
amat, bmat, cmat, s0, 1.)

sims = np.column_stack([s1*X1, fx[:, 0]*X1, \
-fx[:, 1]*X1, -fx[:, 2]*X1])
sims = np.column_stack([s1*X1, -fx[:, 1]*X1, \
-fx[:, 2]*X1, -fx[:, 3]*X1])

plt.close("all")
fig, axs = plt.subplots(nrows=4, figsize=(15, 10), layout="constrained")
names = ["Store level", "Intercepted Rain", "Actual Evapotranspiration", \
names = ["Store level", "Effective rainfall", "Actual Evapotranspiration", \
"Percolation"]
for iax, ax in enumerate(axs):
ax.plot(time, sims[:, iax])
Expand Down
7 changes: 7 additions & 0 deletions examples/quadroute/compile_unix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rm -f *.o, *.out, *.log, *.csv
gcc -pedantic -Wall -Wextra -g -O3 -c ../../src/pyquasoare/c_quasoare_utils.c
gcc -pedantic -Wall -Wextra -g -O3 -c ../../src/pyquasoare/c_quasoare_core.c
gcc -pedantic -Wall -Wextra -g -O3 -c example_quadroute.c
gcc -pedantic -g -O3 *.o -lm -o example_quadroute.out
./example_quadroute.out

6 changes: 6 additions & 0 deletions examples/quadroute/compile_windows.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
del *.obj, *.exe
cl /W4 %~dp0..\..\src\pyquasoare\c_quasoare_utils.c c_quasoare_utils.obj
cl /W4 %~dp0..\..\src\pyquasoare\c_quasoare_core.c c_quasoare_core.obj
cl /W4 example_quadroute.c example_quadroute.obj
link *.obj /OUT:"example_quadroute.exe"
example_quadroute.exe
33 changes: 27 additions & 6 deletions examples/example.c → examples/quadroute/example_quadroute.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@

#include <stdio.h>
#include "../src/pyquasoare/c_quasoare_utils.h"
#include "../src/pyquasoare/c_quasoare_quad.h"
#include "../../src/pyquasoare/c_quasoare_utils.h"
#include "../../src/pyquasoare/c_quasoare_core.h"

/**
* Code to solve the unit inflow quadratic routing reservoir:
* dS/dt = 1-S^2
*
* The reservoir has 2 flux functions:
* f1(S) = 1 (constant)
* f2(S) = -S^2
*
* The analytical solution of this reservoir is
* S(t) = [s0+tanh(t)]/[1+s0*tanh(t)]
*
* This model can be solved exactly with Quasoare.
*/


int main(){
int nalphas = 4;
Expand All @@ -11,15 +26,19 @@ int main(){
int nfluxes = 2;
double amat[6], bmat[6], cmat[6];

/* Quadratic reservoir ds/dt = 1-s^2 */
/* Interpolation coefficients */
/* .. quadratic terms (only for f2) */
fprintf(stdout, ".. Initialise coefficients\n");
amat[0] = 0; amat[1] = -1.;
amat[2] = 0; amat[3] = -1.;
amat[4] = 0; amat[5] = -1.;

/* .. linear terms (none) */
bmat[0] = 0; bmat[1] = 0;
bmat[2] = 0; bmat[3] = 0;
bmat[4] = 0; bmat[5] = 0;

/* .. constant terms (only for f1) */
cmat[0] = 1; cmat[1] = 0;
cmat[2] = 1; cmat[3] = 0;
cmat[4] = 1; cmat[5] = 0;
Expand All @@ -37,21 +56,23 @@ int main(){
double anl, omega;

/* Print header in result file */
FILE *fp = fopen("example.csv", "w");
fprintf(fp, "time,s1,flux1,flux2,s1_analytical\n");
FILE *fp = fopen("example_quadroute.csv", "w");
fprintf(fp, "time,store,inflow,outflow,store_analytical\n");

/* integrate */
fprintf(stdout, ".. Run model\n");
for(i=0; i<nval; i++){
t1 = t0+timestep*i;
omega = tanh(t1);
anl = (s0+omega)/(1+s0*omega);
c_quad_integrate(nalphas, nfluxes, alphas, scalings,
amat, bmat, cmat, t0, s0, t1,
niter, s1, fluxes);

fprintf(fp, "%0.8f,%0.8f,%0.8f,%0.8f,%0.8f\n",
t1, s1[0], fluxes[0], fluxes[1], anl);
}
fclose(fp);

fprintf(stdout, ".. process completed\n");
return 0;
}
2 changes: 1 addition & 1 deletion examples/run_all_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#----------------------------------------------------------------------
# Get data
#----------------------------------------------------------------------
lf = froot.glob("*.py")
lf = froot.glob("*/*.py")

#----------------------------------------------------------------------
# Process
Expand Down
6 changes: 6 additions & 0 deletions examples/vicmod/compile_unix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rm -f *.o, *.out, *.log, *.csv
gcc -pedantic -Wall -Wextra -g -O3 -c ../../src/pyquasoare/c_quasoare_utils.c
gcc -pedantic -Wall -Wextra -g -O3 -c ../../src/pyquasoare/c_quasoare_core.c
gcc -pedantic -Wall -Wextra -g -O3 -c example_vicmod.c
gcc -pedantic -g -O3 *.o -lm -o example_vicmod.out
./example_vicmod.out
6 changes: 6 additions & 0 deletions examples/vicmod/compile_windows.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
del *.obj, *.exe
cl /W4 %~dp0..\..\src\pyquasoare\c_quasoare_utils.c c_quasoare_utils.obj
cl /W4 %~dp0..\..\src\pyquasoare\c_quasoare_core.c c_quasoare_core.obj
cl /W4 example_vicmod.c example_vicmod.obj
link *.obj /OUT:"example_vicmod.exe"
example_vicmod.exe
Loading

0 comments on commit 74d7786

Please sign in to comment.