Hello,
I am programming a fluid properties calculator, which interpolates from a lookup table, and I selected xBase as database system for storing the data.
The original data is based on a 2-D(pressure and temperature) lookup table. I re-arranged it to a long list and saved it to a DBF file.
In general cases, a state with pressure P & temperature T would be enveloped by 4 neighbor points: PLTL,PLTH,PHTL, and PHTH.(L for lower, H for higher)
I write the following code for test:
//---------------------------------------------------------------------
var
DataDBF:TDBF;
P,T,PL,PH:Double; //Fluid states
PLTL,PLTH,PHTL,PHTH:Integer; //Record number of neighbor points
T1,T2:Integer; //For executing time measurement
begin
DataDBF:=TDbf.Create(Nil);
DataDBF.FilePathFull:=ExtractFilePath(ParamStr(0));
DataDBF.ReadOnly:=True;
DataDBF.TableName:='Sample.DBF';
DataDBF.TableLevel:=4;
DataDBF.Open;
P:=37.945;
T:=-62.066;
T1:=GetTickCount();
DataDBF.Filter:='P>='+floattostr(P);
DataDBF.Filtered:=True;
DataDBF.First; //This line relies on the raw data sequence. I feel that it's not so ideal, but I don't know how to get the minimum P from the search result....
PH:=DataDBF.FieldByName('P').AsFloat;
DataDBF.Filter:='P<='+floattostr(P);
DataDBF.Filtered:=True;
DataDBF.Last;
PL:=DataDBF.FieldByName('P').AsFloat;
DataDBF.Filter:='P='+floattostr(PL)+' and T<='+floattostr(T);
DataDBF.Filtered:=True;
DataDBF.Last;
PLTL:=DataDBF.PhysicalRecNo;
DataDBF.Filter:='P='+floattostr(PL)+' and T>='+floattostr(T);
DataDBF.Filtered:=True;
DataDBF.First;
PLTH:=DataDBF.PhysicalRecNo;
DataDBF.Filter:='P='+floattostr(PH)+' and T<='+floattostr(T);
DataDBF.Filtered:=True;
DataDBF.Last;
PHTL:=DataDBF.PhysicalRecNo;
DataDBF.Filter:='P='+floattostr(PH)+' and T>='+floattostr(T);
DataDBF.Filtered:=True;
DataDBF.First;
PHTH:=DataDBF.PhysicalRecNo;
T2:=GetTickCount();
DataDBF.Close;
DataDBF.Free;
end;
//---------------------------------------------------------------------
I can get the correct result from the above code. The only issue is that the execution speed. On my laptop it takes more than 1 second(T2-T1) to perform once.
I tried using index by P(Primary), but the result is not correct. Is there any suggestion for improving it?
The sample DBF file can be downloaded here:
https://drive.google.com/file/d/1bGrwKyE5oMHsmdxnpDyHh4507-c_QSW1/view?usp=sharingBTW, does anyone have recommend website for Tdbf tips? I just know the official site and Lazarus tutorial.
Thanks!
Chen, Yu-Chih
(Lazarus 1.8.4 official release)