Skip to content

Commit

Permalink
Merge pull request #116 from canmod/refactorcpp
Browse files Browse the repository at this point in the history
Refactorcpp
  • Loading branch information
stevencarlislewalker authored Jan 18, 2024
2 parents bdc323d + 6884145 commit 097a4e0
Show file tree
Hide file tree
Showing 427 changed files with 29,095 additions and 6,036 deletions.
14 changes: 14 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ LICENSE
^doc$
^Meta$
drat/
.*\.vscode

^vignettes/calibration[.]Rmd$
^vignettes/example_models[.]Rmd$
^vignettes/flow_types[.]Rmd$
^vignettes/hello_products[.]Rmd$
^vignettes/model_definitions[.]Rmd$
^vignettes/quickstart2[.]Rmd$
^vignettes/quickstart_products[.]Rmd$
^vignettes/sir_radial_basis_transmission[.]Rmd$
^vignettes/time_varying_parameters[.]Rmd$
## should we ignore tmb_model.Rmd as well ... ??

.*/html/.*
6 changes: 4 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ jobs:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
# - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
# - {os: macos-latest, r: 'release'}
# - {os: windows-latest, r: 'release'}
# - {os: ubuntu-latest, r: 'release'}
# - {os: ubuntu-latest, r: 'oldrel-1'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true

steps:
- uses: actions/checkout@v3
Expand All @@ -48,6 +49,7 @@ jobs:
with:
extra-packages: any::rcmdcheck
needs: check
upgrade: 'TRUE'

- uses: r-lib/actions/check-r-package@v2
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.RData
.Ruserdata
inst/doc
hoard
*.o
*.so
*.DS_Store
Expand Down
48 changes: 48 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Contributing to `macpan2`

Thank you for contributing to `macpan2`. Pull requests and issues are welcome!

Developers can see [here](https://canmod.github.io/macpan2/articles/index.html#developer) for documentation useful those who will contribute code.

## Developer Installation

Developers and contributors should clone this repository and call `make` at the command-line in the top level directory. The following `make` rules are available for getting more control over the build process.

```
make quick-install # for changes that only modify R source
make quick-doc-install # for changes that modify R source and roxygen comments
make quick-test # quick-doc-install + run-examples + run-tests
make run-examples # help file checks only (without package rebuild)
make run-tests # run scripts in tests (without package rebuild)
make full-install # for all changes, including changes to C++ source
make src-update # push changes to dev.cpp to macpan2.cpp (see below)
make enum-update # register new C++ engine functions on the R-side
make engine-doc-update # generate roxygen comments from comments in dev.cpp
make doc-update # roxygenize
make pkg-build # build the R package
make pkg-install # install the R package from the build
make pkg-check # R package checks
```

## `C++` Development

In most R packages with compiled code, developers edit the source files to be compiled in the `src` directory. In `macpan2` there is a single file in that directory called `macpan2`, which is generated automatically from the file `misc/dev/dev.cpp`. This setup allows for quicker C++ development cycles, because developers can edit `misc/dev/dev.cpp` and then use this file in tests without needing to re-install the package with the new source. In particular, the above hello-world example could use `dev.cpp` as follows.

```
library(macpan2)
macpan2:::dev_compile() ## compile dev.cpp
sir = Compartmental(system.file("starter_models", "sir", package = "macpan2"))
N = 100
simulator = sir$simulators$tmb(time_steps = 100
, state = c(S = N - 1, I = 1, R = 0)
, flow = c(foi = 0, gamma = 0.1)
, N = N
, beta = 0.2
, .tmb_cpp = "dev" ## use dev.cpp
)
sir_sims = simulator$report()
```

To update `src/macpan2` to the state of `misc/dev/dev.cpp` one may run `make src-update`.

Running with `misc/dev/dev.cpp` will print out debugging information in a verbose manner, whereas `src/macpan2.cpp` will not. The `src-update` make rule removes the `#define MP_VERBOSE` flag at the top of the file.
19 changes: 11 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
Package: macpan2
Title: Fast and Flexible Compartmental Modelling
Version: 0.0.3
Version: 1.0.0
Authors@R: c(
person("Steve Walker", email="swalk@mcmaster.ca", role=c("cre", "aut")),
person("Darren Flynn-Primrose", role="aut"),
person("Irena Papst", role="ctb"),
person("Michael Li", role="ctb"),
person("Weiguang Guan", role="aut"),
person("Ben Bolker", role="aut"),
person("Michael Li", role="ctb"),
person("Irena Papst", role="ctb")
)
person("Jen Freeman", role="aut"),
person("Darren Flynn-Primrose", role="aut")
)
Description: Fast and flexible compartmental modelling with Template Model Builder.
License: GPL-3
Depends:
Expand All @@ -17,7 +18,8 @@ Imports:
TMB,
oor,
jsonlite,
MASS
MASS,
memoise
Suggests:
covr,
knitr,
Expand All @@ -36,7 +38,8 @@ Suggests:
numDeriv,
parallel
Remotes:
canmod/oor
canmod/oor,
canmod/macpan2helpers
LinkingTo:
TMB,
RcppEigen,
Expand All @@ -47,5 +50,5 @@ Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
URL: https://canmod.github.io/macpan2/, https://github.com/canmod/macpan2
103 changes: 82 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ SED_RE = \(\,\)*[ ]*\/\/[ ]*\(.*\)
ALIAS_RE = [ ]*MP2_\(.*\)\: \(.*\)(\(.*\))
ROXY_RE = ^.*\(\#'.*\)$
VERSION := $(shell sed -n '/^Version: /s///p' DESCRIPTION)
TEST := testthat::test_package(\"macpan2\", reporter = \"progress\")


all:
make src-update
make enum-update
make engine-doc-update
make doc-update
make pkg-build
make pkg-install
make full-install
make pkg-check


install-deps:
Rscript -e "remotes::install_github('canmod/oor@validity')"


# Use this rule if you are doing R development or want to promote
# dev.cpp to macpan2.cpp before doing R development.
full-install:
make src-update
make enum-update
make enum-meth-update
make engine-doc-update
make doc-update
make pkg-build
Expand All @@ -29,21 +31,31 @@ full-install:
# Use this rule if (1) you are in a development cycle, (2) you
# haven't updated macpan.cpp (but have perhaps modified dev.cpp)
# and (3) do not require a roxygen update.
quick-install: enum-update
quick-install: enum-update enum-meth-update
R CMD INSTALL --no-multiarch --install-tests .

install-deps:
Rscript -e "remotes::install_github('canmod/oor')"

quick-doc-install: R/*.R misc/dev/dev.cpp
make engine-doc-update
make doc-update
make quick-install


quick-test:
forced-quick-doc-install:
touch misc/old-r-source/*.R
make doc-update
make quick-install


quick-test-all:
make quick-doc-install
make run-vignette-code
make run-tests
make run-examples


quick-test:
make quick-doc-install
make run-tests


Expand All @@ -52,14 +64,52 @@ run-examples:


run-tests:
Rscript -e "library(macpan2); testthat::test_package(\"macpan2\")"
Rscript -e "library(macpan2); $(TEST)"


run-vignette-code:
cd vignettes
Rscript misc/build/run_vignette_code.R


coverage-report:: coverage.html
coverage.html: R/*.R src/macpan2.cpp tests/testthat/*.R
Rscript -e "covr::report(file = \"coverage.html\")"


misc/dev/%.run: misc/dev/%.R
cd misc/dev; Rscript ../../$^


misc/**/%.svg: misc/**/%.drawio
draw.io --export $^ --format svg


misc/**/%.png: misc/**/%.drawio
draw.io --export $^ --format png


svg-readme:: misc/readme/*.svg
png-readme:: misc/readme/*.png


readme:: README.md
README.md: README.Rmd misc/readme/*.svg R/*.R NAMESPACE
Rscript -e "rmarkdown::render('README.Rmd')"
echo '<!-- Auto-generated - do not edit by hand -->' > temp
echo '<!-- Edit README.Rmd instead -->' | cat - $@ >> temp && mv temp $@


push-readme:
make README.md
git add misc/readme/*.svg
git add misc/readme/*.png
git add misc/readme/*.drawio
git add README.md README.Rmd
git commit -m "update readme" || true
git push || true


enum-update:: R/enum.R
R/enum.R: misc/dev/dev.cpp misc/build/enum_tail.R
echo "## Auto-generated - do not edit by hand" > $@
Expand All @@ -70,6 +120,11 @@ R/enum.R: misc/dev/dev.cpp misc/build/enum_tail.R
echo "valid_funcs = setNames(as.list(valid_funcs), valid_funcs)" >> $@


enum-meth-update:: R/enum_methods.R
R/enum_methods.R: misc/dev/dev.cpp misc/build/method_head.R misc/build/build_from_enum_methods.R
Rscript misc/build/build_from_enum_methods.R


src-update:: src/macpan2.cpp
src/macpan2.cpp: misc/dev/dev.cpp
echo "// Auto-generated - do not edit by hand" > $@
Expand All @@ -86,26 +141,32 @@ R/engine_functions.R: src/macpan2.cpp
echo "NULL" >> $@


doc-update: R/*.R misc/dev/dev.cpp
doc-update: R/*.R misc/dev/dev.cpp misc/old-r-source/*.R
echo "suppressWarnings(roxygen2::roxygenize(\".\",roclets = c(\"collate\", \"rd\", \"namespace\")))" | R --slave
touch doc-update


pkg-build:: macpan2_$(VERSION).tar.gz
macpan2_$(VERSION).tar.gz: DESCRIPTION man/*.Rd R/*.R src/*.cpp tests/testthat/test-*.R tests/testthat.R inst/starter_models/**/*.csv inst/starter_models/**/*.json
R CMD build .
pkg-build:: ../macpan2_$(VERSION).tar.gz
../macpan2_$(VERSION).tar.gz: DESCRIPTION man/*.Rd R/*.R src/*.cpp tests/testthat/test-*.R tests/testthat.R inst/starter_models/**/*.csv inst/starter_models/**/*.json doc-update
cd .. && R CMD build --no-build-vignettes macpan2
touch pkg-build


pkg-check: macpan2_$(VERSION).tar.gz
R CMD check macpan2_$(VERSION).tar.gz
pkg-check: ../macpan2_$(VERSION).tar.gz pkg-build
cd .. && R CMD check macpan2_$(VERSION).tar.gz
touch pkg-check


pkg-install: macpan2_$(VERSION).tar.gz
R CMD INSTALL --no-multiarch --install-tests macpan2_$(VERSION).tar.gz
pkg-install: ../macpan2_$(VERSION).tar.gz pkg-build
cd .. && R CMD INSTALL --no-multiarch --install-tests macpan2_$(VERSION).tar.gz


compile-dev: misc/dev/dev.cpp
cd misc/dev; echo "TMB::compile(\"dev.cpp\")" | R --slave


misc/dev/%.run: misc/dev/%.R
cd misc/dev; Rscript ../../$^
inst/model_library/%/README.md: inst/model_library/%/README.Rmd
echo "rmarkdown::render(input = \"$^\", intermediates_dir = NULL)" | R --slave
cat $(dir $@)/header.yaml $(dir $@)/README.md > $(dir $@)/tmp.md
cp $(dir $@)/tmp.md $(dir $@)/README.md
rm $(dir $@)/tmp.md
Loading

0 comments on commit 097a4e0

Please sign in to comment.