Recent

Author Topic: Lazarus & Freesteam libraries  (Read 5795 times)

DiversantBivnev

  • Newbie
  • Posts: 6
Lazarus & Freesteam libraries
« on: March 10, 2017, 10:43:51 am »
Hello everyone!
I'm newbie in Lazarus, but it is necessary for me to write small calculation app.
I need to use IAPWS-IF97 steam properties software library, implemented in C.
https://sourceforge.net/projects/freesteam/
Is there a simple way to access this library functions from Lazarus?
I don't know, maybe it's simpler to access .dll in Windows OS?
Any help appreciated.

linnemann

  • New Member
  • *
  • Posts: 34
Re: Lazarus & Freesteam libraries
« Reply #1 on: March 10, 2017, 11:45:14 am »
Hi

I tried downloading the python win32 binary and open with 7zip.

There is a freesteam.dll inside there, extracting it and inspecting gives you access to all the functions.

So using from lazarus should be fine as long as you know the type to pass to the functions.

DiversantBivnev

  • Newbie
  • Posts: 6
Re: Lazarus & Freesteam libraries
« Reply #2 on: March 11, 2017, 04:11:34 pm »
I've made several procedures, addressing freesteam library,

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Edit2: TEdit;
  18.     Edit3: TEdit;
  19.     Edit4: TEdit;
  20.     P: TLabel;
  21.     P1: TLabel;
  22.     T: TLabel;
  23.     procedure Button1Click(Sender: TObject);
  24.     procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  25.     procedure P1Click(Sender: TObject);
  26.   private
  27.     { private declarations }
  28.   public
  29.     { public declarations }
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.   p1, t1: real;
  35.   p, t, v, Tsat, SteamState: real;
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. function freesteam_p(SteamState : double):double ; stdcall;
  41.          external 'libfreesteam.so' name 'freesteam_p';
  42. function freesteam_T(SteamState : double):double ; stdcall;
  43.          external 'libfreesteam.so' name 'freesteam_T';
  44. function freesteam_rho(SteamState : double):double ; stdcall;
  45.          external 'libfreesteam.so' name 'freesteam_rho';
  46. function freesteam_v(SteamState : double ):double ; stdcall;
  47.          external 'libfreesteam.so' name 'freesteam_v';
  48. function freesteam_u(SteamState : double):double ; stdcall;
  49.          external 'libfreesteam.so' name 'freesteam_u';
  50. function freesteam_h(SteamState : double):double ; stdcall;
  51.          external 'libfreesteam.so' name 'freesteam_h';
  52. function freesteam_s(SteamState : double):double ; stdcall;
  53.          external 'libfreesteam.so' name 'freesteam_s';
  54. function freesteam_cp(SteamState : double):double ; stdcall;
  55.          external 'libfreesteam.so' name 'freesteam_cp';
  56. function freesteam_cv(SteamState : double):double ; stdcall;
  57.          external 'libfreesteam.so' name 'freesteam_cv';
  58. function freesteam_w(SteamState : double):double ; stdcall;
  59.          external 'libfreesteam.so' name 'freesteam_w';
  60.  
  61. function freesteam_x(SteamState : double):double ; stdcall;
  62.          external 'libfreesteam.so' name 'freesteam_x';
  63.  
  64. function freesteam_mu(SteamState : double):double ; stdcall;
  65.          external 'libfreesteam.so' name 'freesteam_mu';
  66. function freesteam_k(SteamState : double):double ; stdcall;
  67.          external 'libfreesteam.so' name 'freesteam_k';
  68.  
  69. function freesteam_set_pT(p,t:double):double; stdcall;
  70.          external 'libfreesteam.so' name 'freesteam_set_pT';
  71.  
  72. procedure calc;
  73. var
  74.   vol_dbl, S: double;
  75.   vol_str: String;
  76.   p,t, p1, t1 : double;
  77. begin
  78.  
  79.   p1 := StrToFloat(Form1.Edit1.Text);
  80.   t1 := StrToFloat(Form1.Edit2.Text);
  81.  
  82.   p := p1*10e6;
  83.   t := t1+273.15;
  84.  
  85.    S := freesteam_set_pT(10,100);
  86.  
  87. end;
  88.  
  89. { TForm1 }
  90.  
  91. procedure TForm1.Button1Click(Sender: TObject);
  92. begin
  93.   calc;
  94. end;
  95.  
  96. end.

As explained here http://freesteam.sourceforge.net I set steam state using syntax like SteamState S = freesteam_set_ph(p,h).

But in my code on this string  S := freesteam_set_pT(10,100) the program just exits.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: Lazarus & Freesteam libraries
« Reply #3 on: March 11, 2017, 04:13:02 pm »
It is unlikely that the calls use "stdcall" on *nix. So use cdecl on unix and stdcall on windows.

For most libraries it works that way

DiversantBivnev

  • Newbie
  • Posts: 6
Re: Lazarus & Freesteam libraries
« Reply #4 on: March 11, 2017, 09:42:39 pm »
Sadly it didn't help, made no difference.
Guess I've missed something considering freesteam libraries. The description is not so obvious to me.

linnemann

  • New Member
  • *
  • Posts: 34
Re: Lazarus & Freesteam libraries
« Reply #5 on: March 13, 2017, 08:57:57 am »
Sadly it didn't help, made no difference.
Guess I've missed something considering freesteam libraries. The description is not so obvious to me.

Hi, you main problem is that FPC have no idea of the classes of freesteam.

as you can see here http://freesteam.sourceforge.net/docs/class_steam_state.html you need to define a SteamState class in FPC.

It seams that the freesteam.dll only have the functions associated to the Class but not the Class itself.

I cannot help you here, maybe some of the other gurus can help in making the transfer from C class to FPC class.


DiversantBivnev

  • Newbie
  • Posts: 6
Re: Lazarus & Freesteam libraries
« Reply #6 on: March 14, 2017, 10:40:54 am »
Thanks for an explanation. Sadly the problem stays.  :(
Don't know whom to ask...

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Lazarus & Freesteam libraries
« Reply #7 on: March 15, 2017, 11:49:13 am »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

sky_khan

  • Guest
Re: Lazarus & Freesteam libraries
« Reply #8 on: March 15, 2017, 01:18:23 pm »
I've looked at its source,
result type of freesteam_set_pT function is not double but a record defined as SteamState

Code: C  [Select][+][-]
  1. typedef struct SteamState_R1_struct{
  2.         double p, T;
  3. } SteamState_R1;
  4.  
  5. typedef struct SteamState_R2_struct{
  6.         double p, T;
  7. } SteamState_R2;
  8.  
  9. typedef struct SteamState_R3_struct{
  10.         double rho, T;
  11. } SteamState_R3;
  12.  
  13. typedef struct SteamState_R4_struct{
  14.         double T, x;
  15. } SteamState_R4;
  16.  
  17. typedef struct SteamState_struct{
  18.         char region;
  19.         union{
  20.                 SteamState_R1 R1;
  21.                 SteamState_R2 R2;
  22.                 SteamState_R3 R3;
  23.                 SteamState_R4 R4;
  24.         };
  25. } SteamState;
  26.  

I think you may define this struct in pascal like below but I'm not so sure
Code: Pascal  [Select][+][-]
  1. type
  2.     SteamState_R1 = record
  3.         p, T: Double;
  4.     end;
  5.     SteamState_R2 = record
  6.         p, T: Double;
  7.     end;
  8.     SteamState_R3 = record
  9.       rho, T : Double;
  10.     end;
  11.     SteamState_R4 = record
  12.       T, x : Double;
  13.     end;
  14.  
  15.     SteamStateRec = record
  16.       case region : char of
  17.         #0: (R1: SteamState_R1);
  18.         #1: (R2: SteamState_R2);
  19.         #2: (R3: SteamState_R3);
  20.         #3: (R4: SteamState_R4);
  21.     end;                                    
  22. ...
  23. function freesteam_set_pT(p,t:double):SteamStateRec; cdecl;...
  24.  

DiversantBivnev

  • Newbie
  • Posts: 6
Re: Lazarus & Freesteam libraries
« Reply #9 on: March 20, 2017, 02:54:50 pm »
My bad I still didn't get how to use those record types to use funtions like freesteam_p, freesteam_T, freesteam_v, etc. (mentioned in C code in steam.h). For example:
Code: [Select]
FREESTEAM_DLL double freesteam_v(SteamState S);

I declare function
Code: [Select]
function freesteam_v ( SteamState: SteamStateRec ): double;
         stdcall; external 'freesteam.dll' name 'freesteam_v'; 

And when I do the following
Code: [Select]
procedure TForm1.SpeedButton1Click(Sender : TObject);
var
  volume: double;
  St : SteamStateRec;
begin
  St := freesteam_set_pT(p,t);
  Volume := freesteam_v(St); ;
  Form1.Edit5.Caption := FloatToStr(Volume);
end;       
the program crashes.
« Last Edit: March 21, 2017, 02:41:27 pm by DiversantBivnev »

DiversantBivnev

  • Newbie
  • Posts: 6
Re: Lazarus & Freesteam libraries
« Reply #10 on: March 22, 2017, 08:40:20 am »
Well.. My problem was in using  stdcall instead of cdecl (I've been trying to write code on Win and Linux, just complicated myself)  :-[. From now on it seems to work.

 

TinyPortal © 2005-2018