From f5de4d363f6c973174e97c87590fe926d07bd986 Mon Sep 17 00:00:00 2001 From: Ian Bell Date: Tue, 30 Dec 2014 20:38:49 -0500 Subject: [PATCH] Fixed initialization to remove copy and remove allocation error Signed-off-by: Ian Bell --- src/CoolProp.cpp | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/CoolProp.cpp b/src/CoolProp.cpp index f19ecad7..009f3da0 100644 --- a/src/CoolProp.cpp +++ b/src/CoolProp.cpp @@ -189,7 +189,6 @@ void _PropsSI_initialize(const std::string &backend, if (fluid_names.empty()){throw ValueError("fluid_names cannot be empty");} - std::vector fluids = fluids; Dictionary dict; // If a predefined mixture, set it up. Fractions and names are given @@ -214,28 +213,32 @@ void _PropsSI_initialize(const std::string &backend, } } else{ - std::vector fractions; + std::vector fractions(1, 1.0); // Default to one component, unity mole fraction + const std::vector *fractions_ptr; // Pointer to the array to be used; std::string fluid_string; - if (!z.empty()){ - // Make a copy of the provided fractions - fractions = z; - } if (fluid_names.size() == 1){ - // Extract fractions from the string if you can + // Extract fractions from the string if you can (or need to) fluid_string = extract_fractions(fluid_names[0], fractions); + // Reset the state + State.reset(AbstractState::factory(backend, fluid_string)); + // Set the pointer - we are going to use the extracted fractions + fractions_ptr = &fractions; + } + else{ + // Set the pointer - we are going to use the supplied fractions + fractions_ptr = &z; + // Reset the state + State.reset(AbstractState::factory(backend, fluid_names)); } - - // Reset the state - State.reset(AbstractState::factory(backend, fluid_string)); // Set the fraction for the state if (State->using_mole_fractions()){ - State->set_mole_fractions(fractions); + State->set_mole_fractions(*fractions_ptr); } else if (State->using_mass_fractions()){ - State->set_mass_fractions(fractions); + State->set_mass_fractions(*fractions_ptr); } else if (State->using_volu_fractions()){ - State->set_volu_fractions(fractions); + State->set_volu_fractions(*fractions_ptr); } else { if (get_debug_level()>50) std::cout << format("%s:%d: _PropsSI, could not set composition to %s, defaulting to mole fraction.\n",__FILE__,__LINE__, vec_to_string(z).c_str()).c_str(); } @@ -349,7 +352,7 @@ std::vector > PropsSImulti(const std::vector &O } catch(std::exception &e){ // Initialization failed. Stop. - throw ValueError(format("_PropsSI_initialize failed for backend: \"%s\", fluid: \"%s\" fractions \"%s\"; error: %s",backend.c_str(), strjoin(fluids,"&").c_str(), vec_to_string(fractions, "%0.10f").c_str(), e.what()) ); + throw ValueError(format("Initialize failed for backend: \"%s\", fluid: \"%s\" fractions \"%s\"; error: %s",backend.c_str(), strjoin(fluids,"&").c_str(), vec_to_string(fractions, "%0.10f").c_str(), e.what()) ); } try{ @@ -362,7 +365,7 @@ std::vector > PropsSImulti(const std::vector &O } catch (std::exception &e){ // Input parameter parsing failed. Stop - throw ValueError(format("_PropsSI input pair parsing failed for Name1: \"%s\", Name2: \"%s\"; error: %s", Name1.c_str(), Name2.c_str(),e.what())); + throw ValueError(format("Input pair parsing failed for Name1: \"%s\", Name2: \"%s\"; error: %s", Name1.c_str(), Name2.c_str(),e.what())); } try{ @@ -370,7 +373,7 @@ std::vector > PropsSImulti(const std::vector &O } catch (std::exception &e){ // Output parameter parsing failed. Stop. - throw ValueError(format("_PropsSI output parameter parsing failed; error: %s", e.what())); + throw ValueError(format("Output parameter parsing failed; error: %s", e.what())); } // Calculate the output(s). In the case of a failure, all values will be filled with _HUGE