Skip to content

Support forest vertical profiles in runpointmodel with fast C++ implementation#8

Open
JohannaTrost wants to merge 6 commits intoilyamaclean:mainfrom
JohannaTrost:cpp-time-z2
Open

Support forest vertical profiles in runpointmodel with fast C++ implementation#8
JohannaTrost wants to merge 6 commits intoilyamaclean:mainfrom
JohannaTrost:cpp-time-z2

Conversation

@JohannaTrost
Copy link

@JohannaTrost JohannaTrost commented Jul 11, 2025

Hi Ilya,

I’ve added new C++ functions to simulate microclimate along the forest profile (i.e., vertically across canopy height). This was originally developed to speed up my own use case, but I thought it might be useful for others as well, so I'm sharing it here :)

Usage

You can now pass a vector of heights to runpointmodel() via the reqhgt argument, instead of a single value. For example:

heights <- seq(0.5, length(paii))
pmout <- micropoint::runpointmodel(
  climdata, 
  reqhgt = heights, 
  vegp,
  paii, 
  groundp, 
  lat = -22.39573, 
  long = -42.73781
) 

The output is a named list:

> names(pmout)
[1] "tair"      "relhum"    "windspeed" "Rdirdown"  "Rdifdown"  "Rlwdown"   "Rswup"     "Rlwup"     "heights"

Each element is a 2D array with dimensions number of time steps × number of heights.

New C++ functions

The following C++ functions have been added:

  • runmodelProfile
    Called from runpointmodel() in R. It calls AboveCanopyProfile for heights above the vegetation canopy (vegp[1]), and BelowCanopyProfile for those below. Results are combined into a single output list.

  • AboveCanopyProfile
    Similar to AboveCanopy, but extended to handle multiple heights above the canopy across all time steps.

  • BelowCanopyProfile
    Similar to BelowCanopy, but for heights below the canopy, also across all time steps.

Performance improvement

The new implementation significantly improves runtime. Benchmarks show it's 66× faster than looping over heights using the original runpointmodel.

For 58 heights and 8784 timesteps:

  • New implementation: ~2.92 seconds
  • Old implementation: ~193.95 seconds

Of note, performance could be improved further, e.g., by parallelising the main loop in the C++ code.

Known Issue: Small discrepancies in Microclimate values

The example below compares the new implementation with the previous one (computing microclimate for each height separately). We can observe that the simulations are not identical, although differences seem to be small. So far I haven't found the cause for these discrepancies.

test_profile_tair_old_vs_new_comp_13_6_v2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant