Hi Ian, Wrapping `sim.output_field_function` in a step function should do the trick (leaving esq_mag in its original form).
``` def my_output(sim): sim.output_field_function('E_sq', [mp.Ex, mp.Ey, mp.Ez], esq_mag) sim.run(mp.to_appended('test', mp.at_every(0.1, my_output)), until=10) ``` `at_every` expects a function, but was getting the result of calling `mp.output_field_function`, which returns 'None'. I'll make some improvements to the documentation based on your helpful suggestions. Thanks, Chris On Wed, Feb 7, 2018 at 6:25 PM, Ian Sage <i...@sage.myzen.co.uk> wrote: > The documentation is not all that clear about the procedure to follow, > when using the Python interface. I'd like to call a function like: > > def esq_mag(r, ex, ey, ez): > return ex*ex.conjugate()+ey*ey.conjugate()+ez*ez.conjugate() > > from the at_every construct - I initially used the above function together > with: > > sim.run(mp.to_appended(prefix, mp.at_every(0.1, > sim.output_field_function('E_sq', [mp.Ex, mp.Ey, mp.Ez], esq_mag))), > until=t) > > but the program runs to the point where output is triggered, then stops > with a lengthy error message, terminating: > > File "/usr/lib/python3.6/site-packages/meep/simulation.py", line > 28, in get_num_args > return func.__code__.co_argcount > AttributeError: 'NoneType' object has no attribute '__code__' > > That does not surprise me as the docs state I have to pass a function to > the sim.run function, not the result of evaluating a function. So I tried > again, supplying in the spirit of the instructions related to Scheme "a > function my-weird-output of no arguments that, when called, outputs our > function f." So I constructed a function to do that: > > def esq_mag(): > def _esq_mag(r, ex, ey, ez): > return ex*ex.conjugate()+ey*ey.conjugate()+ez*ez.conjugate() > return _esq_mag > > but the result is another error: > > TypeError: esq_mag() takes 0 positional arguments but 4 were given > > OK - I need to handle those arguments to esq_mag, so > > def esq_mag(r, ex, ey, ez): > def _esq_mag(r, ex, ey, ez): > return ex*ex.conjugate()+ey*ey.conjugate()+ez*ez.conjugate() > return _esq_mag > > but now the program fails with: > > TypeError: must be real number, not function > > The above exception was the direct cause of the following exception: > > SystemError: PyEval_EvalFrameEx returned a result with an error set > > I have tried several variations without success; please, what construct > should I be using? > > Also, if I try to use output_real_field_function as described in the docs, > I get the error: > > AttributeError: 'Simulation' object has no attribute > 'output_real_field_function' > > I'll be very grateful for any help, > > Ian > > _______________________________________________ > meep-discuss mailing list > meep-discuss@ab-initio.mit.edu > http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss >
_______________________________________________ meep-discuss mailing list meep-discuss@ab-initio.mit.edu http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/meep-discuss