Giter Club home page Giter Club logo

dynareadout_python's Introduction

dynareadout

High-Performance and Thread-Safe C/C++ library for parsing binary output files and key files of LS Dyna (d3plot, binout, input deck) with bindings for python.

Documentation

You can find a Wiki with API Documentation for python.

Examples

Binout

from dynareadout import Binout

bin_file = None
try:
  # This library also supports opening multiple binout files at once by globing them
  bin_file = Binout("path/to/your/binout*")
except RuntimeError as e:
  print("Failed to open binout: {}".format(e))
  exit(1)

# Print the children of the binout
children = bin_file.read()
for (i, child) in enumerate(children):
  print("Child {}: {}".format(i, child))

# Read some data. This method can read variables of all different types
node_ids = bin_file.read("nodout/ids")
for i in range(len(node_ids)):
  print("Node ID {}: {}".format(i, node_ids[i]))

# You can also find out if a variable exists
node_ids_exist = bin_file.variable_exists("nodout/ids")

# Get the number of time steps in the binout
nodout_timesteps = bin_file.get_num_timesteps("nodout")
# The time steps can vary inside the binout
rcforc_timesteps = bin_file.get_num_timesteps("rcforc")

# If you want to read "timed" data (x_displacement, x_force, etc.) you can do so also with the read method
x_displacement = bin_file.read("nodout/x_displacement")
for (t, time_step) in enumerate(x_displacement):
  for (n, x_disp) in enumerate(time_step):
    print("X Displacement time_step={}, node_id={}: {}".format(t, node_ids[n], x_displacement[t][n]))

D3plot

from dynareadout import D3plot

plot_file = None
try:
  # Just give it the first d3plot file and it opens all of them
  plot_file = D3plot("path/to/your/d3plot")
except RuntimeError as e:
  print("Failed to open: {}".format(e))
  exit(1)

# Read the title
title = plot_file.read_title()
print("Title: {}".format(title))

# Read node ids
node_ids = plot_file.read_node_ids()
print("Nodes: {}".format(len(node_ids)))
for (i, nid) in enumerate(node_ids):
  print("Node {}: {}".format(i, nid))

# Read node coordinates of time step 10
node_coords = plot_file.read_node_coordinates(10)
for i in range(len(node_coords)):
  print("Node Coords {}: ({:.2f}, {:.2f}, {:.2f})".format(i, node_coords[i][0], node_coords[i][1], node_coords[i][2]))

KeyFile

from dynareadout import key_file_parse

keywords = key_file_parse("path/to/your/input.k")

# Parse all nodes
node_keywords = keywords["NODE"]

# Loop over all *NODE keywords
for i in range(len(node_keywords)):
  # Loop over all cards of each *NODE keyword
  for j in range(len(node_keywords[i])):
    node = node_keywords[i][j]
    # Then you can parse the variables of each card as integers and floats
    # The list of integers holds all the widths of each variable in the card in characters
    nid, x, y, z = node.parse_whole([8, 16, 16, 16])

    print(f"NODE {nid:d}: ({x:.3f}; {y:.3f}; {z:.3f})")

Other languages

This library is also available for C and C++ this version can be found here.

Installation

python -m pip install dynareadout

Uploading to PyPI

  1. Make sure that the dynareadout submodule has the correct version

  2. Update the version in setup.py and pyproject.toml. Also check if new source files have been added

  3. Publish a new release

  4. Create source distribution

python setup.py sdist
  1. Upload to test.pypi.org
python -m twine upload --repository testpypi dist/*

Then insert __token__ as username and the token as password.

  1. Install package from test.pypi.org to test it
python -m pip install --upgrade --no-build-isolation --index-url https://test.pypi.org/simple/ dynareadout
  1. If it works upload it to pypi.org

  2. Create windows wheel

python -m build
  1. Upload windows wheel to test.pypi.org and test it.

  2. If it works upload it to pypi.org

python -m twine upload dist/*

dynareadout_python's People

Contributors

pucklaj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dynareadout_python's Issues

Mutiple files read (binout*) not working some times [Python]

Issue:
In some cases, while reading multiple binout file, not returning anything.
Not even showing any error.

Here I have shared my code and binout files.

binout0000.zip

Kindly check the issue.

Code:

import numpy as np
# import matplotlib.pyplot as plt
import csv
from dynareadout import Binout

bin_file_path = r"binout*"

def open_binout(file_path):
    bin_file = None
    try:
        bin_file = Binout(file_path)
    except RuntimeError as e:
        print(f"Failed to open binout at {file_path}: {e}")
        exit(1)
    return bin_file


bin_file = open_binout(bin_file_path)

node_ids_displacement = bin_file.read("rbdout/ids")
global_dx = bin_file.read("rbdout/global_dx")

num_timesteps_displacement = len(global_dx)
node_ids_custom = [6, 7]

force_data_array = np.array([[x_disp[n] for x_disp in global_dx] for n in range(len(node_ids_custom))])

result_curve = np.abs(force_data_array[0] - force_data_array[1])
#--------------------------------------------
node_ids_force = bin_file.read("bndout/velocity/rigidbodies/ids")
x_force = bin_file.read("bndout/velocity/rigidbodies/x_force")

num_timesteps_force = len(x_force)
x_force_array = np.array(x_force)


for n, node_id in enumerate(node_ids_force):
    force_data = [x_forc[n] for x_forc in x_force_array]
    # plt.plot(result_curve, force_data, label="Result")

# plt.xlabel('Displacement')
# plt.ylabel('Force')
# plt.legend()
# plt.title('Force vs Displacement')
# plt.show()

csv_data = np.column_stack((result_curve, force_data))
csv_header = ["Displacement", "Force"]

with open("force_vs_displacement.csv", mode="w", newline='') as file:
    writer = csv.writer(file)
    writer.writerow(csv_header)
    writer.writerows(csv_data)
print("File has been Written Successfully")

Can't read card with empty fields

Hello, thanks for this great library!

I was trying to parse an old .key file, that has this keyword

*MAT_PIECEWISE_LINEAR_PLASTICITY
$      mid        ro         e        pr      sigy      etan      eppf      tdel
         1 7.830E-06     200.0       0.3     0.366               0.750

when I run this line to load all values:

            mid ,  ro,    e,   pr, sigy, etan, eppf, tdel = material.parse_whole([10, 10, 10, 10, 10, 10, 10, 10])

I get an error:

    mid ,  ro,    e,   pr, sigy, etan, eppf, tdel = node.parse_whole([10, 10, 10, 10, 10, 10, 10, 10])
                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Trying to parse 8 values out of card "         1 7.830E-06     200.0       0.3     0.366               0.750" with

If I add a 0 at the end of the card (so that tdel is defined) then all is good.

Can this be handled automatically?

Issue with Linux machine [User Installation only allowed]

Kinldy help abou the issues. we did have full access to the linux machine.

pip install dynareadout
Defaulting to user installation because normal site-packages is not writeable
Collecting dynareadout
  Using cached dynareadout-23.12.tar.gz (266 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: dynareadout
  Building wheel for dynareadout (pyproject.toml) ... error
  ERROR: Command errored out with exit status 1:
   command: /usr/bin/python3 /home/siddharth.l/.local/lib/python3.6/site-packages/pip/_vendor/pep517/in_process/_in_process.py build_wheel /tmp/tmpv732drtn
       cwd: /tmp/pip-install-vnt99m3e/dynareadout_589c4ed632944d7bb328c99db15526e4
  Complete output (36 lines):
  running bdist_wheel
  running build
  running build_clib
  building 'dynareadout_c' library
  creating build
  creating build/temp.linux-x86_64-3.6
  creating build/temp.linux-x86_64-3.6/lib
  creating build/temp.linux-x86_64-3.6/lib/dynareadout
  creating build/temp.linux-x86_64-3.6/lib/dynareadout/src
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/binary_search.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/binary_search.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/binout_directory.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/binout_directory.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/binout_glob.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/binout_glob.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/binout_read.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/binout_read.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/binout.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/binout.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/d3_buffer.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/d3_buffer.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/d3plot_data.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/d3plot_data.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/d3plot_part_nodes.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/d3plot_part_nodes.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/d3plot_state.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/d3plot_state.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/d3plot.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/d3plot.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/extra_string.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/extra_string.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/include_transform.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/include_transform.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/key.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/key.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/line.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/line.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/multi_file.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/multi_file.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/path_view.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/path_view.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/path.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/path.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/string_builder.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/string_builder.o -ansi -w
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Ilib/dynareadout/src -c lib/dynareadout/src/sync.c -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/sync.o -ansi -w
  ar rcs build/temp.linux-x86_64-3.6/libdynareadout_c.a build/temp.linux-x86_64-3.6/lib/dynareadout/src/binary_search.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/binout_directory.o build/t                                           emp.linux-x86_64-3.6/lib/dynareadout/src/binout_glob.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/binout_read.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/binout.o build/temp.linux-x86                                           _64-3.6/lib/dynareadout/src/d3_buffer.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/d3plot_data.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/d3plot_part_nodes.o build/temp.linux-x86_64-                                           3.6/lib/dynareadout/src/d3plot_state.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/d3plot.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/extra_string.o build/temp.linux-x86_64-3.6/lib/dyn                                           areadout/src/include_transform.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/key.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/line.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/mult                                           i_file.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/path_view.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/path.o build/temp.linux-x86_64-3.6/lib/dynareadout/src/string_builder.o build                                           /temp.linux-x86_64-3.6/lib/dynareadout/src/sync.o
  running build_ext
  building 'dynareadout' extension
  creating build/temp.linux-x86_64-3.6/lib/dynareadout/src/cpp
  creating build/temp.linux-x86_64-3.6/lib/dynareadout/src/python
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -m                                           tune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/tmp/pip-install-vnt99m3e/dynareadout_589c4ed632944d7bb328c99db15526e4/lib/pybind11/include -Ilib/dynareadout/src -Ilib/dynareadout/src/cpp -I/us                                           r/include/python3.6m -c lib/dynareadout/src/cpp/binout.cpp -o build/temp.linux-x86_64-3.6/lib/dynareadout/src/cpp/binout.o -std=c++17 -w
  gcc: error: unrecognized command line option ‘-std=c++17’
  error: command 'gcc' failed with exit status 1

ERROR: Failed building wheel for dynareadout
Failed to build dynareadout
ERROR: Could not build wheels for dynareadout, which is required to install pyproject.toml-based projects
'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.