Skip to content

Commit

Permalink
Added callct2 utility function to handle all string outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ssun30 authored and speth committed May 11, 2023
1 parent 94f556c commit b980351
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
10 changes: 2 additions & 8 deletions interfaces/matlab_experimental/1D/Domain1D.m
Expand Up @@ -222,14 +222,8 @@ function clear(d)
s = cell(m);
for i = 1:n
id = index(i)-1;
buflen = callct('domain_componentName', ...
d.domainID, id, 0, 0);
if buflen > 0
aa = char(zeros(1, buflen));
[out_buf, aa] = callct('domain_componentName', ...
d.domainID, id, buflen, aa);
s{i} = aa;
end
output = callct2('domain_componentName', d.domainID, id);
s{i} = output;
end
end

Expand Down
11 changes: 3 additions & 8 deletions interfaces/matlab_experimental/Base/Kinetics.m
Expand Up @@ -327,14 +327,9 @@ function kinClear(kin)
rxn = cell(m, n);
for i = 1:m
for j = 1:n
buflen = callct('kin_getReactionString', kin.kinID, ...
irxn - 1, 0, '');
if buflen > 0
aa = char(zeros(1, buflen));
[~, aa] = callct('kin_getReactionString', ...
kin.kinID, irxn - 1, buflen, aa);
rxn{i, j} = aa;
end
output = callct2('kin_getReactionString', kin.kinID, ...
irxn - 1);
rxn{i, j} = output;
end
end
end
Expand Down
29 changes: 10 additions & 19 deletions interfaces/matlab_experimental/Base/ThermoPhase.m
Expand Up @@ -94,10 +94,15 @@ function display(tp, threshold)
threshold = 1e-14;
end
buflen = 0 - callct('thermo_report', tp.tpID, 0, '', 1);
aa = char([zeros(1, buflen, 'int8')]);
aa = char(ones(1, buflen));
ptr = libpointer('cstring', aa);
[iok, bb] = callct('thermo_report', tp.tpID, buflen, ptr, 1);
disp(bb);
if iok < 0
error(geterr);
else
disp(bb);
end
clear aa bb ptr
end

function tpClear(tp)
Expand Down Expand Up @@ -497,14 +502,8 @@ function tpClear(tp)
for i = 1:m
for j = 1:n
ksp = k(i, j) - 1;
buflen = callct('thermo_getSpeciesName', ...
tp.tpID, ksp, 0, '');
if buflen > 0
aa = char(zeros(1, buflen));
[~, aa] = callct('thermo_getSpeciesName', ...
tp.tpID, ksp, buflen, aa);
nm{i, j} = aa;
end
output = callct2('thermo_getSpeciesName', tp.tpID, ksp);
nm{i, j} = aa;
end
end
end
Expand Down Expand Up @@ -826,15 +825,7 @@ function tpClear(tp)
% :return:
% An string identifying the equation of state.
%
buflen = callct('thermo_getEosType', tp.tpID, 0, '');
if buflen > 0
aa = char([zeros(1, buflen, 'int8')]);
ptr = libpointer('cstring', aa);
[~, bb] = callct('thermo_getEosType', ...
tp.tpID, buflen, ptr);
e = bb;
clear aa bb ptr;
end
e = callct2('thermo_getEosType', tp.tpID);
end

function v = isIdealGas(tp)
Expand Down
13 changes: 7 additions & 6 deletions interfaces/matlab_experimental/Utility/callct.m
@@ -1,16 +1,17 @@
function output = callct(varargin)
% CALLCT
% This is a simplified single output variant
% Calls Cantera library functions with single outputs and returns
% errors if necessary.

err1 = -1;
err2 = -999.999;
err3 = double(intmax('uint64'));

methodname = varargin{1};
output = calllib(ct, methodname, varargin{2:end});
output = calllib(ct, varargin);
if output == err2 || output == err3
output = -1;
output = err1;
end
if output == -1
geterr;
if output == err1
error(geterr);
end
end
21 changes: 21 additions & 0 deletions interfaces/matlab_experimental/Utility/callct2.m
@@ -0,0 +1,21 @@
function output = callct2(varargin)
% CALLCT2
% Calls Cantera library functions with string outputs and returns
% errors if necessary.

err1 = -1;

buflen = calllib(ct, varargin, 0, '');
if buflen > 0
aa = char(ones(1, buflen));
ptr = libpointer('cstring', aa);
[iok, bb] = calllib(ct, varargin, buflen, ptr);
output = bb;
clear aa bb ptr;
else
error(geterr);
end
if iok == -err1
error(geterr);
end
end
6 changes: 3 additions & 3 deletions interfaces/matlab_experimental/Utility/geterr.m
Expand Up @@ -4,9 +4,9 @@
checklib;
try
buflen = calllib(ct, 'ct_getCanteraError', 0, '');
aa = zeros(1, buflen+1, 'int8');
ptr = libpointer('voidPtr', aa);
[~, bb] = calllib(ct, 'ct_getCanteraError', buflen, ptr);
aa = char(ones(1, buflen));
ptr = libpointer('cstring', aa);
[iok, bb] = calllib(ct, 'ct_getCanteraError', buflen, ptr);
e = bb;
clear aa bb ptr
catch ME
Expand Down

0 comments on commit b980351

Please sign in to comment.