The Script Element
Download
Report
Transcript The Script Element
GoldSim Monthly Webinar Series
Goals
Learn the basics of simple scripts
Learn the basics of the GoldSim Script
Element
Not a lesson on numerical methods
What is a Script?
A set of instructions
Carried out by another program
Instructions are executed and
something(s) is (usually) returned as
output
Script languages are easier and faster to
code in
Why use the Scripts?
Clarify complex logic
Nested IF statements
Iterate through items of an array
Condense equations
Repetitive equations
Long series of equations
Numerical solution required
Iterative solvers like bisection
Different Ways in GoldSim
GoldSim elements, looping containers
Equations in Excel produce output that
is read by GoldSim
External functions connected to
GoldSim
DLL Element
Write a script inside GoldSim
Script Element
Advantages of the Script
All in one program
Transparent
No need to learn a new language
Main Functions
Variable definition
Variable assignment
Flow control (i.e. IF, looping, break)
Comments
Log Statements
Variable Definition
Syntax:
Define: peak_price = 0.0 $/m3
Declaration:
Number (float: double precision) or Boolean
scalar or non-scalar
Initialize to a value
Description
Expose as Output
Variable Assignment
Syntax:
~peak_price = 50 $/m3
Result = ~peak_price
Local vs. Global variables
For Loop
Loop Variable, Start Value, Loop while
Condition, Increment by
Syntax:
FOR (i = 0; ~i < 10; i = ~i + 1)
{do some work}
END FOR
DO Loop
Loop Variable, Start Value, End Value,
Increment by
Syntax:
DO i = 0, 10, 1
{do some work}
END DO
REPEAT-UNTIL Loop
Loop until true: Some Condition
Syntax:
REPEAT
{do some work}
UNTIL (~price > 30 $/m3)
WHILE Loop
Loop while true: Some Condition
Syntax:
WHILE (~price < 10 $/m3)
{do some work}
END WHILE
BREAK and CONTINUE
Only allowed inside Loop structures
BREAK
Break out of a loop
Breaks the nearest enclosing loop
CONTINUE
Transfer control to the bottom of the loop
Used to skip to the next loop without
executing block in the loop
Comments
Document the operation of the program
Intended for others and yourself
Max 100 characters
Ctrl-Enter extra lines only seen in
printout
White space
Log Statements
Message
Message to Run Log
No warning
Warning
Message
Warning dialogue message
Error
Message
Simulation is stopped with dialogue
message
Prioritize Days By Price
Save days as numbers in a vector in order of
greatest unit price to lowest. Only show values
that meet criteria.
Peak Price Days
8
2
1
7
9
Formulation
Save all values to an array of days
Loop thru the list (array)
If the max(list) is <= $0/m3, then done!
Stuff the day of max price to temp
variable
Assign temp to Result Array at current
index
Remove the current max price from list
Loop
Create Table of Best Days
Day
1
2
3
4
5
6
7
8
9
10
price
0.84147
0.9093
0.14112
-0.7568
-0.95892
-0.27942
0.65699
0.98936
0.41212
-0.54402
1
2
3
4
5
6
7
8
9
10
Best days
8
2
1
7
9
3
Final Result