Skip to content

Commit

Permalink
Merge pull request #80 in JGCRI/gcam-core from feature/sd-curves to m…
Browse files Browse the repository at this point in the history
…aster

* commit '40c128572a7da899a69c2836497655ce7912fb4e':
  Change SD header instead of non descriptive Fx call it normalizedExcessDemand
  Update Visual Studio project file for SD curve saver
  Update Xcode project file for SD curve saver
  Remove toInputXML from SupplyDemandCurveSaver which no longer exits.
  Address PR concerns including subtle C++ stuff. Better documentation. Move example supply demand curve XML input file to input/extra
  Remove diagnostic output
  Debugged S & D curve generator; fixed scaling and allowed relative or actual prices to be specified in XML.
  Cleaned up some names and comments as per Pralit's PR comments
  Various changes to support writing out supply/demand curves for designated markets: - New class SupplyDemandCurveSaver created, and added to parsing in scenario.cpp - Modified SupplyDemandCurve to print to ostream rather than logger - Created SupplyDemandCurve::print2 function to create a proper CSV format file - Modified AutoOutputFile to support option file opening mode (now allowing append mode)
  • Loading branch information
pralitp committed Oct 15, 2018
2 parents 3aaf030 + 40c1285 commit f40dcee
Show file tree
Hide file tree
Showing 12 changed files with 480 additions and 69 deletions.
2 changes: 2 additions & 0 deletions cvs/objects/build/vc10/objects.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@
<ClCompile Include="..\..\util\base\source\linear_interpolation_function.cpp" />
<ClCompile Include="..\..\util\base\source\manage_state_variables.cpp" />
<ClCompile Include="..\..\util\base\source\model_time.cpp" />
<ClCompile Include="..\..\util\base\source\supply_demand_curve_saver.cpp" />
<ClCompile Include="..\..\util\base\source\s_curve_interpolation_function.cpp" />
<ClCompile Include="..\..\util\base\source\supply_demand_curve.cpp" />
<ClCompile Include="..\..\util\base\source\timer.cpp" />
Expand Down Expand Up @@ -1101,6 +1102,7 @@
<ClInclude Include="..\..\util\base\include\manage_state_variables.hpp" />
<ClInclude Include="..\..\util\base\include\model_time.h" />
<ClInclude Include="..\..\util\base\include\object_meta_info.h" />
<ClInclude Include="..\..\util\base\include\supply_demand_curve_saver.h" />
<ClInclude Include="..\..\util\base\include\s_curve_interpolation_function.h" />
<ClInclude Include="..\..\util\base\include\string_hash.h" />
<ClInclude Include="..\..\util\base\include\supply_demand_curve.h" />
Expand Down
8 changes: 7 additions & 1 deletion cvs/objects/build/vc10/objects.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,9 @@
<ClCompile Include="..\..\emissions\source\readin_control.cpp">
<Filter>Source Files\emissions</Filter>
</ClCompile>
<ClCompile Include="..\..\util\base\source\supply_demand_curve_saver.cpp">
<Filter>Source Files\util\base</Filter>
</ClCompile>
<ClCompile Include="..\..\util\base\source\initialize_tech_vector_helper.cpp">
<Filter>Source Files\util\base</Filter>
</ClCompile>
Expand Down Expand Up @@ -2179,8 +2182,11 @@
<ClInclude Include="..\..\emissions\include\readin_control.h">
<Filter>Header Files\emissions</Filter>
</ClInclude>
<ClInclude Include="..\..\util\base\include\supply_demand_curve_saver.h">
<Filter>Header Files\util\base</Filter>
</ClInclude>
<ClInclude Include="..\..\util\base\include\initialize_tech_vector_helper.hpp">
<Filter>Header Files\util\base</Filter>
</ClInclude>
</ItemGroup>
</Project>
</Project>
6 changes: 6 additions & 0 deletions cvs/objects/build/xcode3/objects.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
CD8FDEC91C0647140099C752 /* pass_through_sector.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD8FDEC81C0647140099C752 /* pass_through_sector.cpp */; };
CD8FDECC1C0647A20099C752 /* pass_through_technology.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD8FDECB1C0647A20099C752 /* pass_through_technology.cpp */; };
CD966E751D92F1CD00A93938 /* libhector-lib.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CD966E721D92F1BB00A93938 /* libhector-lib.a */; };
CDAACD88216C546D00D13FD6 /* supply_demand_curve_saver.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDAACD87216C546D00D13FD6 /* supply_demand_curve_saver.cpp */; };
CDAF62F0130DAB6900D93AFB /* MAGICC_array.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDAF62EC130DAB6900D93AFB /* MAGICC_array.cpp */; };
CDAF62F1130DAB6900D93AFB /* MAGICC_IO_helpers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDAF62ED130DAB6900D93AFB /* MAGICC_IO_helpers.cpp */; };
CDAF62F2130DAB6900D93AFB /* ObjECTS_MAGICC_others.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDAF62EE130DAB6900D93AFB /* ObjECTS_MAGICC_others.cpp */; };
Expand Down Expand Up @@ -993,6 +994,8 @@
CD8FDECA1C0647640099C752 /* pass_through_technology.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pass_through_technology.h; sourceTree = "<group>"; };
CD8FDECB1C0647A20099C752 /* pass_through_technology.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = pass_through_technology.cpp; sourceTree = "<group>"; };
CD966E671D92F1BB00A93938 /* hector.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = hector.xcodeproj; path = ../../climate/source/hector/project_files/Xcode/hector.xcodeproj; sourceTree = "<group>"; };
CDAACD84216C545F00D13FD6 /* supply_demand_curve_saver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = supply_demand_curve_saver.h; sourceTree = "<group>"; };
CDAACD87216C546D00D13FD6 /* supply_demand_curve_saver.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = supply_demand_curve_saver.cpp; sourceTree = "<group>"; };
CDAF62EA130DAB6100D93AFB /* MAGICC_array.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MAGICC_array.h; sourceTree = "<group>"; };
CDAF62EB130DAB6100D93AFB /* ObjECTS_MAGICC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjECTS_MAGICC.h; sourceTree = "<group>"; };
CDAF62EC130DAB6900D93AFB /* MAGICC_array.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MAGICC_array.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2229,6 +2232,7 @@
CD4886CA122873C200F5A88A /* include */ = {
isa = PBXGroup;
children = (
CDAACD84216C545F00D13FD6 /* supply_demand_curve_saver.h */,
CD2420002162D2250071DB2B /* initialize_tech_vector_helper.hpp */,
0E3C49651EC4BBC6005EDC19 /* iyeared.h */,
0E3C49661EC4BBC6005EDC19 /* manage_state_variables.hpp */,
Expand Down Expand Up @@ -2278,6 +2282,7 @@
CD4886EE122873C200F5A88A /* source */ = {
isa = PBXGroup;
children = (
CDAACD87216C546D00D13FD6 /* supply_demand_curve_saver.cpp */,
CD2420012162D2310071DB2B /* initialize_tech_vector_helper.cpp */,
0E3C49691EC4BBD8005EDC19 /* manage_state_variables.cpp */,
0E05C9001E435B3600C73D94 /* gcam_fusion.cpp */,
Expand Down Expand Up @@ -2538,6 +2543,7 @@
CD488754122873C200F5A88A /* co2_emissions.cpp in Sources */,
CD488755122873C200F5A88A /* emissions_driver_factory.cpp in Sources */,
CD488756122873C200F5A88A /* emissions_summer.cpp in Sources */,
CDAACD88216C546D00D13FD6 /* supply_demand_curve_saver.cpp in Sources */,
CD488758122873C200F5A88A /* ghg_factory.cpp in Sources */,
CD693FA01AEFE0CE00805384 /* relative_cost_logit.cpp in Sources */,
CD48875B122873C200F5A88A /* input_driver.cpp in Sources */,
Expand Down
10 changes: 7 additions & 3 deletions cvs/objects/containers/source/scenario.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "solution/util/include/solution_info_param_parser.h"
#include "containers/include/imodel_feedback_calc.h"
#include "util/base/include/manage_state_variables.hpp"
#include "util/base/include/supply_demand_curve_saver.h"

#if GCAM_PARALLEL_ENABLED && PARALLEL_DEBUG
#include <stdlib.h>
Expand Down Expand Up @@ -170,6 +171,9 @@ bool Scenario::XMLParse( const DOMNode* node ){
else if( nodeName == SolutionInfoParamParser::getXMLNameStatic() ) {
parseSingleNode( curr, mSolutionInfoParamParser, new SolutionInfoParamParser );
}
else if ( nodeName == SupplyDemandCurveSaver::getXMLNameStatic()) {
parseContainerNode( curr, mModelFeedbacks, new SupplyDemandCurveSaver );
}

/*!
* \warning Parsing of solution algorithms are a special case. They must be
Expand Down Expand Up @@ -467,9 +471,6 @@ bool Scenario::calculatePeriod( const int aPeriod,


bool success = solve( aPeriod ); // solution uses Bisect and NR routine to clear markets

delete mManageStateVars;
mManageStateVars = 0;

mWorld->postCalc( aPeriod );

Expand Down Expand Up @@ -500,6 +501,9 @@ bool Scenario::calculatePeriod( const int aPeriod,
writeDebuggingFiles( aXMLDebugFile, aTabs, aPeriod );
}

delete mManageStateVars;
mManageStateVars = 0;

return success;
}

Expand Down
2 changes: 1 addition & 1 deletion cvs/objects/solution/util/include/solution_info_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class SolutionInfoSet {
bool hasSingularUnsolved();
void unsetBisectedFlag();
void printUnsolved( std::ostream& out );
void findAndPrintSD( World* aWorld, Marketplace* aMarketplace, const int aPeriod, ILogger& aLogger );
void findAndPrintSD( World* aWorld, Marketplace* aMarketplace, const int aPeriod, std::ostream& aOut );
void printMarketInfo( const std::string& comment, const double worldCalcCount, std::ostream& out ) const;
void printDerivatives( std::ostream& aOut ) const;

Expand Down
6 changes: 3 additions & 3 deletions cvs/objects/solution/util/source/solution_info_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,11 @@ void SolutionInfoSet::printMarketInfo( const string& aLocation,
* \param aPeriod Period for which to print supply-demand curves.
* \param aLogger Logger stream to print the curves to.
*/
void SolutionInfoSet::findAndPrintSD( World* aWorld, Marketplace* aMarketplace, const int aPeriod, ILogger& aLogger ) {
void SolutionInfoSet::findAndPrintSD( World* aWorld, Marketplace* aMarketplace, const int aPeriod, ostream& aOut) {
const Configuration* conf = Configuration::getInstance();
const int numMarketsToFindSD = conf->getInt( "numMarketsToFindSD", 5 );
const int numPointsForSD = conf->getInt( "numPointsForSD", 5 );

// Sort the vector so the worst markets are first.
sort( solvable.begin(), solvable.end(), SolutionInfo::GreaterRelativeED() );

Expand All @@ -642,7 +642,7 @@ void SolutionInfoSet::findAndPrintSD( World* aWorld, Marketplace* aMarketplace,
}
SupplyDemandCurve sdCurve( i, solvable[ i ].getName() );
sdCurve.calculatePoints( numPointsForSD, *this, aWorld, aMarketplace, aPeriod );
sdCurve.print( aLogger );
sdCurve.print( aOut );
}
}

Expand Down
7 changes: 3 additions & 4 deletions cvs/objects/util/base/include/auto_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,12 @@ class AutoOutputFile {
/*! \brief Open an output file with the given name.
* \details Opens an automatically closing output file with a given filename.
* \param aFileName Name of the file to open.
* \todo This interface is dangerous because forgetting an argument calls a
* different function.
* \param aOpenMode The file opening mode. Use std::ios_base::app to open in append mode.
*/
explicit AutoOutputFile( const std::string& aFileName )
explicit AutoOutputFile( const std::string& aFileName, std::ios_base::openmode aOpenMode = std::ios_base::out )
:mShouldWrite( true )
{
boost::iostreams::file_sink fileBuffer( aFileName );
boost::iostreams::file_sink fileBuffer( aFileName, aOpenMode );
mWrappedFile.push( fileBuffer );
util::checkIsOpen( fileBuffer, aFileName );
}
Expand Down
13 changes: 10 additions & 3 deletions cvs/objects/util/base/include/supply_demand_curve.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ class SupplyDemandCurve {
public:
SupplyDemandCurve( int aMarketNumber, const std::string& aMarketName );
~SupplyDemandCurve();

void calculatePoints( const std::vector<double>& aPrices, SolutionInfoSet& aSolnSet, World* aWorld,
Marketplace* aMarketplace, const int aPeriod, bool aIsPricesRelative );

// Legacy version
void calculatePoints( const int aNumPoints, SolutionInfoSet& aSolnSet, World* aWorld,
Marketplace* aMarketplace, const int aPeriod );
void print( ILogger& aSDLog ) const;


void print( std::ostream& aOut ) const;
void printCSV( std::ostream& aOut, int period, bool aPrintHeader ) const;

private:
//! Index to the market which the curve is calculating for.
int mMarketNumber;
Expand All @@ -88,7 +95,7 @@ class SupplyDemandPoint
public:
SupplyDemandPoint( const double aPrice, const double aDemand, const double aSupply, const double aFx );
double getPrice() const;
void print( ILogger& aSDLog ) const;
void print( std::ostream& aOut ) const;

/*!
* \brief Binary comparison operator used for SavePoint pointers to order by increasing price.
Expand Down
113 changes: 113 additions & 0 deletions cvs/objects/util/base/include/supply_demand_curve_saver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#ifndef _SUPPLY_DEMAND_CURVE_SAVER_H_
#define _SUPPLY_DEMAND_CURVE_SAVER_H_
#if defined(_MSC_VER)
#pragma once
#endif

/*
* LEGAL NOTICE
* This computer software was prepared by Battelle Memorial Institute,
* hereinafter the Contractor, under Contract No. DE-AC05-76RL0 1830
* with the Department of Energy (DOE). NEITHER THE GOVERNMENT NOR THE
* CONTRACTOR MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY
* LIABILITY FOR THE USE OF THIS SOFTWARE. This notice including this
* sentence must appear on any copies of this computer software.
*
* EXPORT CONTROL
* User agrees that the Software will not be shipped, transferred or
* exported into any country or used in any manner prohibited by the
* United States Export Administration Act or any other applicable
* export laws, restrictions or regulations (collectively the "Export Laws").
* Export of the Software may require some form of license or other
* authority from the U.S. Government, and failure to obtain such
* export control license may result in criminal liability under
* U.S. laws. In addition, if the Software is identified as export controlled
* items under the Export Laws, User represents and warrants that User
* is not a citizen, or otherwise located within, an embargoed nation
* (including without limitation Iran, Syria, Sudan, Cuba, and North Korea)
* and that User is not otherwise prohibited
* under the Export Laws from receiving the Software.
*
* Copyright 2011 Battelle Memorial Institute. All Rights Reserved.
* Distributed as open-source under the terms of the Educational Community
* License version 2.0 (ECL 2.0). http://www.opensource.org/licenses/ecl2.php
*
* For further details, see: http://www.globalchange.umd.edu/models/gcam/
*
*/

/*!
* \file supply_demand_curve_saver.h
* \ingroup Objects
* \brief The SupplyDemandCurveSaver class header file.
* \author Rich Plevin
*/

#include <cassert>
#include <vector>
#include <iostream>
#include "containers/include/imodel_feedback_calc.h"

class SolutionInfo;

/*!
* \brief Writes out supply & demand curves for user-designated markets after each
* model period has solved.
* \details Users can configure via XML parse
* - mName The market name to create the Supply/Demand curves for
* - mPrices A vector of prices at which to calculate Supply / Demand points.
* - mIsPricesRelative A flag to indicate if mPrices are relative to the solved price in each model period.
* All results will be written to the output file specified in the Configuration
* parameter "supplyDemandCurves". Note that the file will get reset the first
* time any instanve of this class needs to write to the file and will append to
* it thereafter.
*
* \sa SupplyDemandCurve
* \author Rich Plevin
*/
class SupplyDemandCurveSaver : public IModelFeedbackCalc
{
public:
SupplyDemandCurveSaver();
virtual ~SupplyDemandCurveSaver();

static const std::string& getXMLNameStatic();

// INamed methods
virtual const std::string& getName() const;

// IParsable methods
virtual bool XMLParse( const xercesc::DOMNode* aNode );

// IModelFeedbackCalc methods
virtual void calcFeedbacksBeforePeriod( Scenario* aScenario,
const IClimateModel* aClimateModel,
const int aPeriod );

virtual void calcFeedbacksAfterPeriod( Scenario* aScenario,
const IClimateModel* aClimateModel,
const int aPeriod );

protected:
//! The name of this feedback
std::string mName;

//! A flag indicating if a user wanted the mPrices to be relative to the
//! the solved price for the configured market in each model period.
bool mIsPricesRelative;

//! The price points for which we should calculate supply and demands
std::vector<double> mPrices;

//! A flag to help us determine if we need to reset the output file (the
//! first time around) or simply append to it.
static std::ios_base::openmode mOpenMode;

void printCSV( std::ostream& aOut, Scenario* aScenario, const int aPeriod, bool aPrintHeader );

int getMarketIndex(const std::string& aMarketName, std::vector<SolutionInfo> &aSolvable );

};

#endif // _SUPPLY_DEMAND_CURVE_SAVER_H_

Loading

0 comments on commit f40dcee

Please # to comment.