power spectra in python

25 views
Skip to first unread message

Axel Brandenburg

unread,
Jun 13, 2024, 3:19:43 AMJun 13
to pencil-co...@googlegroups.com
Hi all,
I'm told that the python power spectra don't work,
and that the time cannot be accessed. He may want to
modify the code. If there is an example showing that
the code works, let us know. There could be some other
routines that are not accesssed, e.g., self.t already.
Axel

Illa R. Losada

unread,
Jun 13, 2024, 5:05:20 AMJun 13
to pencil-co...@googlegroups.com
Hi Axel,

Could you please specify what exactly doesn't work?
I just ran an old script and it works for me.

There is a change needed in the python code though:

In the file 'pencil-code/python/pencil/read/powers.py' , line 282, "np.complex" need to change to "complex".
(I don't have the new git remote setup in this computer to do the change right now).

After this change, pc.read.power works for me:
In [4]: pux = pc.read.power(datadir=mydatadir, fn=fnin)
Reading only  powerux_xy.dat
appending ux_xy
powerux_xy.dat
More than 1 z-pos

In [6]: pux.keys()
t
kx
ky
nzpos
zpos
ux_xy

In [7]: pux.t
Out[7]:  
array([7.8260250e-02, 1.2521636e-01, 1.7217250e-01, ..., 6.2597211e+02,
      6.2601910e+02, 6.2606604e+02], dtype=float32)


Illa





--
You received this message because you are subscribed to the Google Groups "pencil-code-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pencil-code-dis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pencil-code-discuss/ZmqdiOqIBHnGZAUt%40nl6.

Kishore Gopalakrishnan

unread,
Jun 13, 2024, 5:11:26 AMJun 13
to pencil-code-discuss
There is a test in the Python testsuite (python/tests/read.py/test_read_power).

If someone has problems, they should report
1. Python version
2. What kind of spectra they are reading (power, power_xy, ...)
3. Pencil code commit which they are testing

Kishore G.

வியா., 13 ஜூன், 2024, பிற்பகல் 12:49க்கு, Axel Brandenburg <axelbra...@gmail.com> எழுதியது:

Kishore G

unread,
Jun 13, 2024, 5:19:36 AMJun 13
to pencil-co...@googlegroups.com
வியாழன், 13 ஜூன், 2024 பிற்பகல் 2:35:06 IST, Illa R. Losada எழுதியது
(on Thursday, 13 June 2024 14:35:06 IST Illa R. Losada wrote):
>
> There is a change needed in the python code though:
>
> In the file 'pencil-code/python/pencil/read/powers.py' , line 282,
> "np.complex" need to change to "complex".

Are you using the latest version of the Python code? np.complex has been removed in recent versions of numpy (I vaguely remember making the relevant changes to the Python module some time ago). I don't find any occurrences of np.complex in /python/pencil/read/powers.py as of the latest version (commit fc83e474b5a38544d56d4d78bd39003f7edb2c15 ).

--
Kishore




Illa R. Losada

unread,
Jun 13, 2024, 5:22:31 AMJun 13
to pencil-co...@googlegroups.com
You are right, I am not on the latest version. And yep, I saw that too and that is the change I made to make it work.

--
You received this message because you are subscribed to the Google Groups "pencil-code-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pencil-code-dis...@googlegroups.com.

Dahl, Jani I

unread,
Jun 13, 2024, 8:23:25 AMJun 13
to pencil-co...@googlegroups.com
Hi,

I think I found the cause for the error I got when trying to access spectrum data with pencil.read in Python. I was trying to read data from a run that Axel checked in that didn't have the proc files in the data directory. This gave me an "UnboundLocalError: local variable 't' referenced before assignment" error on line 278 of the grids.py file (in python/pencil/read).

The method there (read) seems to first check whether grid.h5 exists, and if not, then goes on to create a list of all the proc directories, which in my case ends up being empty because they don't exist. Because the code loops over an empty list, t and other quantities on lines 278-284 are never assigned and I end up with the UnboundLocalError.

The method that accesses grid.read in this case is _get_nk_xyz in powers.py and there it's excepting only FileNotFoundError on line 396, which is never raised because an empty list is created instead. Changing that line to "except UnboundLocalError:" solves the issue and allows me to load the data, as it then assumes a cubical box instead of terminating.

Therefore, I think the loading of the proc/grid.dat files in grids.py could be changed so that a FileNotFoundError is raised, or the current if-else statement starting from line 109 could be changed to if-elif-else, where it first checks for grid.h5, then for grid.dat, and in the else clause raises the FileNotFoundError.

Jani



Lähettäjä: pencil-co...@googlegroups.com <pencil-co...@googlegroups.com> käyttäjän Illa R. Losada <illa.rive...@gmail.com> puolesta
Lähetetty: torstai 13. kesäkuuta 2024 12.22
Vastaanottaja: pencil-co...@googlegroups.com <pencil-co...@googlegroups.com>
Aihe: Re: power spectra in python
 

Kishore G

unread,
Jun 14, 2024, 12:40:18 AMJun 14
to pencil-co...@googlegroups.com
வியாழன், 13 ஜூன், 2024 பிற்பகல் 5:53:19 IST, Dahl, Jani I எழுதியது
(on Thursday, 13 June 2024 17:53:19 IST Dahl, Jani I wrote):
> I think I found the cause for the error I got when trying to access spectrum data with pencil.read in Python. I was trying to read data from a run that Axel checked in that didn't have the proc files in the data directory. This gave me an "UnboundLocalError: local variable 't' referenced before assignment" error on line 278 of the grids.py file (in python/pencil/read).
>

Thanks for debugging the issue. I have now made the change you suggested (raise FileNotFoundError ; commit e918ce547ab1c57f1ea161a5d243c2a83cd48ea8 in the master branch).

--
Kishore G.



Reply all
Reply to author
Forward
0 new messages