haykh / graph-et

https://pypi.org/project/graph-et/
BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

Reading data for Tristanv2 #4

Open Tissot11 opened 4 months ago

Tissot11 commented 4 months ago

Thanks for sharing the link on Tristan website. I did try it but somehow I noticed few problems. I need to understand few things

  1. Does the package needs to be installed by running some script? It appears from the requirements.txt file that it needs to be installed in a virtual environment. This is what I'm used to for other codes.
  2. My Python Projects are on my Home Directory while the simulation data is always on the scratch type partition. So I always define a path string to the data and run the scripts in Spyder in my home directory. This I could not make it work with the example shown in the package. What should be the best way to define the path string and pass it to Data() routine? Also the path string needs be changed at least at one place in the package?
  3. I liked the lazy loading feature and I essentially need it to just read the data/metadata for TristanV1 and V2, rest of the stuff I could do myself or use your routines to plot the data. I tried to write my own scripts but I noticed some problems which I didn't fix due to lack of time. Does this still work for TristanV2?

https://github.com/PrincetonUniversity/tristan-mp-pu/wiki/Writing-your-own-python-scripts

Thanks for the help!

haykh commented 4 months ago

thanks for the feedback. this is very much a work-in-progress package.

  1. yes, the best way is to simply install it in a virtual environment:

    # create virtual environment in the .venv directory
    python3 -m venv .venv
    # activate the virtual environment
    source .venv/bin/activate
    # check to ensure you are using the `pip` from that virtual environment
    which pip # should print `.../.venv/bin/pip`
    # install the package
    pip install graph-et

    if you use the python kernels from the same virtual environment -- it should directly recognize the package.

  2. as long as the data is accessible from the place python instance is running, you can pass the absolute path to the data:

    
    path = "/path/to/scratch/directory/simulation_dir/"
    data_output_dir = f"{path}output/"
    cfg_file = f"{path}input.cfg"

d = Data( TristanV2, # plugin steps=range(150), # steps to load metadata for path=data_output_dir", # path to the data cfg_fname=cfg_file, # configuration file )


if a functionality is missing (or is different from what you need -- you can always override the plugin with your own (inheriting from the `TristanV2` one):

```python
class MyOwnTristanV2(TristanV2):
    def readField(self, field: str, step: int) -> h5py.Dataset:
        ... # here you can define your own `readField` 
        # rest of the functions will remain the same as in `TristanV2`
  1. i'm not sure what the question is. on the backend, i use xarray and dask libraries, which load the metadata, and stack them properly. and you access the raw xarray metadata via, for instance, data.fields. then in principle you can do any sort of data manipulation including plotting etc:
    
    d = Data(...) # load the metadata

compute total EM energy in the box (for all timesteps)

tot_EM_energy = 0.5 * (d.fields.Ex2 + d.fields.Ey2 + d.fields.Ez2 + d.fields.Bx2 + d.fields.By2 + d.fields.Bz2).sum(("x", "y", "z"))

plot vs time

tot_EM_energy.plot()

or use matplotlib to plot

plt.plot(tot_EM_energy.t, tot_EM_energy)

take a slice of dens1+dens2 at z=0 and t~10.0

slice_dens = (d.fields.dens1 + d.fields.dens2).sel(z=0, t=10, method="nearest")

2d plot against x and y

slice_dens.plot()

or using matplotlib

plt.pcolormesh(slice_dens.x, slice_dens.y, slice_dens)

Tissot11 commented 3 months ago

Thanks for your answer and the example. I could now use the plugin and plot the data for the test simulation.

By basic functionality I meant what you indeed provide for in this plugin e.g. being able to read simulation data file along with attributes (defined in the input and user files) and able to extract data between time-steps and physical domains like you showed in the example above. Since I am new to Tristan, I still can get occasionally confused by few things and I still wanted to ask few questions related to this Plugin for TristanV2

  1. What is this cfg_fname=cfg_file in your Plugin?
  2. If I try to extract attributes from d.particles then I see u, v, w, ind, proc and wei. I suppose these are three component of velocities, indices of particles, processors IDs, and weight, respectively? I didn't save or used these in the test simulation but rather they were asserted in the testTristan routine of your plugin. So do they appear always in all simulations? As mentioned above, I essentially would only be expecting to get the info and extract the attributes and parameters of a given simulation using your plugin.
  3. In my cursory reading, I noticed that TristanV1 and TristanV2 have different structures for saving the data. So essentially one could use your TristanV2 class to write TristanV1?

Thanks again!