From 8257ad0690d35dffacc67f284dfd4059ae482483 Mon Sep 17 00:00:00 2001 From: David Hadka Date: Wed, 4 Sep 2024 21:56:22 +0000 Subject: [PATCH] Add language examples --- .github/workflows/examples.yml | 28 +++++++++++++++++++++++----- .gitignore | 27 ++++++++++++++------------- examples/Languages/C/Makefile | 10 ++++++---- examples/Languages/C/lakeModelInC.py | 6 +++--- examples/Languages/R/lakeModelInR.py | 3 ++- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index ea1e650..11b492f 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -12,11 +12,16 @@ jobs: fail-fast: false matrix: example: [ - "Basic/dps_example.py", - "Basic/example_decorators.py", - "Basic/example_prallelization.py", "Basic/example.py", - "Eijgenraam/eijgenram.py"] + "Basic/example_decorators.py", + "Basic/example_parallelization.py", + "Basic/sensitivity_analysis.py", + "Basic/dps_example.py", + "Eijgenraam/eijgenraam.py", + "Languages/Python/lakeModelInPython.py", + "Languages/R/lakeModelInR.py", + #"Languages/Excel/lakeModelInExcel.py" # requires Windows + "Languages/C/lakeModelInC.py"] steps: - uses: actions/checkout@v4 @@ -34,8 +39,21 @@ jobs: - name: Install dependencies run: | + sudo apt update + suto apt install -y graphviz r-base pip install .[test] - name: Run example run: | - python examples/${{ matrix.example }} + EXAMPLE="examples/${{ matrix.example }}" + EXAMPLE_DIR=$(dirname "${EXAMPLE}") + EXAMPLE_FILE=$(basename "${EXAMPLE}") + + pushd "${EXAMPLE_DIR}" + + if [ -f "Makefile" ]; then + make + fi + + LD_LIBRARY_PATH="$(pwd):${LD_LIBRARY_PATH}" + python "${EXAMPLE_FILE}" diff --git a/.gitignore b/.gitignore index 1f13a36..52e4b23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,19 +1,20 @@ +.eggs/ +.ipynb_checkpoints/ +.settings/ +build/ +dist/ +examples/Languages/C/src/boost_* +Rhodium.egg-info/ .project .pydevproject -*.pyc +*_results.csv +*.cache *.cache.bak *.cache.dat *.cache.dir -.settings/ -.eggs/ -build/ -dist/ -Rhodium.egg-info/ -.ipynb_checkpoints/ -examples/Languages/C/src/boost_1_56_0/ -examples/Basic/*_results.csv +*.dll +*.o +*.pyc +*.so +.Rhistory .DS_Store -libtest.so -test.dll -test.o -rhodium.cache diff --git a/examples/Languages/C/Makefile b/examples/Languages/C/Makefile index ba78f21..21eb9d5 100644 --- a/examples/Languages/C/Makefile +++ b/examples/Languages/C/Makefile @@ -1,14 +1,16 @@ CPP = g++ SRC_DIR = src TARGET = lake.so -CPP_FLAGS = -m64 -O3 -Wno-unused-local-typedefs -I$(SRC_DIR) -I$(SRC_DIR)/boost_1_56_0 +CPP_FLAGS = -m64 -fPIC -O3 -Wno-unused-local-typedefs -I$(SRC_DIR) -I$(SRC_DIR)/boost_1_56_0 all: if [ ! -d "$(SRC_DIR)/boost_1_56_0" ]; \ then \ - wget http://sourceforge.net/projects/boost/files/boost/1.56.0/boost_1_56_0.zip; \ - unzip boost_1_56_0.zip; \ + cd "$(SRC_DIR)"; \ + wget "http://sourceforge.net/projects/boost/files/boost/1.56.0/boost_1_56_0.zip"; \ + unzip "boost_1_56_0.zip"; \ + cd ..; \ fi - $(CPP) $(CPP_FLAGS) -o $(TARGET) -shared -fPIC $(SRC_DIR)/main-lake.cpp + $(CPP) $(CPP_FLAGS) -o $(TARGET) -shared $(SRC_DIR)/main-lake.cpp diff --git a/examples/Languages/C/lakeModelInC.py b/examples/Languages/C/lakeModelInC.py index f352b4a..9bf4f05 100644 --- a/examples/Languages/C/lakeModelInC.py +++ b/examples/Languages/C/lakeModelInC.py @@ -1,9 +1,9 @@ +import sys from rhodium import * from rhodium.ffi import NativeModel -# Provide the DLL/SO file and the function name. The appropriate extension, -# such as .dll or .so, will be automatically added. -model = NativeModel("lake", "lake_problem") +# Provide the DLL/SO file and the function name. +model = NativeModel("lake.so" if sys.platform == "linux" else "lake.dll", "lake_problem") # List the inputs. The order matters! model.parameters = [Parameter("pollution_limit", type="double*"), diff --git a/examples/Languages/R/lakeModelInR.py b/examples/Languages/R/lakeModelInR.py index e12732b..096ede0 100644 --- a/examples/Languages/R/lakeModelInR.py +++ b/examples/Languages/R/lakeModelInR.py @@ -2,7 +2,8 @@ from rhodium.rbridge import RModel # Provide the R file and function name -model = RModel("lake.R", "lake.eval", RCMD=r"C:\Program Files\R\R-3.2.1\bin\R.exe") +# TIP: If unable to locate R, the path can be set with RCMD=r"C:\Program Files\R\R-3.2.1\bin\R.exe" +model = RModel("lake.R", "lake.eval") # The parameter names must match the R arguments exactly model.parameters = [Parameter("pollution_limit"),