Beispiel #1
0
/* print vector of statistics to outs */
void printStatisticsVector(pmols::HJStatistics &stat, std::ostream &outs) {
    glm::vec3 appos_point;
    std::tuple<float, float, float> box_size;
    std::tie(appos_point, box_size) = stat.GetBoundingBox();

    outs << "\"" << stat.PackedMolsNumber() << "\"" << ", "         // number of packed mols (0)
         << "\"" << vec_to_string(appos_point) << "\"" << ", "      // apposition point of bounding box (1)

         << "\"" << std::get<0>(box_size) << "x" << std::get<1>(box_size) << "x" << std::get<2>(box_size) << "\"" << ", "
                                                                    // size of bounding box (2)

         << "\"" << stat.TotalAtomDistance() << "\"" << ", "        // total distance between atoms (3)
         << "\"" << stat.AvgAtomDistance() << "\"" << ", "          // average distance between atoms (4)

         << "\"" << stat.IntersectionsNumber() << "\"" << ", "      // total number of intersections between atoms (5)
         << "\"" << stat.TotalIntersection() << "\"" << ", "        // total value of intersections  ... (6)
         << "\"" << stat.AvgIntersection() << "\"" << ", "          // average value of intersections ... (7)
         << "\"" << stat.MaxIntersection() << "\"" << ", "          // max value of intersections ... (8)

         << "\"" << stat.EmptyCellsNumber() << "\"" << ", "         // number of empty cells ... (9)

         << "\"" << stat.TotalIterationsNumber() << "\"" << ", "    // total number of iterations (10)
         << "\"" << stat.ESIterationsNumber() << "\"" << ", "       // number of exploring search iterations (11)
         << "\"" << stat.PSIterationsNumber() << "\"" << ", "       // number of pattern search iterations (12)
         << "\"" << stat.PSFalsePositiveIterationsNumber() << "\""; // number of false positive iterations of pattern search (13)
}
    bool RangeLookupInBST::test() {
        vector<int> preOrder = {19, 7, 3, 2, 5, 11, 17, 13, 43, 23, 37, 29, 31, 41, 47, 53};
        unique_ptr<BSTNode<int>> tree = createBSTFromPreorder(preOrder);
        Interval interval{16, 31};

        vector<int> ans = {17,19, 23,29,31};
        vector<int> res = rangeLookupInBST(tree, interval);

        if (res != ans) {
            cout << "Should be " << vec_to_string(ans) << endl;
            cout << "Result " << vec_to_string(ans) << endl;

            return false;
        }

        return true;
    }
Beispiel #3
0
    bool PlusOne::test() {
        for (int i = 0; i != 131; ++i) {
            vector<int> input = generateVectorFromNum(i);

            vector<int> shouldBe = plusOneBruteForce(input);
            vector<int> res = plusOne(input);

            if (res != shouldBe) {
                cout << "Increase " << vec_to_string(input)
                        << " by one is " << vec_to_string(res)
                        << endl;
                return false;
            }
        }

        return true;
    }
bool CanReachEnd::test() {
    int temp_arr1[] = { 3, 3, 1, 0, 2, 0, 1 };
    vector<int> a ( temp_arr1, temp_arr1 + sizeof(temp_arr1) / sizeof(temp_arr1[0]) );

    int temp_arr2[] = { 3, 2, 0, 0, 2, 0, 1 };
    vector<int> b ( temp_arr2, temp_arr2 + sizeof(temp_arr2) / sizeof(temp_arr2[0]) );

    cout << "In " << vec_to_string(a) << ", can ";
    if (!canReachEnd(a))
        cout << "not ";
    cout << "reach the end. " << endl;

    cout << "In " << vec_to_string(b) << ", can ";
    if (!canReachEnd(b))
        cout << "not ";
    cout << "reach the end. " << endl;

    return true;
}
    bool StackByHeap::test() {
        vector<int> input = {1, 2, 3, 4, 5};
        Stack s;
        for (int i : input) {
            s.push(i);
        }

        vector<int> ans = {5, 4, 3, 2, 1};
        vector<int> res;
        while (!s.empty()) {
            res.push_back(s.pop());
        }

        if (ans != res) {
            cout << "Should be " << vec_to_string(ans) << endl;
            cout << "Result " << vec_to_string(res) << endl;
            return false;
        }

        return true;
    }
    bool NextPermutation::test() {
        vector<int> perm1 = {1, 0, 2, 3};
        vector<int> perm2 = {1, 0, 3, 2};
        vector<int> perm3 = {0, 3, 2, 1};
        vector<int> perm4 = {3, 2, 1, 0};
        vector<int> perm5 = {6, 2, 1, 5, 4, 3, 0};
        vector<int> perm6 = {2, 1, 0, 1};
        vector<int> perm7 = {2, 1, 1, 3};
        vector<int> perm8 = {2, 3, 1, 1};
        vector<vector<int>> perms = {perm1, perm2, perm3, perm4, perm5, perm6, perm7, perm8};

        for (vector<int> perm : perms) {
            if (nextPermutationBruteForce(perm) != nextPermutation(perm)) {
                cout << "Should be: " << vec_to_string(nextPermutationBruteForce(perm)) << endl;
                cout << "Result: " << vec_to_string(nextPermutation(perm)) << endl;
                return false;
            }
        }

        return true;
    }
    bool GeneratePowerSet::test() {
        vector<int> array = {1, 2, 3, 2};
        vector<vector<int>> res = generatePowerSet(array);

        if (res.size() != 12) {
            cout << "Wrong number of results" << endl;
            for (vector<int> vec : res) {
                cout << vec_to_string(vec) << endl;
            }

            return false;
        }

        return true;
    }
Beispiel #8
0
    bool NQueens::test() {
        vector<vector<int>> res = nQueens(4);

        vector<int> ans1 = {1, 3, 0, 2};
        vector<int> ans2 = {2, 0, 3, 1};
        for (vector<int> vec : res) {
            if (vec != ans1 && vec != ans2) {
                cout << "Wrong ans " << endl;
                cout << vec_to_string(vec) << endl;

                return false;
            }
        }

        return true;
    }
Beispiel #9
0
void _PropsSImulti(const std::vector<std::string> &Outputs,
                   const std::string &Name1,
                   const std::vector<double> &Prop1,
                   const std::string &Name2,
                   const std::vector<double> &Prop2,
                   const std::string &backend,
                   const std::vector<std::string> &fluids,
                   const std::vector<double> &fractions,
                   std::vector<std::vector<double> > &IO)
{
    shared_ptr<AbstractState> State;
    CoolProp::parameters key1, key2;
    CoolProp::input_pairs input_pair;
    std::vector<output_parameter> output_parameters;
    std::vector<double> v1, v2;

    try{
        // Initialize the State class
        _PropsSI_initialize(backend, fluids, fractions, State);
    }
    catch(std::exception &e){
        // Initialization failed.  Stop.
        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{
        // Get update pair
        is_valid_parameter(Name1, key1);
        is_valid_parameter(Name2, key2);
        input_pair = generate_update_pair(key1, Prop1, key2, Prop2, v1, v2);
    }
    catch (std::exception &e){
        // Input parameter parsing failed.  Stop
        throw ValueError(format("Input pair parsing failed for Name1: \"%s\", Name2: \"%s\"; err: %s", Name1.c_str(), Name2.c_str(), e.what()));
    }

    try{
        output_parameters = output_parameter::get_output_parameters(Outputs);
    }
    catch (std::exception &e){
        // Output parameter parsing failed.  Stop.
        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
    _PropsSI_outputs(State, output_parameters, input_pair, v1, v2, IO);
}
Beispiel #10
0
void _PropsSI_outputs(shared_ptr<AbstractState> &State,
	     			 const std::vector<output_parameter> &output_parameters,
		    		 CoolProp::input_pairs input_pair,
			    	 const std::vector<double> &in1,
			    	 const std::vector<double> &in2,
			    	 std::vector<std::vector<double> > &IO){

	// Check the inputs
	if (in1.size() != in2.size()){ throw ValueError(format("lengths of in1 [%d] and in2 [%d] are not the same", in1.size(), in2.size()));}
	bool one_input_one_output = (in1.size() == 1 && in2.size() == 1 && output_parameters.size() == 1);
    // If all trivial outputs, never do a state update
    bool all_trivial_outputs = true;
    for (std::size_t j = 0; j < output_parameters.size(); ++j){
        if (output_parameters[j].type != output_parameter::OUTPUT_TYPE_TRIVIAL){
            all_trivial_outputs = false;
        }
    }
    parameters p1, p2;
    // If all outputs are also inputs, never do a state update
    bool all_outputs_in_inputs = true;
    if (input_pair != INPUT_PAIR_INVALID){
        // Split the input pair into parameters
        split_input_pair(input_pair, p1, p2);
        // See if each parameter is in the output vector and is a normal type input
        for (std::size_t j = 0; j < output_parameters.size(); ++j){
            if (output_parameters[j].type != output_parameter::OUTPUT_TYPE_NORMAL){
                all_outputs_in_inputs = false; break;
            }
            if (!(output_parameters[j].Of1 == p1 || output_parameters[j].Of1 == p2)){
                all_outputs_in_inputs = false; break;
            }
        }
    }
    else{
        if (!all_trivial_outputs){
            throw ValueError(format("Input pair variable is invalid and output(s) are non-trivial; cannot do state update"));
        }
        all_outputs_in_inputs = false;
    }

	if (get_debug_level() > 100)
	{
	   std::cout << format("%s (%d): input pair = %d ",__FILE__,__LINE__, input_pair)          << std::endl;
	   std::cout << format("%s (%d): in1 = %s ",__FILE__,__LINE__, vec_to_string(in1).c_str()) << std::endl;
	   std::cout << format("%s (%d): in2 = %s ",__FILE__,__LINE__, vec_to_string(in2).c_str()) << std::endl;
	}

	// Resize the output matrix
    std::size_t N1 = std::max(static_cast<std::size_t>(1), in1.size());
    std::size_t N2 = std::max(static_cast<std::size_t>(1), output_parameters.size());
	IO.resize(N1, std::vector<double>(N2, _HUGE));

    // Throw an error if at the end, there were no successes
    bool success = false;

    if (get_debug_level() > 100)
    {
        std::cout << format("%s (%d): Iterating over %d input value pairs.",__FILE__,__LINE__,IO.size()) << std::endl;
    }
	// Iterate over the state variable inputs
	for (std::size_t i = 0; i < IO.size(); ++i){
		try{
            if (input_pair != INPUT_PAIR_INVALID && !all_trivial_outputs && !all_outputs_in_inputs){
                // Update the state since it is a valid set of inputs
                State->update(input_pair, in1[i], in2[i]);
            }
        }
        catch(...){
            if (one_input_one_output){IO.clear(); throw;} // Re-raise the exception since we want to bubble the error
            // All the outputs are filled with _HUGE; go to next input
            for (std::size_t j = 0; j < IO[i].size(); ++j){ IO[i][j] = _HUGE; }
            continue;
        }

        for (std::size_t j = 0; j < IO[i].size(); ++j){
            // If all the outputs are inputs, there is no need for a state input
            if (all_outputs_in_inputs){
                if (p1 == output_parameters[j].Of1){
                    IO[i][j] = in1[i]; success = true; continue;
                }
                else if (p2 == output_parameters[j].Of1){
                    IO[i][j] = in2[i]; success = true; continue;
                }
                else{
                    throw ValueError();
                }
            }
            try{
                const output_parameter &output = output_parameters[j];
                switch (output.type){
                    case output_parameter::OUTPUT_TYPE_TRIVIAL:
                    case output_parameter::OUTPUT_TYPE_NORMAL:
                        IO[i][j] = State->keyed_output(output.Of1); break;
                    case output_parameter::OUTPUT_TYPE_FIRST_DERIVATIVE:
                        IO[i][j] = State->first_partial_deriv(output.Of1, output.Wrt1, output.Constant1); break;
                    case output_parameter::OUTPUT_TYPE_FIRST_SATURATION_DERIVATIVE:
                        IO[i][j] = State->first_saturation_deriv(output.Of1, output.Wrt1); break;
                    case output_parameter::OUTPUT_TYPE_SECOND_DERIVATIVE:
                        IO[i][j] = State->second_partial_deriv(output.Of1, output.Wrt1, output.Constant1, output.Wrt2, output.Constant2); break;
                    default:
                        throw ValueError(format("")); break;
                }
                // At least one has succeeded
                success = true;
            }
            catch(...){
                if (one_input_one_output){IO.clear(); throw;} // Re-raise the exception since we want to bubble the error
                IO[i][j] = _HUGE;
            }
        }
	}
    if (success == false) { IO.clear(); throw ValueError(format("No outputs were able to be calculated"));}
}
Beispiel #11
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 #12
0
std::string extract_fractions(const std::string &fluid_string, std::vector<double> &fractions)
{
    if (has_fractions_in_string(fluid_string))
    {
        fractions.clear();
        std::vector<std::string> names;

        // Break up into pairs - like "Ethane[0.5]&Methane[0.5]" -> ("Ethane[0.5]","Methane[0.5]")
        std::vector<std::string> pairs = strsplit(fluid_string, '&');

        for (std::size_t i = 0; i < pairs.size(); ++i)
        {
            const std::string &fluid = pairs[i];

            // Must end with ']'
            if (fluid[fluid.size()-1] != ']')
                throw ValueError(format("Fluid entry [%s] must end with ']' character",pairs[i].c_str()));

            // Split at '[', but first remove the ']' from the end by taking a substring
            std::vector<std::string> name_fraction = strsplit(fluid.substr(0, fluid.size()-1), '[');

            if (name_fraction.size() != 2){throw ValueError(format("Could not break [%s] into name/fraction", fluid.substr(0, fluid.size()-1).c_str()));}

            // Convert fraction to a double
            char *pEnd;
            const std::string &name = name_fraction[0], &fraction = name_fraction[1];
            double f = strtod(fraction.c_str(), &pEnd);

            // If pEnd points to the last character in the string, it wasn't able to do the conversion
            if (pEnd == &(fraction[fraction.size()-1])){throw ValueError(format("Could not convert [%s] into number", fraction.c_str()));}

            // And add to vector
            fractions.push_back(f);

            // Add name
            names.push_back(name);
        }

        if (get_debug_level()>10) std::cout << format("%s:%d: Detected fractions of %s for %s.",__FILE__,__LINE__,vec_to_string(fractions).c_str(), (strjoin(names, "&")).c_str());
        // Join fluids back together
        return strjoin(names, "&");
    }
    else if (has_solution_concentration(fluid_string))
    {
        fractions.clear();
        double x;

        std::vector<std::string> fluid_parts = strsplit(fluid_string,'-');
        // Check it worked
        if (fluid_parts.size() != 2){
            throw ValueError(format("Format of incompressible solution string [%s] is invalid, should be like \"EG-20%\" or \"EG-0.2\" ", fluid_string.c_str()) );
        }

        // Convert the concentration into a string
        char* pEnd;
        x = strtod(fluid_parts[1].c_str(), &pEnd);

        // Check if per cent or fraction syntax is used
        if (!strcmp(pEnd,"%")){    x *= 0.01;}
        fractions.push_back(x);
        if (get_debug_level()>10) std::cout << format("%s:%d: Detected incompressible concentration of %s for %s.",__FILE__,__LINE__,vec_to_string(fractions).c_str(), fluid_parts[0].c_str());
        return fluid_parts[0];
    }
    else
    {
        return fluid_string;
    }
}
Beispiel #13
0
void exostore::load()
{
    std::ifstream in(db_path_, std::ifstream::binary);
    if (!in.is_open())  // File doesn't exist
    {
        return;
    }
    in.exceptions(std::ifstream::failbit | std::ifstream::badbit);

    // Add the keys to this map first, then copy only if there are no errors.
    boost::unordered_map<std::vector<unsigned char>, boost::any> temp_map;
    try
    {
        // Read header.
        std::vector<unsigned char> header = std::move(vec_from_file(in, 5));
        if (vec_to_string(header) != "EXODB")
        {
            throw exostore::load_error("Incorrect file header");
        }

        // Read number of keys.
        std::size_t num_keys;
        in.read(reinterpret_cast<char*>(&num_keys), sizeof(num_keys));

        for (std::size_t i = 0; i < num_keys; i++)
        {
            // Read the key size.
            std::size_t key_size;
            in.read(reinterpret_cast<char*>(&key_size), sizeof(key_size));
            // Read the key.
            auto key = std::move(vec_from_file(in, key_size));
            // Read the value marker.
            auto value_marker = std::move(vec_from_file(in, 4));
            std::string marker_str = vec_to_string(value_marker);
            if (marker_str == "BSTR")   // Binary string
            {
                // Read bstring length.
                std::size_t bstring_size;
                in.read(reinterpret_cast<char*>(&bstring_size),
                    sizeof(bstring_size));
                // Read bstring content.
                auto bstring_content = std::move(vec_from_file(in,
                    bstring_size));
                // Add to database.
                temp_map[key] = exostore::bstring(bstring_content);
            }
            else if(marker_str == "ZSET")   // Sorted set
            {
                // Read size of zset.
                std::size_t zset_size;
                in.read(reinterpret_cast<char*>(&zset_size), sizeof(zset_size));
                // Read score-member pairs into zset.
                exostore::zset zset;
                for (std::size_t j = 0; j < zset_size; j++)
                {
                    // Read score.
                    double score;
                    in.read(reinterpret_cast<char*>(&score), sizeof(score));
                    // Read member size.
                    std::size_t member_size;
                    in.read(reinterpret_cast<char*>(&member_size),
                        sizeof(member_size));
                    // Read member.
                    auto member = std::move(vec_from_file(in, member_size));
                    // Add to zset.
                    zset.add(member, score);
                }
                // Add to database.
                temp_map[key] = zset;
            }
        }
    }
    catch (std::ifstream::failure e)
    {
        throw exostore::load_error("Bad file format");
    }

    map_ = std::move(temp_map);
}
/**
@param volu_fractions The vector of volume fractions of the components
*/
void IncompressibleBackend::set_volu_fractions(const std::vector<long double> &volu_fractions){
	if (get_debug_level()>=10) std::cout << format("Incompressible backend: Called set_volu_fractions with %s ",vec_to_string(volu_fractions).c_str()) << std::endl;
	if (volu_fractions.size()!=1) throw ValueError(format("The incompressible backend only supports one entry in the volume fraction vector and not %d.",volu_fractions.size()));
	if (fluid->getxid()==IFRAC_PURE) {
		this->set_fractions(std::vector<long double>(1,0));
		if (get_debug_level()>=20) std::cout << format("Incompressible backend: Overwriting fractions for pure fluid with %s -> %s",vec_to_string(volu_fractions).c_str(),vec_to_string(this->_fractions).c_str()) << std::endl;
	} else if (fluid->getxid()==IFRAC_VOLUME) {
		this->set_fractions(volu_fractions);
	} else {
		std::vector<long double> tmp_fractions;
		for (std::size_t i = 0; i < volu_fractions.size(); i++) {
			tmp_fractions.push_back((long double) fluid->inputFromVolume(0.0, volu_fractions[i]));
	    }
		this->set_fractions(tmp_fractions);
	}
}
void IncompressibleBackend::update(CoolProp::input_pairs input_pair, double value1, double value2) {
    //if (mass_fractions.empty()){
    //    throw ValueError("mass fractions have not been set");
    //}

	if (get_debug_level()>=10) {
		//throw ValueError(format("%s (%d): You have to provide a dimension, 0 or 1, for the solver, %d is not valid. ",__FILE__,__LINE__,axis));
		std::cout << format("Incompressible backend: Called update with %d and %f, %f ",input_pair, value1, value2) << std::endl;
	}

	clear();

	if (get_debug_level()>=50) std::cout << format("Incompressible backend: _fractions are %s ",vec_to_string(_fractions).c_str()) << std::endl;
	if (fluid->is_pure()){
		this->_fluid_type = FLUID_TYPE_INCOMPRESSIBLE_LIQUID;
		if (get_debug_level()>=50) std::cout << format("Incompressible backend: Fluid type is  %d ",this->_fluid_type) << std::endl;
		if ((_fractions.size()!=1) || (_fractions[0]!=0.0)){
			throw ValueError(format("%s is a pure fluid. The composition has to be set to a vector with one entry equal to 0.0 or nothing. %s is not valid.",this->name().c_str(),vec_to_string(_fractions).c_str()));
		}
	} else {
		this->_fluid_type = FLUID_TYPE_INCOMPRESSIBLE_SOLUTION;
		if (get_debug_level()>=50) std::cout << format("Incompressible backend: Fluid type is  %d ",this->_fluid_type) << std::endl;
		if (_fractions.size()!=1 || ((_fractions[0]<0.0) || (_fractions[0]>1.0)) ){
			throw ValueError(format("%s is a solution or brine. Mass fractions must be set to a vector with one entry between 0 and 1. %s is not valid.",this->name().c_str(),vec_to_string(_fractions).c_str()));
		}
	}

	this->_phase = iphase_liquid;
	if (get_debug_level()>=50) std::cout << format("Incompressible backend: Phase type is  %d ",this->_phase) << std::endl;

	switch (input_pair)
	{
        case PT_INPUTS: {
            _p = value1;
            _T = value2;
            break;
        }
        case DmassP_INPUTS: {
        	_p = value2;
        	_T = this->DmassP_flash(value1,value2);
        	break;
        }
        case PUmass_INPUTS: {
        	_p = value1;
        	_T = this->PUmass_flash(value1, value2);
            break;
        }
        case PSmass_INPUTS: {
        	_p = value1;
        	_T = this->PSmass_flash(value1, value2);
            break;
        }
        case HmassP_INPUTS: {
        	_p = value2;
        	_T = this->HmassP_flash(value1, value2);
            break;
        }
        case QT_INPUTS: {
        	if (value1!=0) {throw ValueError("Incompressible fluids can only handle saturated liquid, Q=0.");}
        	_T = value2;
        	_p = fluid->psat(value2, _fractions[0]);
            break;
        }
        default: {
            throw ValueError(
                    format("This pair of inputs [%s] is not yet supported",
                            get_input_pair_short_desc(input_pair).c_str()));
        }
	}
	if (_p < 0){ throw ValueError("p is less than zero");}
    if (!ValidNumber(_p)){ throw ValueError("p is not a valid number");}
    if (_T < 0){ throw ValueError("T is less than zero");}
    if (!ValidNumber(_T)){ throw ValueError("T is not a valid number");}
    if (get_debug_level()>=50) std::cout << format("Incompressible backend: Update finished T=%f, p=%f, x=%s ",this->_T,this->_p,vec_to_string(_fractions).c_str()) << std::endl;
    fluid->checkTPX(_T,_p,_fractions[0]);
}
Beispiel #16
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());
    }
}
/**
@param fractions The vector of fractions of the components converted to the correct input
*/
void IncompressibleBackend::set_fractions(const std::vector<long double> &fractions){
	if (get_debug_level()>=10) std::cout << format("Incompressible backend: Called set_fractions with %s ",vec_to_string(fractions).c_str()) << std::endl;
	if (fractions.size()!=1) throw ValueError(format("The incompressible backend only supports one entry in the fraction vector and not %d.",fractions.size()));
	if ( ( this->_fractions.size()!=1 ) ||
		 ( this->_fractions[0]!=fractions[0] ) ) { // Change it!
		if (get_debug_level()>=20) std::cout << format("Incompressible backend: Updating the fractions triggered a change in reference state %s -> %s",vec_to_string(this->_fractions).c_str(),vec_to_string(fractions).c_str()) << std::endl;
		this->_fractions = fractions;
		fluid->set_reference_state(fluid->getTref(), fluid->getpref(), this->_fractions[0], fluid->gethref(), fluid->getsref());
	}
}