Note
Go to the end to download the full example code.
John Reid pendulum example#
This example is inspired by John Reid’s “Pendulum” example on the LS-DYNA Knowledge Base site. It shows how to use PyDyna to create a keyword file for LS-DYNA and solve it within a Pythonic environment.
Perform required imports#
Import required packages, including those for the keywords, deck, and solver.
import os
import shutil
import tempfile
import pandas as pd
from ansys.dyna.core import Deck
from ansys.dyna.core import keywords as kwd
from ansys.dyna.core.pre.examples.download_utilities import EXAMPLES_PATH, DownloadManager
from ansys.dyna.core.run import run_dyna
mesh_file_name = "nodes.k"
mesh_file = DownloadManager().download_file(
mesh_file_name, "ls-dyna", "John_Reid_Pendulum", destination=os.path.join(EXAMPLES_PATH, "John_Reid_Pendulum")
)
rundir = tempfile.TemporaryDirectory()
dynafile = "pendulum.k"
Create a deck and keywords#
Create a deck, which is the container for all the keywords. Then, create and append individual keywords to the deck.
def write_deck(filepath):
deck = Deck()
# Append control keywords
deck.extend(
[
kwd.ControlTermination(endtim=11.0),
kwd.ControlEnergy(hgen=2, rwen=2),
kwd.ControlOutput(npopt=1, neecho=3),
kwd.ControlShell(istupd=1, theory=2),
]
)
# Append database keywords
deck.extend(
[
kwd.DatabaseBinaryD3Plot(dt=1.00),
kwd.DatabaseExtentBinary(ieverp=1),
kwd.DatabaseBinaryD3Thdt(dt=999999),
kwd.DatabaseGlstat(dt=0.10),
kwd.DatabaseMatsum(dt=0.10),
kwd.DatabaseNodout(dt=0.10),
kwd.DatabaseHistoryNode(id1=350, id2=374, id3=678, id4=713),
kwd.DatabaseRbdout(dt=0.10),
kwd.DatabaseRcforc(dt=0.10),
]
)
# Define contacts
deck.extend([kwd.ContactAutomaticSingleSurface(ssid=0, fs=0.08, fd=0.08), kwd.ControlContact(shlthk=2)])
# Define gravity
curve1 = kwd.DefineCurve(lcid=1)
curve1.curves = pd.DataFrame({"a1": [0.00, 10000.00], "o1": [1.000, 1.000]})
deck.extend([kwd.LoadBodyY(lcid=1, sf=0.00981), curve1])
# Define boundary conditions
# BoundarySpcNode edited needs to redo code gen
BoundarySpcNode = kwd.BoundarySpcNode()
BoundarySpcNode.nodes = pd.DataFrame(
{
"nid": [45004, 45005, 45010, 45011],
"cid": [0, 0, 0, 0],
"dofx": [1, 1, 1, 1],
"dofy": [1, 1, 1, 1],
"dofz": [1, 1, 1, 1],
"dofrx": [0, 0, 0, 0],
"dofry": [0, 0, 0, 0],
"dofrz": [0, 0, 0, 0],
}
)
deck.extend(
[
BoundarySpcNode,
kwd.InitialVelocity(boxid=5, vx=0.0, vy=-12.0, vz=0.0),
kwd.DefineBox(boxid=5, xmn=-120.0, xmx=-80.0, ymn=80.0, ymx=120.0, zmn=-30.0, zmx=30.0),
]
)
# Define parts and materials
spherePart = kwd.Part()
spherePart.parts = pd.DataFrame({"heading": ["sphere1", "sphere2"], "pid": [1, 2], "secid": [1, 2], "mid": [1, 1]})
beamPart = kwd.Part()
beamPart.parts = pd.DataFrame(
{"heading": ["Pendulum Wires - Elastic Beams"], "pid": [45], "secid": [45], "mid": [45]}
)
deck.extend(
[
spherePart,
# Aluminium
kwd.MatPlasticKinematic(mid=1, ro=2.7e-6, e=68.9, pr=0.330, sigy=0.286, etan=0.00689),
# Sections
kwd.SectionShell(secid=1, elfrom=2, t1=1.0, t2=1.0, t3=1.0, t4=1.0),
kwd.SectionShell(secid=2, elfrom=2, t1=1.0, t2=1.0, t3=1.0, t4=1.0),
# Pendu Wires
beamPart,
kwd.SectionBeam(secid=45, elform=3, shrf=1.00000, qr_irid=1.0, a=10.0),
kwd.MatElastic(mid=45, ro=7.86e-6, e=210.0, pr=0.30),
]
)
# Define deformable switching
deck.extend([kwd.DeformableToRigid(pid=1), kwd.DeformableToRigid(pid=2)])
# Define nodes and elements
deck.append(kwd.Include(filename=mesh_file_name))
deck.export_file(filepath)
return deck
def run_post(filepath):
pass
shutil.copy(mesh_file, os.path.join(rundir.name, mesh_file_name))
deck = write_deck(os.path.join(rundir.name, dynafile))
View the model#
You can use the PyVista plot
method in the deck
class to view
the model.
deck.plot(cwd=rundir.name)
Run the Dyna solver#
filepath = run_dyna(dynafile, working_directory=rundir.name)
run_post(rundir.name)
License option : check ansys licenses only
***************************************************************
* ANSYS LEGAL NOTICES *
***************************************************************
* *
* Copyright 1971-2023 ANSYS, Inc. All rights reserved. *
* Unauthorized use, distribution or duplication is *
* prohibited. *
* *
* Ansys is a registered trademark of ANSYS, Inc. or its *
* subsidiaries in the United States or other countries. *
* See the ANSYS, Inc. online documentation or the ANSYS, Inc. *
* documentation CD or online help for the complete Legal *
* Notice. *
* *
***************************************************************
* *
* THIS ANSYS SOFTWARE PRODUCT AND PROGRAM DOCUMENTATION *
* INCLUDE TRADE SECRETS AND CONFIDENTIAL AND PROPRIETARY *
* PRODUCTS OF ANSYS, INC., ITS SUBSIDIARIES, OR LICENSORS. *
* The software products and documentation are furnished by *
* ANSYS, Inc. or its subsidiaries under a software license *
* agreement that contains provisions concerning *
* non-disclosure, copying, length and nature of use, *
* compliance with exporting laws, warranties, disclaimers, *
* limitations of liability, and remedies, and other *
* provisions. The software products and documentation may be *
* used, disclosed, transferred, or copied only in accordance *
* with the terms and conditions of that software license *
* agreement. *
* *
* ANSYS, Inc. is a UL registered *
* ISO 9001:2015 company. *
* *
***************************************************************
* *
* This product is subject to U.S. laws governing export and *
* re-export. *
* *
* For U.S. Government users, except as specifically granted *
* by the ANSYS, Inc. software license agreement, the use, *
* duplication, or disclosure by the United States Government *
* is subject to restrictions stated in the ANSYS, Inc. *
* software license agreement and FAR 12.212 (for non-DOD *
* licenses). *
* *
***************************************************************
Date: 11/20/2024 Time: 10:21:00
___________________________________________________
| |
| LS-DYNA, A Program for Nonlinear Dynamic |
| Analysis of Structures in Three Dimensions |
| Date : 10/16/2023 Time: 19:29:09 |
| Version : smp d R14 |
| Revision: R14.1-205-geb5348f751 |
| AnLicVer: 2024 R1 (20231025+2752148) |
| |
| Features enabled in this version: |
| Shared Memory Parallel |
| CESE CHEMISTRY EM ICFD STOCHASTIC_PARTICLES |
| FFTW (multi-dimensional FFTW Library) |
| ARPACK (nonsymmetric eigensolver library) |
| ANSYSLIC enabled |
| |
| Platform : Xeon64 System |
| OS Level : Linux 3.10.0 uum |
| Compiler : Intel Fortran Compiler 19.0 SSE2 |
| Hostname : e627a9b60214 |
| Precision : Double precision (I8R8) |
| |
| Unauthorized use infringes Ansys Inc. copyrights |
|___________________________________________________|
>ncpu=1 i=pendulum.k memory=20m
[license/info] Successfully checked out 1 of "dyna_solver_core".
[license/info] --> Checkout ID: e627a9b60214-root-8-000004 (days left: 153)
[license/info] --> Customer ID: 0
[license/info] Successfully started "LSDYNA (Core-based License)".
Executing with ANSYS license
Command line options: ncpu=1
i=pendulum.k
memory=20m
Input file: pendulum.k
The native file format : 64-bit small endian
Memory size from command line: 20000000
on UNIX computers note the following change:
ctrl-c interrupts ls-dyna and prompts for a sense switch.
type the desired sense switch: sw1., sw2., etc. to continue
the execution. ls-dyna will respond as explained in the users manual
type response
----- ------------------------------------------------------------
quit ls-dyna terminates.
stop ls-dyna terminates.
sw1. a restart file is written and ls-dyna terminates.
sw2. ls-dyna responds with time and cycle numbers.
sw3. a restart file is written and ls-dyna continues calculations.
sw4. a plot state is written and ls-dyna continues calculations.
sw5. ls-dyna enters interactive graphics phase.
swa. ls-dyna flushes all output i/o buffers.
swb. a dynain is written and ls-dyna continues calculations.
swc. a restart and dynain are written and ls-dyna continues calculations.
swd. a restart and dynain are written and ls-dyna terminates.
swe. stop dynamic relaxation just as though convergence
endtime=time change the termination time
lpri toggle implicit lin. alg. solver output on/off.
nlpr toggle implicit nonlinear solver output on/off.
iter toggle implicit output to d3iter database on/off.
prof output timing data to messag and continue.
conv force implicit nonlinear convergence for current time step.
ttrm terminate implicit time step, reduce time step, retry time step.
rtrm terminate implicit at end of current time step.
******** notice ******** notice ******** notice ********
* *
* This is the LS-DYNA Finite Element code. *
* *
* Neither LST nor the authors assume any responsibility for *
* the validity, accuracy, or applicability of any results *
* obtained from this system. Users must verify their own *
* results. *
* *
* LST endeavors to make the LS-DYNA code as complete, *
* accurate and easy to use as possible. *
* Suggestions and comments are welcomed. Please report any *
* errors encountered in either the documentation or results *
* immediately to LST through your site focus. *
* *
* Copyright (C) 1990-2021 *
* by Livermore Software Technology, LLC *
* All rights reserved *
* *
******** notice ******** notice ******** notice ********
Beginning of keyword reader 11/20/24 10:21:04
11/20/24 10:21:04
Open include file: nodes.k
Memory required to process keyword : 256550
Additional dynamic memory required : 2135813
input of data is completed
initial kinetic energy = 0.17315762E+01
*** Warning 30131 (INI+131)
total number of massless nodes = 8
The LS-DYNA time step size should not exceed 6.171E-04
to avoid contact instabilities. If the step size is
bigger then scale the penalty of the offending surface.
Memory required to begin solution : 257K
Additional dynamically allocated memory: 2223K
Total: 2480K
initialization completed
1 t 0.0000E+00 dt 1.81E-02 flush i/o buffers 11/20/24 10:21:04
1 t 0.0000E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
cpu time per zone cycle............ 104 nanoseconds
average cpu time per zone cycle.... 354 nanoseconds
average clock time per zone cycle.. 636 nanoseconds
estimated total cpu time = 0 sec ( 0 hrs 0 mins)
estimated cpu time to complete = 0 sec ( 0 hrs 0 mins)
estimated total clock time = 3 sec ( 0 hrs 0 mins)
estimated clock time to complete = 0 sec ( 0 hrs 0 mins)
termination time = 1.100E+01
56 t 9.9766E-01 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
111 t 1.9953E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
166 t 2.9930E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
221 t 3.9906E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
276 t 4.9883E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
331 t 5.9860E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
386 t 6.9836E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
442 t 7.9994E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
497 t 8.9971E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
552 t 9.9948E+00 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
607 t 1.0992E+01 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
*** termination time reached ***
607 t 1.1011E+01 dt 1.81E-02 write d3dump01 file 11/20/24 10:21:04
607 t 1.1011E+01 dt 1.81E-02 write d3plot file 11/20/24 10:21:04
N o r m a l t e r m i n a t i o n 11/20/24 10:21:04
Memory required to complete solution : 257K
Additional dynamically allocated memory: 2223K
Total: 2480K
T i m i n g i n f o r m a t i o n
CPU(seconds) %CPU Clock(seconds) %Clock
----------------------------------------------------------------
Keyword Processing ... 1.1002E-02 6.66 1.1003E-02 0.30
KW Reading ......... 4.1084E-03 2.49 4.1090E-03 0.11
KW Writing ......... 1.6575E-03 1.00 1.6580E-03 0.05
Initialization ....... 7.8532E-02 47.51 3.5845E+00 97.57
Init Proc Phase 1 .. 5.2454E-03 3.17 7.3180E-03 0.20
Init Proc Phase 2 .. 1.3901E-03 0.84 2.0920E-03 0.06
Init solver .......... 7.8186E-05 0.05 7.9000E-05 0.00
Element processing ... 7.3925E-03 4.47 7.3810E-03 0.20
Shells ............. 6.8465E-04 0.41 7.1700E-04 0.02
Beams .............. 1.5302E-03 0.93 1.5290E-03 0.04
ISO Shells ......... 4.0838E-04 0.25 4.1900E-04 0.01
E Other ............ 1.4804E-03 0.90 1.4930E-03 0.04
Binary databases ..... 8.1021E-03 4.90 8.1120E-03 0.22
ASCII database ....... 9.8818E-03 5.98 9.8690E-03 0.27
Contact algorithm .... 1.2393E-02 7.50 1.2416E-02 0.34
Interf. ID 1 9.6114E-03 5.81 9.5670E-03 0.26
Rigid Bodies ......... 1.0577E-02 6.40 1.0506E-02 0.29
Time step size ....... 6.1770E-04 0.37 6.1800E-04 0.02
Group force file ..... 4.6808E-04 0.28 5.2400E-04 0.01
Others ............... 1.4768E-03 0.89 1.4520E-03 0.04
Misc. 1 .............. 9.2928E-03 5.62 1.1812E-02 0.32
Scale Masses ....... 4.3815E-04 0.27 4.5100E-04 0.01
Force Constraints .. 4.3417E-04 0.26 4.1900E-04 0.01
Force to Accel ..... 8.9103E-04 0.54 8.4600E-04 0.02
Update RB nodes .... 3.0972E-03 1.87 3.0380E-03 0.08
Misc. 2 .............. 2.6958E-03 1.63 2.6360E-03 0.07
Misc. 3 .............. 5.0436E-03 3.05 5.2190E-03 0.14
Misc. 4 .............. 7.7573E-03 4.69 7.7310E-03 0.21
Timestep Init ...... 1.3345E-03 0.81 1.2820E-03 0.03
Apply Loads ........ 1.3183E-03 0.80 1.3250E-03 0.04
Compute exwork ..... 3.7693E-03 2.28 3.7410E-03 0.10
----------------------------------------------------------------
T o t a l s 1.6531E-01 100.00 3.6739E+00 100.00
Problem time = 1.1011E+01
Problem cycle = 607
Total CPU time = 0 seconds ( 0 hours 0 minutes 0 seconds)
CPU time per zone cycle = 160.873 nanoseconds
Clock time per zone cycle= 166.348 nanoseconds
Number of CPU's 1
NLQ used/max 192/ 192
Start time 11/20/2024 10:21:04
End time 11/20/2024 10:21:04
Elapsed time 0 second for 607 cycles using 1 SMP thread
( 0 hour 0 minute 0 second )
N o r m a l t e r m i n a t i o n 11/20/24 10:21:04
Total running time of the script: (0 minutes 8.241 seconds)