Skip to content

Commit

Permalink
Fixed calcScalers segmentation fault bug
Browse files Browse the repository at this point in the history
Was trying to access beyond end of array for copying ELM area.
  • Loading branch information
Alan Divittorio committed Jul 18, 2024
1 parent 38812d0 commit 205a815
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cpl/source/carbon_scalers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,22 @@ void CarbonScalers::calcScalers(int aGCAMYear, double *aELMArea, double *aELMPFT
std::vector<int>& aYears, std::vector<std::string>& aRegions, std::vector<std::string>& aLandTechs, std::vector<double>& aAboveScalers,
std::vector<double>& aBelowScalers, std::string aBaseNPPFileName, std::string aBaseHRFileName, std::string aBasePFTWtFileName,
int& aNumScalars) {

// Open the coupling log
ILogger& coupleLog = ILogger::getLogger( "coupling_log" );
coupleLog.setLevel( ILogger::NOTICE );

coupleLog << "In calcScalers" << endl;

// First, read spatial data
readBaseYearData(aBaseNPPFileName, aBaseHRFileName, aBasePFTWtFileName);

// first copy the area into the local array, expanded to all pfts
// this is because the area needs to be zeroed out also for outliers
// an outlier may be for one pft in a cell, but not for another
for (int z=0; z < mNumPFT; z++) {
for (int i=0; i < (mNumLat*mNumLon); i++) {
mELMArea[(z*mNumLat*mNumLon)+i] = aELMArea[(z*mNumLat*mNumLon)+i];
mELMArea[(z*mNumLat*mNumLon)+i] = aELMArea[i];
}
}

Expand Down Expand Up @@ -515,7 +522,7 @@ void CarbonScalers::excludeOutliers( double *aELMNPP, double *aELMHR) {
int length = mNumLat * mNumLon * mNumPFT;
std::vector<double> scaledNPP(aELMNPP+0, aELMNPP+length);
std::vector<double> scaledHR(aELMHR+0, aELMHR+length);

// Calculate raw scalars
std::transform(scaledNPP.begin(), scaledNPP.end(), mBaseNPPVector.begin(), scaledNPP.begin(), std::divides<double>());
std::transform(scaledHR.begin(), scaledHR.end(), mBaseHRVector.begin(), scaledHR.begin(), std::divides<double>());
Expand Down

2 comments on commit 205a815

@evasinha
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aldivi Do we need to add coupleLog.precision(20);

@aldivi
Copy link
Collaborator

@aldivi aldivi commented on 205a815 Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. there are no numbers output to coupleLog here. I could have deleted the coupleLog altogether, but i left it just to denote that the code entered this function.

Please sign in to comment.