http://jdj.mit.edu/wiki/index.php?title=The_run_function_is_not_a_loop&action=history&feed=atomThe run function is not a loop - Revision history2024-03-28T22:37:14ZRevision history for this page on the wikiMediaWiki 1.7.3http://jdj.mit.edu/wiki/index.php?title=The_run_function_is_not_a_loop&diff=4073&oldid=prevStevenj: /* The wrong way */2010-05-03T21:01:10Z<p><span class="autocomment">The wrong way</span></p>
<table border='0' width='98%' cellpadding='0' cellspacing='4' style="background-color: white;">
<tr>
<td colspan='2' width='50%' align='center' style="background-color: white;">←Older revision</td>
<td colspan='2' width='50%' align='center' style="background-color: white;">Revision as of 21:01, 3 May 2010</td>
</tr>
<tr><td colspan="2" align="left"><strong>Line 19:</strong></td>
<td colspan="2" align="left"><strong>Line 19:</strong></td></tr>
<tr><td> </td><td style="background: #eee; font-size: smaller;"> (print "Hello world!\n"))</td><td> </td><td style="background: #eee; font-size: smaller;"> (print "Hello world!\n"))</td></tr>
<tr><td> </td><td style="background: #eee; font-size: smaller;"></td><td> </td><td style="background: #eee; font-size: smaller;"></td></tr>
<tr><td>-</td><td style="background: #ffa; font-size: smaller;">'''This is wrong'''. It will output "Hello world!" ''once'', then give an error. What is going on? The problem is that you are thinking of <code>run-until</code> in the wrong way, as if it <span style="color: red; font-weight: bold;">is </span>a loop:</td><td>+</td><td style="background: #cfc; font-size: smaller;">'''This is wrong'''. It will output "Hello world!" ''once'', then give an error. What is going on? The problem is that you are thinking of <code>run-until</code> in the wrong way, as if it <span style="color: red; font-weight: bold;">were </span>a loop:</td></tr>
<tr><td> </td><td style="background: #eee; font-size: smaller;"></td><td> </td><td style="background: #eee; font-size: smaller;"></td></tr>
<tr><td> </td><td style="background: #eee; font-size: smaller;"> for time &leq; 200 do</td><td> </td><td style="background: #eee; font-size: smaller;"> for time &leq; 200 do</td></tr>
</table>
Stevenjhttp://jdj.mit.edu/wiki/index.php?title=The_run_function_is_not_a_loop&diff=3259&oldid=prevStevenj at 01:10, 24 September 20082008-09-24T01:10:59Z<p></p>
<table border='0' width='98%' cellpadding='0' cellspacing='4' style="background-color: white;">
<tr>
<td colspan='2' width='50%' align='center' style="background-color: white;">←Older revision</td>
<td colspan='2' width='50%' align='center' style="background-color: white;">Revision as of 01:10, 24 September 2008</td>
</tr>
<tr><td colspan="2" align="left"><strong>Line 54:</strong></td>
<td colspan="2" align="left"><strong>Line 54:</strong></td></tr>
<tr><td> </td><td style="background: #eee; font-size: smaller;"></td><td> </td><td style="background: #eee; font-size: smaller;"></td></tr>
<tr><td> </td><td style="background: #eee; font-size: smaller;">Here, the <code>(lambda () ...)</code> defines a function of no arguments <code>()</code> that, when called, executes the <code>...</code> statements.</td><td> </td><td style="background: #eee; font-size: smaller;">Here, the <code>(lambda () ...)</code> defines a function of no arguments <code>()</code> that, when called, executes the <code>...</code> statements.</td></tr>
<tr><td colspan="2"> </td><td>+</td><td style="background: #cfc; font-size: smaller;"></td></tr>
<tr><td colspan="2"> </td><td>+</td><td style="background: #cfc; font-size: smaller;">[[Category:Meep examples]]</td></tr>
</table>
Stevenjhttp://jdj.mit.edu/wiki/index.php?title=The_run_function_is_not_a_loop&diff=2080&oldid=prevStevenj: created2007-08-09T02:09:58Z<p>created</p>
<p><b>New page</b></p><div>{{Meep}}<br />
<br />
In Meep, there are functions <code>run-until</code> and similar that run the simulation, and take arguments allowing custom actions to be performed on every time step, or on some subset of the time steps. Many users misunderstand this, however, and make the same mistake: they think the <code>run</code> function is a "looping" construct of some kind, and that you can just put any code you want into it and it will get executed for every time step. This mistake and how to correct it are described in this article.<br />
<br />
=== Hello world ===<br />
<br />
Let's consider a "Hello world" example. Suppose we start with a control file that runs for 200 time units and outputs <math>E_z</math> on each time step:<br />
<br />
(run-until 200 output-efield-z)<br />
<br />
and now we want to modify it to also print "Hello world!" for every time step, as it is running. <br />
<br />
==== The wrong way ====<br />
<br />
Many users will naively write:<br />
<br />
(run-until 200<br />
output-efield-z<br />
(print "Hello world!\n"))<br />
<br />
'''This is wrong'''. It will output "Hello world!" ''once'', then give an error. What is going on? The problem is that you are thinking of <code>run-until</code> in the wrong way, as if it is a loop:<br />
<br />
for time &leq; 200 do<br />
output-efield-z<br />
(print "Hello world!\n")<br />
<br />
This is '''not''' what is happening. Instead, <code>run-until</code> is just a ''function'' that runs the simulation, and its arguments should be ''functions'' that are called for each time step. That is, it is really doing something like:<br />
<br />
evaluate the arguments: 200: a number<br />
output-efield-z: a function<br />
(print "Hello world!\n"): prints output and returns #&lt;unspecified&gt;<br />
call run-until:<br />
time-step until t=200<br />
at each time step, call the arguments:<br />
call (output-efield-z)<br />
call (#<unspecified>)<br />
<br />
Two things went wrong. First, the arguments are evaluated ''before'' calling the function, which means that the <code>print</code> statement is executed before <code>run-until</code> even starts! Second, <code>run-until</code> then tries to call the ''result'' of <code>(print ...)</code> as if it were a function, which causes an error because <code>(print ...)</code> does not return a function. (The <code>print</code> returns a special Scheme code <code>#&lt;unspecified&gt;</code> that means it doesn't really return anything at all.)<br />
<br />
==== The right way ====<br />
<br />
What we ''should'' have passed to <code>run-until</code>, instead of the ''result'' of calling <code>(print ...)</code>, is a ''function'' that calls <code>(print ...)</code>. There are two ways to do it.<br />
<br />
First, we could explicitly define a function, call it <code>my-hello</code>, that does what we want:<br />
<br />
(define (my-hello) (print "Hello world!\n"))<br />
(run-until 200 output-efield-z my-hello)<br />
<br />
Notice two things. First, <code>my-hello</code> is a function of no arguments, which means that it is just called at every time step (another, more complicated, possibility is described in the [[Meep Reference#Writing your own step functions|Meep Reference]]). Second, when we call <code>run-until</code>, we just pass the ''name'' of the function <code>my-hello</code>, and not the ''result'' of calling the function <code>(my-hello)</code>.<br />
<br />
A second possibility is that we could use Scheme's <code>lambda</code> construct to define our function in-line. The <code>lambda</code> syntax in Scheme allows you to define "anonymous" functions without assigning them a name via <code>define</code>, and to stick the function definition right into another expression. It works like this:<br />
<br />
(run-until 200 output-efield-z (lambda () (print "Hello world!\n")))<br />
<br />
Here, the <code>(lambda () ...)</code> defines a function of no arguments <code>()</code> that, when called, executes the <code>...</code> statements.</div>Stevenj