Skip to content

Commit

Permalink
Use shared ptr instead of raw pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
HomesGH committed Jul 30, 2024
1 parent 0c2d3e3 commit c4103f5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
17 changes: 6 additions & 11 deletions src/ensemble/EnsembleBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
Ensemble::~Ensemble() {
delete _domain;
_domain = nullptr;
for(auto const &cid1 : _mixingrules) {
for(auto const &cid2 : cid1.second) {
delete(cid2.second);
}
}
}

void Ensemble::readXML(XMLfileUnits& xmlconfig) {
Expand Down Expand Up @@ -54,13 +49,13 @@ void Ensemble::readXML(XMLfileUnits& xmlconfig) {
for(mixingruletIter = query.begin(); mixingruletIter; mixingruletIter++) {
xmlconfig.changecurrentnode(mixingruletIter);
// MixingRuleBase* mixingrule = nullptr;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
std::unique_ptr<MixingRuleBase> mixingrule;
std::shared_ptr<MixingRuleBase> mixingrule;
std::string mixingruletype;

xmlconfig.getNodeValue("@type", mixingruletype);
Log::global_log->info() << "Mixing rule type: " << mixingruletype << std::endl;
if ("LB" == mixingruletype) {
mixingrule = std::make_unique<LorentzBerthelotMixingRule>();
mixingrule = std::make_shared<LorentzBerthelotMixingRule>();

} else {
Log::global_log->error() << "Unknown mixing rule " << mixingruletype << std::endl;
Expand All @@ -77,17 +72,17 @@ void Ensemble::readXML(XMLfileUnits& xmlconfig) {
<< numComponents << ")" << std::endl;
Simulation::exit(1);
}
_mixingrules[cid1][cid2] = mixingrule.get();
_mixingrules[cid1][cid2] = mixingrule;
}
// Use xi=eta=1.0 as default if no rule was specified
for (int cidi = 0; cidi < numComponents; ++cidi) {
for (int cidj = cidi+1; cidj < numComponents; ++cidj) { // cidj is always larger than cidi
if (_mixingrules[cidi].count(cidj) == 0) {
// Only LorentzBerthelot is supported until now
std::unique_ptr<LorentzBerthelotMixingRule> mixingrule = std::make_unique<LorentzBerthelotMixingRule>();
std::shared_ptr<LorentzBerthelotMixingRule> mixingrule = std::make_shared<LorentzBerthelotMixingRule>();
mixingrule->setCid1(cidi);
mixingrule->setCid2(cidj);
_mixingrules[cidi][cidj] = mixingrule.get();
_mixingrules[cidi][cidj] = mixingrule;
Log::global_log->warning() << "Mixing coefficients for components "
<< mixingrule->getCid1()+1 << " + " << mixingrule->getCid2()+1 // +1 due to internal cid
<< " set to default (LB with xi=eta=1.0)" << std::endl;
Expand All @@ -113,7 +108,7 @@ void Ensemble::setComponentLookUpIDs() {
}
}

void Ensemble::setMixingrule(MixingRuleBase* mixingrule) {
void Ensemble::setMixingrule(std::shared_ptr<MixingRuleBase> mixingrule) {
const int cid1 = mixingrule->getCid1();
const int cid2 = mixingrule->getCid2();
// Check if cids are valid
Expand Down
5 changes: 3 additions & 2 deletions src/ensemble/EnsembleBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include <map>
#include <memory>

#include "DomainBase.h"
#include "molecules/MoleculeForwardDeclaration.h"
Expand Down Expand Up @@ -123,7 +124,7 @@ class Ensemble {
auto & getMixingrules() { return _mixingrules; }

// Set one mixing rule
void setMixingrule(MixingRuleBase* mixingrule);
void setMixingrule(std::shared_ptr<MixingRuleBase> mixingrule);

protected:

Expand All @@ -132,7 +133,7 @@ class Ensemble {
std::map<std::string, int> _componentnamesToIds;
// The mixing rules (xi,eta) can be accessed by _mixingrules[cid1][cid2]
// Note that cid1 < cid2 and that cid is in internal format, i.e. starts with 0
std::map<int, std::map<int, MixingRuleBase*>> _mixingrules;
std::map<int, std::map<int, std::shared_ptr<MixingRuleBase>>> _mixingrules;
DomainBase* _domain;
Type _type = undefined;

Expand Down
3 changes: 2 additions & 1 deletion src/io/ASCIIReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <climits>
#include <string>
#include <memory>

#include "Domain.h"
#include "ensemble/BoxDomain.h"
Expand Down Expand Up @@ -219,7 +220,7 @@ void ASCIIReader::readPhaseSpaceHeader(Domain* domain, double timestep) {
<< " : xi=" << xi << " eta=" << eta << std::endl;
#endif
// Only LB mixing rule is supported for now
LorentzBerthelotMixingRule* mixingrule = new LorentzBerthelotMixingRule();
std::shared_ptr<LorentzBerthelotMixingRule> mixingrule = std::make_shared<LorentzBerthelotMixingRule>();
mixingrule->setCid1(cidi);
mixingrule->setCid2(cidj);
mixingrule->setEta(eta);
Expand Down
3 changes: 2 additions & 1 deletion src/molecules/Comp2Param.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define COMP2PARAM_H_

#include <vector>
#include <memory>

#include "molecules/Component.h"
#include "molecules/Array2D.h"
Expand All @@ -15,7 +16,7 @@
*/
class Comp2Param {

using MixRulesType = std::map<int,std::map<int,MixingRuleBase*>>;
using MixRulesType = std::map<int,std::map<int,std::shared_ptr<MixingRuleBase>>>;

public:
/** Create a new empty parameter stream. */
Expand Down

0 comments on commit c4103f5

Please sign in to comment.