Skip to content

Commit

Permalink
format C scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Feb 22, 2024
1 parent 38d81bf commit c2957eb
Show file tree
Hide file tree
Showing 31 changed files with 3,686 additions and 3,849 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ Suggests:
LinkingTo:
RcppArmadillo,
Rcpp
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export(vic_params)
export(vic_version)
import(magrittr)
importFrom(Rcpp,sourceCpp)
importFrom(foreach,"%do%")
importFrom(foreach,foreach)
importFrom(lubridate,day)
importFrom(lubridate,month)
importFrom(lubridate,year)
Expand Down
9 changes: 9 additions & 0 deletions R/VIC5-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @docType package
#' @importFrom Rcpp sourceCpp
#' @importFrom stats setNames convolve median
#' @importFrom foreach %do% foreach
#' @importFrom utils str read.table
#' @keywords internal
"_PACKAGE"
Expand All @@ -13,3 +14,11 @@
## usethis namespace: start
## usethis namespace: end
NULL

.onLoad <- function (libname, pkgname){
if(getRversion() >= "2.15.1") {
utils::globalVariables(
c(".", "i")
)
}
}
2 changes: 1 addition & 1 deletion R/tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ set_dimnames <- function(x, value) {

check_matrix <- function(x) {
if (is.vector(x)) {
lake %<>% t()
x %<>% t()
} else if (!is.null(x)) {
x %<>% as.matrix()
}
Expand Down
6 changes: 3 additions & 3 deletions R/vic_forcing.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ check_forcing <- function(forcing, soil = NULL) {
ngrid = ncol(forcing[[1]])
# Estimate forcing data that not supplied.
if ("PRESS" %in% forc_lack) {
forcing$PRESS = foreach(i = 1:ngrid, icount(), .c = cbind) %do% {
cal_Pa(frocing$TEMP[, i], elev = soil[, "ELEV"])
forcing$PRESS = foreach(i = 1:ngrid, .c = cbind) %do% {
cal_Pa(forcing$TEMP[, i], elev = soil[, "ELEV"])
}
}

if ("LW" %in% forc_lack) {
J <- get_J()
forcing$LW = foreach(i = 1:ngrid, icount(), .c = cbind) %do% {
forcing$LW = foreach(i = 1:ngrid, .c = cbind) %do% {
lat = soil[i, "LAT"]
cal_lw(forcing$TEMP[, i], forcing$VP[, i], forcing$SW[, i], lat, J)
}
Expand Down
2 changes: 1 addition & 1 deletion man/VIC5.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Makevars
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ PKG_CPPFLAGS = $(HDRS) \

SOURCES_C = vic/vic_run/src/advected_sensible_heat.c vic/vic_run/src/alloc_and_free.c vic/vic_run/src/arno_evap.c vic/vic_run/src/calc_atmos_energy_bal.c vic/vic_run/src/calc_gridcell_avg_albedo.c vic/vic_run/src/calc_Nscale_factors.c vic/vic_run/src/calc_rainonly.c vic/vic_run/src/calc_snow_coverage.c vic/vic_run/src/calc_surf_energy_bal.c vic/vic_run/src/calc_veg_params.c vic/vic_run/src/CalcAerodynamic.c vic/vic_run/src/CalcBlowingSnow.c vic/vic_run/src/canopy_assimilation.c vic/vic_run/src/canopy_evap.c vic/vic_run/src/comparisons.c vic/vic_run/src/compute_coszen.c vic/vic_run/src/compute_derived_lake_dimensions.c vic/vic_run/src/compute_pot_evap.c vic/vic_run/src/compute_soil_resp.c vic/vic_run/src/compute_zwt.c vic/vic_run/src/correct_precip.c vic/vic_run/src/estimate_T1.c vic/vic_run/src/faparl.c vic/vic_run/src/frozen_soil.c vic/vic_run/src/func_atmos_energy_bal.c vic/vic_run/src/func_atmos_moist_bal.c vic/vic_run/src/func_canopy_energy_bal.c vic/vic_run/src/func_surf_energy_bal.c vic/vic_run/src/ice_melt.c vic/vic_run/src/IceEnergyBalance.c vic/vic_run/src/initialize_lake.c vic/vic_run/src/interpoloation.c vic/vic_run/src/lake_utils.c vic/vic_run/src/lakes.eb.c vic/vic_run/src/latent_heat_from_snow.c vic/vic_run/src/massrelease.c vic/vic_run/src/newt_raph_func_fast.c vic/vic_run/src/penman.c vic/vic_run/src/photosynth.c vic/vic_run/src/physics.c vic/vic_run/src/prepare_full_energy.c vic/vic_run/src/root_brent.c vic/vic_run/src/runoff.c vic/vic_run/src/snow_intercept.c vic/vic_run/src/snow_melt.c vic/vic_run/src/snow_utility.c vic/vic_run/src/SnowPackEnergyBalance.c vic/vic_run/src/soil_carbon_balance.c vic/vic_run/src/soil_conduction.c vic/vic_run/src/soil_thermal_eqn.c vic/vic_run/src/solve_snow.c vic/vic_run/src/StabilityCorrection.c vic/vic_run/src/surface_fluxes.c vic/vic_run/src/svp.c vic/vic_run/src/vic_run.c vic/vic_run/src/water_energy_balance.c vic/vic_run/src/water_under_ice.c vic/vic_run/src/write_layer.c vic/vic_run/src/write_vegvar.c vic/drivers/shared_all/src/agg_data.c vic/drivers/shared_all/src/alarms.c vic/drivers/shared_all/src/calc_root_fraction.c vic/drivers/shared_all/src/cmd_proc.c vic/drivers/shared_all/src/compress_files.c vic/drivers/shared_all/src/compute_derived_state_vars.c vic/drivers/shared_all/src/compute_lake_params.c vic/drivers/shared_all/src/compute_treeline.c vic/drivers/shared_all/src/forcing_utils.c vic/drivers/shared_all/src/free_all_vars.c vic/drivers/shared_all/src/free_vegcon.c vic/drivers/shared_all/src/generate_default_lake_state.c vic/drivers/shared_all/src/generate_default_state.c vic/drivers/shared_all/src/get_parameters.c vic/drivers/shared_all/src/history_metadata.c vic/drivers/shared_all/src/initialize_energy.c vic/drivers/shared_all/src/initialize_global.c vic/drivers/shared_all/src/initialize_options.c vic/drivers/shared_all/src/initialize_parameters.c vic/drivers/shared_all/src/initialize_snow.c vic/drivers/shared_all/src/initialize_soil.c vic/drivers/shared_all/src/initialize_veg.c vic/drivers/shared_all/src/input_tools.c vic/drivers/shared_all/src/make_all_vars.c vic/drivers/shared_all/src/make_cell_data.c vic/drivers/shared_all/src/make_dmy.c vic/drivers/shared_all/src/make_energy_bal.c vic/drivers/shared_all/src/make_snow_data.c vic/drivers/shared_all/src/make_veg_var.c vic/drivers/shared_all/src/open_file.c vic/drivers/shared_all/src/print_library_shared.c vic/drivers/shared_all/src/put_data.c vic/drivers/shared_all/src/set_output_defaults.c vic/drivers/shared_all/src/soil_moisture_from_water_table.c vic/drivers/shared_all/src/timing.c vic/drivers/shared_all/src/update_step_vars.c vic/drivers/shared_all/src/vic_history.c vic/drivers/shared_all/src/vic_log.c vic/drivers/shared_all/src/vic_time.c vic/drivers/shared_all/src/zero_output_list.c

SOURCES_CPP = ./conv.cpp ./display_current_settings.cpp ./force.cpp ./get_options.cpp ./globals.cpp ./initiate.cpp ./make_output_info.cpp ./make_params.cpp ./RcppExports.cpp ./vic_run_cell.cpp ./vic_version.cpp vic_run_cells_all.cpp XAJ.cpp
SOURCES_CPP = ./conv.cpp ./force.cpp ./get_options.cpp ./globals.cpp ./initiate.cpp ./make_output_info.cpp ./make_params.cpp ./RcppExports.cpp ./vic_run_cell.cpp ./vic_version.cpp vic_run_cells_all.cpp XAJ.cpp
# profile.cpp

OBJECTS = $(SOURCES_C:.c=.o) $(SOURCES_CPP:.cpp=.o)
Expand Down
4 changes: 0 additions & 4 deletions src/display_current_settings.cpp

This file was deleted.

153 changes: 71 additions & 82 deletions src/force.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#include <vic_R.h>

void make_force(force_data_struct &force, NumericMatrix &forcing_data,
soil_con_struct* soil_con, int rec, dmy_struct *dmy) {

extern option_struct options;
//extern global_param_struct global_param;
extern parameters_struct param;
extern size_t NR, NF;
soil_con_struct *soil_con, int rec, dmy_struct *dmy) {
extern option_struct options;
// extern global_param_struct global_param;
extern parameters_struct param;
extern size_t NR, NF;

int offset;
double t_offset;
size_t i;
size_t uidx;
double *Tfactor;
int offset;
double t_offset;
size_t i;
size_t uidx;
double *Tfactor;

Tfactor = soil_con -> Tfactor;
Tfactor = soil_con->Tfactor;
t_offset = Tfactor[0];
for (i = 1; i < options.SNOW_BAND; i++) {
if (Tfactor[i] < t_offset) {
Expand Down Expand Up @@ -45,35 +44,34 @@ void make_force(force_data_struct &force, NumericMatrix &forcing_data,
}
// air density in kg/m3
force.density[i] = air_density(force.air_temp[i],
force.pressure[i]);
force.pressure[i]);
// wind speed in m/s
force.wind[i] = forcing_data(uidx, 6);
// snow flag
force.snowflag[i] = will_it_snow(&(force.air_temp[i]),
t_offset,
param.SNOW_MAX_SNOW_TEMP,
&(force.prec[i]), 1);
t_offset,
param.SNOW_MAX_SNOW_TEMP,
&(force.prec[i]), 1);

// Optional inputs
if (options.LAKES) {
// Channel inflow from upstream (into lake)
// if (param_set.TYPE[CHANNEL_IN].SUPPLIED) {
if (false) { // TODO: for channel in.
offset ++;
force.channel_in[i] = forcing_data(uidx, 6+offset);
}
else {
if (false) { // TODO: for channel in.
offset++;
force.channel_in[i] = forcing_data(uidx, 6 + offset);
} else {
force.channel_in[i] = 0;
}
}
if (options.CARBON) {
offset ++;
offset++;
// Atmospheric CO2 concentration
force.Catm[i] = forcing_data(uidx, 6+offset);
force.Catm[i] = forcing_data(uidx, 6 + offset);
// Fraction of shortwave that is direct
force.fdir[i] = forcing_data(uidx, 7+offset);
force.fdir[i] = forcing_data(uidx, 7 + offset);
// photosynthetically active radiation
force.par[i] = forcing_data(uidx, 8+offset);
force.par[i] = forcing_data(uidx, 8 + offset);
// Cosine of solar zenith angle
force.coszen[i] = compute_coszen(soil_con->lat,
soil_con->lng,
Expand Down Expand Up @@ -102,7 +100,7 @@ void make_force(force_data_struct &force, NumericMatrix &forcing_data,
}
if (options.LAKES) {
force.channel_in[NR] =
average(force.channel_in, NF) * NF;
average(force.channel_in, NF) * NF;
}
if (options.CARBON) {
force.Catm[NR] = average(force.Catm, NF);
Expand All @@ -119,35 +117,32 @@ void make_force(force_data_struct &force, NumericMatrix &forcing_data,
}

IntegerVector get_veg_force_types(NumericMatrix &forcing_veg_data) {
//extern option_struct options;

CharacterVector veg_par_types;
int Ntypes;
// extern option_struct options;

IntegerVector temp;
CharacterVector veg_par_types;
int Ntypes;

if(is<CharacterVector>(forcing_veg_data.attr("types"))) {
IntegerVector temp;

if (is<CharacterVector>(forcing_veg_data.attr("types"))) {
veg_par_types = as<CharacterVector>(forcing_veg_data.attr("types"));
Ntypes = veg_par_types.length();
temp = IntegerVector(Ntypes, -1);

for(int i = 0; i < Ntypes; i++) {
for (int i = 0; i < Ntypes; i++) {
if (veg_par_types[i] == "albedo") {
temp[i] = 0;
}
else if (veg_par_types[i] == "LAI") {
} else if (veg_par_types[i] == "LAI") {
temp[i] = 1;
}
else if (veg_par_types[i] == "fcanopy") {
} else if (veg_par_types[i] == "fcanopy") {
temp[i] = 2;
}
else {
log_err("Invalid vegetation forcing data type:"
" %s.", ((String)veg_par_types[i]).get_cstring());
} else {
log_err(
"Invalid vegetation forcing data type:"
" %s.",
((String)veg_par_types[i]).get_cstring());
}
}

}

return temp;
Expand All @@ -156,19 +151,18 @@ IntegerVector get_veg_force_types(NumericMatrix &forcing_veg_data) {
void make_force_veg(NumericMatrix &forcing_veg_data,
IntegerVector &veg_force_types,
all_vars_struct *all_vars,
veg_con_struct *veg_con,
veg_con_struct *veg_con,
int rec, dmy_struct *dmy) {

extern option_struct options;

unsigned short iveg;
size_t Nveg;
unsigned short band;
size_t Nbands;
veg_var_struct **veg_var;
CharacterVector veg_par_types;
int Ntypes;
double tmp_veg_val;
unsigned short iveg;
size_t Nveg;
unsigned short band;
size_t Nbands;
veg_var_struct **veg_var;
CharacterVector veg_par_types;
int Ntypes;
double tmp_veg_val;

veg_var = all_vars->veg_var;

Expand All @@ -179,78 +173,74 @@ void make_force_veg(NumericMatrix &forcing_veg_data,
for (iveg = 0; iveg <= Nveg; iveg++) {
for (band = 0; band < Nbands; band++) {
veg_var[iveg][band].albedo =
veg_con[iveg].albedo[dmy[rec].month - 1];
veg_con[iveg].albedo[dmy[rec].month - 1];
veg_var[iveg][band].displacement =
veg_con[iveg].displacement[dmy[rec].month - 1];
veg_con[iveg].displacement[dmy[rec].month - 1];
veg_var[iveg][band].fcanopy =
veg_con[iveg].fcanopy[dmy[rec].month - 1];
veg_con[iveg].fcanopy[dmy[rec].month - 1];
veg_var[iveg][band].LAI =
veg_con[iveg].LAI[dmy[rec].month - 1];
veg_con[iveg].LAI[dmy[rec].month - 1];
veg_var[iveg][band].roughness =
veg_con[iveg].roughness[dmy[rec].month - 1];
veg_con[iveg].roughness[dmy[rec].month - 1];
}
}

Ntypes = veg_force_types.length();
for (iveg = 0; iveg <= Nveg; iveg++) {
for (band = 0; band < Nbands; band++) {
for(int i = 0; i < Ntypes; i++) {
for (int i = 0; i < Ntypes; i++) {
tmp_veg_val = forcing_veg_data(rec, iveg + i * Nveg);

if(veg_force_types[i] == 0 && options.ALB_SRC == FROM_VEGHIST) {
if (veg_force_types[i] == 0 && options.ALB_SRC == FROM_VEGHIST) {
veg_var[iveg][band].albedo = tmp_veg_val;
}
else if(veg_force_types[i] == 1 && options.LAI_SRC == FROM_VEGHIST) {
} else if (veg_force_types[i] == 1 && options.LAI_SRC == FROM_VEGHIST) {
veg_var[iveg][band].LAI = tmp_veg_val;
}
else if(veg_force_types[i] == 2 && options.FCAN_SRC == FROM_VEGHIST) {
} else if (veg_force_types[i] == 2 && options.FCAN_SRC == FROM_VEGHIST) {
veg_var[iveg][band].fcanopy = tmp_veg_val;
}
}
}
}

}

void alloc_force(force_data_struct &force)
{
void alloc_force(force_data_struct &force) {
extern option_struct options;
extern size_t NR;

force.air_temp = (double*)calloc(NR + 1, sizeof(force.air_temp));
force.air_temp = (double *)calloc(NR + 1, sizeof(force.air_temp));
check_alloc_status(force.air_temp, "Memory allocation error.");
force.density = (double*)calloc(NR + 1, sizeof(force.density));
force.density = (double *)calloc(NR + 1, sizeof(force.density));
check_alloc_status(force.density, "Memory allocation error.");
force.longwave = (double*)calloc(NR + 1, sizeof(force.longwave));
force.longwave = (double *)calloc(NR + 1, sizeof(force.longwave));
check_alloc_status(force.longwave, "Memory allocation error.");
force.prec = (double*)calloc(NR + 1, sizeof(force.prec));
force.prec = (double *)calloc(NR + 1, sizeof(force.prec));
check_alloc_status(force.prec, "Memory allocation error.");
force.pressure = (double*)calloc(NR + 1, sizeof(force.pressure));
force.pressure = (double *)calloc(NR + 1, sizeof(force.pressure));
check_alloc_status(force.pressure, "Memory allocation error.");
force.shortwave = (double*)calloc(NR + 1, sizeof(force.shortwave));
force.shortwave = (double *)calloc(NR + 1, sizeof(force.shortwave));
check_alloc_status(force.shortwave, "Memory allocation error.");
force.snowflag = (bool*)calloc(NR + 1, sizeof(force.snowflag));
force.snowflag = (bool *)calloc(NR + 1, sizeof(force.snowflag));
check_alloc_status(force.snowflag, "Memory allocation error.");
force.vp = (double*)calloc(NR + 1, sizeof(force.vp));
force.vp = (double *)calloc(NR + 1, sizeof(force.vp));
check_alloc_status(force.vp, "Memory allocation error.");
force.vpd = (double*)calloc(NR + 1, sizeof(force.vpd));
force.vpd = (double *)calloc(NR + 1, sizeof(force.vpd));
check_alloc_status(force.vpd, "Memory allocation error.");
force.wind = (double*)calloc(NR + 1, sizeof(force.wind));
force.wind = (double *)calloc(NR + 1, sizeof(force.wind));
check_alloc_status(force.wind, "Memory allocation error.");
if (options.LAKES) {
force.channel_in =
(double*)calloc(NR + 1, sizeof(force.channel_in));
(double *)calloc(NR + 1, sizeof(force.channel_in));
check_alloc_status(force.channel_in,
"Memory allocation error.");
}
if (options.CARBON) {
force.Catm = (double*)calloc(NR + 1, sizeof(force.Catm));
force.Catm = (double *)calloc(NR + 1, sizeof(force.Catm));
check_alloc_status(force.Catm, "Memory allocation error.");
force.coszen = (double*)calloc(NR + 1, sizeof(force.coszen));
force.coszen = (double *)calloc(NR + 1, sizeof(force.coszen));
check_alloc_status(force.coszen, "Memory allocation error.");
force.fdir = (double*)calloc(NR + 1, sizeof(force.fdir));
force.fdir = (double *)calloc(NR + 1, sizeof(force.fdir));
check_alloc_status(force.fdir, "Memory allocation error.");
force.par = (double*)calloc(NR + 1, sizeof(force.par));
force.par = (double *)calloc(NR + 1, sizeof(force.par));
check_alloc_status(force.par, "Memory allocation error.");
}
}
Expand Down Expand Up @@ -278,4 +268,3 @@ void free_force(force_data_struct &force) {
free(force.par);
}
}

Loading

0 comments on commit c2957eb

Please sign in to comment.