Cantera  2.1.2
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 <cstring>
15 
16 #include "cantera/equil/vcs_defs.h"
17 #include "cantera/base/global.h"
18 
19 namespace VCSnonideal
20 {
21 using Cantera::npos;
22 
23 //! Points to the data in a std::vector<> object
24 #define VCS_DATA_PTR(vvv) (&(vvv[0]))
25 
26 //! define this Cantera function to replace printf
27 /*!
28  * We can replace this with printf easily
29  */
30 #define plogf Cantera::writelogf
31 
32 //! define this Cantera function to replace cout << endl;
33 /*!
34  * We use this to place an endl in the log file, and
35  * ensure that the IO buffers are flushed.
36  */
37 #define plogendl() Cantera::writelogendl()
38 
39 //! Global hook for turning on and off time printing.
40 /*!
41  * Default is to allow printing. But, you can assign this to zero globally to
42  * turn off all time printing. This is helpful for test suite purposes where
43  * you are interested in differences in text files.
44  */
45 extern int vcs_timing_print_lvl;
46 
47 // Forward references
48 class VCS_SPECIES_THERMO;
49 class VCS_PROB;
50 
51 //! Class to keep track of time and iterations
52 /*!
53  * class keeps all of the counters together.
54  */
56 {
57 public:
58  //! Total number of iterations in the main loop
59  //! of vcs_TP() to solve for thermo equilibrium
60  int T_Its;
61 
62  //! Current number of iterations in the main loop
63  //! of vcs_TP() to solve for thermo equilibrium
64  int Its;
65 
66  //! Total number of optimizations of the components basis set done
68 
69  //! number of optimizations of the components basis set done
71 
72  //! Current number of times the initial thermo
73  //! equilibrium estimator has been called
75 
76  //! Current number of calls to vcs_TP
78 
79  //! Current time spent in vcs_TP
80  double T_Time_vcs_TP;
81 
82  //! Current time spent in vcs_TP
83  double Time_vcs_TP;
84 
85  //! Total Time spent in basopt
86  double T_Time_basopt;
87 
88  //! Current Time spent in basopt
89  double Time_basopt;
90 
91  //! Time spent in initial estimator
92  double T_Time_inest;
93 
94  //! Time spent in the vcs suite of programs
95  double T_Time_vcs;
96 };
97 
98 //! Returns the value of the gas constant in the units specified by parameter
99 /*!
100  * @param mu_units Specifies the units.
101  * - VCS_UNITS_KCALMOL: kcal gmol-1 K-1
102  * - VCS_UNITS_UNITLESS: 1.0 K-1
103  * - VCS_UNITS_KJMOL: kJ gmol-1 K-1
104  * - VCS_UNITS_KELVIN: 1.0 K-1
105  * - VCS_UNITS_MKS: joules kmol-1 K-1 = kg m2 s-2 kmol-1 K-1
106  */
107 double vcsUtil_gasConstant(int mu_units);
108 
109 //! Invert an n x n matrix and solve m rhs's
110 /*!
111  * Solve a square matrix with multiple right hand sides
112  *
113  * \f[
114  * C X + B = 0;
115  * \f]
116  *
117  * This routine uses Gauss elimination and is optimized for the solution
118  * of lots of rhs's. A crude form of row pivoting is used here.
119  * The matrix C is destroyed during the solve.
120  *
121  * @return The solution x[] is returned in the matrix <I>B</I>.
122  * Routine returns an integer representing success:
123  * - 1 : Matrix is singular
124  * - 0 : solution is OK
125  *
126  * @param c Matrix to be inverted. c is in fortran format, i.e., rows
127  * are the inner loop. Row numbers equal to idem.
128  * c[i+j*idem] = c_i_j = Matrix to be inverted:
129  * - i = row number
130  * - j = column number
131  *
132  * @param idem number of row dimensions in c
133  * @param n Number of rows and columns in c
134  * @param b Multiple RHS. Note, b is actually the negative of
135  * most formulations. Row numbers equal to idem.
136  * b[i+j*idem] = b_i_j = vectors of rhs's:
137  * - i = row number
138  * - j = column number
139  * (each column is a new rhs)
140  * @param m number of rhs's
141  */
142 int vcsUtil_mlequ(double* c, size_t idem, size_t n, double* b, size_t m);
143 
144 //! Invert an n x n matrix and solve m rhs's
145 /*!
146  * Solve a square matrix with multiple right hand sides
147  *
148  * \f[
149  * C X + B = 0;
150  * \f]
151  *
152  * This routine uses Gauss-Jordan elimination and is optimized for the solution
153  * of lots of rhs's. Full row and column pivoting is used here. It's been
154  * shown to be necessary in at least one case.
155  * The matrix C is destroyed during the solve.
156  *
157  * @return The solution x[] is returned in the matrix <I>B</I>.
158  * Routine returns an integer representing success:
159  * - 1 : Matrix is singular
160  * - 0 : solution is OK
161  *
162  * @param c Matrix to be inverted. c is in fortran format, i.e., rows
163  * are the inner loop. Row numbers equal to idem.
164  * c[i+j*idem] = c_i_j = Matrix to be inverted:
165  * - i = row number
166  * - j = column number
167  *
168  * @param idem number of row dimensions in c
169  * @param n Number of rows and columns in c
170  * @param b Multiple RHS. Note, b is actually the negative of
171  * most formulations. Row numbers equal to idem.
172  * b[i+j*idem] = b_i_j = vectors of rhs's:
173  * - i = row number
174  * - j = column number
175  * (each column is a new rhs)
176  * @param m number of rhs's
177  */
178 int vcsUtil_gaussj(double* c, size_t idem, size_t n, double* b, size_t m);
179 
180 
181 //! Definition of the function pointer for the root finder
182 /*!
183  * see vcsUtil_root1d for a definition of how to use this.
184  */
185 typedef double(*VCS_FUNC_PTR)(double xval, double Vtarget,
186  int varID, void* fptrPassthrough,
187  int* err);
188 
189 //! One dimensional root finder
190 /*!
191  * This root finder will find the root of a one dimensional equation
192  * \f[
193  * f(x) = 0
194  * \f]
195  * where x is a bounded quantity: \f$ x_{min} < x < x_max \f$
196  *
197  * The function to be minimized must have the following call structure:
198  *
199  * @code
200  * typedef double (*VCS_FUNC_PTR)(double xval, double Vtarget,
201  * int varID, void *fptrPassthrough,
202  * int *err); @endcode
203  *
204  * xval is the current value of the x variable. Vtarget is the requested
205  * value of f(x), usually 0. varID is an integer that is passed through.
206  * fptrPassthrough is a void pointer that is passed through. err is a return
207  * error indicator. err = 0 is the norm. anything else is considered a fatal
208  * error. The return value of the function is the current value of f(xval).
209  *
210  * @param xmin Minimum permissible value of the x variable
211  * @param xmax Maximum permissible value of the x parameter
212  * @param itmax Maximum number of iterations
213  * @param func function pointer, pointing to the function to be
214  * minimized
215  * @param fptrPassthrough Pointer to void that gets passed through
216  * the rootfinder, unchanged, to the func.
217  * @param FuncTargVal Target value of the function. This is usually set
218  * to zero.
219  * @param varID Variable ID. This is usually set to zero.
220  * @param xbest Pointer to the initial value of x on input. On output
221  * This contains the root value.
222  * @param printLvl Print level of the routine.
223  *
224  * Following is a nontrial example for vcs_root1d() in which the position of a
225  * cylinder floating on the water is calculated.
226  *
227  * @code
228  * #include <cmath>
229  * #include <cstdlib>
230  *
231  * #include "equil/vcs_internal.h"
232  *
233  * const double g_cgs = 980.;
234  * const double mass_cyl = 0.066;
235  * const double diam_cyl = 0.048;
236  * const double rad_cyl = diam_cyl / 2.0;
237  * const double len_cyl = 5.46;
238  * const double vol_cyl = Pi * diam_cyl * diam_cyl / 4 * len_cyl;
239  * const double rho_cyl = mass_cyl / vol_cyl;
240  * const double rho_gas = 0.0;
241  * const double rho_liq = 1.0;
242  * const double sigma = 72.88;
243  * // Contact angle in radians
244  * const double alpha1 = 40.0 / 180. * Pi;
245  *
246  * double func_vert(double theta1, double h_2, double rho_c) {
247  * double f_grav = - Pi * rad_cyl * rad_cyl * rho_c * g_cgs;
248  * double tmp = rad_cyl * rad_cyl * g_cgs;
249  * double tmp1 = theta1 + sin(theta1) * cos(theta1) - 2.0 * h_2 / rad_cyl * sin(theta1);
250  * double f_buoy = tmp * (Pi * rho_gas + (rho_liq - rho_gas) * tmp1);
251  * double f_sten = 2 * sigma * sin(theta1 + alpha1 - Pi);
252  * return f_grav + f_buoy + f_sten;
253  * }
254  * double calc_h2_farfield(double theta1) {
255  * double rhs = sigma * (1.0 + cos(alpha1 + theta1));
256  * rhs *= 2.0;
257  * rhs = rhs / (rho_liq - rho_gas) / g_cgs;
258  * double sign = -1.0;
259  * if (alpha1 + theta1 < Pi) sign = 1.0;
260  * double res = sign * sqrt(rhs);
261  * return res + rad_cyl * cos(theta1);
262  * }
263  * double funcZero(double xval, double Vtarget, int varID, void *fptrPassthrough, int *err) {
264  * double theta = xval;
265  * double h2 = calc_h2_farfield(theta);
266  * return func_vert(theta, h2, rho_cyl);
267  * }
268  * int main () {
269  * double thetamax = Pi;
270  * double thetamin = 0.0;
271  * int maxit = 1000;
272  * int iconv;
273  * double thetaR = Pi/2.0;
274  * int printLvl = 4;
275  *
276  * iconv = VCSnonideal::vcsUtil_root1d(thetamin, thetamax, maxit,
277  * funcZero,
278  * (void *) 0, 0.0, 0,
279  * &thetaR, printLvl);
280  * printf("theta = %g\n", thetaR);
281  * double h2Final = calc_h2_farfield(thetaR);
282  * printf("h2Final = %g\n", h2Final);
283  * return 0;
284  * }
285  * @endcode
286  */
287 int vcsUtil_root1d(double xmin, double xmax, size_t itmax, VCS_FUNC_PTR func,
288  void* fptrPassthrough,
289  double FuncTargVal, int varID, double* xbest,
290  int printLvl = 0);
291 
292 //! Returns the system wall clock time in seconds
293 /*!
294  * @return time in seconds.
295  */
296 double vcs_second();
297 
298 //! This define turns on using memset and memcpy. I have not run into
299 //! any systems where this is a problem. It's the fastest way to do
300 //! low lvl operations where applicable. There are alternative routines
301 //! available if this ever fails.
302 #define USE_MEMSET
303 #ifdef USE_MEMSET
304 
305 //! Zero a double vector
306 /*!
307  * @param vec_to vector of doubles
308  * @param length length of the vector to zero.
309  */
310 inline void vcs_dzero(double* const vec_to, const size_t length)
311 {
312  (void) memset((void*) vec_to, 0, length * sizeof(double));
313 }
314 
315 //! Zero an int vector
316 /*!
317  * @param vec_to vector of ints
318  * @param length length of the vector to zero.
319  */
320 inline void vcs_izero(int* const vec_to, const size_t length)
321 {
322  (void) memset((void*) vec_to, 0, length * sizeof(int));
323 }
324 
325 //! Copy a double vector
326 /*!
327  * @param vec_to Vector to copy into. This vector must be dimensioned
328  * at least as large as the vec_from vector.
329  * @param vec_from Vector to copy from
330  * @param length Number of doubles to copy.
331  */
332 inline void vcs_dcopy(double* const vec_to,
333  const double* const vec_from, const size_t length)
334 {
335  (void) memcpy((void*) vec_to, (const void*) vec_from,
336  (length) * sizeof(double));
337 }
338 
339 //! Copy an int vector
340 /*!
341  * @param vec_to Vector to copy into. This vector must be dimensioned
342  * at least as large as the vec_from vector.
343  * @param vec_from Vector to copy from
344  * @param length Number of int to copy.
345  */
346 inline void vcs_icopy(int* const vec_to,
347  const int* const vec_from, const size_t length)
348 {
349  (void) memcpy((void*) vec_to, (const void*) vec_from,
350  (length) * sizeof(int));
351 }
352 
353 //! Zero a std double vector
354 /*!
355  * @param vec_to vector of doubles
356  * @param length length of the vector to zero.
357  */
358 inline void vcs_vdzero(std::vector<double> &vec_to, const size_t length)
359 {
360  (void) memset((void*)VCS_DATA_PTR(vec_to), 0, (length) * sizeof(double));
361 }
362 
363 //! Zero a std int vector
364 /*!
365  * @param vec_to vector of ints
366  * @param length length of the vector to zero.
367  */
368 inline void vcs_vizero(std::vector<int> &vec_to, const size_t length)
369 {
370  (void) memset((void*)VCS_DATA_PTR(vec_to), 0, (length) * sizeof(int));
371 }
372 
373 //! Copy one std double vector into another
374 /*!
375  * This is an inlined function that uses memcpy. memcpy is probably
376  * the fastest way to do this. This routine requires the vectors to be
377  * previously dimensioned appropriately. No error checking is done.
378  *
379  * @param vec_to Vector to copy into. This vector must be dimensioned
380  * at least as large as the vec_from vector.
381  * @param vec_from Vector to copy from
382  * @param length Number of doubles to copy.
383  */
384 inline void vcs_vdcopy(std::vector<double> & vec_to,
385  const std::vector<double> & vec_from, size_t length)
386 {
387  (void) memcpy((void*)&(vec_to[0]), (const void*) &(vec_from[0]),
388  (length) * sizeof(double));
389 }
390 
391 //! Copy one std integer vector into another
392 /*!
393  * This is an inlined function that uses memcpy. memcpy is probably
394  * the fastest way to do this.
395  *
396  * @param vec_to Vector to copy into. This vector must be dimensioned
397  * at least as large as the vec_from vector.
398  * @param vec_from Vector to copy from
399  * @param length Number of integers to copy.
400  */
401 inline void vcs_vicopy(std::vector<int> & vec_to,
402  const std::vector<int> & vec_from, const int length)
403 {
404  (void) memcpy((void*)&(vec_to[0]), (const void*) &(vec_from[0]),
405  (length) * sizeof(int));
406 }
407 #else
408 extern void vcs_dzero(double* const, const int);
409 extern void vcs_izero(int* const , const int);
410 extern void vcs_dcopy(double* const, const double* const, const int);
411 extern void vcs_icopy(int* const, const int* const, const int);
412 extern void vcs_vdzero(std::vector<double> &vvv, const int len = -1);
413 extern void vcs_vizero(std::vector<double> &vvv, const int len = -1);
414 void vcs_vdcopy(std::vector<double> &vec_to,
415  const std::vector<double> vec_from, const int len = -1);
416 void vcs_vicopy(std::vector<int> &vec_to,
417  const std::vector<int> vec_from, const int len = -1);
418 #endif
419 
420 //! determine the l2 norm of a vector of doubles
421 /*!
422  * @param vec vector of doubles
423  *
424  * @return Returns the l2 norm of the vector
425  */
426 double vcs_l2norm(const std::vector<double> vec);
427 
428 //! Finds the location of the maximum component in a double vector
429 /*!
430  * @param x pointer to a vector of doubles
431  * @param xSize pointer to a vector of doubles used as a multiplier to x[]
432  * before making the decision. Ignored if set to NULL.
433  * @param j lowest index to search from
434  * @param n highest index to search from
435  * @return Return index of the greatest value on X(i) searched
436  * j <= i < n
437  */
438 size_t vcs_optMax(const double* x, const double* xSize, size_t j, size_t n);
439 
440 //! Returns the maximum integer in a list
441 /*!
442  * @param vector pointer to a vector of ints
443  * @param length length of the integer vector
444  *
445  * @return returns the max integer value in the list
446  */
447 int vcs_max_int(const int* vector, int length);
448 
449 //! Prints a line consisting of multiple occurrences of the same string
450 /*!
451  * This prints a string num times, and then terminate with a
452  * end of line character
453  *
454  * @param str C string that is null terminated
455  * @param num number of times the string is to be printed
456  */
457 void vcs_print_line(const char* str, int num);
458 
459 //! Returns a const char string representing the type of the
460 //! species given by the first argument
461 /*!
462  * @param speciesStatus Species status integer representing the type
463  * of the species.
464  * @param length Maximum length of the string to be returned.
465  * Shorter values will yield abbreviated strings.
466  * Defaults to a value of 100.
467  */
468 const char* vcs_speciesType_string(int speciesStatus, int length = 100);
469 
470 //! Print a string within a given space limit
471 /*!
472  * This routine limits the amount of the string that will be printed to a
473  * maximum of "space" characters. Printing is done to
474  * to Cantera's writelog() function.
475  *
476  * @param str String, which must be null terminated.
477  * @param space space limit for the printing.
478  * @param alignment Alignment of string within the space:
479  * - 0 centered
480  * - 1 right aligned
481  * - 2 left aligned
482  */
483 void vcs_print_stringTrunc(const char* str, size_t space, int alignment);
484 
485 //! Simple routine to check whether two doubles are equal up to
486 //! roundoff error
487 /*!
488  * Currently it's set to check for 10 digits of relative accuracy.
489  *
490  * @param d1 first double
491  * @param d2 second double
492  *
493  * @return returns true if the doubles are "equal" and false otherwise
494  */
495 bool vcs_doubleEqual(double d1, double d2);
496 
497 //! Sorts a vector of ints in place from lowest to the highest values
498 /*!
499  * The vector is returned sorted from lowest to highest.
500  *
501  * @param x Reference to a vector of ints.
502  * @deprecated
503  */
504 void vcs_heapsort(std::vector<int> &x);
505 
506 //! Sorts a vector of ints and eliminates duplicates from the resulting list
507 /*!
508  * @param xOrderedUnique Ordered vector of unique ints that were part of the original list
509  * @param x Reference to a constant vector of ints.
510  * @deprecated
511  */
512 void vcs_orderedUnique(std::vector<int> & xOrderedUnique, const std::vector<int> & x);
513 
514 }
515 
516 #endif
void vcs_print_stringTrunc(const char *str, size_t space, int alignment)
Print a string within a given space limit.
Definition: vcs_util.cpp:614
int vcs_timing_print_lvl
Global hook for turning on and off time printing.
Definition: vcs_solve.cpp:27
double vcsUtil_gasConstant(int mu_units)
Returns the value of the gas constant in the units specified by parameter.
Definition: vcs_util.cpp:506
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:185
int T_Calls_vcs_TP
Current number of calls to vcs_TP.
Definition: vcs_internal.h:77
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
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:173
#define VCS_DATA_PTR(vvv)
Points to the data in a std::vector<> object.
Definition: vcs_internal.h:24
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:544
int Basis_Opts
number of optimizations of the components basis set done
Definition: vcs_internal.h:70
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, HTML_logs (see Input File Handling, Diagnostic Output, Writing messages to the screen and Writing HTML Logfiles).
Class to keep track of time and iterations.
Definition: vcs_internal.h:55
double vcs_second()
Returns the system wall clock time in seconds.
double Time_vcs_TP
Current time spent in vcs_TP.
Definition: vcs_internal.h:83
int T_Basis_Opts
Total number of optimizations of the components basis set done.
Definition: vcs_internal.h:67
Defines and definitions within the vcs package.
double T_Time_vcs
Time spent in the vcs suite of programs.
Definition: vcs_internal.h:95
int T_Its
Total number of iterations in the main loop of vcs_TP() to solve for thermo equilibrium.
Definition: vcs_internal.h:60
void vcs_orderedUnique(std::vector< int > &xOrderedUnique, const std::vector< int > &x)
Sorts a vector of ints and eliminates duplicates from the resulting list.
Definition: vcs_util.cpp:700
void vcs_print_line(const char *string, int num)
Prints a line consisting of multiple occurrences of the same string.
Definition: vcs_util.cpp:534
int vcsUtil_mlequ(double *c, size_t idem, size_t n, double *b, size_t m)
Invert an n x n matrix and solve m rhs's.
Definition: vcs_util.cpp:297
void vcs_heapsort(std::vector< int > &x)
Sorts a vector of ints in place from lowest to the highest values.
Definition: vcs_util.cpp:655
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:110
int Its
Current number of iterations in the main loop of vcs_TP() to solve for thermo equilibrium.
Definition: vcs_internal.h:64
double T_Time_inest
Time spent in initial estimator.
Definition: vcs_internal.h:92
double T_Time_vcs_TP
Current time spent in vcs_TP.
Definition: vcs_internal.h:80
void vcs_vicopy(std::vector< int > &vec_to, const std::vector< int > &vec_from, int length)
Copy one std integer vector into another.
Definition: vcs_util.cpp:103
double Time_basopt
Current Time spent in basopt.
Definition: vcs_internal.h:89
double vcs_l2norm(const std::vector< double > vec)
determine the l2 norm of a vector of doubles
Definition: vcs_util.cpp:69
bool vcs_doubleEqual(double d1, double d2)
Simple routine to check whether two doubles are equal up to roundoff error.
Definition: vcs_util.cpp:645
int T_Calls_Inest
Current number of times the initial thermo equilibrium estimator has been called. ...
Definition: vcs_internal.h:74
int vcsUtil_gaussj(double *c, size_t idem, size_t n, double *b, size_t m)
Invert an n x n matrix and solve m rhs's.
Definition: vcs_util.cpp:408
int vcs_max_int(const int *vector, int length)
Returns the maximum integer in a list.
Definition: vcs_util.cpp:136
double T_Time_basopt
Total Time spent in basopt.
Definition: vcs_internal.h:86