-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python API to the plasma-state #2
Comments
There MAY be an easier path. Long time ago, the guys at Tech-X were interested in coding up a C++ interface top the PS. Alex Pletzer (I think) actually did this, and Doug incorporated the code into the Python script that generates the plasma state. The main idea is to put a simple F77 wrapper around all PS calls, and then put a C and C++ interface around those F77 wrappers . The code is in the old ORNL swim repo (which now lives in https://cswim.org/svn/cswim/ips/branches/combined-code) in state/src/ccps_lib and state/src/ccps_test. The code in ccps_lib is augmented greatly when the state is actually built (see it in its full glory on Edison in /project/projectdirs/m876/elwasif/trunk/components/state/src/ccps_lib ) Now I managed to build this coder and the associated test code on Edison (I had to fill in some gaps for some missing header files, so it may not be the 100% correct). When I run the test I get some weird output that suggests some MPI functionality is being invoked and I'm not really sure if the test is succeeding or not. Soooooooo, if we convince ourselves that this C/C++ interface actually works against the current incarnation of the PS (it was done over 5 years ago from some of the dates in the source files), it should be relatively straightforward to build a Python wrapper around it (using swig http://www.swig.org/). For that we will need to code up some meaningful tests that exercise the routines we care about. We may have to deal with some issues related to the use of static libraries as the basis for a Python module, but a quick google search suggests this may not be totally insane. |
ccps_lib is a part of public release of PS. Not sure how often components/state is updated. It might be good idea to start with http://w3.pppl.gov/ntcc/PlasmaState/. @dlg0 already built most kind of stuffs including NTCC, ... |
@parkjm Indeed. I've just rebuilt the PS version from PPPL on edison (I needed to add -fpic flag for Python's sake), and the included ccps_lib builds fine. I plan on using this version to build the Python module. |
The (very alpha) PS python module, based on the ntcc plasma state code is in /project/projectdirs/atom/users/elwasif/plasma_state_build/pyplasmastate on edison. This one uses the Intel build of the NTCC code, but that shouldn't matter (I think). I can import the module and create a new instance of the PS from Python. Data movement in/out of the underlying Fortran code "may" need more work, especially if we want this to work seamlessly with Numpy (the C++ PS interface uses its own multiarray class).
|
Great!!! Could you please make PS module access the plasma state variables (access to ps%ns, ...)? The component helpers generally look like (for example https://github.com/ORNL-Fusion/ips-fastran/blob/master/src/zgenray.py ) Input helper: Output helper: Then perfect, I think!! |
@parkjm Right now, almost everything there is pretty much auto-generated by SWIG. I'm still exploring what can be done using this starting point, and then see how best to modify it and/or build something on top to make it more intuitive. |
@elwasif, |
@parkjm Well, I think I can get something in there (Alpha/Beta quality) for get/set functionality. The interface may not be exactly what you have in mind, but we will be able to change that incrementally. Having access to the other routines (interpolation, ..etc) is a different story, but I suspect you're not using them anyway. |
@elwasif Thanks. Yes, we can change the interface incrementally. I'm not a big fan of the PS interpolation API, but many of IPS components in the SWIM repo rely on the interpolation API. I think the interpolation API is one of the key elements. Otherwise, I'm actually using most of the routines in PS (through calling a general utility fortran code -- sure in ugly fashion). |
@bernhold @batchelordb The initial version of the Plasma state Python API is now on Edison in /project/projectdirs/atom/users/elwasif/plasma_state_build/pyplasmastate Some notes
|
Cool!! Could you please add getitem and setitem to alias getData() and setData()? |
@parkjm My next step is to try and add a dictionary-like interface on top of this, but I'd prefer if this layer was better tested before we slap another layer on top of it - especially using the multiarray and vector classes to make sure we have correct functionality. |
@elwasif I was able to replace a fortran code pstool by pstool.py using pyplasmastate. The really great thing is that now we can handle plasma state inside of the component wrapper!! The getData and setData are not too bad but having more intuitive data access like ps["Zeff"] will be super great. Thanks. |
@parkjm I'm working on it. I think I have it working for scalars and one-D vectors, and now i'm looking into higher dimension data and a couple of other calls to the PS |
@parkjm Is the Python PS API base pstool replacement at the point where I can use it? I'm trying to alter the ips-fastran/src/zgenray.py to read the abridged species list rather than the full one, but it seems I'd need to look at the pstool source to do it. When possible, perhaps you could make the pstool replacement available? |
Gents. In the FACETS project they were critical of the "native" interface to plasma state data although the file format itself they liked just fine. I think this is an important issue that we may want to discuss as a group soon. |
What was the criticism of the "native" interface? Was it just that it was in Fortran and not C++? |
:-) Its been a while but I think it was an issue of portability and/or difficulty to build. |
That is a legitimate criticism |
A new version of the PS Python interface is now in place. This version is built on top the earlier module and presents a more Pythonic interface (Dictionary-like access, support for native 1-D Python vector-like data strctures, and support for Numpy arrays). The code is on Edison in Some comments
|
👍 Observations:
For example
|
This was actually a bug in the PPPL PS C++ code. It's fixed now (which begs another question, how do we contribute this bug fix and/or the whole Python interface back to PPPL?)
I think I've fixed this now
So the lower case is hard-wired in the f77 code, we can make the Python layer case insensitive |
@dlg0 you have access to TRANSP repo. How do you report any bug? I guess correction in TRANSP repo populates to the NTCC library? |
Can you give me a simple example to use for testing
I think you're using an older version of the module, this was an informational/debug message to keep track of variable lookup in the internal dictionaries, and it has been suppressed in the latest version. It should still work (even if you get this message). I wonder if you're also not getting the latest fix for the transpose issue |
O.K, no more diagnostic output. Fort test of muil-dimensional variable, |
@elwasif So the routine below
is supposed to generate screen output
The output is, however,
|
It should be there, where's that test file you're using? |
I see, could it be that it's not allocated or intialized? |
It's in |
Actually that's the run directory. The files are in the
|
The problem seems to be with all vectors of strings (scalar strings seem to work fine). |
A quick Fortran question, what does the following code return in data? subroutine psget_nbion(iobj, n1, data, ierr)
....
integer, intent(in) :: n1 ! list of dimensions (if any)
CHARACTER*(*), intent(in) :: data(n1) ! to read to/write from
....
data(1:n1) = ss % nbion
end subroutine psget_nbion |
I'm surprised that even compiles since my understanding of intent(in) was that the variable cannot be changed. If it runs I'd hope that data should be whatever it was upon entry. What does happen? Dr. David L Green From: Wael Elwasif [email protected] A quick Fortran question, what does the following code return in data? subroutine psget_nbion(iobj, n1, data, ierr) Reply to this email directly or view it on GitHubhttps://github.com//issues/2#issuecomment-107998988. |
That's what I'm trying to figure out (this is the code that ends up causing the seg fault). I'm not sure if this is accessing an array of characters or an array of strings, and if the later then how big will each string be?. I tried to search in the PS code for the place where ss%nbion is allocated, but I can't find it. |
Actually the intent is out, I copied from the wrong routine subroutine psget_nbion(iobj, n1, data, ierr)
use plasma_state_mod, only : plasma_state_container, plasma_state, &
& load_ps_container, rspec
implicit NONE
integer, intent(in) :: iobj(*) ! object handle
integer, intent(in) :: n1 ! list of dimensions (if any)
CHARACTER*(*), intent(out) :: data(n1) ! to read to/write from
integer, intent(out) :: ierr ! error code (0=OK)
! local
type (plasma_state_container) :: ptr
type (plasma_state), pointer :: ss
ierr = 0
call ccps_argcheck('psget_nbion', iobj, ierr)
if(ierr.ne.0) return
! proceed...
call load_ps_container(iobj,ptr)
ss => ptr % ps_ptr
! set
data(1:n1) = ss % nbion
end subroutine psget_nbion |
So I've had a look in
and found at least where
|
So is it an array of characters (i.e. a single string), or an array of strings? |
From
So it is an array of allocatable length, with each entry being 32 characters. |
So when you capture this value using the statement from above data(1:n1) = ss % nbion Then does one know what the memory layout of data will be (since it'll need to be accessed from C++)? |
Any progress on this? |
On 06/25/2015 09:07 AM, David L Green wrote:
Wael R. Elwasif, PhD. Office : (865)241-0002 |
Revisiting two issues @dlg0 raised:
interp1d 1D profile, like Ti(dm1_nrho), works fine
Array of 1D profile , like nbeami(dim_nspec_beam, dm1_nrho_nbi), does not work. tmp = ps.interp1d(“nbeami",0,rho,0) Note that for nbeami, there needs to be another "last" argument . In order to fix it, interp1d routine in plasmastate.py if index:
ret_vec = self._ps.interp1d(nm, deriv, vec, index)
else
ret_vec = self._ps.interp1d(nm, deriv, vec) needs to be changed something like if index!=None:
... get string data There is a bug in ccps_lib/cxxps_getset.cpp, line 472. There should be & in front of resFlat like: res[i].assign(&resFlat[i * sz], sz); NTCC library with the bug fix was built on /project/projectdirs/atom/users/parkjm/ntcc-intel. Test cases are available on /project/projectdirs/atom/users/parkjm/pyps with a quick fix for interp1d. The python API is now stable, it would be nice for @elwasif to put them to somewhere in the git repo. |
@parkjm I'm not sure I get why if index : would give different result from if index != None: |
@parkjm Similar code is also used in def interp2d(self, nm, deriv1, deriv2, x1s, x2s, index = None):
...
if index:
ret_vec = self._ps.interp2d(nm, deriv1, deriv2, x1s_vec, x2s_vec,
index)
else:
ret_vec = self._ps.interp2d(nm, deriv1, deriv2, x1s_vec, x2s_vec)
''' So that doesn't work? |
In order to access the first beam ion species,
, which should call
rather than
|
Same for interp2d. |
@parkjm I see. |
@parkjm I'm unable to access the files in /project/projectdirs/atom/users/parkjm/pyps (I'm still getting the error in arrays of strings after applying your patch to my local build) |
@elwasif the file permission fixed... |
@parkjm Where do you have the sources and the binaries for the plasma state build that you use? |
@elwasif /global/project/projectdirs/atom/users/parkjm/ntcc I added only one character. |
This simple program still fails for me using your version from plasmastate import *
ps = PlasmaState("ips",1)
ps.read("ips-state.nc")
print ps["NBION"] ips-state.nc is taken from an IPS run (its at /project/projectdirs/atom/users/elwasif/plasma_state_build/pyplasmastate) |
@elwasif Looks O.K. now. Let me know if it still does not work. |
This version work (at least for the case that was giving me segfault before). I've applied the change to my local build, which uses PS 2.041. You're using 2.042. It mabe a good idea to update everything to 2.042, but that would require re-building the ntcc libs, Nubeam, SWIM wrappers, ..etc. Any thoughts @dlg0 ??? |
IPS-FASTRAN has been updated with the python-API of plasma state (devel branch, ORNL-Fusion/ips-fastran@9595be2). So far no difference between fortran and python-API for the complicated use of the plasma state file in IPS-FASTRAN. I'm sure that this great new capability is ready to use (and must to use) for the new component development. Thanks again to @elwasif! |
The bug fix to cxxps_getset.cpp is now in TRANSP svn trunk. Will try to get it pushed to the NTCC repo version ASAP. Thanks, Johan |
More to come ...
The text was updated successfully, but these errors were encountered: