Skip to content

Commit 526c5b0

Browse files
authored
More tests (#369)
* Got tests from exprtk_ new branch. * Added new function rnd which honors global seed. * Fixed derivative tests. muParser Derivative is not very precise. * More tests and cleanup in muParser interface. * muParser is bit less precise on OSX.
1 parent aec42e2 commit 526c5b0

18 files changed

+1602
-476
lines changed

biophysics/Neuron.cpp

Lines changed: 286 additions & 286 deletions
Large diffs are not rendered by default.

builtins/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ set(SRCS
5151
Arith.cpp
5252
Group.cpp
5353
Mstring.cpp
54-
Func.cpp
54+
# Func.cpp
55+
MooseParser.cpp
5556
Function.cpp
5657
Variable.cpp
5758
InputVariable.cpp

builtins/Function.cpp

Lines changed: 79 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,29 @@ static const double TriggerThreshold = 0.0;
5959
static SrcFinfo1<double> *valueOut()
6060
{
6161
static SrcFinfo1<double> valueOut("valueOut",
62-
"Evaluated value of the function for the current variable values.");
62+
"Evaluated value of the function for the current variable values.");
6363
return &valueOut;
6464
}
6565

6666
static SrcFinfo1< double > *derivativeOut()
6767
{
6868
static SrcFinfo1< double > derivativeOut("derivativeOut",
69-
"Value of derivative of the function for the current variable values");
69+
"Value of derivative of the function for the current variable values");
7070
return &derivativeOut;
7171
}
7272

7373
static SrcFinfo1< double > *rateOut()
7474
{
7575
static SrcFinfo1< double > rateOut("rateOut",
76-
"Value of time-derivative of the function for the current variable values");
76+
"Value of time-derivative of the function for the current variable values");
7777
return &rateOut;
7878
}
7979

8080
static SrcFinfo1< vector < double > *> *requestOut()
8181
{
8282
static SrcFinfo1< vector < double > * > requestOut(
83-
"requestOut",
84-
"Sends request for input variable from a field on target object");
83+
"requestOut",
84+
"Sends request for input variable from a field on target object");
8585
return &requestOut;
8686

8787
}
@@ -168,8 +168,9 @@ const Cinfo * Function::initCinfo()
168168
"max var. max of all arguments\n"
169169
"sum var. sum of all arguments\n"
170170
"avg var. mean value of all arguments\n"
171+
"rnd 0 rand(), random float between 0 and 1, honors global moose.seed.\n"
171172
"rand 1 rand(seed), random float between 0 and 1, \n"
172-
" if seed = -1, then a 'random' seed is created.\n"
173+
" if seed = -1, then a 'random' seed is used.\n"
173174
"rand2 3 rand(a, b, seed), random float between a and b, \n"
174175
" if seed = -1, a 'random' seed is created using either\n"
175176
" by random_device or by reading system clock\n"
@@ -235,89 +236,89 @@ const Cinfo * Function::initCinfo()
235236
// Shared messages
236237
///////////////////////////////////////////////////////////////////
237238
static DestFinfo process( "process",
238-
"Handles process call, updates internal time stamp.",
239-
new ProcOpFunc< Function >( &Function::process ) );
239+
"Handles process call, updates internal time stamp.",
240+
new ProcOpFunc< Function >( &Function::process ) );
240241
static DestFinfo reinit( "reinit",
241-
"Handles reinit call.",
242-
new ProcOpFunc< Function >( &Function::reinit ) );
242+
"Handles reinit call.",
243+
new ProcOpFunc< Function >( &Function::reinit ) );
243244
static Finfo* processShared[] = { &process, &reinit };
244245

245246
static SharedFinfo proc( "proc",
246-
"This is a shared message to receive Process messages "
247-
"from the scheduler objects."
248-
"The first entry in the shared msg is a MsgDest "
249-
"for the Process operation. It has a single argument, "
250-
"ProcInfo, which holds lots of information about current "
251-
"time, thread, dt and so on. The second entry is a MsgDest "
252-
"for the Reinit operation. It also uses ProcInfo. ",
253-
processShared, sizeof( processShared ) / sizeof( Finfo* )
254-
);
255-
/*
256-
static DestFinfo trigger( "trigger",
257-
"Handles trigger input. Argument is timestamp of event. This is "
258-
"compatible with spike events as well as chemical ones. ",
259-
new OpFunc1< Function, double >( &Function::trigger ) );
260-
*/
247+
"This is a shared message to receive Process messages "
248+
"from the scheduler objects."
249+
"The first entry in the shared msg is a MsgDest "
250+
"for the Process operation. It has a single argument, "
251+
"ProcInfo, which holds lots of information about current "
252+
"time, thread, dt and so on. The second entry is a MsgDest "
253+
"for the Reinit operation. It also uses ProcInfo. ",
254+
processShared, sizeof( processShared ) / sizeof( Finfo* )
255+
);
256+
/*
257+
static DestFinfo trigger( "trigger",
258+
"Handles trigger input. Argument is timestamp of event. This is "
259+
"compatible with spike events as well as chemical ones. ",
260+
new OpFunc1< Function, double >( &Function::trigger ) );
261+
*/
261262

262263
static Finfo *functionFinfos[] =
263-
{
264-
&value,
265-
&rate,
266-
&derivative,
267-
&mode,
268-
&useTrigger,
269-
&doEvalAtReinit,
270-
&expr,
271-
&numVars,
272-
&inputs,
273-
&constants,
274-
&independent,
275-
&proc,
276-
requestOut(),
277-
valueOut(),
278-
rateOut(),
279-
derivativeOut(),
280-
};
264+
{
265+
&value,
266+
&rate,
267+
&derivative,
268+
&mode,
269+
&useTrigger,
270+
&doEvalAtReinit,
271+
&expr,
272+
&numVars,
273+
&inputs,
274+
&constants,
275+
&independent,
276+
&proc,
277+
requestOut(),
278+
valueOut(),
279+
rateOut(),
280+
derivativeOut(),
281+
};
281282

282283
static string doc[] =
283-
{
284-
"Name", "Function",
285-
"Author", "Subhasis Ray",
286-
"Description",
287-
"General purpose function calculator using real numbers.\n"
288-
"It can parse mathematical expression defining a function and evaluate"
289-
" it and/or its derivative for specified variable values."
290-
"You can assign expressions of the form::\n"
291-
"\n"
292-
"f(c0, c1, ..., cM, x0, x1, ..., xN, y0,..., yP ) \n"
293-
"\n"
294-
" where `ci`'s are constants and `xi`'s and `yi`'s are variables."
295-
296-
"The constants must be defined before setting the expression and"
297-
" variables are connected via messages. The constants can have any"
298-
" name, but the variable names must be of the form x{i} or y{i}"
299-
" where i is increasing integer starting from 0.\n"
300-
" The variables can be input from other moose objects."
301-
" Such variables must be named `x{i}` in the expression and the source"
302-
" field is connected to Function.x[i]'s `input` destination field.\n"
303-
" In case the input variable is not available as a source field, but is"
304-
" a value field, then the value can be requested by connecting the"
305-
" `requestOut` message to the `get{Field}` destination on the target"
306-
" object. Such variables must be specified in the expression as y{i}"
307-
" and connecting the messages should happen in the same order as the"
308-
" y indices.\n"
309-
" This class handles only real numbers (C-double). Predefined constants"
310-
" are: pi=3.141592..., e=2.718281..."
311-
};
284+
{
285+
"Name", "Function",
286+
"Author", "Subhasis Ray",
287+
"Description",
288+
"General purpose function calculator using real numbers.\n"
289+
"It can parse mathematical expression defining a function and evaluate"
290+
" it and/or its derivative for specified variable values."
291+
"You can assign expressions of the form::\n"
292+
"\n"
293+
"f(c0, c1, ..., cM, x0, x1, ..., xN, y0,..., yP ) \n"
294+
"\n"
295+
" where `ci`'s are constants and `xi`'s and `yi`'s are variables."
296+
297+
"The constants must be defined before setting the expression and"
298+
" variables are connected via messages. The constants can have any"
299+
" name, but the variable names must be of the form x{i} or y{i}"
300+
" where i is increasing integer starting from 0.\n"
301+
" The variables can be input from other moose objects."
302+
" Such variables must be named `x{i}` in the expression and the source"
303+
" field is connected to Function.x[i]'s `input` destination field.\n"
304+
" In case the input variable is not available as a source field, but is"
305+
" a value field, then the value can be requested by connecting the"
306+
" `requestOut` message to the `get{Field}` destination on the target"
307+
" object. Such variables must be specified in the expression as y{i}"
308+
" and connecting the messages should happen in the same order as the"
309+
" y indices.\n"
310+
" This class handles only real numbers (C-double). Predefined constants"
311+
" are: pi=3.141592..., e=2.718281..."
312+
};
312313

313314
static Dinfo< Function > dinfo;
314315
static Cinfo functionCinfo("Function",
315-
Neutral::initCinfo(),
316-
functionFinfos,
317-
sizeof(functionFinfos) / sizeof(Finfo*),
318-
&dinfo,
319-
doc,
320-
sizeof(doc)/sizeof(string));
316+
Neutral::initCinfo(),
317+
functionFinfos,
318+
sizeof(functionFinfos) / sizeof(Finfo*),
319+
&dinfo,
320+
doc,
321+
sizeof(doc)/sizeof(string));
321322
return &functionCinfo;
322323

323324
}

builtins/Function.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,12 @@
1212
// URL:
1313
// Keywords:
1414
// Compatibility:
15-
//
16-
//
17-
18-
// Commentary:
19-
//
20-
// A new version of Func with FieldElements to collect data.
21-
//
22-
//
23-
24-
// Change log:
25-
//
26-
//
27-
//
28-
//
29-
// This program is free software; you can redistribute it and/or
30-
// modify it under the terms of the GNU General Public License as
31-
// published by the Free Software Foundation; either version 3, or
32-
// (at your option) any later version.
33-
//
34-
// This program is distributed in the hope that it will be useful,
35-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
36-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37-
// General Public License for more details.
38-
//
39-
// You should have received a copy of the GNU General Public License
40-
// along with this program; see the file COPYING. If not, write to
41-
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth
42-
// Floor, Boston, MA 02110-1301, USA.
43-
//
44-
//
4515

46-
// Code:
4716

4817
#ifndef _MOOSE_FUNCTION_H_
4918
#define _MOOSE_FUNCTION_H_
5019

51-
#include "../external/muparser/include/muParser.h"
20+
#include "MooseParser.h"
5221

5322
/**
5423
Simple function parser and evaluator for MOOSE. This can take a mathematical
@@ -158,7 +127,7 @@ class Function
158127
map< string, double *> _constbuf; // for constants
159128
string _independent; // index of independent variable
160129

161-
mu::Parser _parser;
130+
MooseParser _parser;
162131

163132
void _clearBuffer();
164133
void _showError(mu::Parser::exception_type &e) const;

builtins/MooseParser.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/***
2+
* Description: Class MooseParser.
3+
*
4+
* Created: 2019-05-30
5+
6+
* Author: Dilawar Singh <dilawars@ncbs.res.in>
7+
* Organization: NCBS Bangalore
8+
* License: MIT License
9+
*/
10+
11+
#include "MooseParser.h"
12+
13+
MooseParser::MooseParser()
14+
{}
15+
16+
MooseParser::~MooseParser()
17+
{}
18+

builtins/MooseParser.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/***
2+
* Description: MooseParser class.
3+
*
4+
* Created: 2019-05-30
5+
6+
* Author: Dilawar Singh <dilawars@ncbs.res.in>
7+
* Organization: NCBS Bangalore
8+
* License: MIT License
9+
*/
10+
11+
#ifndef MOOSEPARSER_H
12+
#define MOOSEPARSER_H
13+
14+
#include "../external/muparser/include/muParser.h"
15+
16+
class MooseParser : public mu::Parser
17+
{
18+
public:
19+
MooseParser();
20+
~MooseParser();
21+
22+
private:
23+
/* data */
24+
};
25+
26+
#endif /* end of include guard: MOOSEPARSER_H */

external/muparser/include/muParser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ namespace mu
107107
static value_type Rand2(value_type, value_type, value_type);
108108

109109
// Random number between 0 and 1, non-deterministic seed.
110-
static value_type Rand( value_type );
110+
static value_type Rand( value_type seed );
111+
static value_type Rnd( );
111112

112113
// Prefix operators
113114
// !!! Unary Minus is a MUST if you want to use negative signs !!!

external/muparser/src/muParser.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626
#include "../include/muParser.h"
2727
#include "../include/muParserTemplateMagic.h"
28+
#include "../../../basecode/global.h"
2829

2930
//--- Standard includes ------------------------------------------------------------------------
3031
#include <cmath>
@@ -112,10 +113,24 @@ namespace mu
112113
value_type Parser::Fmod(value_type v1, value_type v2) { return fmod(v1, v2); }
113114
value_type Parser::Quot(value_type v1, value_type v2) { return (int)(v1 / v2); }
114115

116+
value_type Parser::Rand( value_type seed = -1 )
117+
{
118+
static bool isSeedSet_ = false;
119+
120+
if( ! isSeedSet_ )
121+
{
122+
mu::rng.setSeed( seed );
123+
isSeedSet_ = true;
124+
}
125+
return rng.uniform( ); /* Between 0 and 1 */
126+
}
127+
115128
// If no seed is given,
116-
value_type Parser::Rand( value_type seed )
129+
value_type Parser::Rnd( )
117130
{
118131
static bool isSeedSet_ = false;
132+
// check if global seed is set
133+
size_t seed = moose::getGlobalSeed();
119134
if( ! isSeedSet_ )
120135
{
121136
mu::rng.setSeed( seed );
@@ -330,6 +345,7 @@ namespace mu
330345
DefineFun(_T("rint"), Rint);
331346
DefineFun(_T("abs"), Abs);
332347
DefineFun(_T("fmod"), Fmod);
348+
DefineFun(_T("rnd"), Rnd);
333349
DefineFun(_T("rand"), Rand);
334350
DefineFun(_T("rand2"), Rand2);
335351
// Functions with variable number of arguments

ksolve/FuncTerm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef _FUNC_TERM_H
1111
#define _FUNC_TERM_H
1212

13-
#include "../external/muparser/include/muParser.h"
13+
#include "../builtins/MooseParser.h"
1414

1515
class FuncTerm
1616
{
@@ -44,7 +44,7 @@ class FuncTerm
4444
double* args_;
4545
// Look up reactants in the S vec.
4646
vector< unsigned int > reactantIndex_;
47-
mu::Parser parser_;
47+
MooseParser parser_;
4848
string expr_;
4949
/**
5050
* Scale factor to account for pool volume if we are assigning conc

0 commit comments

Comments
 (0)