Beispiel #1
0
void _PropsSI_initialize(const std::string &backend,
                         const std::vector<std::string> &fluid_names,
                         const std::vector<double> &z,
                         shared_ptr<AbstractState> &State){

    if (fluid_names.empty()){throw ValueError("fluid_names cannot be empty");}

    std::vector<double> fractions(1, 1.0); // Default to one component, unity fraction
    const std::vector<double> *fractions_ptr = NULL; // Pointer to the array to be used;

    if (fluid_names.size() > 1){
        // Set the pointer - we are going to use the supplied fractions; they must be provided
        fractions_ptr = &z;
        // Reset the state
        State.reset(AbstractState::factory(backend, fluid_names));
    }
    else if (fluid_names.size() == 1){
        if (has_fractions_in_string(fluid_names[0]) || has_solution_concentration(fluid_names[0])){
            // Extract fractions from the string
            std::string fluid_string = extract_fractions(fluid_names[0], fractions);
            // Set the pointer - we are going to use the extracted fractions
            fractions_ptr = &fractions;
            // Reset the state
            State.reset(AbstractState::factory(backend, fluid_string));
        }
        else{
            if (z.empty()){
                // Set the pointer - we are going to use the default fractions
                fractions_ptr = &fractions;
            }
            else{
                // Set the pointer - we are going to use the provided fractions
                fractions_ptr = &z;
            }
            // Reset the state
            State.reset(AbstractState::factory(backend, fluid_names));
        }
    }
    else { // The only path where fractions_ptr stays NULL
      throw ValueError("fractions_ptr is NULL");
    }
    if (!State->available_in_high_level()){
        throw ValueError("This AbstractState derived class cannot be used in the high-level interface; see www.coolprop.org/dev/coolprop/LowLevelAPI.html");
    }

    // Set the fraction for the state
    if (State->using_mole_fractions()){
        // If a predefined mixture or a pure fluid, the fractions will already be set
        if (State->get_mole_fractions().empty()){
            State->set_mole_fractions(*fractions_ptr);
        }
    } else if (State->using_mass_fractions()){
        State->set_mass_fractions(*fractions_ptr);
    } else if (State->using_volu_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();
    }
}
Beispiel #2
0
double PropsSI(const std::string &Output, const std::string &Name1, double Prop1, const std::string &Name2, double Prop2, const std::string &Ref)
{
    #if !defined(NO_ERROR_CATCHING)
    try{
    #endif

        // BEGIN OF TRY
        // Here is the real code that is inside the try block
    

        std::string backend, fluid;
        extract_backend(Ref, backend, fluid);
        std::vector<double> fractions(1, 1.0);
        // extract_fractions checks for has_fractions_in_string / has_solution_concentration; no need to double check
        std::string fluid_string = extract_fractions(fluid, fractions);
        std::vector<std::vector<double> > IO;
        _PropsSImulti(strsplit(Output,'&'), Name1, std::vector<double>(1, Prop1), Name2, std::vector<double>(1, Prop2), backend, strsplit(fluid_string, '&'), fractions, IO);
        if (IO.empty()){ throw ValueError(get_global_param_string("errstring").c_str()); }
        if (IO.size()!= 1 || IO[0].size() != 1){ throw ValueError(format("output should be 1x1; error was %s", get_global_param_string("errstring").c_str())); }

        double val = IO[0][0];

        if (get_debug_level() > 1){ std::cout << format("_PropsSI will return %g",val) << std::endl; }
        return val;
        // END OF TRY
    #if !defined(NO_ERROR_CATCHING)
    }
    catch(const std::exception& e){
        set_error_string(e.what() + format(" : PropsSI(\"%s\",\"%s\",%0.10g,\"%s\",%0.10g,\"%s\")",Output.c_str(),Name1.c_str(), Prop1, Name2.c_str(), Prop2, Ref.c_str()));
        #if defined (PROPSSI_ERROR_STDOUT)
        std::cout << e.what() << std::endl;
        #endif
        if (get_debug_level() > 1){std::cout << e.what() << std::endl;}
        return _HUGE;
    }
    catch(...){
        return _HUGE;
    }
    #endif
}
Beispiel #3
0
// Internal function to do the actual calculations, make this a wrapped function so
// that error bubbling can be done properly
double _PropsSI(const std::string &Output, const std::string &Name1, double Prop1, const std::string &Name2, double Prop2, const std::string &backend, const std::string &Ref, const std::vector<double> &z)
{
    parameters iOutput = iundefined_parameter;
    parameters iOf = iundefined_parameter, iWrt = iundefined_parameter, iConstant = iundefined_parameter, 
               iOf1 = iundefined_parameter, iWrt1 = iundefined_parameter, iConstant1 = iundefined_parameter, 
               iWrt2 = iundefined_parameter, iConstant2 = iundefined_parameter;
    double x1, x2;
	
    if (get_debug_level()>5){
        std::cout << format("%s:%d: _PropsSI(%s,%s,%g,%s,%g,%s,%s)\n",__FILE__,__LINE__,Output.c_str(),Name1.c_str(),Prop1,Name2.c_str(),Prop2,backend.c_str(),Ref.c_str(), vec_to_string(z).c_str()).c_str();
    }

    // The state we are going to use
    shared_ptr<AbstractState> State;
    
	// If the fractions of the components have been encoded in the string, extract them
	// If they have not, this function does nothing
	std::vector<double> fractions;
	if (z.empty())
	{
		// Make a one-element vector
		fractions = std::vector<double>(1, 1);
	}
	else{
		// Make a copy
		fractions = z;
	}
	
	std::string fluid_string = extract_fractions(Ref, fractions);
	
	// We are going to let the factory function load the state
	State.reset(AbstractState::factory(backend, fluid_string));
    
	// First check if it is a trivial input (critical/max parameters for instance)
	if (is_valid_parameter(Output, iOutput) && is_trivial_parameter(iOutput))
	{
		double val = State->trivial_keyed_output(iOutput);
		return val;
	}
	
	long iName1 = get_parameter_index(Name1);
	long iName2 = get_parameter_index(Name2);

	if (State->using_mole_fractions()){
		State->set_mole_fractions(fractions);
	} else if (State->using_mass_fractions()){
		State->set_mass_fractions(fractions);
	} else if (State->using_volu_fractions()){
		State->set_volu_fractions(fractions);
	} 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();
	}

	// Obtain the input pair
	CoolProp::input_pairs pair = generate_update_pair(iName1, Prop1, iName2, Prop2, x1, x2);

	// Update the state
	State->update(pair, x1, x2);
    
    if (iOutput != iundefined_parameter){
        // Get the desired output
        double val = State->keyed_output(iOutput);
        
        // Return the value
        return val;
    }
    else if (is_valid_first_derivative(Output, iOf, iWrt, iConstant)){
        // Return the desired output
        double val = State->first_partial_deriv(iOf, iWrt, iConstant);
        
        // Return the value
        return val;
    }
    else if (is_valid_second_derivative(Output, iOf1, iWrt1, iConstant1, iWrt2, iConstant2)){
        // Return the desired output
        double val = State->second_partial_deriv(iOf1, iWrt1, iConstant1, iWrt2, iConstant2);
        
        // Return the value
        return val;
    }
    else{
        throw ValueError(format("Output [%s] is not a parameter or a string representation of a derivative",Output.c_str()).c_str());
    }
}