MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FuncTerm.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment,
4 ** also known as GENESIS 3 base code.
5 ** copyright (C) 2014 Upinder S. Bhalla. and NCBS
6 ** It is made available under the terms of the
7 ** GNU Lesser General Public License version 2.1
8 ** See the file COPYING.LIB for the full notice.
9 **********************************************************************/
10 
22 #include <vector>
23 #include <sstream>
24 using namespace std;
25 
26 #include "muParser.h"
27 #include "FuncTerm.h"
28 #include "../utility/numutil.h"
29 
31  : reactantIndex_( 1, 0 ),
32  volScale_( 1.0 ),
33  target_( ~0U)
34 {
35  args_ = 0;
36  parser_.DefineConst(_T("pi"), (mu::value_type)M_PI);
37  parser_.DefineConst(_T("e"), (mu::value_type)M_E);
38 }
39 
41 {
42  if (args_) {
43  delete[] args_;
44  }
45 }
46 
47 void FuncTerm::setReactantIndex( const vector< unsigned int >& mol )
48 {
49  reactantIndex_ = mol;
50  if ( args_ ) {
51  delete[] args_;
52  args_ = 0;
53  }
54  args_ = new double[ mol.size() + 1 ];
55  // args_.resize( mol.size() + 1, 0.0 );
56  for ( unsigned int i = 0; i < mol.size(); ++i ) {
57  stringstream ss;
58  args_[i] = 0.0;
59  ss << "x" << i;
60  parser_.DefineVar( ss.str(), &args_[i] );
61  }
62  // Define a 't' variable even if we don't always use it.
63  args_[mol.size()] = 0.0;
64  parser_.DefineVar( "t", &args_[mol.size()] );
65 }
66 
67 const vector< unsigned int >& FuncTerm::getReactantIndex() const
68 {
69  return reactantIndex_;
70 }
71 
72 
73 void showError(mu::Parser::exception_type &e)
74 {
75  cout << "Error occurred in parser.\n"
76  << "Message: " << e.GetMsg() << "\n"
77  << "Formula: " << e.GetExpr() << "\n"
78  << "Token: " << e.GetToken() << "\n"
79  << "Position: " << e.GetPos() << "\n"
80  << "Error code: " << e.GetCode() << endl;
81 }
82 
83 void FuncTerm::setExpr( const string& expr )
84 {
85  try {
86  parser_.SetExpr( expr );
87  expr_ = expr;
88  } catch(mu::Parser::exception_type &e) {
89  showError(e);
90  //return;
91  throw(e);
92  }
93 }
94 
95 const string& FuncTerm::getExpr() const
96 {
97  return expr_;
98 }
99 
100 void FuncTerm::setTarget( unsigned int t )
101 {
102  target_ = t;
103 }
104 
105 const unsigned int FuncTerm::getTarget() const
106 {
107  return target_;
108 }
109 
110 void FuncTerm::setVolScale( double vs )
111 {
112  volScale_ = vs;
113 }
114 
115 double FuncTerm::getVolScale() const
116 {
117  return volScale_;
118 }
119 
120 const FuncTerm& FuncTerm::operator=( const FuncTerm& other )
121 {
122  args_ = 0; // Don't delete it, the original one is still using it.
123  parser_ = other.parser_;
124  expr_ = other.expr_;
125  volScale_ = other.volScale_;
126  target_ = other.target_;
128  return *this;
129 }
130 
135 double FuncTerm::operator() ( const double* S, double t ) const
136 {
137  if ( !args_ )
138  return 0.0;
139  unsigned int i;
140  for ( i = 0; i < reactantIndex_.size(); ++i )
141  args_[i] = S[reactantIndex_[i]];
142  args_[i] = t;
143  try
144  {
145  double result = parser_.Eval() * volScale_;
146  return result;
147  }
148  catch (mu::Parser::exception_type &e )
149  {
150  cerr << "Error: " << e.GetMsg() << endl;
151  throw e;
152  }
153 
154 }
155 
156 void FuncTerm::evalPool( double* S, double t ) const
157 {
158  if ( !args_ || target_ == ~0U )
159  return;
160  unsigned int i;
161  for ( i = 0; i < reactantIndex_.size(); ++i )
162  args_[i] = S[reactantIndex_[i]];
163  args_[i] = t;
164  try
165  {
166  S[ target_] = parser_.Eval() * volScale_;
167  }
168  catch ( mu::Parser::exception_type & e )
169  {
170  showError( e );
171  //throw e;
172  }
173 }
void setExpr(const string &e)
Definition: FuncTerm.cpp:83
unsigned int target_
Definition: FuncTerm.h:54
double * args_
Definition: FuncTerm.h:43
double getVolScale() const
Definition: FuncTerm.cpp:115
string expr_
Definition: FuncTerm.h:47
const unsigned int getTarget() const
Definition: FuncTerm.cpp:105
double value_type
~FuncTerm()
Definition: FuncTerm.cpp:40
void setVolScale(double vs)
Definition: FuncTerm.cpp:110
void setReactantIndex(const vector< unsigned int > &mol)
Definition: FuncTerm.cpp:47
double volScale_
Definition: FuncTerm.h:53
mu::Parser parser_
Definition: FuncTerm.h:46
double operator()(const double *S, double t) const
Definition: FuncTerm.cpp:135
#define M_PI
Definition: numutil.h:34
FuncTerm()
Definition: FuncTerm.cpp:30
void evalPool(double *s, double t) const
Definition: FuncTerm.cpp:156
void setTarget(unsigned int tgt)
Definition: FuncTerm.cpp:100
void showError(mu::Parser::exception_type &e)
Definition: FuncTerm.cpp:73
const vector< unsigned int > & getReactantIndex() const
Definition: FuncTerm.cpp:67
#define M_E
Definition: numutil.h:38
const string & getExpr() const
Definition: FuncTerm.cpp:95
const FuncTerm & operator=(const FuncTerm &other)
Definition: FuncTerm.cpp:120
vector< unsigned int > reactantIndex_
Definition: FuncTerm.h:45