pydstool icon indicating copy to clipboard operation
pydstool copied to clipboard

Duffing oscillator with PyCont

Open JonEhrmann opened this issue 3 years ago • 5 comments

Hi Rob, I'm a Ph.D. student trying to implement the Duffing oscillator in PyCont for path continuation of the periodic solutions. I'm struggeling with the mathematical implementation of the differential equation. I used the following code:

from PyDSTool import *

DSargs = args()
DSargs.name = "duffing"
DSargs.ics = {"q": 0.1, "dq": 0}
DSargs.pars = {"wdach": 0.05, "D": 0.05, "g": -1, "eta": 0.7}
DSargs.tdata = [0, 500]
DSargs.varspecs = {"q": "dq",
                   "dq": "wdach*cos(eta*t)-2*D*dq-q-g*pow(q,3)"}

DS = Generator.Vode_ODEsystem(DSargs)

traj = DS.compute("duffing")
pts = traj.sample()
# plt.plot(pts["t"], pts["q"])
# plt.plot(pts["t"], pts["dq"])

q0 = max(pts["q"][30000:])
dq0 = max(pts["dq"][30000:])

PC = ContClass(DS)
PCargs = args(
    name="duff_cont",
    type="LC-C",
    freepars=["eta"],
    initpoint={"q": q0, "dq": dq0},
    # MaxNumPoints=450,
    # MaxStepSize=1e-4,
    # MinStepSize=1e-5,
    # StepSize=1e-5,
    LocBifPoints=["all"],
    StopAtPoints=["B"],
    SaveEigen=True,
)
PC.newCurve(PCargs)
PC["duff_cont"].forward()
PC["duff_cont"].display(["eta", "q"], stability=True)

Is it possible to implement the differential equation like this? Because the current result of the simulation is like a constant value (q plot over eta).

It would be nice to hear from you!

JonEhrmann avatar Nov 24 '21 14:11 JonEhrmann

Hi, I think you'll need to provide a starting trajectory that is close to one cycle so that it has the period information. At least on my system, AUTO tells me that it cannot compute the period. If you're not getting an issue from AUTO then are you sure you have your system set up correctly to use AUTO?

Maybe you could try following the pattern indicated in the solution code attached at the end of this thread https://sourceforge.net/p/pydstool/discussion/472291/thread/aa15f0ee/ ?

On Wed, Nov 24, 2021 at 9:49 AM JonEhrmann @.***> wrote:

Hi Rob, I'm a Ph.D. student trying to implement the Duffing oscillator in PyCont for path continuation of the periodic solutions. I'm struggeling with the mathematical implementation of the differential equation. I used the following code:

from PyDSTool import *

DSargs = args() DSargs.name = "duffing" DSargs.ics = {"q": 0.1, "dq": 0} DSargs.pars = {"wdach": 0.05, "D": 0.05, "g": -1, "eta": 0.7} DSargs.tdata = [0, 500] DSargs.varspecs = {"q": "dq", "dq": "wdachcos(etat)-2Ddq-q-g*pow(q,3)"}

DS = Generator.Vode_ODEsystem(DSargs)

traj = DS.compute("duffing") pts = traj.sample()

plt.plot(pts["t"], pts["q"])

plt.plot(pts["t"], pts["dq"])

q0 = max(pts["q"][30000:]) dq0 = max(pts["dq"][30000:])

PC = ContClass(DS) PCargs = args( name="duff_cont", type="LC-C", freepars=["eta"], initpoint={"q": q0, "dq": dq0}, # MaxNumPoints=450, # MaxStepSize=1e-4, # MinStepSize=1e-5, # StepSize=1e-5, LocBifPoints=["all"], StopAtPoints=["B"], SaveEigen=True, ) PC.newCurve(PCargs) PC["duff_cont"].forward() PC["duff_cont"].display(["eta", "q"], stability=True)

Is it possible to implement the differential equation like this? Because the current result of the simulation is like a constant value (q plot over eta).

It would be nice to hear from you!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/robclewley/pydstool/issues/166, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFEUZI5S5TOU45F6VG5W2TUNT3OBANCNFSM5IWIKVGQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub .

robclewley avatar Nov 24 '21 15:11 robclewley

There's a good chance that's a problem with the way you've entered strings, esp. if you've copy-pasted anything. Sometimes "smart" quotes get in there and mess things up, for instance. If you still have trouble please attach your script.

On Thu, Nov 25, 2021 at 3:42 AM JonEhrmann @.***> wrote:

Thanks for your quick answer!

I think my setup was not right... But now, I'm getting the following error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x94 in position 179: invalid start byte

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/robclewley/pydstool/issues/166#issuecomment-978957870, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFEUZIZQ7MGPEJ6N7V7QP3UNXZH5ANCNFSM5IWIKVGQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

robclewley avatar Nov 25 '21 14:11 robclewley

Thanks again for your answer!

I'm still not completely sure, if PyDSTool is set up correctly. I implemented the following script

from PyDSTool import *

DSargs = args()
DSargs.name = "duffing"
DSargs.ics = {"q": 0.1, "dq": 0}
DSargs.pars = {"wdach": 0.05, "D": 0.05, "g": -1, "eta": 0.7}
DSargs.tdata = [0, 500]
DSargs.varspecs = {"q": "dq",
                   "dq": "wdach*cos(eta*t)-2*D*dq-q-g*pow(q,3)"}

DS = Generator.Vode_ODEsystem(DSargs)

traj = DS.compute("duffing")
pts = traj.sample()

q0 = max(pts["q"][30000:])
dq0 = max(pts["dq"][30000:])

PC = ContClass(DS)
PCargs = args(
    name="duff_cont",
    type="LC-C",
    freepars=["eta"],
    initpoint={"q": q0, "dq": dq0},
    LocBifPoints=["all"],
    StopAtPoints=["B"],
    SaveEigen=True,
)
PC.newCurve(PCargs)
PC["duff_cont"].forward()
PC["duff_cont"].display(["eta", "q"], stability=True)

And I get the following error

  File "C:\pydstool-master\PyDSTool\core\context_managers.py", line 30, in _stdchannel_redirected
    oldstdchannel = os.dup(stdchannel.fileno())

UnsupportedOperation: fileno


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "L:\duffing_pycont2.py", line 41, in <module>
    PC.newCurve(PCargs)

  File "C:\pydstool-master\PyDSTool\PyCont\ContClass.py", line 211, in newCurve
    self.loadAutoMod()

  File "C:\pydstool-master\PyDSTool\PyCont\ContClass.py", line 423, in loadAutoMod
    self.compileAutoLib()

  File "C:\pydstool-master\PyDSTool\PyCont\ContClass.py", line 704, in compileAutoLib
    setup(name="Auto 2000 continuer",

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\core.py", line 169, in setup
    return old_setup(**new_attr)

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\core.py", line 148, in setup
    dist.run_commands()

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\dist.py", line 966, in run_commands
    self.run_command(cmd)

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\command\build.py", line 61, in run
    old_build.run(self)

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\command\build.py", line 135, in run
    self.run_command(cmd_name)

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\command\build_ext.py", line 135, in run
    self.compiler = new_compiler(compiler=compiler_type,

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\ccompiler.py", line 761, in new_compiler
    compiler = klass(None, dry_run, force)

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 97, in __init__
    build_import_library()

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 411, in build_import_library
    return _build_import_library_amd64()

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 467, in _build_import_library_amd64
    generate_def(dll_file, def_file)

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 293, in generate_def
    dump = dump_table(dll)

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 285, in dump_table
    st = subprocess.check_output(["objdump.exe", "-p", dll])

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\subprocess.py", line 424, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,

  File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,

CalledProcessError: Command '['objdump.exe', '-p', 'C:\\WinPython\\WPy64-3950\\python-3.9.5.amd64\\python39.dll']' returned non-zero exit status 1.


objdump.exe: C:\WinPython\WPy64-3950\python-3.9.5.amd64\python39.dll: File format not recognized

I hope you could help...!

JonEhrmann avatar Nov 26 '21 08:11 JonEhrmann

Hi, yes there has been no testing done with recent python versions. I think the last one that was well-tested was ~v3.7.5. Getting fortran code to build automatically is always tricky with Windows installations, given the incompatibility with the usual MSVC compiler builds for libraries vs. the MINGW compiler accessible to us to build extensions. You could try following the steps in the top solution posted here, but I can't guarantee anything. In the short term, you'll get ahead on this most easily if you can avoid Windows and use py 3.7.

https://stackoverflow.com/questions/48826283/compile-fortran-module-with-f2py-and-python-3-6-on-windows-10?rq=1

On Fri, Nov 26, 2021 at 3:30 AM JonEhrmann @.***> wrote:

Thanks again for your answer!

I'm still not completely sure, if PyDSTool is set up correctly. I implemented the following script

from PyDSTool import *

DSargs = args() DSargs.name = "duffing" DSargs.ics = {"q": 0.1, "dq": 0} DSargs.pars = {"wdach": 0.05, "D": 0.05, "g": -1, "eta": 0.7} DSargs.tdata = [0, 500] DSargs.varspecs = {"q": "dq", "dq": "wdachcos(etat)-2Ddq-q-g*pow(q,3)"}

DS = Generator.Vode_ODEsystem(DSargs)

traj = DS.compute("duffing") pts = traj.sample()

q0 = max(pts["q"][30000:]) dq0 = max(pts["dq"][30000:])

PC = ContClass(DS) PCargs = args( name="duff_cont", type="LC-C", freepars=["eta"], initpoint={"q": q0, "dq": dq0}, LocBifPoints=["all"], StopAtPoints=["B"], SaveEigen=True, ) PC.newCurve(PCargs) PC["duff_cont"].forward() PC["duff_cont"].display(["eta", "q"], stability=True)

And I get the following error

File "C:\pydstool-master\PyDSTool\core\context_managers.py", line 30, in _stdchannel_redirected oldstdchannel = os.dup(stdchannel.fileno())

UnsupportedOperation: fileno

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "L:\duffing_pycont2.py", line 41, in PC.newCurve(PCargs)

File "C:\pydstool-master\PyDSTool\PyCont\ContClass.py", line 211, in newCurve self.loadAutoMod()

File "C:\pydstool-master\PyDSTool\PyCont\ContClass.py", line 423, in loadAutoMod self.compileAutoLib()

File "C:\pydstool-master\PyDSTool\PyCont\ContClass.py", line 704, in compileAutoLib setup(name="Auto 2000 continuer",

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\core.py", line 169, in setup return old_setup(**new_attr)

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\core.py", line 148, in setup dist.run_commands()

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\dist.py", line 966, in run_commands self.run_command(cmd)

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\dist.py", line 985, in run_command cmd_obj.run()

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\command\build.py", line 61, in run old_build.run(self)

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\command\build.py", line 135, in run self.run_command(cmd_name)

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command)

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\distutils\dist.py", line 985, in run_command cmd_obj.run()

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\command\build_ext.py", line 135, in run self.compiler = new_compiler(compiler=compiler_type,

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\ccompiler.py", line 761, in new_compiler compiler = klass(None, dry_run, force)

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 97, in init build_import_library()

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 411, in build_import_library return _build_import_library_amd64()

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 467, in _build_import_library_amd64 generate_def(dll_file, def_file)

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 293, in generate_def dump = dump_table(dll)

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\site-packages\numpy\distutils\mingw32ccompiler.py", line 285, in dump_table st = subprocess.check_output(["objdump.exe", "-p", dll])

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\subprocess.py", line 424, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,

File "C:\WinPython\WPy64-3950\python-3.9.5.amd64\lib\subprocess.py", line 528, in run raise CalledProcessError(retcode, process.args,

CalledProcessError: Command '['objdump.exe', '-p', 'C:\WinPython\WPy64-3950\python-3.9.5.amd64\python39.dll']' returned non-zero exit status 1.

objdump.exe: C:\WinPython\WPy64-3950\python-3.9.5.amd64\python39.dll: File format not recognized

I hope you could help...!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/robclewley/pydstool/issues/166#issuecomment-979782503, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFEUZKFHN6ZBWDFYAFJPLTUN5ARPANCNFSM5IWIKVGQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

robclewley avatar Nov 26 '21 16:11 robclewley

Ok thank you for your help! I will look, how I get around.

JonEhrmann avatar Nov 30 '21 13:11 JonEhrmann