Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Scripting

The primary reason to use the solver as a standalone application is to automate simulations and analyze the results programmatically. This is especially useful for tasks such as design optimization, parameter sweeps, or studying how specific model parameters influence the simulation outcome.

Scripts that interact with VirtualBow must be able to launch the solver through its command-line interface and read or write the model and result files that serve as its input and output. Because the solver uses JSON for input and MessagePack for output, any programming language with support for these formats can be used. Both are widely supported, either natively or through readily available libraries.

To help you get started, the following sections demonstrate how to interface with VirtualBow in several programming languages commonly used in scientific computing. Each example performs the same sequence of basic tasks:

  1. Load, modify, and save a model file.

  2. Run a static simulation using that model.

  3. Load the result file and print the final draw force.

Python

The Python example below uses the external msgpack package for reading the result files. It can be installed with pip install msgpack.

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-cli", "static", "input.bow", "output.res"])

# Load the result file
with open("output.res", "rb") as file:
    output = msgpack.unpack(file, raw=False)

# Evaluate final draw force
print(output["statics"]["final_draw_force"])

Matlab

The 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-cli static input.bow output.res');

% Load the result file
output = loadmsgpack('output.res');

% Evaluate final draw force
disp(output.statics.final_draw_force);

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-cli static input.bow output.res`)

# Load the result file
stream = open("output.res", "r")
output = unpack(stream)
close(stream)

# Evaluate final draw force
println(output["statics"]["final_draw_force"])