-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ability to animate shape variables to a gif
- Loading branch information
1 parent
03fb1c5
commit 29dc592
Showing
6 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
Sean Engelstad, November 2023 | ||
GT SMDO Lab, Dr. Graeme Kennedy | ||
Caps to TACS example | ||
The purpose of this file is to show how to animate shape variables in the structure. | ||
Once you run this script it will generate .f5 files in the capsStruct/Scratch/tacs work directory. | ||
You'll need to use f5tovtk to convert to *.vtk files and open up each group of shape_var_..vtk files | ||
using the .. shortcut which opens up a group of files at once. For each of these groups of vtk files, | ||
click file -> save animation and save all the png files in a subfolder. Then, either use the GifWriter in the | ||
caps2tacs module or copy the GifWriter script into that directory and use it to make a gif. Repeat for each shape variable. | ||
""" | ||
|
||
from tacs import caps2tacs | ||
from mpi4py import MPI | ||
import openmdao.api as om | ||
|
||
# --------------------------------------------------------------# | ||
# Setup CAPS Problem | ||
# --------------------------------------------------------------# | ||
comm = MPI.COMM_WORLD | ||
tacs_model = caps2tacs.TacsModel.build(csm_file="large_naca_wing.csm", comm=comm) | ||
tacs_model.mesh_aim.set_mesh( # need a refined-enough mesh for the derivative test to pass | ||
edge_pt_min=15, | ||
edge_pt_max=20, | ||
global_mesh_size=0.01, | ||
max_surf_offset=0.01, | ||
max_dihedral_angle=5, | ||
).register_to( | ||
tacs_model | ||
) | ||
|
||
aluminum = caps2tacs.Isotropic.aluminum().register_to(tacs_model) | ||
|
||
# setup the thickness design variables + automatic shell properties | ||
nribs = int(tacs_model.get_config_parameter("nribs")) | ||
nspars = int(tacs_model.get_config_parameter("nspars")) | ||
nOML = nribs - 1 | ||
for irib in range(1, nribs + 1): | ||
caps2tacs.ThicknessVariable( | ||
caps_group=f"rib{irib}", value=0.03, material=aluminum | ||
).register_to(tacs_model) | ||
for ispar in range(1, nspars + 1): | ||
caps2tacs.ThicknessVariable( | ||
caps_group=f"spar{ispar}", value=0.03, material=aluminum | ||
).register_to(tacs_model) | ||
for iOML in range(1, nOML + 1): | ||
caps2tacs.ThicknessVariable( | ||
caps_group=f"OML{iOML}", value=0.1, material=aluminum | ||
).register_to(tacs_model) | ||
|
||
# register one shape variable rib_a1 | ||
rib_a1 = caps2tacs.ShapeVariable("rib_a1", value=1.0).register_to(tacs_model) | ||
caps2tacs.ShapeVariable("rib_a2", value=0.0).register_to(tacs_model) | ||
spar_a1 = caps2tacs.ShapeVariable("spar_a1", value=1.0).register_to(tacs_model) | ||
caps2tacs.ShapeVariable("spar_a2", value=0.0).register_to(tacs_model) | ||
|
||
# add constraints and loads | ||
caps2tacs.PinConstraint("root").register_to(tacs_model) | ||
caps2tacs.GridForce("OML", direction=[0, 0, 1.0], magnitude=10.0).register_to( | ||
tacs_model | ||
) | ||
|
||
# add analysis functions to the model | ||
caps2tacs.AnalysisFunction.mass().register_to(tacs_model) | ||
|
||
# run the pre analysis to build tacs input files | ||
tacs_model.setup(include_aim=True) | ||
|
||
shape_var_dict = { | ||
rib_a1: [_ * 0.1 for _ in range(6, 14)], | ||
spar_a1: [_ * 0.1 for _ in range(6, 14)], | ||
} | ||
tacs_model.animate_shape_vars(shape_var_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
__all__ = ["GifWriter"] | ||
import imageio, os | ||
|
||
|
||
class GifWriter: | ||
""" | ||
module to write gifs from a set of pngs | ||
""" | ||
|
||
def __init__(self, frames_per_second: int = 4): | ||
self._fps = frames_per_second | ||
|
||
def __call__(self, gif_filename: str, path: str): | ||
""" | ||
call on current path to create gif of given filename | ||
""" | ||
gif_filepath = os.path.join(path, gif_filename) | ||
with imageio.get_writer(gif_filepath, mode="I", fps=self._fps) as writer: | ||
path_dir = os.listdir(path) | ||
path_dir = sorted(path_dir) | ||
for image_file in path_dir: | ||
print(image_file) | ||
if ".png" in image_file: | ||
image = imageio.imread(image_file) | ||
writer.append_data(image) | ||
|
||
|
||
# example of how to use the GifWriter | ||
# if __name__ == "__main__": | ||
# my_writer = GifWriter(frames_per_second=4) | ||
# my_writer("sizing1.gif", os.getcwd()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters