(********************************************
  Version: 7.0
  Date: 2003/11/25
  This is shareware.
  Copyright (C) Michal Pohanka
  All rights reserved.

  Internet: http://www.volny.cz/profipohanka
 ********************************************)

unit HCModel;

interface

uses HCArrays, HCMaterial;

type
tHCModel = record
	dim: integer; // object dimension 1D, 2D, 3D
	coord: integer; // coordinate system; 1-cartesian; 2-cylindical

	// control volume deffinitions
	vBorderX, vBorderY, vBorderZ: tFloatArray; // borders [m]
	vT: tFloatArray; // temperature of the center node [K]; the latest temperatures
	vMat: tIntArray; // material index
	vSrc: tFloatArray; // heat source [W/m^3]

  preIterations: integer; // number of pre-iterations to speed up computation for 2D and 3D models
	maxIterations: integer;
	twoWayAlgorithm: boolean; // true means line-by-line method in both directions; useful for slow convergence
  responce: pointer; // pointer to function to call before subiterations
  responceAfterPreIter: pointer; // pointer to function to call after each pre-iteration
  responceAfterIter: pointer; // pointer to function to call after each iteration
	maxVolSizeRatio: float;
	interpolateT: boolean;
	interpolateSrc: boolean;

	// boundary conditions, E-east, W-west, N-north, S-south, F-front, B-back
	bE_kind, bW_kind, bN_kind, bS_kind, bF_kind, bB_kind: tIntArray; // 1=Ts, 2=q, 3=HTC
	bE_v, bW_v, bN_v, bS_v, bF_v, bB_v: tFloatArray; // value of boundary condition [K], [W/m^2], [W/K.m^2]
	bE_e, bW_e, bN_e, bS_e, bF_e, bB_e: tFloatArray; // emissivity (for radiation) <0..1>
	bE_t, bW_t, bN_t, bS_t, bF_t, bB_t: tFloatArray; // surrounding temperature [K]; only for kind=3 -> HTC;

	// thermocouple definition
	thermPos: tXYZArray; // position [m]
	thermT: tFloatArray; // computed temperature [K]

	// other
	accuracyX, accuracyY, accuracyZ: tFloatArray; // computed accuracy in all X, Y, Z directions; [K]

	// indexes for corner extrapolation
	corWNF, corWNFx, corWNFy, corWNFz: integer;
	corENF, corENFx, corENFy, corENFz: integer;
	corWSF, corWSFx, corWSFy, corWSFz: integer;
	corESF, corESFx, corESFy, corESFz: integer;

	corWNB, corWNBx, corWNBy, corWNBz: integer;
	corENB, corENBx, corENBy, corENBz: integer;
	corWSB, corWSBx, corWSBy, corWSBz: integer;
	corESB, corESBx, corESBy, corESBz: integer;

	corWNFx2, corWNFy2, corWNFz2: integer;
	corENFx2, corENFy2, corENFz2: integer;
	corWSFx2, corWSFy2, corWSFz2: integer;
	corESFx2, corESFy2, corESFz2: integer;

	corWNBx2, corWNBy2, corWNBz2: integer;
	corENBx2, corENBy2, corENBz2: integer;
	corWSBx2, corWSBy2, corWSBz2: integer;
	corESBx2, corESBy2, corESBz2: integer;

	corX0, corX01, corX02, corXN2, corXN1, corXN: integer;
	corY0, corY01, corY02, corYN2, corYN1, corYN: integer;
	corZ0, corZ01, corZ02, corZN2, corZN1, corZN: integer;

	// temporar dimensions
	nBx, nBy, nBz: integer; // equal to vBorder.n;
	nBx_1, nBy_1, nBz_1: integer; // equal to vBorder.n-1;
	nBx1, nBy1, nBz1: integer; // equal to vBorder.n+1;
	nBxy: integer; // equal to vBorderX.n*vBorderY.n;
	nBx1y1: integer; // equal to (vBorderX.n+1)*(vBorderY.n+1);
	nBx1y1z1: integer; // equal to (vBorderX.n+1)*(vBorderY.n+1)*(vBorderZ.n+1);
	nVol: integer; // for 1D =vBorderX.n+1; 2D =(vBorderX.n+1)*(vBorderY.n+1); 3D =(vBorderX.n+1)*(vBorderY.n+1)*(vBorderZ.n+1)
	// temporary arrays - for fast computation
	tempNodeX, tempNodeY, tempNodeZ: tFloatArray; // center node position, computed using borders [m]
	tempTold: tFloatArray; // old temperature [K]
	tempT1: tFloatArray; // temperature in iter-1 [K]; available after 2 iterations when calling responceAfterIter (max_err must be >conductAccuracy)
	tempT2: tFloatArray; // temperature in iter-2 [K]; available after 3 iterations when calling responceAfterIter (max_err must be >conductAccuracy)
	tempK: tFloatArray; // conduction - temperature dependent [SI]
	tempRoC: tFloatArray; // enthalpy - temperature dependent [SI]
	tempNodeXDist, tempNodeYDist, tempNodeZDist: tFloatArray; // node distances in all directions [m]
	tempVol: tFloatArray; //volume of the control volume
	tempVolXWidth, tempVolYWidth, tempVolZWidth: tFloatArray; // width of the control volume in all directions
	tempVolXWidth2, tempVolYWidth2, tempVolZWidth2: tFloatArray; // half width of the control volume in all directions
	tempCentR: tFloatArray; // for cylindrical model
	tempRv: tFloatArray; // for cylindrical model
	tempNXXW: tFloatArray; // for cylindrical model
	tempBX_1YW: tFloatArray; // for cylindrical model
	tempBXYW: tFloatArray; // for cylindrical model
	a, b, c, d, P, Q: tFloatArray; // for fast eqn. solver
end;

///////////////////////////////////////////////////////////////////////////////////////////////
function fHC_GetModelAccuracy: float; cdecl; external 'HC.DLL' name '?fHC_GetModelAccuracy@@YANXZ';
procedure fHC_SetModelAccuracy(accuracy: float); cdecl; external 'HC.DLL' name '?fHC_SetModelAccuracy@@YAXN@Z';

procedure fHC_Init(var m: tHCModel); overload; cdecl; external 'HC.DLL' name '?fHC_Init@@YAXAAUtHCModel@@@Z'; // object dimension 1D, 2D, 3D; coordinate system 1-cartesian 2-cylindical
procedure fHC_Free(var m: tHCModel);  overload; cdecl; external 'HC.DLL' name '?fHC_Free@@YAXAAUtHCModel@@@Z';
procedure fHC_FreeBoundaryCond(var m: tHCModel); cdecl; external 'HC.DLL' name '?fHC_FreeBoundaryCond@@YAXAAUtHCModel@@@Z';

function fHC_CopyFastHCModel(var d: tHCModel; var s: tHCModel): integer; cdecl; external 'HC.DLL' name '?fHC_CopyFast@@YAHAAUtHCModel@@0@Z'; // copy from source to destination; does not copy temp# and accuracy# arrays; return 0=OK;
function fHC_CopyHCModel(var d: tHCModel; var s: tHCModel): integer; cdecl; external 'HC.DLL' name '?fHC_Copy@@YAHAAUtHCModel@@0@Z'; // copy from source to destination; return 0=OK;

procedure fHC_GetErrHCModel(var s: tMyString); cdecl; external 'HC.DLL' name '?fHC_GetErrHCModel@@YAXAAUtMyString@@@Z'; // returns error description
procedure fHC_ClearErrHCModel(); cdecl; external 'HC.DLL' name '?fHC_ClearErrHCModel@@YAXXZ'; // clears error description

function fHC_Create1D(var m: tHCModel; west, width, temperature: float; materialIdx: integer; heatSrc: float): integer; cdecl; external 'HC.DLL' name '?fHC_Create1D@@YAHAAUtHCModel@@NNNHN@Z'; // creates 1D HC model in cartesian coordinates; 0=OK
function fHC_Create2D(var m: tHCModel; west, width, north, height, temperature: float; materialIdx: integer; heatSrc: float): integer; cdecl; external 'HC.DLL' name '?fHC_Create2D@@YAHAAUtHCModel@@NNNNNHN@Z'; // creates 2D HC model in cartesian coordinates; 0=OK
function fHC_Create3D(var m: tHCModel; west, width, north, height, front, depth, temperature: float; materialIdx: integer; heatSrc: float): integer; cdecl; external 'HC.DLL' name '?fHC_Create3D@@YAHAAUtHCModel@@NNNNNNNHN@Z'; // creates 3D HC model in cartesian coordinates; 0=OK

function fHC_CreateCylinder1D(var m: tHCModel; innerRadius, outerRadius, temperature: float; materialIdx: integer; heatSrc: float): integer; cdecl; external 'HC.DLL' name '?fHC_CreateCylinder1D@@YAHAAUtHCModel@@NNNHN@Z'; // creates 1D HC model in cylindrical coordinates, innerRadius=west, outerRadius=east; 0=OK
function fHC_CreateCylinder2D(var m: tHCModel; innerRadius, outerRadius, north, height, temperature: float; materialIdx: integer; heatSrc: float): integer; cdecl; external 'HC.DLL' name '?fHC_CreateCylinder2D@@YAHAAUtHCModel@@NNNNNHN@Z'; // creates 2D HC model in cylindrical coordinates, innerRadius=west, outerRadius=east; 0=OK

function fHC_InsertX(var m: tHCModel; idx, count: integer): integer; cdecl; external 'HC.DLL' name '?fHC_InsertX@@YAHAAUtHCModel@@HH@Z'; // return 0=OK; idx~~vBorder, for "vT,..." idx~~vBorder+1; inserted volumes are undefined
function fHC_InsertY(var m: tHCModel; idx, count: integer): integer; cdecl; external 'HC.DLL' name '?fHC_InsertY@@YAHAAUtHCModel@@HH@Z'; // return 0=OK; idx~~vBorder, for "vT,..." idx~~vBorder+1; inserted volumes are undefined
function fHC_InsertZ(var m: tHCModel; idx, count: integer): integer; cdecl; external 'HC.DLL' name '?fHC_InsertZ@@YAHAAUtHCModel@@HH@Z'; // return 0=OK; idx~~vBorder, for "vT,..." idx~~vBorder+1; inserted volumes are undefined

function fHC_DeleteX(var m: tHCModel; idx, count: integer): integer; cdecl; external 'HC.DLL' name '?fHC_DeleteX@@YAHAAUtHCModel@@HH@Z'; // return 0=OK; idx~~vBorder, for "vT,..." idx~~vBorder+1;
function fHC_DeleteY(var m: tHCModel; idx, count: integer): integer; cdecl; external 'HC.DLL' name '?fHC_DeleteY@@YAHAAUtHCModel@@HH@Z'; // return 0=OK; idx~~vBorder, for "vT,..." idx~~vBorder+1; temperature interpolation is not implemented
function fHC_DeleteZ(var m: tHCModel; idx, count: integer): integer; cdecl; external 'HC.DLL' name '?fHC_DeleteZ@@YAHAAUtHCModel@@HH@Z'; // return 0=OK; idx~~vBorder, for "vT,..." idx~~vBorder+1; temperature interpolation is not implemented

function fHC_FindBorderIdxL(var b: tFloatArray; pos: float): integer; cdecl; external 'HC.DLL' name '?fHC_FindBorderIdxL@@YAHAAUtRealArray@@N@Z'; // return -2=Error;
function fHC_FindBorderIdxR(var b: tFloatArray; pos: float): integer; cdecl; external 'HC.DLL' name '?fHC_FindBorderIdxR@@YAHAAUtRealArray@@N@Z'; // return -2=Error;

function fHC_InsBorderX(var m: tHCModel; pos: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsBorderX@@YAHAAUtHCModel@@N@Z'; // return 0=OK; Inserts border
function fHC_InsBorderY(var m: tHCModel; pos: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsBorderY@@YAHAAUtHCModel@@N@Z'; // return 0=OK; Inserts border
function fHC_InsBorderZ(var m: tHCModel; pos: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsBorderZ@@YAHAAUtHCModel@@N@Z'; // return 0=OK; Inserts border

function fHC_DivVolX(var m: tHCModel; idx, count: integer): integer; cdecl; external 'HC.DLL' name '?fHC_DivVolX@@YAHAAUtHCModel@@HH@Z'; // return 0=OK; Divides volume
function fHC_DivVolY(var m: tHCModel; idx, count: integer): integer; cdecl; external 'HC.DLL' name '?fHC_DivVolY@@YAHAAUtHCModel@@HH@Z'; // return 0=OK; Divides volume
function fHC_DivVolZ(var m: tHCModel; idx, count: integer): integer; cdecl; external 'HC.DLL' name '?fHC_DivVolZ@@YAHAAUtHCModel@@HH@Z'; // return 0=OK; Divides volume

function fHC_ReduceVolSizeX(var m: tHCModel; maxVolSize: float; idx1, idx2: integer): integer; cdecl; external 'HC.DLL' name '?fHC_ReduceVolSizeX@@YAHAAUtHCModel@@NHH@Z'; // return 0=OK;
function fHC_ReduceVolSizeY(var m: tHCModel; maxVolSize: float; idx1, idx2: integer): integer; cdecl; external 'HC.DLL' name '?fHC_ReduceVolSizeY@@YAHAAUtHCModel@@NHH@Z'; // return 0=OK;
function fHC_ReduceVolSizeZ(var m: tHCModel; maxVolSize: float; idx1, idx2: integer): integer; cdecl; external 'HC.DLL' name '?fHC_ReduceVolSizeZ@@YAHAAUtHCModel@@NHH@Z'; // return 0=OK;

function fHC_Comp2MatX(var m: tHCModel; idx1, idx2: integer): integer; cdecl; external 'HC.DLL' name '?fHC_Comp2MatX@@YAHAAUtHCModel@@HH@Z'; // return 0=not equal; -1=all equal; compares two adjoining material planes (1D, 2D, 3D)
function fHC_Comp2MatY(var m: tHCModel; idx1, idx2: integer): integer; cdecl; external 'HC.DLL' name '?fHC_Comp2MatY@@YAHAAUtHCModel@@HH@Z'; // return 0=not equal; -1=all equal; compares two adjoining material planes (1D, 2D, 3D)
function fHC_Comp2MatZ(var m: tHCModel; idx1, idx2: integer): integer; cdecl; external 'HC.DLL' name '?fHC_Comp2MatZ@@YAHAAUtHCModel@@HH@Z'; // return 0=not equal; -1=all equal; compares two adjoining material planes (1D, 2D, 3D)

function fHC_DelSmallVolX(var m: tHCModel; minVolSize: float): integer; cdecl; external 'HC.DLL' name '?fHC_DelSmallVolX@@YAHAAUtHCModel@@N@Z'; // return 0=OK;
function fHC_DelSmallVolY(var m: tHCModel; minVolSize: float): integer; cdecl; external 'HC.DLL' name '?fHC_DelSmallVolY@@YAHAAUtHCModel@@N@Z'; // return 0=OK;
function fHC_DelSmallVolZ(var m: tHCModel; minVolSize: float): integer; cdecl; external 'HC.DLL' name '?fHC_DelSmallVolZ@@YAHAAUtHCModel@@N@Z'; // return 0=OK;

function fHC_InsertPlaneX(var m: tHCModel; x1, x2, t: float; mat: integer; src: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsertPlaneX@@YAHAAUtHCModel@@NNNHN@Z'; // return 0=OK;
function fHC_InsertPlaneY(var m: tHCModel; y1, y2, t: float; mat: integer; src: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsertPlaneY@@YAHAAUtHCModel@@NNNHN@Z'; // return 0=OK;
function fHC_InsertPlaneZ(var m: tHCModel; z1, z2, t: float; mat: integer; src: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsertPlaneZ@@YAHAAUtHCModel@@NNNHN@Z'; // return 0=OK;

function fHC_InsertBox(var m: tHCModel; x1, x2, y1, y2, z1, z2, t: float; mat: integer; src: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsertBox@@YAHAAUtHCModel@@NNNNNNNHN@Z'; // return 0=OK;
function fHC_InsertCylinderX(var m: tHCModel; x1, x2, y1, y2, z1, z2, maxVolSize, t: float; mat: integer; src: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsertCylinderX@@YAHAAUtHCModel@@NNNNNNNNHN@Z'; // return 0=OK;
function fHC_InsertCylinderY(var m: tHCModel; x1, x2, y1, y2, z1, z2, maxVolSize, t: float; mat: integer; src: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsertCylinderY@@YAHAAUtHCModel@@NNNNNNNNHN@Z'; // return 0=OK;
function fHC_InsertCylinderZ(var m: tHCModel; x1, x2, y1, y2, z1, z2, maxVolSize, t: float; mat: integer; src: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsertCylinderZ@@YAHAAUtHCModel@@NNNNNNNNHN@Z'; // return 0=OK;
function fHC_InsertSphere(var m: tHCModel; x1, x2, y1, y2, z1, z2, maxVolSize, t: float; mat: integer; src: float): integer; cdecl; external 'HC.DLL' name '?fHC_InsertSphere@@YAHAAUtHCModel@@NNNNNNNNHN@Z'; // return 0=OK;

// boundary
function fHC_SetBoundaryIntWE(var m: tHCModel; var a: tIntArray; value: integer): integer; cdecl; external 'HC.DLL' name '?fHC_SetBoundaryIntWE@@YAHAAUtHCModel@@AAUtIntArray@@H@Z'; // return 0=OK; for b#_kind: 1=Ts, 2=q, 3=HTC; 1D, 2D, 3D model
function fHC_SetBoundaryIntNS(var m: tHCModel; var a: tIntArray; value: integer): integer; cdecl; external 'HC.DLL' name '?fHC_SetBoundaryIntNS@@YAHAAUtHCModel@@AAUtIntArray@@H@Z'; // return 0=OK; for b#_kind: 1=Ts, 2=q, 3=HTC; 2D and 3D model only
function fHC_SetBoundaryIntFB(var m: tHCModel; var a: tIntArray; value: integer): integer; cdecl; external 'HC.DLL' name '?fHC_SetBoundaryIntFB@@YAHAAUtHCModel@@AAUtIntArray@@H@Z'; // return 0=OK; for b#_kind: 1=Ts, 2=q, 3=HTC; 3D model only

function fHC_SetBoundaryFloatWE(var m: tHCModel; var a: tFloatArray; value: float): integer; cdecl; external 'HC.DLL' name '?fHC_SetBoundaryRealWE@@YAHAAUtHCModel@@AAUtRealArray@@N@Z'; // return 0=OK; 1D, 2D, 3D model
function fHC_SetBoundaryFloatNS(var m: tHCModel; var a: tFloatArray; value: float): integer; cdecl; external 'HC.DLL' name '?fHC_SetBoundaryRealNS@@YAHAAUtHCModel@@AAUtRealArray@@N@Z'; // return 0=OK; 2D and 3D model only
function fHC_SetBoundaryFloatFB(var m: tHCModel; var a: tFloatArray; value: float): integer; cdecl; external 'HC.DLL' name '?fHC_SetBoundaryRealFB@@YAHAAUtHCModel@@AAUtRealArray@@N@Z'; // return 0=OK; 3D model only

// model preparing
procedure fHC_FixBorders(var m: tHCModel); cdecl; external 'HC.DLL' name '?fHC_FixBorders@@YAXAAUtHCModel@@@Z'; // vT, vMat, vSrc
function fHC_PrepareNodePos(var m: tHCModel): integer; cdecl; external 'HC.DLL' name '?fHC_PrepareNodePos@@YAHAAUtHCModel@@@Z'; // return 0=OK; prepares m.tempNode# and nB##
function fHC_PrepareModel(var m: tHCModel): integer; cdecl; external 'HC.DLL' name '?fHC_PrepareModel@@YAHAAUtHCModel@@@Z'; // return 0=OK; prepares model for computation; required by some functions; does not allocate accuracy#;

// advanced functions
function fHC_GetInterpValHCModel(var r: tFloatArray; var m: tHCModel; x, y, z: float): float; cdecl; external 'HC.DLL' name '?fHC_GetInterpVal@@YANAAUtRealArray@@AAUtHCModel@@NNN@Z'; // fHC_PrepareNodePos or fHC_PrepareModel is required
function fHC_GetMaxDiffFloat(var a: tFloatArray; var b: tFloatArray; var idx: integer): float; cdecl; external 'HC.DLL' name '?fHC_GetMaxDiff@@YANAAUtRealArray@@0AAH@Z'; // idx returns index of max diff;
function fHC_ClearAccuracy(var m: tHCModel): integer; cdecl; external 'HC.DLL' name '?fHC_ClearAccuracy@@YAHAAUtHCModel@@@Z'; // return 0=OK; fHC_PrepareModel is required
function fHC_ClearAccuracyFast(var m: tHCModel): integer; cdecl; external 'HC.DLL' name '?fHC_ClearAccuracyFast@@YAHAAUtHCModel@@@Z'; // return 0=OK; fHC_PrepareModel is required
function fHC_Accuracy(var m: tHCModel): integer; cdecl; external 'HC.DLL' name '?fHC_Accuracy@@YAHAAUtHCModel@@@Z'; // return 0=OK; fHC_PrepareModel and fHC_ClearAccuracy is required; computes accuracy; old values are overwritten
function fHC_AccuracyAdd(var m: tHCModel; dt: float): integer; cdecl; external 'HC.DLL' name '?fHC_AccuracyAdd@@YAHAAUtHCModel@@N@Z';  // return 0=OK; dt=time step; computes accuracy K/s; fHC_PrepareModel and fHC_ClearAccuracy is required; computes accuracy; new values are added
function fHC_AccuracyMax(var m: tHCModel): integer; cdecl; external 'HC.DLL' name '?fHC_AccuracyMax@@YAHAAUtHCModel@@@Z'; // return 0=OK; fHC_PrepareModel and fHC_ClearAccuracy is required; _MAX(old, new) is applied
function fHC_AccuracyFast(var m: tHCModel): integer; cdecl; external 'HC.DLL' name '?fHC_AccuracyFast@@YAHAAUtHCModel@@@Z'; // return 0=OK; fHC_PrepareModel and fHC_ClearAccuracyFast is required; computes accuracy; old values are overwritten

function fHC_RefineModel(var m: tHCModel; dTmax, minXsize, minYsize, minZsize: float; maxDivCountX, maxDivCountY, maxDivCountZ: integer): integer; cdecl; external 'HC.DLL' name '?fHC_RefineModel@@YAHAAUtHCModel@@NNNNHHH@Z'; //  return 0-no change, 1-changed, return<0-error; m.accuracy# is destroyed if change is made

function fHC_SaveFloatChart4D_HCModel(var m: tHCModel; var a: tFloatArray; const name: PChar; offset: float=0; dimUnit: float=1): integer; cdecl; external 'HC.DLL' name '?fHC_SaveRealChart4D@@YAHAAUtHCModel@@AAUtRealArray@@QBDNN@Z';
function fHC_SaveIntChart4D_HCModel(var m: tHCModel; var a: tIntArray; const name: PChar; offset: integer=0; dimUnit: float=1): integer; cdecl; external 'HC.DLL' name '?fHC_SaveIntChart4D@@YAHAAUtHCModel@@AAUtIntArray@@QBDHN@Z';
function fHC_SaveFloatIntChart4D(var m: tHCModel; var a: tFloatArray; var aInt: tIntArray; const name: PChar; offset: float=0; offsetInt: integer=0; dimUnit: float=1): integer; cdecl; external 'HC.DLL' name '?fHC_SaveRealIntChart4D@@YAHAAUtHCModel@@AAUtRealArray@@AAUtIntArray@@QBDNHN@Z';
function fHC_SaveFloatFloatChart4D(var m: tHCModel; var a1, a2: tFloatArray; const name: PChar; offset1: float=0; offset2: float=0; dimUnit: float=1): integer; cdecl; external 'HC.DLL' name '?fHC_SaveRealRealChart4D@@YAHAAUtHCModel@@AAUtRealArray@@1QBDNNN@Z';

implementation

end.

