Skip to content

Commit

Permalink
[clib/MATLAB] Expose zeroD object names
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Aug 10, 2024
1 parent 0ae9066 commit 023956d
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 12 deletions.
14 changes: 11 additions & 3 deletions include/cantera/clib/ctreactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ extern "C" {

CANTERA_CAPI int reactor_new(const char* type, int n, const char* name);
CANTERA_CAPI int reactor_del(int i);
CANTERA_CAPI int reactor_name(int i, int len, char* nbuf);
CANTERA_CAPI int reactor_setName(int i, const char* name);
CANTERA_CAPI int reactor_setInitialVolume(int i, double v);
CANTERA_CAPI int reactor_setChemistry(int i, int cflag);
CANTERA_CAPI int reactor_setEnergy(int i, int eflag);
Expand Down Expand Up @@ -46,8 +48,10 @@ extern "C" {
CANTERA_CAPI double reactornet_atol(int i);
CANTERA_CAPI double reactornet_sensitivity(int i, const char* v, int p, int r);

CANTERA_CAPI int flowdev_new(const char* type);
CANTERA_CAPI int flowdev_new(const char* type, const char* name);
CANTERA_CAPI int flowdev_del(int i);
CANTERA_CAPI int flowdev_name(int i, int len, char* nbuf);
CANTERA_CAPI int flowdev_setName(int i, const char* name);
CANTERA_CAPI int flowdev_install(int i, int n, int m);
CANTERA_CAPI int flowdev_setPrimary(int i, int n);
CANTERA_CAPI double flowdev_massFlowRate(int i);
Expand All @@ -57,8 +61,10 @@ extern "C" {
CANTERA_CAPI int flowdev_setPressureFunction(int i, int n);
CANTERA_CAPI int flowdev_setTimeFunction(int i, int n);

CANTERA_CAPI int wall_new(const char* type);
CANTERA_CAPI int wall_new(const char* type, const char* name);
CANTERA_CAPI int wall_del(int i);
CANTERA_CAPI int wall_name(int i, int len, char* nbuf);
CANTERA_CAPI int wall_setName(int i, const char* name);
CANTERA_CAPI int wall_install(int i, int n, int m);
CANTERA_CAPI double wall_expansionRate(int i);
CANTERA_CAPI double wall_heatRate(int i);
Expand All @@ -72,8 +78,10 @@ extern "C" {
CANTERA_CAPI int wall_setEmissivity(int i, double epsilon);
CANTERA_CAPI int wall_ready(int i);

CANTERA_CAPI int reactorsurface_new(int type);
CANTERA_CAPI int reactorsurface_new();
CANTERA_CAPI int reactorsurface_del(int i);
CANTERA_CAPI int reactorsurface_name(int i, int len, char* nbuf);
CANTERA_CAPI int reactorsurface_setName(int i, const char* name);
CANTERA_CAPI int reactorsurface_install(int i, int n);
CANTERA_CAPI int reactorsurface_setkinetics(int i, int n);
CANTERA_CAPI double reactorsurface_area(int i);
Expand Down
17 changes: 15 additions & 2 deletions interfaces/matlab_experimental/Reactor/FlowDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

properties (SetAccess = public)

name % name of flow device.

% Upstream object of type :mat:class:`Reactor` or :mat:class:`Reservoir`.
upstream

Expand All @@ -56,17 +58,20 @@
methods
%% FlowDevice Class Constructor

function x = FlowDevice(typ)
function x = FlowDevice(typ, name)
% Create a :mat:class:`FlowDevice` object.

ctIsLoaded;

if nargin == 0
error('please specify the type of flow device to be created');
end
if nargin < 2
name = '(none)';
end

x.type = typ;
x.id = ctFunc('flowdev_new', typ);
x.id = ctFunc('flowdev_new', typ, name);
x.upstream = -1;
x.downstream = -1;
end
Expand Down Expand Up @@ -112,12 +117,20 @@ function install(f, upstream, downstream)

%% FlowDevice Get Methods

function name = get.name(f)
name = ctString('flowdev_name', f.id);
end

function mdot = get.massFlowRate(f)
mdot = ctFunc('flowdev_massFlowRate2', f.id);
end

%% FlowDevice Set Methods

function set.name(f, name)
ctFunc('flowdev_setName', f.id, name);
end

function set.massFlowRate(f, mdot)

if strcmp(f.type, 'MassFlowController')
Expand Down
10 changes: 10 additions & 0 deletions interfaces/matlab_experimental/Reactor/Reactor.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

properties (SetAccess = public)

name % Name of reactor.

contents

% Density of the reactor contents at the end of the last call to
Expand Down Expand Up @@ -158,6 +160,10 @@ function addSensitivityReaction(r, m)

%% Reactor Get Methods

function name = get.name(r)
name = ctString('reactor_name', r.id);
end

function temperature = get.T(r)
temperature = ctFunc('reactor_temperature', r.id);
end
Expand Down Expand Up @@ -213,6 +219,10 @@ function addSensitivityReaction(r, m)

%% Reactor set methods

function set.name(r, name)
ctFunc('reactor_setName', r.id, name);
end

function set.V(r, v0)

ctFunc('reactor_setInitialVolume', r.id, v0);
Expand Down
17 changes: 15 additions & 2 deletions interfaces/matlab_experimental/Reactor/Wall.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

properties (SetAccess = protected)

name % Name of wall.

left % Reactor on the left.
right % Reactor on the right.

Expand Down Expand Up @@ -87,15 +89,18 @@
methods
%% Wall Class Constructor

function w = Wall(l, r)
function w = Wall(l, r, name)
% Create a :mat:class:`Wall` object.
ctIsLoaded;

% At the moment, only one wall type is implemented
typ = 'Wall';
if nargin < 3
name = '(none)';
end

w.type = char(typ);
w.id = ctFunc('wall_new', w.type);
w.id = ctFunc('wall_new', w.type, name);

% Install the wall between left and right reactors
w.left = l;
Expand Down Expand Up @@ -127,6 +132,10 @@ function delete(w)

%% ReactorNet get methods

function name = get.name(w)
name = ctString('wall_name', w.id);
end

function a = get.area(w)
a = ctFunc('wall_area', w.id);
end
Expand All @@ -141,6 +150,10 @@ function delete(w)

%% ReactorNet set methods

function set.name(w, name)
ctFunc('wall_setName', w.id, name);
end

function set.area(w, a)
ctFunc('wall_setArea', w.id, a);
end
Expand Down
91 changes: 86 additions & 5 deletions src/clib/ctreactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "cantera/thermo/ThermoPhase.h"
#include "cantera/kinetics/Kinetics.h"
#include "cantera/zerodim.h"
#include "cantera/base/stringUtils.h"
#include "clib_utils.h"

using namespace Cantera;
Expand Down Expand Up @@ -59,6 +60,26 @@ extern "C" {
}
}

int reactor_name(int i, int len, char* nbuf)
{
try {
return static_cast<int>(
copyString(ReactorCabinet::item(i).name(), nbuf, len));
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int reactor_setName(int i, const char* name)
{
try {
ReactorCabinet::item(i).setName(name);
return 0;
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int reactor_setInitialVolume(int i, double v)
{
try {
Expand Down Expand Up @@ -349,10 +370,10 @@ extern "C" {

// flow devices

int flowdev_new(const char* type)
int flowdev_new(const char* type, const char* name)
{
try {
return FlowDeviceCabinet::add(newFlowDevice(type));
return FlowDeviceCabinet::add(newFlowDevice(type, name));
} catch (...) {
return handleAllExceptions(-1, ERR);
}
Expand All @@ -368,6 +389,26 @@ extern "C" {
}
}

int flowdev_name(int i, int len, char* nbuf)
{
try {
return static_cast<int>(
copyString(FlowDeviceCabinet::item(i).name(), nbuf, len));
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int flowdev_setName(int i, const char* name)
{
try {
FlowDeviceCabinet::item(i).setName(name);
return 0;
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int flowdev_install(int i, int n, int m)
{
try {
Expand Down Expand Up @@ -455,10 +496,10 @@ extern "C" {

///////////// Walls ///////////////////////

int wall_new(const char* type)
int wall_new(const char* type, const char* name)
{
try {
return WallCabinet::add(newWall(type));
return WallCabinet::add(newWall(type, name));
} catch (...) {
return handleAllExceptions(-1, ERR);
}
Expand All @@ -474,6 +515,26 @@ extern "C" {
}
}

int wall_name(int i, int len, char* nbuf)
{
try {
return static_cast<int>(
copyString(WallCabinet::item(i).name(), nbuf, len));
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int wall_setName(int i, const char* name)
{
try {
WallCabinet::item(i).setName(name);
return 0;
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int wall_install(int i, int n, int m)
{
try {
Expand Down Expand Up @@ -593,7 +654,7 @@ extern "C" {

// ReactorSurface

int reactorsurface_new(int type)
int reactorsurface_new()
{
try {
return ReactorSurfaceCabinet::add(make_shared<ReactorSurface>());
Expand All @@ -612,6 +673,26 @@ extern "C" {
}
}

int reactorsurface_name(int i, int len, char* nbuf)
{
try {
return static_cast<int>(
copyString(ReactorSurfaceCabinet::item(i).name(), nbuf, len));
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int reactorsurface_setName(int i, const char* name)
{
try {
ReactorSurfaceCabinet::item(i).setName(name);
return 0;
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int reactorsurface_install(int i, int n)
{
try {
Expand Down
9 changes: 9 additions & 0 deletions test/clib/test_ctreactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ TEST(ctreactor, reactor_soln)
int sol = soln_newSolution("gri30.yaml", "gri30", "none");
int reactor = reactor_new("IdealGasReactor", sol, "test");
ASSERT_EQ(reactor, 0);

int ret = reactor_setName(reactor, "spam");
ASSERT_EQ(ret, 0);
int buflen = reactor_name(reactor, 0, 0);
char* buf = new char[buflen];
reactor_name(reactor, buflen, buf);
string rName = buf;
ASSERT_EQ(rName, "spam");
delete[] buf;
}

vector<double> T_ctreactor = {
Expand Down

0 comments on commit 023956d

Please sign in to comment.