mirror of
https://github.com/CoolProp/CoolProp.git
synced 2026-04-23 03:00:17 -04:00
* build: replace git submodules with CPM.cmake All 11 submodules (Eigen, fmtlib, msgpack-c, rapidjson, IF97, REFPROP-headers, multicomplex, Catch2, pybind11, ExcelAddinInstaller, FindMathematica) are now fetched by CPM.cmake at configure time. Set CPM_SOURCE_CACHE (e.g. ~/.cache/CPM) to share the download cache across git worktrees and build directories — no more per-worktree `git submodule update --init --recursive`. Vendored deps that have no upstream release cycle (miniz, nlohmann-json, incbin) remain in externals/ as before. Source-level changes: angle-bracket includes for rapidjson, IF97, and REFPROP-headers now that their directories are on the include path via CPM-provided source dirs rather than relative paths from the repo root. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(python): wire up CPM.cmake in Python wrapper, fix include paths The Python wrapper CMakeLists.txt was still referencing submodule paths (externals/Eigen, externals/fmtlib, externals/msgpack-c) removed by the CPM migration. Include CPM.cmake + dependencies.cmake from the root and use the ${Pkg_SOURCE_DIR} variables instead. Also adds missing rapidjson, IF97, and REFPROP_headers include dirs required by CoolProp sources. CI workflows drop the now-meaningless `submodules: recursive` checkout option and add a CPM source-cache step to avoid re-downloading on each macOS/Windows runner. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * docs: remove git submodule references after CPM.cmake migration Dependencies are now managed by CPM.cmake (fetched automatically at CMake configure time), so git submodules no longer exist. - Drop `--recursive` from all `git clone` commands in Web docs and wrapper READMEs (26 .rst/.md files) - Remove `submodules: recursive` from all CI workflow checkout steps (13 workflow files) - Remove `git submodule foreach/update` calls from release.bsh and delete the now-dead pybind11 security-workaround lines - Drop `--recursive` from build_swigged_matlab.sh and gitMirror.bsh - Update CONTRIBUTING.md: drop "and its submodules" phrasing Changelog entries referencing old submodule PRs are left intact as historical records. The --recursive in buildbot.rst is for the Dockerfiles repo (unrelated) and is also left unchanged. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(ci): remove empty 'with:' blocks after submodule removal, apply clang-format After removing 'submodules: recursive' from checkout steps, some workflow files were left with dangling empty 'with:' blocks that GitHub Actions rejects as workflow file errors. Remove the empty 'with:' in 9 workflows. Also apply clang-format to fix spacing in REFPROPMixtureBackend.cpp, HumidAirProp.cpp, and Helmholtz.cpp. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
341 lines
13 KiB
C++
341 lines
13 KiB
C++
#ifndef RAPIDJSON_COOLPROP_H
|
|
#define RAPIDJSON_COOLPROP_H
|
|
|
|
// On PowerPC, we are going to use the stdint.h integer types and not let rapidjson use its own
|
|
#if defined(__powerpc__)
|
|
typedef unsigned int UINT32;
|
|
# include "stdint.h"
|
|
# define RAPIDJSON_NO_INT64DEFINE
|
|
#endif
|
|
|
|
#include "Exceptions.h"
|
|
#include "CoolPropTools.h"
|
|
|
|
#include <rapidjson/rapidjson.h>
|
|
#include <rapidjson/document.h>
|
|
#include <rapidjson/filewritestream.h> // wrapper of C stream for prettywriter as output
|
|
#include <rapidjson/prettywriter.h> // for stringify JSON
|
|
#include <rapidjson/stringbuffer.h> // for string buffer
|
|
#include <rapidjson/schema.h>
|
|
|
|
#include <cassert>
|
|
|
|
namespace cpjson {
|
|
|
|
/// Convert a JSON-formatted string to a rapidjson::Document object
|
|
inline void JSON_string_to_rapidjson(const std::string& JSON_string, rapidjson::Document& doc) {
|
|
doc.Parse<0>(JSON_string.c_str());
|
|
if (doc.HasParseError()) {
|
|
throw CoolProp::ValueError("Unable to load JSON string");
|
|
}
|
|
}
|
|
|
|
struct value_information
|
|
{
|
|
bool isnull, isfalse, istrue, isbool, isobject, isarray, isnumber, isint, isint64, isuint, isuint64, isdouble, isstring;
|
|
};
|
|
inline value_information get_information(rapidjson::Value& v) {
|
|
value_information i;
|
|
i.isnull = v.IsNull();
|
|
i.isfalse = v.IsFalse();
|
|
i.istrue = v.IsTrue();
|
|
i.isbool = v.IsBool();
|
|
i.isobject = v.IsObject();
|
|
i.isarray = v.IsArray();
|
|
i.isnumber = v.IsNumber();
|
|
i.isint = v.IsInt();
|
|
i.isuint = v.IsUint();
|
|
i.isint64 = v.IsInt64();
|
|
i.isuint64 = v.IsUint64();
|
|
i.isdouble = v.IsDouble();
|
|
i.isstring = v.IsString();
|
|
return i;
|
|
};
|
|
|
|
inline std::string json2string(const rapidjson::Value& v) {
|
|
rapidjson::StringBuffer buffer;
|
|
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
|
|
|
v.Accept(writer);
|
|
return buffer.GetString();
|
|
}
|
|
/// A convenience function to get a double from a JSON value, including error checking
|
|
inline int get_integer(const rapidjson::Value& v, std::string m) {
|
|
if (!v.HasMember(m.c_str())) {
|
|
throw CoolProp::ValueError(format("Does not have member [%s]", m.c_str()));
|
|
}
|
|
const rapidjson::Value& el = v[m.c_str()];
|
|
if (!el.IsInt()) {
|
|
throw CoolProp::ValueError(format("Member [%s] is not an integer", m.c_str()));
|
|
} else {
|
|
return el.GetInt();
|
|
}
|
|
};
|
|
/// A convenience function to get a double from a JSON value, including error checking
|
|
inline double get_double(const rapidjson::Value& v, std::string m) {
|
|
if (!v.HasMember(m.c_str())) {
|
|
throw CoolProp::ValueError(format("Does not have member [%s]", m.c_str()));
|
|
}
|
|
const rapidjson::Value& el = v[m.c_str()];
|
|
if (!el.IsNumber()) {
|
|
throw CoolProp::ValueError(format("Member [%s] is not a number", m.c_str()));
|
|
} else {
|
|
return el.GetDouble();
|
|
}
|
|
};
|
|
/// A convenience function to get a bool from a JSON value, including error checking
|
|
inline bool get_bool(const rapidjson::Value& v, std::string m) {
|
|
if (!v.HasMember(m.c_str())) {
|
|
throw CoolProp::ValueError(format("Does not have member [%s]", m.c_str()));
|
|
}
|
|
const rapidjson::Value& el = v[m.c_str()];
|
|
if (!el.IsBool()) {
|
|
throw CoolProp::ValueError(format("Member [%s] is not a boolean", m.c_str()));
|
|
} else {
|
|
return el.GetBool();
|
|
}
|
|
};
|
|
/// A convenience function to get a string from a JSON value, including error checking
|
|
inline std::string get_string(const rapidjson::Value& v, std::string m) {
|
|
if (!v.HasMember(m.c_str())) {
|
|
throw CoolProp::ValueError(format("Does not have member [%s]", m.c_str()));
|
|
}
|
|
const rapidjson::Value& el = v[m.c_str()];
|
|
if (!el.IsString()) {
|
|
throw CoolProp::ValueError(format("Member [%s] is not a string", m.c_str()));
|
|
} else {
|
|
return el.GetString();
|
|
}
|
|
};
|
|
|
|
/// A convenience function to get a double array compactly
|
|
inline std::vector<double> get_double_array(const rapidjson::Value& v) {
|
|
std::vector<double> out;
|
|
if (!v.IsArray()) {
|
|
throw CoolProp::ValueError("input is not an array");
|
|
}
|
|
for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
|
|
if (!itr->IsNumber()) {
|
|
throw CoolProp::ValueError("input is not a number");
|
|
}
|
|
out.push_back(itr->GetDouble());
|
|
}
|
|
return out;
|
|
};
|
|
|
|
/// A convenience function to get a double array compactly
|
|
inline std::vector<double> get_double_array(const rapidjson::Value& v, std::string m) {
|
|
if (!v.HasMember(m.c_str())) {
|
|
throw CoolProp::ValueError(format("Does not have member [%s]", m.c_str()));
|
|
} else {
|
|
return get_double_array(v[m.c_str()]);
|
|
}
|
|
};
|
|
|
|
/// A convenience function to get a long double array compactly
|
|
inline std::vector<CoolPropDbl> get_long_double_array(const rapidjson::Value& v) {
|
|
std::vector<CoolPropDbl> out;
|
|
if (!v.IsArray()) {
|
|
throw CoolProp::ValueError("input is not an array");
|
|
}
|
|
for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
|
|
if (!itr->IsNumber()) {
|
|
throw CoolProp::ValueError("input is not a number");
|
|
}
|
|
out.push_back(itr->GetDouble());
|
|
}
|
|
return out;
|
|
};
|
|
|
|
/// A convenience function to get a 2D double array compactly
|
|
inline std::vector<std::vector<double>> get_double_array2D(const rapidjson::Value& v) {
|
|
std::vector<std::vector<double>> out;
|
|
std::vector<double> tmp;
|
|
if (!v.IsArray()) {
|
|
throw CoolProp::ValueError("input is not an array");
|
|
}
|
|
for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
|
|
// This is here for debugging purposes
|
|
// cpjson::value_information vi = cpjson::get_information((*itr));
|
|
if (!(itr->IsArray())) {
|
|
throw CoolProp::ValueError(format("input \"%s\" is not a 2D array", cpjson::json2string(v).c_str()));
|
|
}
|
|
tmp.clear();
|
|
for (rapidjson::Value::ConstValueIterator i = itr->Begin(); i != itr->End(); ++i) {
|
|
if (!i->IsNumber()) {
|
|
throw CoolProp::ValueError("input is not a number");
|
|
}
|
|
tmp.push_back(i->GetDouble());
|
|
}
|
|
out.push_back(tmp);
|
|
}
|
|
return out;
|
|
};
|
|
|
|
/// A convenience function to get a 2D long double array compactly
|
|
inline std::vector<std::vector<CoolPropDbl>> get_long_double_array2D(const rapidjson::Value& v) {
|
|
std::vector<std::vector<CoolPropDbl>> out;
|
|
std::vector<CoolPropDbl> tmp;
|
|
if (!v.IsArray()) {
|
|
throw CoolProp::ValueError("input is not an array");
|
|
}
|
|
for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
|
|
if (!itr->IsArray()) {
|
|
throw CoolProp::ValueError("input is not a 2D array");
|
|
}
|
|
tmp.clear();
|
|
for (rapidjson::Value::ConstValueIterator i = itr->Begin(); i != itr->End(); ++i) {
|
|
if (!i->IsNumber()) {
|
|
throw CoolProp::ValueError("input is not a number");
|
|
}
|
|
tmp.push_back(i->GetDouble());
|
|
}
|
|
out.push_back(tmp);
|
|
}
|
|
return out;
|
|
};
|
|
|
|
/// A convenience function to get a long double array compactly
|
|
inline std::vector<CoolPropDbl> get_long_double_array(const rapidjson::Value& v, std::string name) {
|
|
std::vector<CoolPropDbl> out;
|
|
if (!v.HasMember(name.c_str())) {
|
|
throw CoolProp::ValueError(format("Does not have member [%s]", name.c_str()));
|
|
}
|
|
if (!v[name.c_str()].IsArray()) {
|
|
throw CoolProp::ValueError("input is not an array");
|
|
}
|
|
for (rapidjson::Value::ConstValueIterator itr = v[name.c_str()].Begin(); itr != v[name.c_str()].End(); ++itr) {
|
|
if (!itr->IsNumber()) {
|
|
throw CoolProp::ValueError("input is not a number");
|
|
}
|
|
out.push_back(itr->GetDouble());
|
|
}
|
|
return out;
|
|
};
|
|
|
|
/// A convenience function to get a string array compactly
|
|
inline std::vector<std::string> get_string_array(const rapidjson::Value& v) {
|
|
std::vector<std::string> out;
|
|
if (!v.IsArray()) {
|
|
throw CoolProp::ValueError("input is not an array");
|
|
}
|
|
for (rapidjson::Value::ConstValueIterator itr = v.Begin(); itr != v.End(); ++itr) {
|
|
out.push_back(itr->GetString());
|
|
}
|
|
return out;
|
|
};
|
|
|
|
/// A convenience function to get a string array compactly
|
|
inline std::vector<std::string> get_string_array(const rapidjson::Value& v, std::string m) {
|
|
if (!v.HasMember(m.c_str())) {
|
|
throw CoolProp::ValueError(format("Does not have member [%s]", m.c_str()));
|
|
} else {
|
|
return get_string_array(v[m.c_str()]);
|
|
}
|
|
};
|
|
|
|
/// A convenience function to get a std::string from a JSON value
|
|
template <typename T>
|
|
inline std::string to_string(const T& v) {
|
|
rapidjson::StringBuffer buffer;
|
|
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
|
v.Accept(writer);
|
|
return buffer.GetString();
|
|
};
|
|
|
|
/// A convenience function to set a 2D array of double compactly
|
|
inline void set_double_array2D(const char* key, const std::vector<std::vector<double>>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
|
|
rapidjson::Value _i(rapidjson::kArrayType);
|
|
for (unsigned int i = 0; i < vec.size(); ++i) {
|
|
rapidjson::Value _j(rapidjson::kArrayType);
|
|
for (unsigned int j = 0; j < vec[i].size(); ++j) {
|
|
rapidjson::Value v(rapidjson::kNumberType);
|
|
v.SetDouble(vec[i][j]);
|
|
_j.PushBack(v, doc.GetAllocator());
|
|
}
|
|
_i.PushBack(_j, doc.GetAllocator());
|
|
}
|
|
value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _i, doc.GetAllocator());
|
|
};
|
|
|
|
/// A convenience function to set a string compactly
|
|
inline void set_string(const std::string& key, const std::string& s, rapidjson::Value& value, rapidjson::Document& doc) {
|
|
value.AddMember(rapidjson::Value(key.c_str(), doc.GetAllocator()).Move(), rapidjson::Value(s.c_str(), doc.GetAllocator()).Move(),
|
|
doc.GetAllocator());
|
|
};
|
|
|
|
/// A convenience function to set a string array compactly
|
|
inline void set_string_array(const char* key, const std::vector<std::string>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
|
|
rapidjson::Value _v(rapidjson::kArrayType);
|
|
for (unsigned int i = 0; i < vec.size(); ++i) {
|
|
_v.PushBack(rapidjson::Value(vec[i].c_str(), doc.GetAllocator()).Move(), doc.GetAllocator());
|
|
}
|
|
value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _v, doc.GetAllocator());
|
|
};
|
|
|
|
/// A convenience function to set an integer array compactly
|
|
inline void set_int_array(const char* key, const std::vector<int>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
|
|
rapidjson::Value _v(rapidjson::kArrayType);
|
|
for (unsigned int i = 0; i < vec.size(); ++i) {
|
|
_v.PushBack(vec[i], doc.GetAllocator());
|
|
}
|
|
value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _v, doc.GetAllocator());
|
|
};
|
|
|
|
/// A convenience function to set a double array compactly
|
|
inline void set_double_array(const char* key, const std::vector<double>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
|
|
rapidjson::Value _v(rapidjson::kArrayType);
|
|
for (unsigned int i = 0; i < vec.size(); ++i) {
|
|
_v.PushBack(vec[i], doc.GetAllocator());
|
|
}
|
|
value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _v, doc.GetAllocator());
|
|
};
|
|
|
|
/// A convenience function to set a double array compactly
|
|
inline void set_long_double_array(const char* const key, const std::vector<CoolPropDbl>& vec, rapidjson::Value& value, rapidjson::Document& doc) {
|
|
rapidjson::Value _v(rapidjson::kArrayType);
|
|
for (unsigned int i = 0; i < vec.size(); ++i) {
|
|
_v.PushBack(static_cast<double>(vec[i]), doc.GetAllocator());
|
|
}
|
|
value.AddMember(rapidjson::Value(key, doc.GetAllocator()).Move(), _v, doc.GetAllocator());
|
|
};
|
|
|
|
enum schema_validation_code
|
|
{
|
|
SCHEMA_VALIDATION_OK = 0,
|
|
SCHEMA_INVALID_JSON,
|
|
INPUT_INVALID_JSON,
|
|
SCHEMA_NOT_VALIDATED
|
|
};
|
|
/**
|
|
* Validate a JSON-formatted string against a JSON-formatted schema string
|
|
*/
|
|
inline schema_validation_code validate_schema(const std::string_view& schemaJson, const std::string_view& inputJson, std::string& errstr) {
|
|
rapidjson::Document sd;
|
|
sd.Parse(schemaJson.data(), schemaJson.size());
|
|
if (sd.HasParseError()) {
|
|
errstr = format("Invalid schema: %s\n", schemaJson);
|
|
return SCHEMA_INVALID_JSON;
|
|
}
|
|
rapidjson::SchemaDocument schema(sd); // Compile a Document to SchemaDocument
|
|
|
|
rapidjson::Document d;
|
|
d.Parse(inputJson.data(), inputJson.size());
|
|
if (d.HasParseError()) {
|
|
errstr = format("Invalid input json: %s\n", inputJson);
|
|
return INPUT_INVALID_JSON;
|
|
}
|
|
|
|
rapidjson::SchemaValidator validator(schema);
|
|
if (!d.Accept(validator)) {
|
|
// Input JSON is invalid according to the schema
|
|
// Output diagnostic information
|
|
errstr = to_string(validator.GetError());
|
|
return SCHEMA_NOT_VALIDATED;
|
|
}
|
|
return SCHEMA_VALIDATION_OK;
|
|
}
|
|
|
|
} // namespace cpjson
|
|
#endif
|