0
$\begingroup$

I am trying to call a .dll file via LibraryFunctionLoad where the .dll file is placed in "C:\ProgramData\Mathematica\SystemFiles\LibraryResources\Windows-\ x86-64\CoolProp.dll".

When I call this library for an individual or a small number of calls, it works. I tried with Map, ParallelMap, multiple kernels (LaunchKernels[8]), Table, ParallelTable.

I got the following messages for both cases for mapping 5000 data.

I got several forms of "-- Message text not found -- ($Failed)".

I attached the screenshot of some of them.

I have tried 2 ways with the LibraryFucntionLoad (1) in the module of the function and (2) using SetSharedFunction.

I tried FindRoot, NMinimize, FindMinimum for searching the root. All cases work perfectly fine for data less than 1000 points. But I got the same type of error for points larger than 2000.

Here are my latest codes. I don't know how to upload .dll file here

ClearAll["Global`*"];
Needs["Utilities`CleanSlate`"];
CleanSlate[];

CloseKernels[]
LaunchKernels[8];
$KernelCount


PropsSI =LibraryFunctionLoad["CoolProp", "PropsSI", {"UTF8String", "UTF8String", Real, "UTF8String", Real, "UTF8String"}, Real];
SetSharedFunction[PropsSI];

(* Mass fraction to mole fraction *)
mOlFracFromMassfracMixRefprop[pHi_?ListQ, rEf_?ListQ] := Module[{p = 101325., rEfRefP, mMassMix, y},
(* Check whether the mass fraction and the refrigerants have the same length *)
If[(Length@pHi == Length@rEf && Total@pHi == 1),
rEfRefP = ({StringJoin["REFPROP::", #]} // StringJoin) & /@ rEf;

mMassMix = 1/Total[pHi/(PropsSI["M", "P", p, "Q", 1, #] & /@ rEfRefP)];
y = mMassMix*pHi/(PropsSI["M", "P", p, "Q", 1, #] & /@ rEfRefP)

,

Print["Size or Fraction Error"]
]
];
SetSharedFunction[mOlFracFromMassfracMixRefprop];

(* Initial guessed pressure values *)
gUessGenRefprop[tEmp_?NumericQ, pHi_?ListQ, rEf_?ListQ ] := 
Module[{(* PropsSI, *)y, rEfMixRefP, pGues},

If[(Length@pHi == Length@rEf && Total@pHi == 1),

y = mOlFracFromMassfracMixRefprop[pHi, rEf]; (* 
mOlFracFromMassfracMixRefprop adds REFPROP:: string *)

rEfMixRefP = {StringJoin["REFPROP::", rEf // First // ToString, 
    "[", y // First // ToString, "]&"], 
   StringJoin[#1 // ToString, "[", #2 // ToString, 
      "]&"] & @@@ ((Take[#, {2, (pHi // Length) - 1}] & /@ {rEf, 
         y}) // Transpose), 
   StringJoin[rEf // Last // ToString, "[", y // Last // ToString,
     "]"]} // StringJoin;

pGues = PropsSI["P", "T", tEmp, "Q", 0, rEfMixRefP]

,

Print["Size or Fraction Error"]
]

];
SetSharedFunction[gUessGenRefprop];

(* Function to calculate the Bubble & Dew point temperature using the Pressure *)
tBubDewMixPhiRefprop[pRs_?NumericQ, pHi_?ListQ, rEf_?ListQ] := 
Module[{rEfMixRefP, y, tBubDew},


If[(Length@pHi == Length@rEf && Total@pHi == 1),

y = mOlFracFromMassfracMixRefprop[pHi, rEf]; (* 
mOlFracFromMassfracMixRefprop adds REFPROP:: string *)

rEfMixRefP = {StringJoin["REFPROP::", rEf // First // ToString, 
    "[", y // First // ToString, "]&"], 
   StringJoin[#1 // ToString, "[", #2 // ToString, 
      "]&"] & @@@ ((Take[#, {2, (pHi // Length) - 1}] & /@ {rEf, 
         y}) // Transpose), 
   StringJoin[rEf // Last // ToString, "[", y // Last // ToString,
     "]"]} // StringJoin;


tBubDew = {PropsSI["T", "P", pRs, "Q", 0, rEfMixRefP], 
  PropsSI["T", "P", pRs, "Q", 1, rEfMixRefP]}
,
Print["Size or Fraction Error"]
]
];
SetSharedFunction[tBubDewMixPhiRefprop];

(* Objective function for using Mathematica function *)
oBjFuncTbulkRefprop[pBulk_?NumericQ, tTarget_?NumericQ, pHi_?ListQ, 
rEf_?ListQ] := Module[{oBjFunc},

oBjFunc = (tTarget - Mean[tBubDewMixPhiRefprop[pBulk, pHi, rEf]])(* 
tBubDewMixPhiRefprop adds REFPROP:: string as necessary *)
];
SetSharedFunction[oBjFuncTbulkRefprop];


(* Local minimum using guessed pressured as an input *)
ClearAll[lOcalMinPguessRefprop];
lOcalMinPguessRefprop[{pStart_?NumericQ, tTarget_?NumericQ, 
pHi_?ListQ, rEf_?ListQ}] := 
Module[{$PerformanceGoal = "Speed", y, rEfMixRefP, rEfRefP, pMin, 
pMax, xIni, pCal},

pCal = 
a /. FindRoot[
   oBjFuncTbulkRefprop[a, tTarget, pHi, rEf], {a, pStart}] // Quiet

];
SetSharedFunction[lOcalMinPguessRefprop];

(* Generation of mass fraction input *)
mFrTernyR32Yf744 = 
Select[Tuples[{Range[0, 1, 0.01], Range[0, 1, 0.01], 
 Range[0, 1, 0.01]}], Total[#] == 1 &];

nLoopTernyR32Yf744 = mFrTernyR32Yf744 // Length

(* Input parameters for the calculation of ideal cycle analysis *)
iNputTernyR32Yf744 := {tEva -> 270.15, tCon -> 303.15, tSup -> 3., 
tSub -> 0., pHiRef -> {0.02, 0.23, 0.75}, 
rEfMixT -> {"CO2", "R32", "R1234yf"}}

(* Initial pressure for Evaporator *)
pIniEvapTernyR32Yf744 = 
Map[gUessGenRefprop[tEva /. iNputTernyR32Yf744, #, 
rEfMixT /. iNputTernyR32Yf744] &, mFrTernyR32Yf744]

(* Calculating the satuation pressure at the evaporator *)
pEvapTernyR32Yf744 = Table[(Pause[1]; 
pSatZeoMixPhiTempRefprop[pIniEvapTernyR32Yf744[[i]], 
tEva /. iNputTernyR32Yf744, mFrTernyR32Yf744[[i]], 
rEfMixT /. iNputTernyR32Yf744]), {i, 1, nLoopTernyR32Yf744}]


LibraryFunctionUnload[PropsSI]

enter image description here

enter image description here

I have been trying to sort it out almost two weeks already and still can't figure it out.

Thank you for your help.

$\endgroup$
  • 1
    $\begingroup$ Show a minimal example please. Find the smallest piece of code that triggers the error and show that. If you want to use a LibraryFunction with parallelization, then do not use SetSharedFunction. Load the LibraryFunction on each subkernel instead. $\endgroup$ – Szabolcs Nov 19 '18 at 8:19
  • $\begingroup$ Thank you for your reply. I am now running the code to trigger the error. Meanwhile, can you tell me how to load libraryfunction on each subkarnel? $\endgroup$ – yaykhel Nov 19 '18 at 8:48
  • 1
    $\begingroup$ Look up ParallelEvaluate. $\endgroup$ – Szabolcs Nov 19 '18 at 8:55
  • $\begingroup$ Thank you for your suggestion. I manage to execute parallel kernels. $\endgroup$ – yaykhel Nov 19 '18 at 13:41
1
$\begingroup$

I don't know what is the exact problem with the previous codes. In this version, I have avoided repeated calling of the functions with StringJoin. The following code doesn't show the error message I got previously. Thanks to Szabolcs, I now use parallel execution which makes the program very fast.

The following is the functions.

pSatZeoMixRefprop[tTarget_?NumericQ, pHi_?ListQ, rEf_?ListQ] :=Module[{PropsSI =LibraryFunctionLoad["CoolProp", 
  "PropsSI", {"UTF8String", "UTF8String", Real, "UTF8String", 
   Real, "UTF8String"}, Real], rEfRefP, mMassMix, y, p = 101325., 
rEfMixRefP, rEfMixRef, pStart, pCal, pTdewBub},
(*Check whether the mass fraction and the refrigerants have the same length*)
If[(Length@pHi == Length@rEf && Total@pHi == 1),

rEfRefP = ({StringJoin["REFPROP::", #]} // StringJoin) & /@ rEf;
mMassMix = 
 1/Total[pHi/(PropsSI["M", "P", p, "Q", 1, #] & /@ rEfRefP)];
y = mMassMix*pHi/(PropsSI["M", "P", p, "Q", 1, #] & /@ rEfRefP);

rEfMixRefP = {StringJoin["REFPROP::", rEf // First // ToString, 
    "[", y // First // ToString, "]&"], 
   StringJoin[#1 // ToString, "[", #2 // ToString, 
      "]&"] & @@@ ((Take[#, {2, (pHi // Length) - 1}] & /@ {rEf, 
         y}) // Transpose), 
   StringJoin[rEf // Last // ToString, "[", y // Last // ToString,
     "]"]} // StringJoin;


pStart = PropsSI["P", "T", tTarget, "Q", 0, rEfMixRefP];

pCal = a /. (FindRoot[(tTarget - Mean[{
          PropsSI["T", "P", a, "Q", 0, rEfMixRefP], 
          PropsSI["T", "P", a, "Q", 1, rEfMixRefP]}])^2, {a, 
      pStart}] // Quiet);

pTdewBub = {pCal, PropsSI["T", "P", pCal, "Q", 0, rEfMixRefP], 
  PropsSI["T", "P", pCal, "Q", 1, rEfMixRefP]} 

,
Print["Size or Fraction Error"]
]

];

Parallelize@Map[pSatZeoMixRefprop[270.15, #, {"CO2", "R32", "R1234yf"}] &, Parallelize@Select[Tuples[{Range[0, 1, 0.1], Range[0, 1, 0.1], Range[0, 1, 0.1]}], Total[#] == 1 &]]

The above way solve the problem, but I don't know what caused the problem.

Thanks

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.