Exploring parametric ODE with TMS Analytics and Physics numerical tool

Thursday, July 13, 2017

Ordinary differential equations (ODE) used in many applications for physical, economical and other problem solution. The equations commonly describe the time evolution of some system from known initial state – initial value problem (https://en.wikipedia.org/wiki/Initial_value_problem).

The TMS Analytics and Physics pack contains the ODE solution tool of solving initial value problems for systems of ODEs. This tool is totally integrated with the symbolic capabilities of the library. This allows solving parametric problems and exploring how the parameter value influences the system behavior.

Let us consider the following simple parametric ODE:



with initial condition y(0)=1 on the time interval t=[0, 10]. The problem is parametric because there are parameters ‘A’ and ‘b’ in the equation. Our goal is exploring how the solution (function y(t)) depends on the value of ‘b’ parameter.

First we create the instance of analytical ODE object, here is the code:

var
  A, b: TVariable;
  prms: TArray<TVariable>;
  fv, equations: TArray<string>;
  ode: TAnalyticalODE;
  solver: TODESolverClass;
  y0: TArray<TFloat>;
  t: TArray<TFloat>;
  y: TArray<TArray<TFloat>>;
  i: Integer;
begin
  A:= TRealVariable.Create('A', 1.0);    // 1
  b:= TRealVariable.Create('b', 0.0);    // 2
  prms:= TArray<TVariable>.Create(A, b); // 3
  fv:= TArray<string>.Create('y');       // 4
  equations:= TArray<string>.Create('sin(t)+A*t/y^b'); // 5
  ode:= TScalarODE.Create('t', fv, equations, prms);   // 6
  ...
 
Line 1: Create the variable for ‘A’ parameter.
Line 2: Create the variable for ‘b’ parameter.
Line 3: Create array of parameters.
Line 4: Create array of function names (one name required for the problem).
Line 5: Create array of equations (one equation).
Line 6: Create the instance of analytical ODE system with specified equation and parameters.

After the instance of the analytical ODE system created we must select appropriate solver and specify initial condition for the problem. We will use the Runge-Kutta direct solver of the 4-th order. The code continued:

solver:= TRungeKutta4Solver;          // 7
y0:= TArray.Create(1.0);      // 8
And finally we must solve the initial value problem for various values of ‘b’ parameter, say b=1..8:

for i:=1 to 8 do
begin
  ode.Parameters['b'].Value:= TValue.From(i); // 9
  y:= solver.Solve(ode, y0, 10.0, 1000, t);           // 10
  
  // Code for using the result data ‘y’ and ‘t’.
end; 
Line 9: Change value of ‘b’ parameter using the array property ‘Parameters’ of the analytical ODE class. Line 10: Solving the specified initial value problem for current value of ‘b’ parameter, on the time interval t=[0, 10], with 1000 discretization steps.

The solution result for changing value of ‘b’ parameter presented on the picture below.


TMSFNCChart cross-platform / cross-framework chart displaying the result

As can be seen from the code above, there is no need creating new analytical ODE instance for exploring the parametric problem. As the analytical ODE is parametric, it is solved for the current parameter values and they can be easily changed via parametric interface.

Other advantages of using the numerical ODE tool with the symbolic calculus are:
- Minimal code writing, because there is no need to write special classes for the function method references (delegates) as in other numerical libraries.
- Convenient data representation for the developer and user as math expressions, not as programming code.
- Easily using the user input data (string data) for manipulations in programming code.
- Easily transfer input data via network as strings, easily storing and serializing the data.

The TMS Analytics and Physics pack includes the library with 5 ready-to-use numerical tools totally integrated with analytical features, such as symbolic derivatives. The trial version of TMS Analytics and Physics pack can be downloaded here. Total source code of the example application of ODE tool can be downloaded here.

Bruno Fierens


Bookmarks: 

This blog post has not received any comments yet.



Add a new comment:
Author:
Email:
  You will receive a confirmation mail with a link to validate your comment, so please use a valid email address.
Comment:
 
Change Image
Fill in the characters from the image above:
 

All fields are required.
 



Storing objects in the Google Firebase cloud with TMS Cloud Pack

Tuesday, June 20, 2017

Since the latest release of TMS Cloud Pack, there is now also built-in support to use Google Firebase for cloud data storage. The architecture of Google's Firebase is quite simple. It offers storage of JSON data in the cloud. The JSON data has a unique identifier in a table and via access with this unique identifier, this JSON data can be read, updated or deleted. In addition, indexing rules can be set to perform query on the data in a Firebase table, for example, retrieve all JSON objects where a field X has value Y.

To make it really easy use to use data on Google Firebase from a Delphi or C++Builder application, we have added capabilities to put objects or generic lists of objects in a Firebase table. This is done via the non-visual component TFirebaseObjectDatabase that you can put on the form.



To demonstrate this, consider a class we want to use in the Delphi application that descends from TFirebaseObject:

  TFirebaseCustomer = class(TFirebaseObject)
  private
    FName: string;
    FStreet: string;
    FZIP: integer;
    FDoB: TDate;
    FCity: string;
  public
    constructor Create; override;
    destructor Destroy; override;
  published
    property Name: string read FName write FName;
    property Street: string read FStreet write FStreet;
    property City: string read FCity write FCity;
    property ZIP: integer read FZIP write FZIP;
    property DoB: TDate read FDoB write FDoB;
  end;

Now, after we have retrieved a connection for TAdvFirebaseObjectDataBase to Firebase via:

  AdvFirebaseObjectDatabase1.DatabaseName := 'TMS';
  AdvFirebaseObjectDatabase1.TableName := 'Customers';
  AdvFirebaseObjectDatabase1.Connect;
we can create and put TFirebaseCustomer objects in the Google Firebase realtime data cloud:

var
  cst: TFireBaseCustomer;
begin
  cst := TFireBaseCustomer.Create;
  try
    cst.Name := 'Bill Gates';
    cst.Street := 'Microsoft Av';
    cst.ZIP := 2123;
    cst.City := 'Redmond';
    cst.DoB := EncodeDate(1969,04,18);
    cst.ID := '1240';
    AdvFirebaseObjectDatabase1.InsertObject(cst);
  finally
    cst.Free;
  end;
end;

All published properties of the object will be automatically persisted on the Google Firebase cloud. This is how the data looks when inspecting it on the Firebase console

Note that we have explicitly set the unique ID of the object via the cst.ID property. When the ID is set at application level, it is the responsibility of the app to use unique IDs. When no ID is set, the AdvFirebaseObjectDatabase will automatically create a GUID as ID.

In this example, we have created a rather simple object with simple data types. But nothing prevents you from using class properties as in this example:

  TCareerPeriod = class(TPersistent)
  private
    FFinish: integer;
    FStart: integer;
  published
    property Start: integer read FStart write FStart;
    property Finish: integer read FFinish write FFinish;
  end;

  TFirebaseCustomer = class(TFirebaseObject)
  private
    FName: string;
    FStreet: string;
    FZIP: integer;
    FDoB: TDate;
    FCity: string;
    FPicture: TFireBasePicture;
    FCareer: TCareerPeriod;
    procedure SetCareer(const Value: TCareerPeriod);
  public
    constructor Create; override;
    destructor Destroy; override;
  published
    property Name: string read FName write FName;
    property Street: string read FStreet write FStreet;
    property City: string read FCity write FCity;
    property ZIP: integer read FZIP write FZIP;
    property DoB: TDate read FDoB write FDoB;
    property Career: TCareerPeriod read FCareer write SetCareer;
  end;

And now this code can be used to persist this slightly more complex object:
var
  cst: TFireBaseCustomer;
begin
  cst := TFireBaseCustomer.Create;
  try
    cst.Name := 'Elon Musk';
    cst.Street := '3500 Deer Creek Road';
    cst.ZIP := 2123;
    cst.City := 'Palo Alto';
    cst.DoB := EncodeDate(1975,03,21);
    cst.ID := '1241';
    cst.Career.Start := 2011;
    cst.Career.Finish := 2017;
    AdvFirebaseObjectDatabase1.InsertObject(cst);
  finally
    cst.Free;
  end;
end;

As you can see, more complex classes can be easily & automatically persisted in the Google Firebase cloud.


With respect to types of fields, there is one caveat though, Google Firebase doesn't offer out of the box support for binary blobs. Imagine that we'd want to persist a Delphi object that has a TPicture property. The TPicture is internally streamed in a custom way to the DFM (via the DefineProperties(Filer: TFiler); override) and the JSON persister does not automatically get this data. There is however an easy workaround to add a published string property to a class descending from TPicture and use this string to hold hex encoded binary data of the picture. The DataString property getter & setter methods use the StreamToHex() and HexToStream() functions that are included in the unit CloudCustomObjectFirebase:

  TFireBasePicture = class(TPicture)
  private
    function GetDataString: string;
    procedure SetDataString(const Value: string);
  published
    property DataString: string read GetDataString write SetDataString;
  end;


{ TFireBasePicture }

function TFireBasePicture.GetDataString: string;
var
  ms: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    SaveToStream(ms);
    Result := StreamToHex(ms);
  finally
    ms.Free;
  end;
end;

procedure TFireBasePicture.SetDataString(const Value: string);
var
  ms: TMemoryStream;
begin
  ms := HexToStream(Value);
  try
    LoadFromStream(ms);
  finally
    ms.Free;
  end;
end;


This way, we can extend the class to have a picture property:

  TFireBaseCustomer = class(TFirebaseObject)
  private
    FName: string;
    FStreet: string;
    FZIP: integer;
    FDoB: TDate;
    FCity: string;
    FPicture: TFireBasePicture;
    FCareer: TCareerPeriod;
    procedure SetPicture(const Value: TFireBasePicture);
    procedure SetCareer(const Value: TCareerPeriod);
  public
    constructor Create; override;
    destructor Destroy; override;
  published
    property Name: string read FName write FName;
    property Street: string read FStreet write FStreet;
    property City: string read FCity write FCity;
    property ZIP: integer read FZIP write FZIP;
    property DoB: TDate read FDoB write FDoB;
    property Career: TCareerPeriod read FCareer write SetCareer;
    property Picture: TFireBasePicture read FPicture write SetPicture;
  end;

and persist the object with picture via:

var
  cst: TFireBaseCustomer;
begin
  cst := TFireBaseCustomer.Create;
  try 
    cst.Name := 'Tim Cook';
    cst.Street := '1 Infinite Loop';
    cst.ZIP := 2123;
    cst.City := 'Cupertino';
    cst.DoB := EncodeDate(1975,03,21);
    cst.ID := '1242';
    cst.Picture.LoadFromFile('tim_cook.jpg');
    cst.Career.Start := 2006;
    cst.Career.Finish := 2017;

    AdvFirebaseObjectDatabase1.InsertObject(cst);
  finally
    cst.Free;
  end;
end;

To retrieve the objects back that were persisted, we can use:

var
  fbo: TFirebaseObject;
begin
  fbo := AdvFirebaseObjectDatabase1.ReadObject('1240');

  if Assigned(fbo) then
  begin
    if (fbo is TFirebaseCustomer) then
      ShowMessage((fbo as TFirebaseCustomer).Name+';'+(fbo as TFirebaseCustomer).City);
    fbo.Free;
  end;
end;



To persist a property change of the object, for example to change the career date of customer 1241 to 2018, we could write:

var
  fbo: TFirebaseObject;
begin
  fbo := AdvFirebaseObjectDatabase1.ReadObject('1241');

  if Assigned(fbo) then
  begin
    if (fbo is TFirebaseCustomer) then
    begin 
      ((fbo as TFirebaseCustomer).Career.Finish := 2018;
      AdvFirebaseObjectDatabase1.WriteObject(fbo);
    end;
  end;
end;

Working with generic lists


The previous samples all showed how to perform CRUD operations on single objects in the Google Firebase cloud. Our component TAdvFirebaseObjectDatabase also facilitates dealing with generic lists of objects. Imagine you want to persist a score table of game and have following simple classes to store this:

  TSimpleClass = class(TFirebaseObject)
  private
    FName: string;
    FScore: integer;
  published
    property Name: string read FName write FName;
    property Score: integer read FScore write FScore;
  end;

  TSimpleList = TList<TSimpleClass>;


With this class and its generic list, we can add a list of data at once in the Google Firebase cloud:

var
  sl: TFirebaseObjectList;
  sc: TSimpleClass;
  I: Integer;
begin
  sl := TFirebaseObjectList.Create;
  try
    sc := TSimpleClass.Create;
    sc.Name := 'Bruno';
    sc.Score := 48;
    sc.ID := '1';
    sl.Add(sc);

    sc := TSimpleClass.Create;
    sc.Name := 'Nancy';
    sc.Score := 83;
    sc.ID := '2';
    sl.Add(sc);

    sc := TSimpleClass.Create;
    sc.Name := 'Pieter';
    sc.Score := 17;
    sc.ID := '3';
    sl.Add(sc);

    sc := TSimpleClass.Create;
    sc.Name := 'Bart';
    sc.Score := 299;
    sc.ID := '4';
    sl.Add(sc);

    sc := TSimpleClass.Create;
    sc.Name := 'Wagner';
    sc.Score := 55;
    sc.ID := '5';
    sl.Add(sc);

    AdvFirebaseObjectDatabase1.TableName:= 'List';
    AdvFirebaseObjectDatabase1.InsertList(sl);

    for I := 0 to sl.Count - 1 do
      sl[I].Free;
  finally 
    sl.Free;
  end;
end;

Inspecting this on the Google Firebase console results in:



If at a later time we'd like to retrieve the score of Bart, we could use the code:

var
  c: TFirebaseObjectList;
  I: Integer;

begin
  AdvFirebaseObjectDatabase1.TableName := 'List';
  c:= AdvFirebaseObjectDatabase1.QueryList('Name', 'Bart');
  if Assigned(c) then
  begin
    for I := 0 to c.Count - 1 do
    begin
      ShowMessage((c[I] as TSimpleClass).Score.ToString);
      c[I].Free;
    end;
    c.Free;
  end;
end;

This performs a query on the List table for value 'Bart' set in the 'Name' field. The result of the query is a generic list (in case multiple matching results are found).

This was a glimpse at the TAdvFirebaseObjectDataBase component as an introduction for the new Google Firebase access features in the TMS Cloud Pack. Explore the full set of powerful capabilities of TAdvFirebaseObjectDataBase and the many other components in TMS Cloud Pack to make consuming cloud services in Delphi & C++Builder a piece of cake.


Bruno Fierens


Bookmarks: 

This blog post has received 7 comments.


1. Tuesday, June 20, 2017 at 4:57:49 PM

One of Firebase''s key strength is real time syncing of data. Is that supported ?
Thanks.

J.


2. Tuesday, June 20, 2017 at 5:07:48 PM

At this moment we have focused on CRUD operation on objects. The notification part isn''t via standard HTTP REST and at this moment not yet in the TMS Cloud Pack.

Bruno Fierens


3. Tuesday, June 20, 2017 at 10:43:43 PM

Is possible use this for work with Firebase Analytics. now that Google analytics is no avaliable for analyticd Apps?

Xavi Pallicera


4. Wednesday, June 21, 2017 at 11:50:59 AM

Separate from our Google Firebase data access you can use analytics. In our component(s) itself, there is at this moment no integration with Firebase analytics, only Google analytics itself.

Bruno Fierens


5. Wednesday, June 21, 2017 at 12:59:38 PM

Excellent News..Thank you...Awaiting updates on this providing access to full capability of firebase from Delphi.

Saajid


6. Saturday, July 15, 2017 at 8:06:00 AM

I take it you can store components in the cloud? For example, primitive compound shapes?

Derek Seymour


7. Sunday, July 16, 2017 at 5:03:07 PM

You could indeed persist components, it can be streamed as object to Firebase.

Bruno Fierens




Add a new comment:
Author:
Email:
  You will receive a confirmation mail with a link to validate your comment, so please use a valid email address.
Comment:
 
Change Image
Fill in the characters from the image above:
 

All fields are required.
 



TMS WebGMaps brings submarkers and extra options to load POI files

We're pleased to announce TMS WebGMaps v2.9 has been released today. TMS WebGMaps is our VCL component that exposes a myriad of geographical functions for integration in Delphi or C++Builder Windows applications.

In this new version, we introduce submarkers. When at a given position, more than one interesting item can be highlighted, it is now possible to add multiple items via submarkers at the same position and when the main marker is clicked, the submarkers are displayed.

In the sample, we've coupled different interesting items to see in Florida via submarkers for the marker at Florida. When the marker is clicked, different submarkers are displayed showing POIs such as Disney park, Cape Canaveral, Orlando resort... This technique keeps the number of simultaneously displayed markers light and only shows the detail when the user is interested and clicks the main marker.



Adding a marker with submarkers is very easy as this code snippet shows:

var
  m: TMarker;
  sm: TSubMarker;
begin
  m := WebGMaps1.Markers.Add(WebGMaps1.MapOptions.DefaultLatitude, WebGMaps1.MapOptions.DefaultLongitude);
  m.Title := 'Florida Landmarks';

  sm := m.SubMarkers.Add;
  sm.Title := 'Walt Disney World';

  sm := m.SubMarkers.Add;
  sm.Title := 'Cape Canaveral';

  ....

end;

In the new version, we have also refined the functions to load & save markers in POI file format. A POI file is a file that contains geocoordinates and description of places of interest. Now it is also possible to specify custom marker images when loading POI files. TMS WebGMaps can handle the POI file format as used by well-known navigation software such as Garmin, Tomtom, Route66, Becker, ...
There is a wealth of already existing POI files you can directly use with TMS WebGMaps. A resource of millions of POI files for 221 countries is for example www.poiplaza.com. In our sample, we have demonstrated this by loading a POI containing for example the IBIS hotels and movie theatres in Germany.



The code needed for this is just two lines:

    WebGMaps1.Markers.LoadFromPOI(FilePath + 'D-Cinema.csv', FilePath + 'projector_24.png');
    WebGMaps1.Markers.LoadFromPOI(FilePath + 'D-IbisHotel.csv', FilePath + 'ibis_24.png');

It refers to the POI filename and the (local) marker image to use. The WebGMaps component does all the needed things to show the custom Ibis image marker on the map in Germany for the Ibis hotel locations and the movie theatre image for the movie theatre locations.

We look forward to learn about your interesting integrations of TMS WebGMaps in your applications!

Bruno Fierens


Bookmarks: 

This blog post has not received any comments yet.



Add a new comment:
Author:
Email:
  You will receive a confirmation mail with a link to validate your comment, so please use a valid email address.
Comment:
 
Change Image
Fill in the characters from the image above:
 

All fields are required.
 



Browser push notifications for announcements from tmssoftware.com

Monday, June 19, 2017

We are pleased to inform that from today, there is an additional channel, you can opt-in for, to keep in the loop with the news from tmssoftware.com.
For some time already, all our news is announced on our social media channels Twitter, Facebook, Google+ and LinkedIn and via a mailing list:

   

and from now on, for users with an account on our website, there is also the possibility to be informed via browser push notifications:



These browser push notifications are at this time a built-in notification mechanism of the Google Chrome and Firefox browsers. When you login on our website, you can opt-in to receive these push notifications when clicking "Allow" on the prompt:



With the new notification system inplace, you'll be aware of news from tmssoftware.com when you just open your browser, so you really can't miss news anymore!

Bruno Fierens


Bookmarks: 

This blog post has not received any comments yet.



Add a new comment:
Author:
Email:
  You will receive a confirmation mail with a link to validate your comment, so please use a valid email address.
Comment:
 
Change Image
Fill in the characters from the image above:
 

All fields are required.
 




Previous  |  Next  |  Index