From a945ef2c234963a97470ad9a67e4e07984e207f6 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 8 Oct 2015 19:05:52 -0400 Subject: [PATCH] Make use of initializer lists where appropriate --- samples/cxx/bvp/BoundaryValueProblem.h | 5 +-- samples/cxx/combustor/combustor.cpp | 19 ++++++----- samples/cxx/flamespeed/flamespeed.cpp | 6 +--- src/base/application.cpp | 2 +- src/kinetics/Kinetics.cpp | 4 +-- src/kinetics/RxnRates.cpp | 2 +- src/oneD/Sim1D.cpp | 5 +-- src/thermo/GeneralSpeciesThermo.cpp | 2 +- src/zeroD/ReactorNet.cpp | 2 +- test/general/string_processing.cpp | 9 ++--- test/kinetics/kineticsFromScratch.cpp | 34 ++++++++----------- test/kinetics/rates.cpp | 16 +++------ test_problems/diamondSurf/runDiamond.cpp | 5 +-- .../diamondSurf_dupl/runDiamondDupl.cpp | 12 ++----- .../stoichSolidKinetics.cpp | 14 +++----- .../surfSolverTest/surfaceSolver.cpp | 5 +-- .../surfSolverTest/surfaceSolver2.cpp | 17 ++-------- test_problems/surfkin/surfdemo.cpp | 7 ++-- 18 files changed, 54 insertions(+), 112 deletions(-) diff --git a/samples/cxx/bvp/BoundaryValueProblem.h b/samples/cxx/bvp/BoundaryValueProblem.h index d67ad4680..f4f6f7fbb 100644 --- a/samples/cxx/bvp/BoundaryValueProblem.h +++ b/samples/cxx/bvp/BoundaryValueProblem.h @@ -225,10 +225,7 @@ protected: // Add dummy terminator domains on either side of this one. m_left = new Cantera::Empty1D; m_right = new Cantera::Empty1D; - std::vector domains; - domains.push_back(m_left); - domains.push_back(this); - domains.push_back(m_right); + std::vector domains { m_left, this, m_right }; // create the Sim1D instance that will control the // solution process diff --git a/samples/cxx/combustor/combustor.cpp b/samples/cxx/combustor/combustor.cpp index 1e5b65841..75a5c78a4 100644 --- a/samples/cxx/combustor/combustor.cpp +++ b/samples/cxx/combustor/combustor.cpp @@ -105,15 +105,16 @@ void runexample() double tres; std::ofstream f("combustor_cxx.csv"); - std::vector k_out; - k_out.push_back(gas.speciesIndex("CH4")); - k_out.push_back(gas.speciesIndex("O2")); - k_out.push_back(gas.speciesIndex("CO2")); - k_out.push_back(gas.speciesIndex("H2O")); - k_out.push_back(gas.speciesIndex("CO")); - k_out.push_back(gas.speciesIndex("OH")); - k_out.push_back(gas.speciesIndex("H")); - k_out.push_back(gas.speciesIndex("C2H6")); + std::vector k_out { + gas.speciesIndex("CH4"), + gas.speciesIndex("O2"), + gas.speciesIndex("CO2"), + gas.speciesIndex("H2O"), + gas.speciesIndex("CO"), + gas.speciesIndex("OH"), + gas.speciesIndex("H"), + gas.speciesIndex("C2H6") + }; while (tnow < tfinal) { tnow += 0.005; diff --git a/samples/cxx/flamespeed/flamespeed.cpp b/samples/cxx/flamespeed/flamespeed.cpp index b390c5137..405db1356 100644 --- a/samples/cxx/flamespeed/flamespeed.cpp +++ b/samples/cxx/flamespeed/flamespeed.cpp @@ -107,11 +107,7 @@ int flamespeed(double phi) //=================== create the container and insert the domains ===== - std::vector domains; - domains.push_back(&inlet); - domains.push_back(&flow); - domains.push_back(&outlet); - + std::vector domains { &inlet, &flow, &outlet }; Sim1D flame(domains); //----------- Supply initial guess---------------------- diff --git a/src/base/application.cpp b/src/base/application.cpp index d3f5407bd..b68558b72 100644 --- a/src/base/application.cpp +++ b/src/base/application.cpp @@ -225,7 +225,7 @@ XML_Node* Application::get_XML_File(const std::string& file, int debug) } } x->lock(); - xmlfiles[path] = std::make_pair(x, mtime); + xmlfiles[path] = {x, mtime}; return x; } diff --git a/src/kinetics/Kinetics.cpp b/src/kinetics/Kinetics.cpp index 322a8be37..b545d0ef2 100644 --- a/src/kinetics/Kinetics.cpp +++ b/src/kinetics/Kinetics.cpp @@ -226,12 +226,12 @@ std::pair Kinetics::checkDuplicates(bool throw_err) const "Reaction {}: {}\nReaction {}: {}\n", i+1, other.equation(), m+1, R.equation()); } else { - return make_pair(i,m); + return {i,m}; } } participants[key].push_back(i); } - return make_pair(npos, npos); + return {npos, npos}; } double Kinetics::checkDuplicateStoich(std::map& r1, diff --git a/src/kinetics/RxnRates.cpp b/src/kinetics/RxnRates.cpp index 7c1e48e46..26357b14b 100644 --- a/src/kinetics/RxnRates.cpp +++ b/src/kinetics/RxnRates.cpp @@ -78,7 +78,7 @@ Plog::Plog(const std::multimap& rates) double logp = std::log(rate.first); if (pressures_.empty() || pressures_.rbegin()->first != logp) { // starting a new group - pressures_[logp] = std::make_pair(j, j+1); + pressures_[logp] = {j, j+1}; } else { // another rate expression at the same pressure pressures_[logp].second = j+1; diff --git a/src/oneD/Sim1D.cpp b/src/oneD/Sim1D.cpp index 3358be3eb..5173a2bb6 100644 --- a/src/oneD/Sim1D.cpp +++ b/src/oneD/Sim1D.cpp @@ -28,10 +28,7 @@ Sim1D::Sim1D(vector& domains) : // set some defaults m_tstep = 1.0e-5; - m_steps.push_back(1); - m_steps.push_back(2); - m_steps.push_back(5); - m_steps.push_back(10); + m_steps = { 1, 2, 5, 10 }; } void Sim1D::setInitialGuess(const std::string& component, vector_fp& locs, vector_fp& vals) diff --git a/src/thermo/GeneralSpeciesThermo.cpp b/src/thermo/GeneralSpeciesThermo.cpp index fc535ff88..ec54466d0 100644 --- a/src/thermo/GeneralSpeciesThermo.cpp +++ b/src/thermo/GeneralSpeciesThermo.cpp @@ -84,7 +84,7 @@ void GeneralSpeciesThermo::install_STIT(size_t index, "GeneralSpeciesThermo::install_STIT", "Index position isn't null, duplication of assignment: {}", index); int type = stit_ptr->reportType(); - m_speciesLoc[index] = std::make_pair(type, m_sp[type].size()); + m_speciesLoc[index] = {type, m_sp[type].size()}; m_sp[type].emplace_back(index, stit_ptr); if (m_sp[type].size() == 1) { m_tpoly[type].resize(stit_ptr->temperaturePolySize()); diff --git a/src/zeroD/ReactorNet.cpp b/src/zeroD/ReactorNet.cpp index d8a9fac91..76d0011f8 100644 --- a/src/zeroD/ReactorNet.cpp +++ b/src/zeroD/ReactorNet.cpp @@ -200,7 +200,7 @@ void ReactorNet::registerSensitivityReaction(void* reactor, "Sensitivity reactions cannot be added after the" "integrator has been initialized."); } - std::pair R = std::make_pair(reactor, leftright); + std::pair R = {reactor, leftright}; if (m_sensOrder.count(R) && m_sensOrder[R].count(reactionIndex)) { throw CanteraError("ReactorNet::registerSensitivityReaction", diff --git a/test/general/string_processing.cpp b/test/general/string_processing.cpp index 9f4e461fe..6a6c28b42 100644 --- a/test/general/string_processing.cpp +++ b/test/general/string_processing.cpp @@ -26,10 +26,7 @@ TEST(parseCompString, extra_spaces) TEST(parseCompString, default_values) { - std::vector x; - x.push_back("foo"); - x.push_back("bar"); - x.push_back("baz"); + std::vector x = { "foo", "bar", "baz" }; compositionMap c = parseCompString("foo:1.0 baz:2", x); ASSERT_EQ((size_t) 3, c.size()); ASSERT_FALSE(c.find("bar") == c.end()); @@ -48,9 +45,7 @@ TEST(parseCompString, delimiters) TEST(parseCompString, missing_element) { - std::vector x; - x.push_back("foo"); - x.push_back("bar"); + std::vector x = { "foo", "bar" }; ASSERT_THROW(parseCompString("foo:1.0 bar:2 baz:1e-4", x), CanteraError); } diff --git a/test/kinetics/kineticsFromScratch.cpp b/test/kinetics/kineticsFromScratch.cpp index 1ee608e88..18e7d65bb 100644 --- a/test/kinetics/kineticsFromScratch.cpp +++ b/test/kinetics/kineticsFromScratch.cpp @@ -117,11 +117,7 @@ TEST_F(KineticsFromScratch, add_falloff_reaction) Composition prod = parseCompString("H2O2:1"); Arrhenius high_rate(7.4e10, -0.37, 0.0); Arrhenius low_rate(2.3e12, -0.9, -1700.0 / GasConst_cal_mol_K); - vector_fp falloff_params; - falloff_params.push_back(0.7346); - falloff_params.push_back(94.0); - falloff_params.push_back(1756.0); - falloff_params.push_back(5182.0); + vector_fp falloff_params { 0.7346, 94.0, 1756.0, 5182.0 }; ThirdBody tbody; tbody.efficiencies = parseCompString("AR:0.7 H2:2.0 H2O:6.0"); shared_ptr R(new FalloffReaction(reac, prod, low_rate, @@ -142,12 +138,12 @@ TEST_F(KineticsFromScratch, add_plog_reaction) // [(100.0, 'atm'), 5.963200e+56, -11.529, 52599.6]) Composition reac = parseCompString("H2:1, O2:1"); Composition prod = parseCompString("OH:2"); - std::multimap rates; - typedef std::multimap::value_type item; - rates.insert(item(0.01*101325, Arrhenius(1.212400e+16, -0.5779, 10872.7 / GasConst_cal_mol_K))); - rates.insert(item(1.0*101325, Arrhenius(4.910800e+31, -4.8507, 24772.8 / GasConst_cal_mol_K))); - rates.insert(item(10.0*101325, Arrhenius(1.286600e+47, -9.0246, 39796.5 / GasConst_cal_mol_K))); - rates.insert(item(100.0*101325, Arrhenius(5.963200e+56, -11.529, 52599.6 / GasConst_cal_mol_K))); + std::multimap rates { + { 0.01*101325, Arrhenius(1.212400e+16, -0.5779, 10872.7 / GasConst_cal_mol_K) }, + { 1.0*101325, Arrhenius(4.910800e+31, -4.8507, 24772.8 / GasConst_cal_mol_K) }, + { 10.0*101325, Arrhenius(1.286600e+47, -9.0246, 39796.5 / GasConst_cal_mol_K) }, + { 100.0*101325, Arrhenius(5.963200e+56, -11.529, 52599.6 / GasConst_cal_mol_K) } + }; shared_ptr R(new PlogReaction(reac, prod, Plog(rates))); kin.addReaction(R); @@ -159,12 +155,12 @@ TEST_F(KineticsFromScratch, plog_invalid_rate) { Composition reac = parseCompString("H2:1, O2:1"); Composition prod = parseCompString("OH:2"); - std::multimap rates; - typedef std::multimap::value_type item; - rates.insert(item(0.01*101325, Arrhenius(1.2124e+16, -0.5779, 10872.7 / GasConst_cal_mol_K))); - rates.insert(item(10.0*101325, Arrhenius(1e15, -1, 10000 / GasConst_cal_mol_K))); - rates.insert(item(10.0*101325, Arrhenius(-2e20, -2.0, 20000 / GasConst_cal_mol_K))); - rates.insert(item(100.0*101325, Arrhenius(5.9632e+56, -11.529, 52599.6 / GasConst_cal_mol_K))); + std::multimap rates { + { 0.01*101325, Arrhenius(1.2124e+16, -0.5779, 10872.7 / GasConst_cal_mol_K) }, + { 10.0*101325, Arrhenius(1e15, -1, 10000 / GasConst_cal_mol_K) }, + { 10.0*101325, Arrhenius(-2e20, -2.0, 20000 / GasConst_cal_mol_K) }, + { 100.0*101325, Arrhenius(5.9632e+56, -11.529, 52599.6 / GasConst_cal_mol_K) } + }; shared_ptr R(new PlogReaction(reac, prod, Plog(rates))); ASSERT_THROW(kin.addReaction(R), CanteraError); @@ -324,9 +320,7 @@ public: , surf("../data/sofc-test.xml", "metal_surface") , surf_ref("../data/sofc-test.xml", "metal_surface") { - std::vector th; - th.push_back(&surf_ref); - th.push_back(&gas_ref); + std::vector th = { &surf_ref, &gas_ref }; importKinetics(surf_ref.xml(), th, &kin_ref); kin.addPhase(surf); kin.addPhase(gas); diff --git a/test/kinetics/rates.cpp b/test/kinetics/rates.cpp index cb944aa53..83a8dbe41 100644 --- a/test/kinetics/rates.cpp +++ b/test/kinetics/rates.cpp @@ -9,14 +9,12 @@ namespace Cantera TEST(FracCoeff, ConvertFracCoeff) { IdealGasPhase thermo1("../data/frac.cti", "gas"); - std::vector phases1; - phases1.push_back(&thermo1); + std::vector phases1 { &thermo1 }; GasKinetics kinetics1; importKinetics(thermo1.xml(), phases1, &kinetics1); IdealGasPhase thermo2("../data/frac.xml", "gas"); - std::vector phases2; - phases2.push_back(&thermo2); + std::vector phases2 { &thermo2 }; GasKinetics kinetics2; importKinetics(thermo2.xml(), phases2, &kinetics2); @@ -40,8 +38,7 @@ public: FracCoeffTest() : therm("../data/frac.xml", "gas") { - std::vector phases; - phases.push_back(&therm); + std::vector phases { &therm }; importKinetics(therm.xml(), phases, &kin); therm.setState_TPX(2000, 4*OneAtm, "H2O:0.5, OH:.05, H:0.1, O2:0.15, H2:0.2"); @@ -150,8 +147,7 @@ public: NegativePreexponentialFactor() {} void setup(const std::string& infile) { therm.reset(newPhase(infile)); - std::vector phases; - phases.push_back(therm.get()); + std::vector phases { therm.get() }; importKinetics(therm->xml(), phases, &kin); therm->setState_TPX(2000, OneAtm, "H2O:1.0, H:0.2, O2:0.3, NH:0.05, NO:0.05, N2O:0.05"); @@ -199,9 +195,7 @@ TEST_F(NegativePreexponentialFactor, fromXml) TEST(InterfaceReaction, CoverageDependency) { IdealGasPhase gas("ptcombust.cti", "gas"); SurfPhase surf("ptcombust.cti", "Pt_surf"); - std::vector phases; - phases.push_back(&gas); - phases.push_back(&surf); + std::vector phases { &gas, &surf }; shared_ptr kin(newKineticsMgr(surf.xml(), phases)); ASSERT_EQ(kin->nReactions(), 25); diff --git a/test_problems/diamondSurf/runDiamond.cpp b/test_problems/diamondSurf/runDiamond.cpp index 875a251a0..fe055466e 100644 --- a/test_problems/diamondSurf/runDiamond.cpp +++ b/test_problems/diamondSurf/runDiamond.cpp @@ -57,10 +57,7 @@ int main(int argc, char** argv) size_t nsp_d100 = diamond100TP->nSpecies(); cout << "Number of species in diamond_100 = " << nsp_d100 << endl; - vector phaseList; - phaseList.push_back(gasTP); - phaseList.push_back(diamondTP); - phaseList.push_back(diamond100TP); + vector phaseList { gasTP, diamondTP, diamond100TP }; InterfaceKinetics* iKin_ptr = new InterfaceKinetics(); importKinetics(*xs, phaseList, iKin_ptr); size_t nr = iKin_ptr->nReactions(); diff --git a/test_problems/diamondSurf_dupl/runDiamondDupl.cpp b/test_problems/diamondSurf_dupl/runDiamondDupl.cpp index 3a31b8d82..a9a43a413 100644 --- a/test_problems/diamondSurf_dupl/runDiamondDupl.cpp +++ b/test_problems/diamondSurf_dupl/runDiamondDupl.cpp @@ -49,10 +49,7 @@ int main(int argc, char** argv) int nsp_d100 = diamond100TP->nSpecies(); cout << "Number of species in diamond_100 = " << nsp_d100 << endl; - vector phaseList; - phaseList.push_back(gasTP); - phaseList.push_back(diamondTP); - phaseList.push_back(diamond100TP); + vector phaseList { gasTP, diamondTP, diamond100TP }; InterfaceKinetics* iKin_ptr = new InterfaceKinetics(); importKinetics(*xs, phaseList, iKin_ptr); int nr = iKin_ptr->nReactions(); @@ -142,11 +139,8 @@ int main(int argc, char** argv) ThermoPhase* diamondTP_dupl = diamondTP->duplMyselfAsThermoPhase(); ThermoPhase* diamond100TP_dupl = diamond100TP->duplMyselfAsThermoPhase(); - - vector phaseList_dupl; - phaseList_dupl.push_back(gasTP_dupl); - phaseList_dupl.push_back(diamondTP_dupl); - phaseList_dupl.push_back(diamond100TP_dupl); + vector phaseList_dupl { gasTP_dupl, diamondTP_dupl, + diamond100TP_dupl }; InterfaceKinetics* iKin_ptr_dupl = new InterfaceKinetics(); importKinetics(*xs, phaseList_dupl, iKin_ptr_dupl); int nr_dupl = iKin_ptr_dupl->nReactions(); diff --git a/test_problems/stoichSolidKinetics/stoichSolidKinetics.cpp b/test_problems/stoichSolidKinetics/stoichSolidKinetics.cpp index de1acc0e2..771bc804d 100644 --- a/test_problems/stoichSolidKinetics/stoichSolidKinetics.cpp +++ b/test_problems/stoichSolidKinetics/stoichSolidKinetics.cpp @@ -57,16 +57,10 @@ void testProblem() unique_ptr feo_s(newPhase("solidPhases.xml", "FeO(S)")); unique_ptr fe_s(newPhase("solidPhases.xml", "Fe(S)")); - vector phaseList; - phaseList.push_back(gasTP.get()); - phaseList.push_back(cao_s.get()); - phaseList.push_back(caco3_s.get()); - phaseList.push_back(c_s.get()); - phaseList.push_back(fe3o4_s.get()); - phaseList.push_back(feo_s.get()); - phaseList.push_back(fe_s.get()); - phaseList.push_back(surfTP.get()); - + vector phaseList { + gasTP.get(), cao_s.get(), caco3_s.get(), c_s.get(), fe3o4_s.get(), + feo_s.get(), fe_s.get(), surfTP.get() + }; InterfaceKinetics iKin; importKinetics(*xg, phaseList, &iKin); diff --git a/test_problems/surfSolverTest/surfaceSolver.cpp b/test_problems/surfSolverTest/surfaceSolver.cpp index d9a36cc6e..e1d7d516e 100644 --- a/test_problems/surfSolverTest/surfaceSolver.cpp +++ b/test_problems/surfSolverTest/surfaceSolver.cpp @@ -244,10 +244,7 @@ int main(int argc, char** argv) cout << "Number of species in surface phase, " << surfParticlePhaseName << " = " << nsp_d100 << endl; - vector phaseList; - phaseList.push_back(gasTP); - phaseList.push_back(bulkPhaseTP); - phaseList.push_back(surfPhaseTP); + vector phaseList { gasTP, bulkPhaseTP, surfPhaseTP }; InterfaceKinetics* iKin_ptr = new InterfaceKinetics(); importKinetics(*xs, phaseList, iKin_ptr); diff --git a/test_problems/surfSolverTest/surfaceSolver2.cpp b/test_problems/surfSolverTest/surfaceSolver2.cpp index 08f3e2eb1..0eed5e765 100644 --- a/test_problems/surfSolverTest/surfaceSolver2.cpp +++ b/test_problems/surfSolverTest/surfaceSolver2.cpp @@ -242,13 +242,7 @@ int main(int argc, char** argv) cout << "Number of species in surface phase, " << surfParticlePhaseName << " = " << nsp_d100 << endl; - vector phaseList; - phaseList.push_back(gasTP); - phaseList.push_back(bulkPhaseTP); - phaseList.push_back(surfPhaseTP); - - - + vector phaseList { gasTP, bulkPhaseTP, surfPhaseTP }; InterfaceKinetics* iKin_ptr = new InterfaceKinetics(); importKinetics(*xs, phaseList, iKin_ptr); size_t nr = iKin_ptr->nReactions(); @@ -265,10 +259,7 @@ int main(int argc, char** argv) cout << "Number of species in 2nd surface phase, " << pname << " = " << nsp2 << endl; - vector phaseList2; - phaseList2.push_back(gasTP); - phaseList2.push_back(bulkPhaseTP); - phaseList2.push_back(surfPhaseTP2); + vector phaseList2 { gasTP, bulkPhaseTP, surfPhaseTP2 }; // create the second InterfaceKinetics object based on the // second surface phase. @@ -329,9 +320,7 @@ int main(int argc, char** argv) * Set-up the Surface Problem * This problem will consist of 2 identical InterfaceKinetics objects */ - vector vecKinPtrs; - vecKinPtrs.push_back(iKin_ptr); - vecKinPtrs.push_back(iKin2_ptr); + vector vecKinPtrs { iKin_ptr, iKin2_ptr }; // Create the ImplicitSurfChem problem // Initialize it and call the pseudo steadystate capability. diff --git a/test_problems/surfkin/surfdemo.cpp b/test_problems/surfkin/surfdemo.cpp index 83deb19dd..513effa59 100644 --- a/test_problems/surfkin/surfdemo.cpp +++ b/test_problems/surfkin/surfdemo.cpp @@ -16,12 +16,9 @@ int main() gas.setState_TPX(1200.0, OneAtm, "H2:2, O2:1, OH:0.01, H:0.01, O:0.01"); - vector phases; - phases.push_back(&gas); + vector phases { &gas }; Interface surf("surface.xml", "surface", phases); - vector_fp cov; - cov.push_back(0.8); - cov.push_back(0.2); + vector_fp cov { 0.8, 0.2 }; cout.precision(4); surf.setCoverages(DATA_PTR(cov)); vector_fp wdot(gas.nSpecies() + surf.nSpecies());