Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vcs_internal.h
Go to the documentation of this file.
1 /**
2  * @file vcs_internal.h Internal declarations for the VCSnonideal package
3  */
4 
5 /*
6  * Copyright (2005) Sandia Corporation. Under the terms of
7  * Contract DE-AC04-94AL85000 with Sandia Corporation, the
8  * U.S. Government retains certain rights in this software.
9  */
10 
11 #ifndef _VCS_INTERNAL_H
12 #define _VCS_INTERNAL_H
13 
14 #include "cantera/base/global.h"
15 namespace Cantera
16 {
17 //! Points to the data in a std::vector<> object
18 #define VCS_DATA_PTR(vvv) (&(vvv[0]))
19 
20 //! define this Cantera function to replace printf
21 /*!
22  * We can replace this with printf easily
23  */
24 #define plogf writelogf
25 
26 //! define this Cantera function to replace cout << endl;
27 /*!
28  * We use this to place an endl in the log file, and
29  * ensure that the IO buffers are flushed.
30  */
31 #define plogendl() writelogendl()
32 
33 //! Global hook for turning on and off time printing.
34 /*!
35  * Default is to allow printing. But, you can assign this to zero globally to
36  * turn off all time printing. This is helpful for test suite purposes where
37  * you are interested in differences in text files.
38  */
39 extern int vcs_timing_print_lvl;
40 
41 // Forward references
42 class VCS_SPECIES_THERMO;
43 class VCS_PROB;
44 
45 //! Class to keep track of time and iterations
46 /*!
47  * class keeps all of the counters together.
48  */
50 {
51 public:
52  //! Total number of iterations in the main loop
53  //! of vcs_TP() to solve for thermo equilibrium
54  int T_Its;
55 
56  //! Current number of iterations in the main loop
57  //! of vcs_TP() to solve for thermo equilibrium
58  int Its;
59 
60  //! Total number of optimizations of the components basis set done
62 
63  //! number of optimizations of the components basis set done
65 
66  //! Current number of times the initial thermo
67  //! equilibrium estimator has been called
69 
70  //! Current number of calls to vcs_TP
72 
73  //! Current time spent in vcs_TP
74  double T_Time_vcs_TP;
75 
76  //! Current time spent in vcs_TP
77  double Time_vcs_TP;
78 
79  //! Total Time spent in basopt
80  double T_Time_basopt;
81 
82  //! Current Time spent in basopt
83  double Time_basopt;
84 
85  //! Time spent in initial estimator
86  double T_Time_inest;
87 
88  //! Time spent in the vcs suite of programs
89  double T_Time_vcs;
90 };
91 
92 //! Returns the value of the gas constant in the units specified by parameter
93 /*!
94  * @param mu_units Specifies the units.
95  * - VCS_UNITS_KCALMOL: kcal gmol-1 K-1
96  * - VCS_UNITS_UNITLESS: 1.0 K-1
97  * - VCS_UNITS_KJMOL: kJ gmol-1 K-1
98  * - VCS_UNITS_KELVIN: 1.0 K-1
99  * - VCS_UNITS_MKS: joules kmol-1 K-1 = kg m2 s-2 kmol-1 K-1
100  */
101 double vcsUtil_gasConstant(int mu_units);
102 
103 //! Definition of the function pointer for the root finder
104 /*!
105  * see vcsUtil_root1d for a definition of how to use this.
106  */
107 typedef double(*VCS_FUNC_PTR)(double xval, double Vtarget,
108  int varID, void* fptrPassthrough,
109  int* err);
110 
111 //! One dimensional root finder
112 /*!
113  * This root finder will find the root of a one dimensional equation
114  * \f[
115  * f(x) = 0
116  * \f]
117  * where x is a bounded quantity: \f$ x_{min} < x < x_max \f$
118  *
119  * The function to be minimized must have the following call structure:
120  *
121  * @code
122  * typedef double (*VCS_FUNC_PTR)(double xval, double Vtarget,
123  * int varID, void *fptrPassthrough,
124  * int *err); @endcode
125  *
126  * xval is the current value of the x variable. Vtarget is the requested
127  * value of f(x), usually 0. varID is an integer that is passed through.
128  * fptrPassthrough is a void pointer that is passed through. err is a return
129  * error indicator. err = 0 is the norm. anything else is considered a fatal
130  * error. The return value of the function is the current value of f(xval).
131  *
132  * @param xmin Minimum permissible value of the x variable
133  * @param xmax Maximum permissible value of the x parameter
134  * @param itmax Maximum number of iterations
135  * @param func function pointer, pointing to the function to be
136  * minimized
137  * @param fptrPassthrough Pointer to void that gets passed through
138  * the rootfinder, unchanged, to the func.
139  * @param FuncTargVal Target value of the function. This is usually set
140  * to zero.
141  * @param varID Variable ID. This is usually set to zero.
142  * @param xbest Pointer to the initial value of x on input. On output
143  * This contains the root value.
144  * @param printLvl Print level of the routine.
145  *
146  * Following is a nontrial example for vcs_root1d() in which the position of a
147  * cylinder floating on the water is calculated.
148  *
149  * @code
150  * #include <cmath>
151  * #include <cstdlib>
152  *
153  * #include "equil/vcs_internal.h"
154  *
155  * const double g_cgs = 980.;
156  * const double mass_cyl = 0.066;
157  * const double diam_cyl = 0.048;
158  * const double rad_cyl = diam_cyl / 2.0;
159  * const double len_cyl = 5.46;
160  * const double vol_cyl = Pi * diam_cyl * diam_cyl / 4 * len_cyl;
161  * const double rho_cyl = mass_cyl / vol_cyl;
162  * const double rho_gas = 0.0;
163  * const double rho_liq = 1.0;
164  * const double sigma = 72.88;
165  * // Contact angle in radians
166  * const double alpha1 = 40.0 / 180. * Pi;
167  *
168  * double func_vert(double theta1, double h_2, double rho_c) {
169  * double f_grav = - Pi * rad_cyl * rad_cyl * rho_c * g_cgs;
170  * double tmp = rad_cyl * rad_cyl * g_cgs;
171  * double tmp1 = theta1 + sin(theta1) * cos(theta1) - 2.0 * h_2 / rad_cyl * sin(theta1);
172  * double f_buoy = tmp * (Pi * rho_gas + (rho_liq - rho_gas) * tmp1);
173  * double f_sten = 2 * sigma * sin(theta1 + alpha1 - Pi);
174  * return f_grav + f_buoy + f_sten;
175  * }
176  * double calc_h2_farfield(double theta1) {
177  * double rhs = sigma * (1.0 + cos(alpha1 + theta1));
178  * rhs *= 2.0;
179  * rhs = rhs / (rho_liq - rho_gas) / g_cgs;
180  * double sign = -1.0;
181  * if (alpha1 + theta1 < Pi) sign = 1.0;
182  * double res = sign * sqrt(rhs);
183  * return res + rad_cyl * cos(theta1);
184  * }
185  * double funcZero(double xval, double Vtarget, int varID, void *fptrPassthrough, int *err) {
186  * double theta = xval;
187  * double h2 = calc_h2_farfield(theta);
188  * return func_vert(theta, h2, rho_cyl);
189  * }
190  * int main () {
191  * double thetamax = Pi;
192  * double thetamin = 0.0;
193  * int maxit = 1000;
194  * int iconv;
195  * double thetaR = Pi/2.0;
196  * int printLvl = 4;
197  *
198  * iconv = vcsUtil_root1d(thetamin, thetamax, maxit,
199  * funcZero,
200  * (void *) 0, 0.0, 0,
201  * &thetaR, printLvl);
202  * printf("theta = %g\n", thetaR);
203  * double h2Final = calc_h2_farfield(thetaR);
204  * printf("h2Final = %g\n", h2Final);
205  * return 0;
206  * }
207  * @endcode
208  * @deprecated Unused. To be removed after Cantera 2.2.
209  */
210 int vcsUtil_root1d(double xmin, double xmax, size_t itmax, VCS_FUNC_PTR func,
211  void* fptrPassthrough,
212  double FuncTargVal, int varID, double* xbest,
213  int printLvl = 0);
214 
215 //! determine the l2 norm of a vector of doubles
216 /*!
217  * @param vec vector of doubles
218  *
219  * @return Returns the l2 norm of the vector
220  */
221 double vcs_l2norm(const std::vector<double> vec);
222 
223 //! Finds the location of the maximum component in a double vector
224 /*!
225  * @param x pointer to a vector of doubles
226  * @param xSize pointer to a vector of doubles used as a multiplier to x[]
227  * before making the decision. Ignored if set to NULL.
228  * @param j lowest index to search from
229  * @param n highest index to search from
230  * @return Return index of the greatest value on X(i) searched
231  * j <= i < n
232  */
233 size_t vcs_optMax(const double* x, const double* xSize, size_t j, size_t n);
234 
235 //! Returns the maximum integer in a list
236 /*!
237  * @param vector pointer to a vector of ints
238  * @param length length of the integer vector
239  *
240  * @return returns the max integer value in the list
241  * @deprecated Unused. To be removed after Cantera 2.2.
242  */
243 int vcs_max_int(const int* vector, int length);
244 
245 //! Returns a const char string representing the type of the
246 //! species given by the first argument
247 /*!
248  * @param speciesStatus Species status integer representing the type
249  * of the species.
250  * @param length Maximum length of the string to be returned.
251  * Shorter values will yield abbreviated strings.
252  * Defaults to a value of 100.
253  */
254 const char* vcs_speciesType_string(int speciesStatus, int length = 100);
255 
256 //! Print a string within a given space limit
257 /*!
258  * This routine limits the amount of the string that will be printed to a
259  * maximum of "space" characters. Printing is done to
260  * to Cantera's writelog() function.
261  *
262  * @param str String, which must be null terminated.
263  * @param space space limit for the printing.
264  * @param alignment Alignment of string within the space:
265  * - 0 centered
266  * - 1 right aligned
267  * - 2 left aligned
268  */
269 void vcs_print_stringTrunc(const char* str, size_t space, int alignment);
270 
271 //! Simple routine to check whether two doubles are equal up to
272 //! roundoff error
273 /*!
274  * Currently it's set to check for 10 digits of relative accuracy.
275  *
276  * @param d1 first double
277  * @param d2 second double
278  *
279  * @return returns true if the doubles are "equal" and false otherwise
280  */
281 bool vcs_doubleEqual(double d1, double d2);
282 }
283 
284 #endif
bool vcs_doubleEqual(double d1, double d2)
Simple routine to check whether two doubles are equal up to roundoff error.
Definition: vcs_util.cpp:185
int vcs_max_int(const int *vector, int length)
Returns the maximum integer in a list.
Definition: vcs_util.cpp:62
int Its
Current number of iterations in the main loop of vcs_TP() to solve for thermo equilibrium.
Definition: vcs_internal.h:58
int T_Calls_Inest
Current number of times the initial thermo equilibrium estimator has been called. ...
Definition: vcs_internal.h:68
double Time_vcs_TP
Current time spent in vcs_TP.
Definition: vcs_internal.h:77
int Basis_Opts
number of optimizations of the components basis set done
Definition: vcs_internal.h:64
double T_Time_basopt
Total Time spent in basopt.
Definition: vcs_internal.h:80
double vcsUtil_gasConstant(int mu_units)
Returns the value of the gas constant in the units specified by parameter.
Definition: vcs_util.cpp:76
int vcs_timing_print_lvl
Global hook for turning on and off time printing.
Definition: vcs_solve.cpp:26
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, (see Input File Handling, Diagnostic Output, and Writing messages to the screen).
int T_Basis_Opts
Total number of optimizations of the components basis set done.
Definition: vcs_internal.h:61
int vcsUtil_root1d(double xmin, double xmax, size_t itmax, VCS_FUNC_PTR func, void *fptrPassthrough, double FuncTargVal, int varID, double *xbest, int printLvl)
One dimensional root finder.
Definition: vcs_root1d.cpp:34
double T_Time_vcs_TP
Current time spent in vcs_TP.
Definition: vcs_internal.h:74
double vcs_l2norm(const std::vector< double > vec)
determine the l2 norm of a vector of doubles
Definition: vcs_util.cpp:23
int T_Its
Total number of iterations in the main loop of vcs_TP() to solve for thermo equilibrium.
Definition: vcs_internal.h:54
void vcs_print_stringTrunc(const char *str, size_t space, int alignment)
Print a string within a given space limit.
Definition: vcs_util.cpp:154
double T_Time_inest
Time spent in initial estimator.
Definition: vcs_internal.h:86
double Time_basopt
Current Time spent in basopt.
Definition: vcs_internal.h:83
double(* VCS_FUNC_PTR)(double xval, double Vtarget, int varID, void *fptrPassthrough, int *err)
Definition of the function pointer for the root finder.
Definition: vcs_internal.h:107
size_t vcs_optMax(const double *x, const double *xSize, size_t j, size_t n)
Finds the location of the maximum component in a double vector.
Definition: vcs_util.cpp:37
const char * vcs_speciesType_string(int speciesStatus, int length)
Returns a const char string representing the type of the species given by the first argument...
Definition: vcs_util.cpp:96
Class to keep track of time and iterations.
Definition: vcs_internal.h:49
double T_Time_vcs
Time spent in the vcs suite of programs.
Definition: vcs_internal.h:89
int T_Calls_vcs_TP
Current number of calls to vcs_TP.
Definition: vcs_internal.h:71