5.4. Scripting Examples
This is a collection of example scripts in different programming languages that show how to interface with VirtualBow. Each of them performs the same series of basic tasks:
-
Load, modify and save a model file
-
Run a static simulation with the model file
-
Load the result file and evaluate the maximum stress of the first layer at full draw
5.4.1. Python
The python example below uses two external packages, msgpack for reading the result files and NumPy for evaluating the stresses.
They can be installed with pip install msgpack numpy
.
import json, msgpack # Loading and saving model and result files
import numpy as np # Evaluating stresses
import subprocess # Runnig the simulation
# Load model file
with open("input.bow", "r") as file:
input = json.load(file)
# Modify model data
input["string"]["n_strands"] += 1
# Save model file
with open("input.bow", "w") as file:
json.dump(input, file, indent=2)
# Run a static simulation
subprocess.call(["virtualbow-slv", "--static", "input.bow", "output.res"])
# Load the result file
with open("output.res", "rb") as file:
output = msgpack.unpack(file, raw=False)
# Evaluate stresses
He_back = np.array(output["setup"]["limb_properties"]["layers"][0]["He_back"])
Hk_back = np.array(output["setup"]["limb_properties"]["layers"][0]["Hk_back"])
epsilon = np.array(output["statics"]["states"]["epsilon"][-1])
kappa = np.array(output["statics"]["states"]["kappa"][-1])
sigma = He_back.dot(epsilon) + Hk_back.dot(kappa)
print(sigma.max())
5.4.2. Matlab
This example uses the JSONLab library, which can read and write both JSON and MessagePack files.
Note
|
The minimum required version of JSONLab is 2.0 . Older versions have a bug when loading empty arrays, which can be the case in VirtualBow result files.
|
% Load model file
input = loadjson('input.bow');
% Modify model data
input.string.n_strands = input.string.n_strands + 1;
% Save model file
savejson('', input, 'input.bow');
% Run a static simulation
system('virtualbow-slv --static input.bow output.res');
% Load the result file
output = loadmsgpack('output.res');
% Evaluate stresses
He_back = output.setup.limb_properties.layers{1}.He_back;
Hk_back = output.setup.limb_properties.layers{1}.Hk_back;
epsilon = output.statics.states.epsilon(:,end);
kappa = output.statics.states.kappa(:,end);
sigma = He_back*epsilon + Hk_back*kappa;
disp(max(sigma));
5.4.3. Julia
Two external packages are used here, JSON for loading model files and MsgPack for loading result files.
They can be installed with julia> import Pkg; Pkg.add("JSON"); Pkg.add("MsgPack")
.
using JSON # Loading and saving model files
using MsgPack # Loading result files
# Load model file
stream = open("input.bow", "r")
input = JSON.parse(stream)
close(stream)
# Modify model data
input["string"]["n_strands"] += 1
# Save model file
stream = open("input.bow", "w")
JSON.print(stream, input, 2)
close(stream)
# Run a static simulation
run(`virtualbow-slv --static input.bow output.res`)
# Load the result file
stream = open("output.res", "r")
output = unpack(stream)
close(stream)
# Evaluate stresses
He_back = hcat(output["setup"]["limb_properties"]["layers"][1]["He_back"]... )
Hk_back = hcat(output["setup"]["limb_properties"]["layers"][1]["Hk_back"]... )
epsilon = output["statics"]["states"]["epsilon"][end]
kappa = output["statics"]["states"]["kappa"][end]
sigma = He_back*epsilon + Hk_back*kappa
println(max(sigma...))