Skip to content

Commit

Permalink
Merge pull request #59 in JGCRI/gcam-core from bugfix/omnibus-fy17-re…
Browse files Browse the repository at this point in the history
…lease to master

* commit 'd49701ea921eb92514e20edb987c88a489cf128b':
  Bump version number in version.h to 4.4
  Update GCAM comparison results provided with gcam_diagnostics
  Some more query tweaks for oil-credits and make sure all diagnostic figures generate correctly
  Fix queries to avoid double accounting oil-credits and other such subtle things
  Add checks if a user is running target finder + negative emiss budget without specifying a CO2 market.  Even though the target finder will eventually create a market that will be after dependecy finder creates orderings and as a result we get bad derivatives.
  Rename negative emissions budget market name to avoid name collision in CO2 queries
  Fix bugs: *the negative emissions correction for non-payed subsidies broke because of typo in cleanup* Ensure the NegativeEmissionsFinalDemand always respects it's dependencies even if the market is solved (aka solving a CO2 constraint)
  Update ModelInterface to get bug fix to allow export and delete
  Bugfixes for: * Catch h_exception in first year of time step. * Restarts when we have a PolicyPortfolioStandard.
  • Loading branch information
kvcalvin committed Nov 6, 2017
2 parents 24e2007 + d49701e commit 174872a
Show file tree
Hide file tree
Showing 25 changed files with 8,060 additions and 8,303 deletions.
2 changes: 1 addition & 1 deletion cvs/objects/climate/source/hector_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ IClimateModel::runModelStatus HectorModel::runModel( const int aYear ) {
// outputs), we can bypass this and the local storage for the
// yearly results.
bool hadError = false;
int lastSuccessYear;
int lastSuccessYear = mLastYear;
for( int year = mLastYear + 1; year <= aYear; ++year ) {
if( !hadError ) {
try {
Expand Down
4 changes: 2 additions & 2 deletions cvs/objects/containers/include/region.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ class Region: public INamed, public IVisitable, public IRoundTrippable, protecte
virtual void completeInit();
const std::string& getName() const;

virtual void initCalc( const int period ) = 0;
virtual void initCalc( const int period );

virtual void postCalc( const int aPeriod ) = 0;
virtual void postCalc( const int aPeriod );

virtual void csvOutputFile() const {};
virtual void dbOutput( const std::list<std::string>& aPrimaryFuelList ) const {};
Expand Down
8 changes: 6 additions & 2 deletions cvs/objects/containers/source/market_dependency_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void MarketDependencyFinder::createOrdering() {
(*it)->mIsSolved = isSolved;
// we don't need to wory about grouping solved markets since they will just
// get disconnected anyways
if( !isSolved ) {
if( !isSolved || !(*it)->mCanBreakCycle ) {
marketDepGrouping[ marketNumber ].insert( *it );
}
}
Expand Down Expand Up @@ -533,7 +533,11 @@ void MarketDependencyFinder::createOrdering() {
}
}
}
continue;
// if this depenency can not have it's dependency broken then we can't skip adding
// it's dependents even if it is solved
if( (*it)->mCanBreakCycle ) {
continue;
}
}

if( (*it)->mDemandVertices.empty() ) {
Expand Down
13 changes: 13 additions & 0 deletions cvs/objects/containers/source/region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,16 @@ void Region::completeInit() {
}
}

/*!
* \brief Function to initialize objects prior to starting a model period.
* \param aPeriod The model period about to begin.
*/
void Region::initCalc( const int aPeriod ) {
for( auto currPolicy : mPolicies ) {
currPolicy->initCalc( mName, aPeriod );
}
}

/*! \brief Function to finalize objects after a period is solved.
* \details This function is used to calculate and store variables which are only
* needed after the current period is complete.
Expand All @@ -336,6 +346,9 @@ void Region::postCalc( const int aPeriod ){
for( ResourceIterator currResource = mResources.begin(); currResource != mResources.end(); ++currResource ){
(*currResource)->postCalc( mName, aPeriod );
}
for( auto currPolicy : mPolicies ) {
currPolicy->postCalc( mName, aPeriod );
}
}

/*! \brief Update a visitor for a Region.
Expand Down
1 change: 1 addition & 0 deletions cvs/objects/containers/source/region_minicam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ bool RegionMiniCAM::isAllCalibrated( const int period, double calAccuracy, const
*/
void RegionMiniCAM::initCalc( const int period )
{
Region::initCalc( period );
for( SectorIterator currSector = mSupplySector.begin(); currSector != mSupplySector.end(); ++currSector ){
(*currSector)->initCalc( 0, mDemographic, period );
}
Expand Down
3 changes: 2 additions & 1 deletion cvs/objects/emissions/source/aghg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ void AGHG::completeInit( const string& aRegionName, const string& aSectorName,
scenario->getMarketplace()->getDependencyFinder()->addDependency( aSectorName,
aRegionName,
getName(),
aRegionName );
aRegionName,
false );
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion cvs/objects/marketplace/source/market_RES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ double MarketRES::getDefaultPrice() const {
*/
void MarketRES::initPrice() {
// If price is near zero it needs to be initialized.
if( mPrice < util::getSmallNumber() ){
if( !mPrice.isInited() ){
if( mSolveMarket ){
mPrice = getDefaultPrice();
}
Expand Down
6 changes: 3 additions & 3 deletions cvs/objects/marketplace/source/marketplace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,9 @@ void Marketplace::init_to_last( const int period ) {
}
else if ( period > 0 && period < restartPeriod ) {
for ( unsigned int i = 0; i < mMarkets.size(); i++ ) {
mMarkets[ i ]->getMarket( period )->set_price_to_last_if_default( mMarkets[ i ]->getMarket( period - 1 )->getPrice() );
mMarkets[ i ]->getMarket( period )->setForecastPrice( mMarkets[ i ]->getMarket( period - 1 )->getRawPrice() );
mMarkets[ i ]->getMarket( period )->setForecastDemand( 1.0 );
double forecastedPrice = mMarkets[ i ]->forecastPrice( period );
mMarkets[ i ]->getMarket( period )->set_price_to_last_if_default( forecastedPrice );
mMarkets[ i ]->forecastDemand( period );
}
}
else if( period >= restartPeriod ){
Expand Down
8 changes: 5 additions & 3 deletions cvs/objects/policy/include/policy_portfolio_standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class PolicyPortfolioStandard: public INamed, public IRoundTrippable, private bo
void toInputXML( std::ostream& out, Tabs* tabs ) const;
void toDebugXML( const int period, std::ostream& out, Tabs* tabs ) const;
void completeInit( const std::string& aRegionName );
void initCalc( const std::string& aRegionName, const int aPeriod );
void postCalc( const std::string& aRegionName, const int aPeriod );
protected:

DEFINE_DATA(
Expand All @@ -89,13 +91,13 @@ class PolicyPortfolioStandard: public INamed, public IRoundTrippable, private bo
DEFINE_VARIABLE( SIMPLE, "isShareBased", mIsShareBased, bool ),

//! Quantity constraint by year
DEFINE_VARIABLE( ARRAY, "constraint", mConstraint, objects::PeriodVector<double> ),
DEFINE_VARIABLE( ARRAY, "constraint", mConstraint, objects::PeriodVector<Value> ),

//! Fixed tax on Fuel by year($/GJ)
DEFINE_VARIABLE( ARRAY, "fixedTax", mFixedTax, objects::PeriodVector<double> ),
DEFINE_VARIABLE( ARRAY, "fixedTax", mFixedTax, objects::PeriodVector<Value> ),

//! Share of total or sectoral output
DEFINE_VARIABLE( ARRAY, "share-of-sector-output", mShareOfSectorOutput, objects::PeriodVector<double> ),
DEFINE_VARIABLE( ARRAY, "share-of-sector-output", mShareOfSectorOutput, objects::PeriodVector<Value> ),

//! The minimum price below which the constraint is considered non-binding.
DEFINE_VARIABLE( ARRAY, "min-price", mMinPrice, objects::PeriodVector<double> ),
Expand Down
63 changes: 42 additions & 21 deletions cvs/objects/policy/source/policy_portfolio_standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ extern Scenario* scenario;

/*! \brief Default constructor. */
PolicyPortfolioStandard::PolicyPortfolioStandard():
mConstraint( -1.0 ),
mFixedTax( -1.0 ),
mShareOfSectorOutput( -1.0 ),
mMinPrice( 0.0 ),
mMaxPrice( util::getLargeNumber() ),
mPriceUnits( "1975$/GJ" ),
Expand Down Expand Up @@ -135,13 +132,13 @@ void PolicyPortfolioStandard::XMLParse( const DOMNode* node ){
mIsShareBased = XMLHelper<bool>::getValue( curr );
}
else if( nodeName == "constraint" ){
XMLHelper<double>::insertValueIntoVector( curr, mConstraint, modeltime );
XMLHelper<Value>::insertValueIntoVector( curr, mConstraint, modeltime );
}
else if( nodeName == "fixedTax" ){
XMLHelper<double>::insertValueIntoVector( curr, mFixedTax, modeltime );
XMLHelper<Value>::insertValueIntoVector( curr, mFixedTax, modeltime );
}
else if( nodeName == "share-of-sector-output" ){
XMLHelper<double>::insertValueIntoVector( curr, mShareOfSectorOutput, modeltime );
XMLHelper<Value>::insertValueIntoVector( curr, mShareOfSectorOutput, modeltime );
// Check to see if the output share is within valid range (between 0 and 1).
double tempShare = XMLHelper<double>::getValue( curr );
if ( tempShare <= 0 || tempShare > 1 ){
Expand Down Expand Up @@ -181,19 +178,22 @@ void PolicyPortfolioStandard::toInputXML( ostream& out, Tabs* tabs ) const {
XMLWriteElement( mIsShareBased, "isShareBased", out, tabs );

const Modeltime* modeltime = scenario->getModeltime();
for( int per = 0; per < modeltime->getmaxper(); ++per ){
XMLWriteElementCheckDefault( mConstraint[ per ],
"constraint", out, tabs, -1.0 );
for( int per = 0; per < modeltime->getmaxper(); ++per ) {
int year = modeltime->getper_to_yr( per );
if( mFixedTax[ per ].isInited() ) {
XMLWriteElement( mFixedTax[ per ], "fixedTax", out, tabs, year );
}
if( mConstraint[ per ].isInited() ) {
XMLWriteElement( mConstraint[ per ], "constraint", out, tabs, year );
}
if( mShareOfSectorOutput[ per ].isInited() ) {
XMLWriteElement( mShareOfSectorOutput[ per ], "share-of-sector-output", out, tabs, year );
}
}
XMLWriteVector( mFixedTax, "fixedTax", out, tabs, modeltime, 0.0 );
XMLWriteVector( mMinPrice, "min-price", out, tabs, modeltime, 0.0 );
XMLWriteVector( mMaxPrice, "max-price", out, tabs, modeltime, 0.0 );
XMLWriteVector( mMaxPrice, "max-price", out, tabs, modeltime, util::getLargeNumber() );
XMLWriteElement( mPriceUnits, "price-unit", out, tabs );
XMLWriteElement( mOutputUnits, "output-unit", out, tabs );
for( int per = 0; per < modeltime->getmaxper(); ++per ){
XMLWriteElementCheckDefault( mShareOfSectorOutput[ per ],
"share-of-sector-output", out, tabs, -1.0 );
}

// finished writing xml for the class members.
XMLWriteClosingTag( getXMLName(), out, tabs );
Expand Down Expand Up @@ -270,20 +270,18 @@ void PolicyPortfolioStandard::completeInit( const string& aRegionName ) {
// Make sure that the market is not solved. It could have been set
// to solve by an earlier run.
marketplace->unsetMarketToSolve( mName, aRegionName, i );
if( mFixedTax[ i ] != -1 ){
if( mFixedTax[ i ].isInited() ){
marketplace->setPrice( mName, aRegionName, mFixedTax[ i ], i );
}
}

// solve the market when given the read-in constraint.
// Initialize temporary vector of contraints to constraint
vector<double> tempConstraint( convertToVector( mConstraint ) );
objects::PeriodVector<Value> tempConstraint = mConstraint;

// Override tempConstraint with shares if shared based.
// Note: the share is based on the total output of the sector that the
// technology is in.
if( mIsShareBased ){
tempConstraint = convertToVector( mShareOfSectorOutput );
tempConstraint = mShareOfSectorOutput;
marketInfo->setBoolean( "isShareBased", true );
}
// Set either of the constraints, quantity or share, into the
Expand All @@ -297,7 +295,7 @@ void PolicyPortfolioStandard::completeInit( const string& aRegionName ) {
// Subtracting the current demand for this period to set the constraint
// because addToDemand adds to any existing demand in the market.
// Passing false to suppress a warning the first time through.
if( tempConstraint[ per ] != -1 ){
if( tempConstraint[ per ].isInited() ){
if ( mPolicyType == "tax" ){
marketplace->setMarketToSolve( mName, aRegionName, per );
marketplace->addToSupply( mName, aRegionName, Value( tempConstraint[ per ] -
Expand All @@ -323,3 +321,26 @@ void PolicyPortfolioStandard::completeInit( const string& aRegionName ) {
}
}
}

/*!
* \brief Perform any initializations that need to occur prior to attempting to solve
* aPeriod.
* \param aRegionName The name of the containing region.
* \param aPeriod The current model period about to begin.
*/
void PolicyPortfolioStandard::initCalc( const string& aRegionName, const int aPeriod ) {
}

/*!
* \brief Perform any computations after a model period has found a solution.
* \param aRegionName The name of the containing region.
* \param aPeriod The current model period which just finished.
*/
void PolicyPortfolioStandard::postCalc( const string& aRegionName, const int aPeriod ) {
// if we are solving for a price because we have a constraint then we should
// save the solved price so we can write it back out in toInputXML for restart
if( mConstraint[ aPeriod ].isInited() ) {
mFixedTax[ aPeriod ] = scenario->getMarketplace()->getPrice( mName, aRegionName, aPeriod );
}
}

13 changes: 12 additions & 1 deletion cvs/objects/sectors/source/negative_emissions_final_demand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ void NegativeEmissionsFinalDemand::completeInit( const string& aRegionName,
<< " in region " << aRegionName << endl;
abort();
}
const static bool isTargetFinding = Configuration::getInstance()->getBool(
"find-path", false, false );
const bool hasTaxMarket = scenario->getMarketplace()->getPrice( mName, aRegionName, 0, false ) !=
Marketplace::NO_MARKET_PRICE;
if( isTargetFinding && !hasTaxMarket ) {
ILogger& mainLog = ILogger::getLogger( "main_log" );
mainLog.setLevel( ILogger::WARNING );
mainLog << "Using " << getXMLNameStatic() << " wth target-finder without explicitly creating a market for "
<< mName << " may hinder solution performance." << endl;
mainLog << "Please read in a policy file with a zero tax." << endl;
}

MarketDependencyFinder* depFinder = scenario->getMarketplace()->getDependencyFinder();
depFinder->addDependency( mPolicyName, aRegionName, mName, aRegionName );
Expand Down Expand Up @@ -190,7 +201,7 @@ void NegativeEmissionsFinalDemand::setFinalDemand( const string& aRegionName,
}
double regionalCO2Emiss = marketplace->getDemand( mName, aRegionName, aPeriod );
double regionalCO2EmissValue = -1.0 * regionalCO2Emiss * co2Price;
if(regionalCO2Emiss > 0.0) {
if(regionalCO2EmissValue > 0.0) {
double policyPrice = std::min( marketplace->getPrice( mPolicyName, aRegionName, aPeriod ), 1.0 );
double policyAdj = ( 1.0 - policyPrice );
regionalCO2EmissValue *= policyAdj;
Expand Down
4 changes: 2 additions & 2 deletions cvs/objects/util/base/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
* NOTE: ADD 1 TO LATEST SUBVERSION REVISION NUMBER
*/
//! The latest SVN revision number for identification of the build.
#define __REVISION_NUMBER__ "gcam-v4.3"
#define __REVISION_NUMBER__ "gcam-v4.4"
/*****************************************************************************/

//! GCAM model version.
#define __ObjECTS_VER__ "4.3"
#define __ObjECTS_VER__ "4.4"

#endif // _VERSION_H_
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ L102.gdp_mil90usd_Scen_R_Y <- readdata( "SOCIO_LEVEL1_DATA", "L102.gdp_mil90usd_
L221.GlobalTechCoef_en <- readdata( "ENERGY_LEVEL2_DATA", "L221.GlobalTechCoef_en", skip=4 )

# Policy assumptions: TODO where to put these
NEG_EMISS_POLICY_NAME <- "CO2_negative_emiss"
NEG_EMISS_POLICY_NAME <- "negative_emiss_budget"
NEG_EMISS_GDP_BUDGET_PCT <- 0.01
NEG_EMISS_MARKT_GLOBAL <- TRUE

Expand Down
Loading

0 comments on commit 174872a

Please sign in to comment.