Scripting

The main use of the solver as a standalone application is the ability to write programs that automatically launch simulations and analyze the results. This is especially useful for performing design optimizations or exploring the influence of certain model parameters on the simulation results.

Scripts that interoperate with VirtualBow have to be able to call the solver via the command line interface as well as read and write the model and result files that constitute the input and output of the solver. As a consequence of the file formats, any programming language that supports JSON and MessagePack can be used. Both are fairly common formats and most languages have either built-in support or there are external libraries available.

For getting started, the following sections show how to interface with VirtualBow for some programming languages commonly used in scientific computing. Each of the examples performs the same series of basic tasks:

  1. Load, modify and save a model file
  2. Run a static simulation with the model file
  3. Load the result file and evaluate the maximum stress of the first layer at full draw

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())

Matlab

This Matlab example uses the JSONLab library, which can read and write both JSON and MessagePack 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));

Julia

For the Julia example, two external packages are used: 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...))