/*------------------------------------------------------------------------- | The software accompanies the paper | | | | Classes and Objects of Chemical Thermodynamics in Object-Oriented | | Programming. 2. A Class of Chemical Species | | | | E.B. Rudnyi | | E-mail: rudnyi@comp.chem.msu.su | | Homepages: http://www.chem.msu.su/~rudnyi/welcome.html | | | | presented at Second Electronic Computational Chemistry Conference, | | November 1995, http://hackberry.chem.niu.edu/ECCC2/ | | | | E.B. Rudnyi, Copyright (c) 1995 | | | | Permission to use, copy, modify, distribute and sell this software and | | its documentation for any purpose is hereby granted without fee, | | provided that the above copyright notice with the name of the paper and | | the conference appear in all copies and that both that copyright notice | | and this permission notice appear in supporting documentation. | | E.B. Rudnyi makes no representations about the suitability of this | | software for any purpose. It is provided "as is" without expressed or | | implied warranty. | --------------------------------------------------------------------------*/ #ifndef _FUNCTION_H #define _FUNCTION_H #ifndef _FUNC_TP_H #include "func_tp.h" #endif // ######################## class smpl_func ########################### //****************************** Purpose ****************************** // Deals with an analytical function of temperature and pressure // defined in some range of temperatures and pressures. //****************************** Syntax ******************************* // String representation of a func_Tp object followed by colon and // then by representaion of a lim_Tp object. All white // spaces are ignored. Input opearators read a stream until the delimiters // (colon or semicolon) or the end of file (string). The colon is // extracted, the semicolon is left in the stream. // // smpl_func // func_Tp : lim_Tp //*************** Relationships with the other classes **************** // Class smpl_func is derived from both func_Tp and lim_Tp. // This is an agregate class that inherits all the base properties. // Class smpl_func is a building block for class function (class // function is a container for keeping several smpl_func objects). class smpl_func : public func_Tp, public lim_Tp { public: //**************************** Operations ***************************** smpl_func(); // Default constructor. Creates an empty function of T and p. smpl_func(const smpl_func &old); // Copy constructor. smpl_func& operator=(const smpl_func &old); // Assignment operator. smpl_func(const char *str); // Constructs the object from a string representation. In the case of // sintactics errors creates an empty object. smpl_func& operator=(const char *str); // Assigns a string representation to the object. In the case of // sintactics errors leaves an empty object. ~smpl_func(); // Destructor. void clear(); // Clears the object (result is the empty object). void copy(const smpl_func &old); // Makes the object equal to another object. const char* anal(const char *in); // Makes the object equal to that given by a string representation. // Returns a pointer to char that was not used by the function. // In the case of sintactics errors leaves an empty object. friend istream& operator>>(istream &in, smpl_func &to); // Reads stream until the delimiters (colon or semicolon) or the end of // file. The colon is extracted, the semicolon is left in the stream. char* to_str(char *buf) const; // Copies a sting representation of the object to a given string buffer. // Returns a pointer to the given buffer. friend ostream& operator<<(ostream &out, const smpl_func &old); // Writes a string representation of the object to the given stream. double est(const double &T = global::T, const double &p = global::p); // Evaluates a value of the function at given temperature and pressure. // Returns HUGE_VAL in the case of an error or when given T and p // are outside of the allowable range. double operator()(const double &T, const double &p); double operator()(const double &T); // assumes p = global::p double operator()(); // assumes T = global::T and p = global::p // Another interface for evaluating a value of the function. double df(global::derivative d, const double &T = global::T, const double &p = global::p); // Evaluates numerically a derivative of the function // at given temperature and pressure. // Returns HUGE_VAL in the case of an error or when given T and p // are outside of the allowable range. friend void oper(smpl_func &res, const smpl_func &left, const smpl_func &right, const char op); // Performs a rudimentary analytical operation // res = left op right // Argument op can be equal to '+', '-', '*', '/'. // The allowable range of res is equal to cross(left, right). friend smpl_func operator+(const smpl_func &left, const smpl_func &right); friend smpl_func operator-(const smpl_func &left, const smpl_func &right); friend smpl_func operator*(const smpl_func &left, const smpl_func &right); friend smpl_func operator/(const smpl_func &left, const smpl_func &right); // Another interface for rudimentary analytical operations. int len() const; // Returns a number of characters necessary for string representation // of the object. operator int() const; // Returns zero if the object is empty and non-zero otherwise. //************************** Error handling *************************** enum smpl_err { OKAY, // everything is okay WRFUNC, // error in func_Tp WRLIM, // error in lim_Tp OUTRNG // out of the allowable range }; static smpl_err errno; // Variable to keep the error code. It may be checked out when // something went wrong. }; // #################### end of class smpl_func ######################## class node_func : public smpl_func { public: node_func *next; node_func() {next = NULL;} ~node_func() {} }; // ######################## class function ############################ //****************************** Purpose ****************************** // Deals with an compound analytical function of temperature and pressure // comprising several simple analytical functions each of which // is defined in its own range of temperatures and pressures. //****************************** Syntax ******************************* // String representations of a smpl_func objects separated by colon. // The last smpl_func object should be followed by semicolon. // It must not be intersection between ranges of distinct smpl_func // objects (nor CROSS, neither CROSS_T for a.compare(b)). In the // internal representation smpl_func objects are sorted according to // the result of a.compare(b). All white spaces are ignored. Input // opearators read a stream until the semicolon or the end of file // (string). The semicolon is extracted. // // function // smpl_func; // smpl_func: function; //*************** Relationships with the other classes **************** // Class function is a container for keeping several smpl_func objects. // It is a base for class species. class function { node_func *start; int add(node_func *t); public: //**************************** Operations ***************************** function(); // Default constructor. Creates an empty compound function of T and p. function(const function &old); // Copy constructor. function& operator=(const function &old); // Assignment operator. function(const char *str); // Constructs the object from a string representation. In the case of // sintactics errors creates an empty object. function& operator=(const char *str); // Assigns a string representation to the object. In the case of // sintactics errors leaves an empty object. ~function(); // Destructor. void clear(); // Clears the object (result is the empty object). void copy(const function &old); // Makes the object equal to another object. const char* anal(const char *str); // Makes the object equal to that given by a string representation. // Returns a pointer to char that was not used by the function. // In the case of sintactics errors leaves an empty object. friend istream& operator>>(istream &in, function &to); // Reads stream until the semicolon or the end of // file. The semicolon is extracted. char* to_str(char *buf) const; // Copies a sting representation of the object to a given string buffer. // Returns a pointer to the given buffer. friend ostream& operator<<(ostream &out, const function &old); // Writes a string representation of the object to the given stream. double est(const double &T = global::T, const double &p = global::p); // Evaluates a value of the compound function at given temperature // and pressure. Returns HUGE_VAL in the case of an error or when given // T and p are outside of all the ranges. double operator()(const double &T, const double &p); double operator()(const double &T); // assumes p = global::p double operator()(); // assumes T = global::T and p = global::p // Another interface for evaluating a value of the function. double df(global::derivative d, const double &T = global::T, const double &p = global::p); // Evaluates numerically a derivative of the compound function // at given temperature and pressure. // Returns HUGE_VAL in the case of an error or when given T and p // are outside of all the ranges. friend void oper(function &res, const function &left, const function &right, const char op); // Performs a rudimentary analytical operation // res = left op right // Argument op can be equal to '+', '-', '*', '/'. friend function operator+(const function &left, const function &right); friend function operator-(const function &left, const function &right); friend function operator*(const function &left, const function &right); friend function operator/(const function &left, const function &right); // Another interface for rudimentary analytical operations. int len() const; // Returns a number of characters necessary for string representation // of the object. operator int() const; // Returns zero if the object is empty and non-zero otherwise. int nfunc() const; // Returns a number of the smpl_func objects. //************************** Error handling *************************** enum function_err { OKAY, // everything is okay WRSMPL, // error in smpl_func OUTRNG, // out of the allowable range CROSS, // there were smpl_func objects with intersection of the // ranges (or CROSS, either CROSS_T) BAD // internal representation of the compound function is // corrupted }; static function_err errno; // Variable to keep the error code. It may be checked out when // something went wrong. }; // #################### end of class function ######################### inline smpl_func::smpl_func() { errno = OKAY; } inline smpl_func::smpl_func(const smpl_func &old) { copy(old); } inline smpl_func& smpl_func::operator=(const smpl_func &old) { if (this == &old) return *this; copy(old); return *this; } inline smpl_func::smpl_func(const char *str) { anal(str); } inline smpl_func& smpl_func::operator=(const char *str) { anal(str); return *this; } inline smpl_func::~smpl_func() { } inline void smpl_func::clear() { func_Tp::clear(); lim_Tp::clear(); } inline void smpl_func::copy(const smpl_func &old) { errno = OKAY; func_Tp::copy(old); lim_Tp::copy(old); } inline int smpl_func::len() const { return func_Tp::len() + lim_Tp::len() + 1; } inline double smpl_func::est(const double &T, const double &p) { if (isin(T, p)) return func_Tp::est(T, p); else errno = OUTRNG; return HUGE_VAL; } inline double smpl_func::df(global::derivative d, const double &T, const double &p) { if (isin(T, p)) return func_Tp::df(d, T, p); else errno = OUTRNG; return HUGE_VAL; } inline smpl_func::operator int() const { return (func_Tp::operator int() || lim_Tp::operator int()); } inline double smpl_func::operator()(const double &T, const double &p) { return est(T, p); } inline double smpl_func::operator()(const double &T) { return est(T, global::p); } inline double smpl_func::operator()() { return est(global::T, global::p); } inline function::function() { errno = OKAY; start = NULL; } inline function::function(const function &old) { start = NULL; copy(old); } inline function& function::operator=(const function &old) { if (this == &old) return *this; copy(old); return *this; } inline function::function(const char *str) { start = NULL; anal(str); } inline function& function::operator=(const char *str) { anal(str); return *this; } inline function::~function() { clear(); } inline function::operator int() const { return (start != NULL); } inline double function::operator()(const double &T, const double &p) { return est(T, p); } inline double function::operator()(const double &T) { return est(T, global::p); } inline double function::operator()() { return est(global::T, global::p); } #endif