pmm  1.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
pmm_muparse.cc
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2008-2010 Robert Higgins
4  Author: Robert Higgins <robert.higgins@ucd.ie>
5 
6  This file is part of PMM.
7 
8  PMM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  PMM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with PMM. If not, see <http://www.gnu.org/licenses/>.
20 */
21 /*!
22  * @file pmm_muparse.cc
23  * @brief interface between pmm and muparser
24  *
25  * Contains code to interface between pmm structures and muparser API
26  */
27 #if HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #ifdef HAVE_MUPARSER
32 
33 #include <iostream>
34 #include <string>
35 #include <muParser.h>
36 
37 #include "pmm_muparse.h"
38 
39 extern "C" {
40 #include "pmm_model.h"
41 #include "pmm_log.h"
42 }
43 
44 /*!
45  * create a new parameter constraint formula structure for muparser
46  *
47  * Initializes muparser object and var array, then links var array to muparser
48  * object
49  *
50  * @param pd_set pointer to the set of parameter definitions that
51  * will contain the constraint formula
52  *
53  * @return 0 on success, -1 on failure
54  */
55 extern "C"
56 int
58 {
59  int i;
60  string_type str;
61 
62  pd_set->pc_parser = new PMM_Param_Constraint_Muparser;
63  if(pd_set->pc_parser == NULL) {
64  ERRPRINTF("Error allocating memory.\n");
65  return -1;
66  }
67 
68  pd_set->pc_parser->n_p = pd_set->n_p;
69  pd_set->pc_parser->vars = new double[pd_set->pc_parser->n_p];
70 
71 
72  // set up parser variables, by name and pointer to element of 'vars'
73  for(i=0; i<pd_set->pc_parser->n_p; i++) {
74  pd_set->pc_parser->vars[i] = 1.0;
75  str = pd_set->pd_array[i].name;
76 
77  pd_set->pc_parser->p.DefineVar(str,
78  &(pd_set->pc_parser->vars[i]));
79  DBGPRINTF("setting up variables:\n");
80  std::cout << str << " with value " << pd_set->pc_parser->vars[i] << "\n";
81  }
82 
83  pd_set->pc_parser->p.SetExpr(pd_set->pc_formula);
84 
85  DBGPRINTF("setting up formula:\n");
86  std::cout << pd_set->pc_formula << "\n";
87 
88  return 0;
89 }
90 
91 /*!
92  * given a set of parameters, evaluate the constraint formula
93  *
94  * Store the values of the parameter in the constraint formula structure
95  * and then call the evaluation.
96  *
97  * @param pc_parser pointer to the parameter constraint formula structure
98  * @param params pointer to array of parameters
99  * @param value pointer to a double where the evaluation will be stored
100  *
101  * @returns 0 on success, -1 on failure
102  */
103 extern "C"
104 int
106  int* params, double *value)
107 {
108 
109  int i;
110 
111  for(i=0; i<pc_parser->n_p; i++)
112  pc_parser->vars[i] = (double)params[i];
113 
114  return evaluate_constraint(pc_parser, value);
115 
116 }
117 
118 /*!
119  * evaluate a constraint formula using its stored values
120  *
121  * @param pc_parser pointer to the parameter constraint formula structure
122  * @param value pointer to a double where the evaluation will be stored
123  *
124  * @return 0 on success, -1 on failure
125  */
126 extern "C"
127 int
129  double *value)
130 {
131 
132  try {
133  *value = pc_parser->p.Eval();
134  DBGPRINTF("evaluated constraint formula to:%f\n", *value);
135  }
136  catch (Parser::exception_type &e) {
137  ERRPRINTF("Error evaluating parameter constraint formula, message:\n");
138  std::cout << e.GetMsg() << "\n";
139 
140  return -1;
141  }
142 
143  return 0;
144 }
145 
146 #endif /* HAVE_MUPARSER */