Is it possible to simulate scenarios using Azure Digital Twins? If so, where can I find the corresponding tool?

S.M 25 Reputation points
2023-04-07T15:46:12.5233333+00:00

Hello!

I'm a university student and I am supposed to find a way to implement a digital twin of machines such as water pumps for a project. In one example, 3 sensors will be attached to the pump in order to read in different physical measurements and use them for visualisations, maintenance etc. I have just set up the Azure Digital Twins instance and wanted to familiarise myself with the platform by loading an example model into the ADT Explorer (I do not yet have access to the real sensor data). One crucial aspect of the Digital Twin application for my project is the ability to simulate different scenarios - for example, simulating the operation of a pump under different conditions. I have read several times that simulation (and even Machine Learning implementations) are supported by ADT. However, I cannot figure out where to find any simulation tool within ADT.

Did I misunderstand and such simulations are not possible?

Any help is greatly appreciated!

Azure Digital Twins
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
231 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,018 questions
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 16,046 Reputation points
    2023-04-07T20:18:59.3666667+00:00

    Hi @S.M Greetings! Welcome to Microsoft Q&A forum. Thank you for posting the question here. There is an Azure Digital Twins Data Simulator which lets you simulate data of the digital twins. However, the simulator is preconfigured with couple of simulation types. A better alternative to achieve this is by using an SDK. I have created a sample Digital model that has three properties and generated the Digital Twins based on the model. Here is the template for my model.

    {
        "@context": "dtmi:dtdl:context;2",
        "@id": "dtmi:com:example:Pump;1",
        "@type": "Interface",
        "displayName": "Pump",
        "contents": [
          {
            "@type": "Property",
            "name": "Sensor1",
            "writable": true,
            "schema": "double"
          },
          {
            "@type": "Property",
            "name": "Sensor2",
            "writable": true,
            "schema": "double"
          },
          {
            "@type": "Property",
            "name": "Sensor3",
            "writable": true,
            "schema": "double"
          }
        ]
      }
    

    I created Digital Twins based on the model. Please refer the article Create a Client App that has the code samples on how to upload a model and create Digital twins. Once you have the Digital Twin created you can simulate the properties by appending a JSON patch to the Digital Twin. Please find the below code that I have used to upload model, generate twins and simulate the data.

    using System;
    using Azure.DigitalTwins.Core;
    using Azure.Identity;
    using Azure;
    using System.Text.Json;
    using System.Text;
    
    namespace PumpDemo // Note: actual namespace depends on the project name.
    {
        internal class Program
        {
            static async Task Main(string[] args)
            {
                string adtInstanceUrl = "<Azure Digital Twin Instance URL>";
    
                var credential = new DefaultAzureCredential();
                var client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credential);
                Console.WriteLine($"Service client created – ready to go");
                Console.WriteLine();
                Console.WriteLine($"Upload a model");
                string dtdl = File.ReadAllText("template.json");
                var models = new List<string> { dtdl };
                
                // Upload the model to the service
                try
                {
                    await client.CreateModelsAsync(models);
                    Console.WriteLine("Models uploaded to the instance:");
                }
                catch (RequestFailedException e)
                {
                    Console.WriteLine($"Upload model error: {e.Status}: {e.Message}");
                }
    
                // Read a list of models back from the service
                AsyncPageable<DigitalTwinsModelData> modelDataList = client.GetModelsAsync();
                await foreach (DigitalTwinsModelData md in modelDataList)
                {
                    Console.WriteLine($"Model: {md.Id}");
                }
    
                //Create digital twins
                await CreateDigitalTwins(client);
    
    
                //simulate the properties of Digital Twin Pump-1
                BasicDigitalTwin digitalTwin = await client.GetDigitalTwinAsync<BasicDigitalTwin>("Pump-1");
    
                var jsonPatch = new Azure.JsonPatchDocument();
                Random random = new Random();
                int randomNumber = random.Next(1, 101);
    
                while (true)
                {
                    randomNumber = random.Next(1, 101);
                    jsonPatch.AppendReplace("/Sensor1", randomNumber);
                    randomNumber = random.Next(1, 101);
                    jsonPatch.AppendReplace("/Sensor2", randomNumber);
                    randomNumber = random.Next(1, 101);
                    jsonPatch.AppendReplace("/Sensor3", randomNumber);
    
                    await client.UpdateDigitalTwinAsync("Pump-1", jsonPatch);
                    Console.WriteLine("Sending updates to Digital Twin");
    
                    Thread.Sleep(10000);
                }         
    
            }
    
            static async Task CreateDigitalTwins(DigitalTwinsClient client)
            {
                //Create Digital Twin from the model
                var twinData = new BasicDigitalTwin();
                twinData.Metadata.ModelId = "dtmi:com:example:Pump;1";
                twinData.Contents.Add("Sensor1", 45);
                twinData.Contents.Add("Sensor2", 79);
                twinData.Contents.Add("Sensor3", 28);
    
                string prefix = "Pump-";
                for (int i = 1; i <= 3; i++)
                {
                    try
                    {
                        twinData.Id = $"{prefix}{i}";
                        await client.CreateOrReplaceDigitalTwinAsync<BasicDigitalTwin>(twinData.Id, twinData);
                        Console.WriteLine($"Created twin: {twinData.Id}");
                    }
                    catch (RequestFailedException e)
                    {
                        Console.WriteLine($"Create twin error: {e.Status}: {e.Message}");
                    }
                }
            }
        }
    }
    
    

    Please make sure to provide your Azure digital twin instance URL for the variable adtInstanceUrl The code sends simulated data to the Digital twin every ten seconds

    Once you have application running, you can see the Twin properties values update every ten seconds on the Azure Digital Twin explorer. User's image

    Hope this helps! Please let us know if you run into any issues or need further clarification. If the response helped, please do click Accept Answer and Yes. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.