Unlock the True Potential of OPCloud Simulations
Leveraging External Computation in OPCloud
Simulation plays an important role in Model-Based Systems Engineering, providing invaluable insights into system dynamics and its interactions with the environment. With simulation, teams can rapidly iterate designs and explore a range of scenarios — all before committing real-world tests that could be costly and time-consuming.
Today, many software tools and services offer an API, enabling seamless data exchange and functionality integration. Whether accessing weather forecasts, connecting to a requirements database, or integrating a custom test rig API, linking external resources with your OPCloud system model opens up vast opportunities for enriched simulation and analysis.
In the first part of this Simulating Systems with OPCloud series, we explored native computation within OPCloud. This second installment dives into leveraging APIs to tap into external computation, expanding the scope and capabilities of your model’s simulations.
What is an API?
An API, or Application Programming Interface, is an intermediary that allows two applications to talk to each other. APIs establish a standardised way for one system to request information or perform actions in another without needing detailed knowledge of its inner workings.
Each function or data query in an API is typically accessed through a unique URL or endpoint, as shown in the following example. Parameters like city and units specify details of the request.
Step-by-Step Guide to Computing with API in OPCloud
Suppose your developing a solar power system for remote locations. To design an effective control system, you need accurate sunrise and sunset times based on location. Incorporating this data into your system model enables you to simulate various design scenarios realistically and verify functionality in a more efficient manner.
In this step-by-step guide, we’ll walk through connecting your OPCloud model to an external service that provides sunrise and sunset times using API, based on latitude and longitude.
Step 1: Create Computational Objects and Processes
To enable external API computations, start by creating a process representing the API service and objects representing its inputs and outputs. These include the “API Querying” process, inputs “Latitude” and “Longitude”, and output “Response”. Link the inputs to the process via the instrument link and the output via the result link.
Convert these elements to computational objects and assign aliases to reference them easily in the following steps (e.g. “Latitude” becomes “la” and “Longitude” becomes “lo”). Modify their states to specify the location values as needed.
Step 2: Configure the API Request
Now, configure the “API Querying” process to connect with the Sunrise Sunset API, using the base URL https://api.sunrisesunset.io/json. This API takes parameters “lat” and “lng” for latitude and longitude. For instance, a sample query might look like this: https://api.sunrisesunset.io/json?lat=38.907192&lng=-77.036873.
In OPCloud, specify parameters in the format “(lat,la)(lng,lo)”. During simulation, OPCloud will retrieve the values from “Latitude” and “Longitude” states to compose the query.
Step 3: Parse the Output
Since the API output is in JSON format, parsing is necessary to extract the sunrise and sunset times. Create two processes “Sunrise Parsing” and “Sunset Parsing”, then define local TypeScript computation to retrieve the respective times from the JSON response.
Each parsed time can now be used in subsequent modelling steps, enabling accurate simulation of the control system in your solar power model.
Final Thoughts
As system models become more complex, outsourcing specialised functions and data through external APIs can provide practical benefits. Integrating APIs within OPCloud allows teams to link their models not only to external sources but also to internal services, such as system mockups, test rigs, or any company database. By expanding beyond standard inputs, models can leverage data that is specific, relevant, and current, leading to simulations that more accurately reflect the systems they represent.
Interested in learning more about OPCloud simulations? Check out our microcourse on OptimiSE Academy!
What other ways can you imagine applying API integrations within system models? Share your thoughts below!
Leave a Reply