Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/NREL/ssc into sam_181_in…
Browse files Browse the repository at this point in the history
…terconnection_limits
  • Loading branch information
brtietz committed Oct 30, 2024
2 parents 8be7ef0 + 1750ef9 commit 5c32c64
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 119 deletions.
157 changes: 102 additions & 55 deletions shared/lib_irradproc.cpp

Large diffs are not rendered by default.

108 changes: 49 additions & 59 deletions shared/lib_irradproc.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,48 @@ double rts_sun_altitude(double latitude, double delta_prime, double h_prime);
double sun_rise_and_set(double* m_rts, double* h_rts, double* delta_prime, double latitude,
double* h_prime, double h0_prime, int sun);

/**
* Table for storing the recently computed solarpos_spa intermediate outputs
* The algorithm reuses the outputs from the last 3 days or so, so the hash table is emptied every 3 days to reduce size
* Latitude, Longitude, Altitude, Tilt and Azimuth are not in the key because they remain constant throughout
*/

struct spa_table_key {
double jd;
double delta_t;
int pressure;
int temp;
// these are both inputs and outputs (e.g. also stored in the output vector)
double ascension;
double declination;

bool operator==(const spa_table_key &other) const;

spa_table_key(double j, double dt, double p, double t, double a, double d);
};

template <>
struct std::hash<spa_table_key>
{
std::size_t operator()(const spa_table_key& k) const;
};

class solarpos_lookup
{
std::unordered_map<spa_table_key, std::vector<double>> spa_table;
int spa_table_day;
void clear_spa_table();

public:

solarpos_lookup();
void roll_spa_table_forward(int day);

std::vector<double>* find(spa_table_key spa_key_inputs);
void insert(spa_table_key spa_key_inputs, std::vector<double> spa_outputs);
};


/**
* calculate_spa function combines numerous functions to calculate the Solar Position Algorithm parameters
*
Expand Down Expand Up @@ -629,66 +671,10 @@ double sun_rise_and_set(double* m_rts, double* h_rts, double* delta_prime, doubl
* \param[out] needed_values[8] azimuth topocentric azimuth angle (degrees)
*/


void calculate_spa(double jd, double lat, double lng, double alt, double pressure, double temp,
double delta_t, double tilt, double azm_rotation, double ascension_and_declination[2], double needed_values[9]);
double delta_t, double tilt, double azm_rotation, double ascension_and_declination[2], double needed_values[9],
std::shared_ptr<solarpos_lookup> spa_table=nullptr);

/**
* Table for storing the recently computed solarpos_spa intermediate outputs
* The algorithm reuses the outputs from the last 3 days or so, so the hash table is emptied every 3 days to reduce size
* Latitude, Longitude, Altitude, Tilt and Azimuth are not in the key because they remain constant throughout
*/
struct spa_table_key {
double jd;
double delta_t;
int pressure;
int temp;
// these are both inputs and outputs (e.g. also stored in the output vector)
double ascension;
double declination;

bool operator==(const spa_table_key &other) const
{ return (jd == other.jd
&& delta_t == other.delta_t
&& pressure == other.pressure
&& temp == other.temp
&& ascension == other.ascension
&& declination == other.declination
);
}

spa_table_key(double j, double dt, double p, double t, double a, double d):
jd(j), delta_t(dt), ascension(a), declination(d)
{
int pressure_bucket = 10;
pressure = ((int)(p + pressure_bucket/2) / pressure_bucket) * pressure_bucket;
pressure = (int)pressure;
int temp_bucket = 5;
temp = ((int)(t + temp_bucket / 2) / temp_bucket) * temp_bucket;
temp = (int)temp;
}
};

template <>
struct std::hash<spa_table_key>
{
std::size_t operator()(const spa_table_key& k) const
{
using std::hash;
// Compute individual hash values for first, second, etc and combine them using XOR and bit shifting:
return
((((
((((hash<double>()(k.jd)
^ (hash<double>()(k.delta_t) << 1)) >> 1)
^ (hash<int>()(k.pressure) << 1)) >> 1)
^ (hash<int>()(k.temp) << 1)) >> 1)
^ (hash<double>()(k.ascension) << 1)) >> 1)
^ (hash<double>()(k.declination) << 1)
;
}
};

void clear_spa_table();

/**
*
Expand Down Expand Up @@ -763,7 +749,8 @@ void calculate_eot_and_sun_rise_transit_set(double jme, double tz, double alpha,
* \param[out] sunn[7] true solar time (hrs)
* \param[out] sunn[8] extraterrestrial solar irradiance on horizontal at particular time (W/m2)
*/
void solarpos_spa(int year, int month, int day, int hour, double minute, double second, double lat, double lng, double tz, double dut1, double alt, double pressure, double temp, double tilt, double azm_rotation, double sunn[9]);
void solarpos_spa(int year, int month, int day, int hour, double minute, double second, double lat, double lng, double tz, double dut1, double alt, double pressure, double temp, double tilt, double azm_rotation, double sunn[9],
std::shared_ptr<solarpos_lookup> spa_table=nullptr);
/** @} */ // end of solarpos_spa group

/**
Expand Down Expand Up @@ -1112,6 +1099,9 @@ class irrad

std::vector<std::vector<double>> solarpos_outputs_for_lifetime; ///< Table of solarpos outputs stored for lifetime simulations
void storeSolarposOutputs();

std::shared_ptr<solarpos_lookup> spa_table;

public:

/// Directive to indicate that if delt_hr is less than zero, do not interpolate sunrise and sunset hours
Expand Down
9 changes: 8 additions & 1 deletion ssc/cmod_mhk_eqns.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ static const char* me_array_cable_length_doc =
" 'number_rows' - double [-]\\n"
" 'row_spacing' - double [m]\\n"
" 'cable_system_overbuild' - double [%]\\n"
" 'floating_array - int [-]\\n"
" 'export_cable_redundancy - int [-]\\n"
" 'water_depth - double [m]\\n"
" 'number_devices - int [-]\\n"
" 'distance_to_shore - double [m]\\n"
"Output: key-value pairs added to var_table\\n"
" 'inter_array_cable_length' - double [m]\\n";

Expand All @@ -63,10 +68,12 @@ static const char* tidal_turbine_calculate_powercurve_doc =
" 'cut_in' - double [m/s]\\n"
" 'cut_out' - double [m/s]\\n"
" 'tidal_resource' - matrix [-]\\n"
" 'generator_rated_capacity' - matrix [-]\\n"
"Output: key-value pairs added to var_table\\n"
" 'tidal_turbine_powercurve_tidespeeds' - array [m/s]\\n"
" 'tidal_turbine_powercurve_powerout' - array [kW]\\n"
" 'tidal_turine_powercurve_powerout_rated' - array [kW]\\n"
" 'tidal_turbine_rated_power' - double [kW]\\n"
" 'tidal_turbine_rated_power_rotor' - double [kW]\\n"
" 'error - string [-]\\n";


Expand Down
2 changes: 1 addition & 1 deletion ssc/cmod_pvwattsv8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static var_info _cm_vtab_pvwattsv8[] = {
{ SSC_INPUT, SSC_NUMBER, "rotlim", "Tracker rotation angle limit", "degrees", "", "System Design", "?=45.0", "", "" },

{ SSC_INPUT, SSC_ARRAY, "soiling", "Soiling loss", "%", "", "System Design", "?", "", "" },
{ SSC_INPUT, SSC_NUMBER, "losses", "Other DC losses", "%", "total system losses", "System Design", "*", "MIN=-5,MAX=99", "" },
{ SSC_INPUT, SSC_NUMBER, "losses", "DC system losses", "%", "total system losses", "System Design", "*", "MIN=-5,MAX=99", "" },

{ SSC_INPUT, SSC_NUMBER, "enable_wind_stow", "Enable tracker stow at high wind speeds", "0/1", "", "System Design", "?=0", "BOOLEAN", "" },
{ SSC_INPUT, SSC_NUMBER, "stow_wspd", "Tracker stow wind speed threshold", "m/s", "", "System Design", "?=10", "", "" },
Expand Down
4 changes: 2 additions & 2 deletions ssc/cmod_singleowner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ static var_info _cm_vtab_singleowner[] = {
{ SSC_OUTPUT, SSC_ARRAY, "cf_revenue_dispatch9", "PPA revenue by year for TOD period 9", "$", "", "Cash Flow Revenue by Month and TOD Period", "ppa_multiplier_model=0", "LENGTH_EQUAL=cf_length", "" },

{ SSC_OUTPUT, SSC_NUMBER, "firstyear_revenue_dispatch1", "PPA revenue in Year 1 TOD period 1", "$", "", "Cash Flow Revenue by Month and TOD Period", "ppa_multiplier_model=0", "", "" },
{ SSC_OUTPUT, SSC_NUMBER, "firstyear_revenue_dispatch2", "PPA revenue from in Year 1 TOD period 2", "$", "", "Cash Flow Revenue by Month and TOD Period", "ppa_multiplier_model=0", "", "" },
{ SSC_OUTPUT, SSC_NUMBER, "firstyear_revenue_dispatch3", "PPA revenue from in Year 1 TOD period 3", "$", "", "Cash Flow Revenue by Month and TOD Period", "ppa_multiplier_model=0", "", "" },
{ SSC_OUTPUT, SSC_NUMBER, "firstyear_revenue_dispatch2", "PPA revenue in Year 1 TOD period 2", "$", "", "Cash Flow Revenue by Month and TOD Period", "ppa_multiplier_model=0", "", "" },
{ SSC_OUTPUT, SSC_NUMBER, "firstyear_revenue_dispatch3", "PPA revenue in Year 1 TOD period 3", "$", "", "Cash Flow Revenue by Month and TOD Period", "ppa_multiplier_model=0", "", "" },
{ SSC_OUTPUT, SSC_NUMBER, "firstyear_revenue_dispatch4", "PPA revenue in Year 1 TOD period 4", "$", "", "Cash Flow Revenue by Month and TOD Period", "ppa_multiplier_model=0", "", "" },
{ SSC_OUTPUT, SSC_NUMBER, "firstyear_revenue_dispatch5", "PPA revenue in Year 1 TOD period 5", "$", "", "Cash Flow Revenue by Month and TOD Period", "ppa_multiplier_model=0", "", "" },
{ SSC_OUTPUT, SSC_NUMBER, "firstyear_revenue_dispatch6", "PPA revenue in Year 1 TOD period 6", "$", "", "Cash Flow Revenue by Month and TOD Period", "ppa_multiplier_model=0", "", "" },
Expand Down
1 change: 0 additions & 1 deletion test/shared_test/lib_irradproc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ TEST_F(IrradTest, sunriseAndSunsetAtDifferentLocationsTest_spa_lib_irradproc) {
for (size_t i = 0; i < latitudes.size(); i++)
{
//run the solarpos function and check sunrise and sunset for each location
clear_spa_table();
solarpos_spa(2010, month[i], day[i], 14, 30, 0, latitudes[i], longitudes[i], time_zones[i], 0, alt[i], 0, 1016, 15, 180, sun_results);
EXPECT_NEAR((double)sun_results[4], sunrise_times[i], e) << "sunrise time for lat " << latitudes[i] << " long " << longitudes[i] << " failed\n";
EXPECT_NEAR((double)sun_results[5], sunset_times[i], e) << "sunset time for lat " << latitudes[i] << " long " << longitudes[i] << "failed\n";
Expand Down

0 comments on commit 5c32c64

Please sign in to comment.