That solved it. Thanks a lot!

Karl-Johan

________________________________
Från: Christopher Hogan <ho...@simpetus.com>
Skickat: den 25 mars 2019 13:52:27
Till: Karl-Johan Olofsson
Kopia: meep-discuss@ab-initio.mit.edu
Ämne: Re: [Meep-discuss] Run function, comparing total flux at different 
intervals

Oh, right. You have to use the 'global' keyword so Python knows it isn't a 
local variable (this only works in python3)

def flux1_step(sim)
    global flux1
    flux1 = ...

Chris

On Mon, Mar 25, 2019 at 10:48 AM Karl-Johan Olofsson 
<karol...@student.liu.se<mailto:karol...@student.liu.se>> wrote:

I doesn't get it to work. The step functions doesn't seem to redefine flux1 and 
flux2 properly. If I print out flux1,2 in the ratio_test function it keep 
printing out the initiated values for some reason.


Karl-Johan

________________________________
Från: Christopher Hogan <ho...@simpetus.com<mailto:ho...@simpetus.com>>
Skickat: den 25 mars 2019 11:51:40
Till: Karl-Johan Olofsson
Kopia: meep-discuss@ab-initio.mit.edu<mailto:meep-discuss@ab-initio.mit.edu>
Ämne: Re: [Meep-discuss] Run function, comparing total flux at different 
intervals

I haven't tested this, but something like this should work. The key is to use 
global variables.

sim = ...
total_flux = ...
flux1 = 0
flux2 = 0

def output_power(sim):
    return mp.get_fluxes(total_flux)[0]

def flux1_step(sim):
    flux1 = output_power(sim)

def flux2_step(sim):
    flux2 = output_power(sim)

def ratio_test()
    return abs(flux2 / flux1 - 1) < 0.01

sim.run(mp.at_every(3, flux1_step), mp.at_every(5, flux2_step), 
until=ratio_test)


On Fri, Mar 22, 2019 at 9:09 AM Karl-Johan Olofsson 
<karol...@student.liu.se<mailto:karol...@student.liu.se>> wrote:

Hello!

I am attempting to implement a stopping criteria that compares the total flux 
at two different points in time, and when the difference between the total 
power radiated is small, I want to stop the simulation. I seem to be 
misunderstanding the programming aspect of it all, I attemped a very basic 
implementation this way:


def output_power(sim):
    return(mp.get_fluxes(flux_total))

def ratio_test(Flux1,Flux2):
    if abs(Flux2/Flux1-1)<0.01:
        return True

    else:
        return False

..

sim.run(flux1=mp.at_every(3,output_power), flux2=mp.at_every(5,output_power),
until=ratio_test(flux1,flux2))

-----

But it clearly doesn't work. (It returns
ValueError: Unrecognized keyword arguments: dict_keys(['flux1', 'flux2']))

How would I go about to implement this. Or maybe you have some more 
sophisticated way to solve the stop condition for my simulation?

Karl-Johan





_______________________________________________
meep-discuss mailing list
meep-discuss@ab-initio.mit.edu<mailto: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

Reply via email to