Skip to content
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

Code for writing variable file #5

Open
Ramogi254 opened this issue Oct 12, 2023 · 4 comments
Open

Code for writing variable file #5

Ramogi254 opened this issue Oct 12, 2023 · 4 comments
Labels
question Further information is requested

Comments

@Ramogi254
Copy link

@tkarabela ,

I was wondering how to write the variable file using the ensightreader library. I commented this on the issue you resolved here -
#4. However, I had not realized the issue had been closed.

My simple question is what attributes can I use to write the variable file?

@tkarabela tkarabela added the question Further information is requested label Oct 12, 2023
@tkarabela
Copy link
Owner

Hi, support for writing is unfortunately limited at the moment, but it is possible. I've been thinking about extending the library in this direction, since it's something that's useful to me as well, but I'm not yet sure about the API to make it simple yet useful.

If you don't mind sharing, what are you trying to do in particular? Maybe I can help or modify the library to make it simpler.

As for the current functionality:

Editing variable contents

The most useful and straightforward thing you can do with the library is to modify existing variables, as per the readme:

with variable.mmap_writable() as mm_var:
    data = variable.read_node_data(mm_var, part.part_id)
    data[:] = np.sqrt(data)                     # transform variable data in-place

That is, use mmap_writable() and then modify the data array directly, it will write to the file on disk automatically thanks to the mmap. You can even use it to combine multiple variables:

with variable_A.mmap_writable() as mm_var_A, variable_B.mmap_writable() as mm_var_B, variable_C.mmap_writable() as mm_var_C:
    A = variable.read_node_data(mm_var_A, part.part_id)
    B = variable.read_node_data(mm_var_B, part.part_id)
    C = variable.read_node_data(mm_var_C, part.part_id)
    C[:] = A + B  # note that C = A + B will not work, as that would allocate another array; you must access the existing C array

This approach is limited to existing variables in the case (so you need to have a variable of the correct type, which is defined for the parts you are interested in). You can make a copy of the variable file beforehand, add it to the .case file, and then "destroy" it, overwriting it with your new data.

Creating a variable from scratch

Aside from the workaround via making a copy of the variable file, as mentioned above, there is currently no support for creating a variable file from scratch. However, this would be easy to add to the library; it's already supported for geometry, see https://github.com/tkarabela/ensight-reader/blob/master/tests/test_write_geometry.py#L223-L248

Adding a variable to .case file

There is no convenience method to do this, but one can modify the EnsightCaseFile.variables dict manually to this effect. Saving a .case file is implemented.

import copy
case = read_case(...)
A = case.variables["A"]
B = copy.copy(A)
B.variable_name = "B"
B.filename = "..."
case.variables["B"] = B
case.to_file("new.case")

@Ramogi254
Copy link
Author

Thanks @tkarabela ,

I believe you have answered what I would like to do. Nonetheless, here is a breakdown of what it is.

  1. Read the variable data file
  2. Modify the data file using singular value decomposition
  3. Reconstruct the data file using a few mode coefficients
  4. Write the new data file into another file.
  5. etc.

This is basically what I hope to do to succeed. I was hoping to get some assistance with steps 1 and 4.

@Ramogi254
Copy link
Author

@tkarabela ,

I have come across an unusual challenge. When visualizing the new variable, it seems that the changes made are only available internally however, the boundary seems to retain the original nodal values. Is there a way to read the data from all the points, including the boundary points?

I have noticed the code reads the data from only part[0]. How can I change the code to work for all points?

@tkarabela
Copy link
Owner

@Ramogi254, you can iterate over all parts like this:

case = read_case(ensight_case_path)
geofile = case.get_geometry_model()

with variable.mmap_writable() as mm_var:
    for part_id, part in geofile.parts.items():
        print("Processing part", part.part_name)
        data = variable.read_node_data(mm_var, part.part_id)
        # ...modify data...

Thanks for describing your use case, I think something like this would make a nice tutorial to add to the documentation :) I'll look into it when I have the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants