From 22bdb58f3389f5a4f78e46325adeec95e566bf19 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 9 Mar 2012 23:00:31 +0000 Subject: [PATCH] Added consistent, complete exception handling to C interface functions --- src/clib/clib_defs.h | 4 +- src/clib/ct.cpp | 679 +++++++++++++++++++++++--------------- src/clib/ctbdry.cpp | 75 +++-- src/clib/ctfunc.cpp | 50 ++- src/clib/ctmultiphase.cpp | 249 ++++++++++---- src/clib/ctonedim.cpp | 83 +++-- src/clib/ctreactor.cpp | 535 +++++++++++++++++++++--------- src/clib/ctrpath.cpp | 241 ++++++++++---- src/clib/ctsurf.cpp | 65 ++-- src/clib/ctxml.cpp | 55 +-- src/fortran/fct.cpp | 507 +++++++++++++++++----------- src/fortran/fctxml.cpp | 132 ++++---- 12 files changed, 1765 insertions(+), 910 deletions(-) diff --git a/src/clib/clib_defs.h b/src/clib/clib_defs.h index 12409718b..606e8beb5 100644 --- a/src/clib/clib_defs.h +++ b/src/clib/clib_defs.h @@ -38,8 +38,8 @@ namespace Cantera { //! Exception handler used at language interface boundaries. /*! * When called from a "catch (...)" block, this function will attempt to save - * an error message in global error and return a value indicating the type of - * exception caught. + * an error message in global error stack and return a value indicating the + * type of exception caught. * * @param ctErrorCode Value to return if a CanteraError is caught * @param otherErrorCode Value to return if a different exception is caught diff --git a/src/clib/ct.cpp b/src/clib/ct.cpp index 436a18206..2a08ea044 100644 --- a/src/clib/ct.cpp +++ b/src/clib/ct.cpp @@ -42,56 +42,12 @@ template<> TransportCabinet* TransportCabinet::__storage = 0; #ifdef WITH_PURE_FLUIDS static PureFluidPhase* purefluid(int n) { - try { - ThermoPhase& tp = ThermoCabinet::item(n); - if (tp.eosType() == cPureFluid) { - return dynamic_cast(&tp); - } else { - throw CanteraError("purefluid","object is not a PureFluidPhase object"); - } - } catch (...) { - return handleAllExceptions(0, 0); - } -} - -static double pfprop(int n, int i, double v=0.0, double x=0.0) -{ - PureFluidPhase* p = purefluid(n); + PureFluidPhase* p = dynamic_cast(&ThermoCabinet::item(n)); if (p) { - switch (i) { - case 0: - return p->critTemperature(); - case 1: - return p->critPressure(); - case 2: - return p->critDensity(); - case 3: - return p->vaporFraction(); - case 4: - return p->satTemperature(v); - case 5: - return p->satPressure(v); - case 6: - p->setState_Psat(v, x); - return 0.0; - case 7: - p->setState_Tsat(v, x); - return 0.0; - } + return p; + } else { + throw CanteraError("purefluid","object is not a PureFluidPhase object"); } - return DERR; -} - - -#else - -static ThermoPhase* purefluid(int n) -{ - return th(n); -} -static double pfprop(int n, int i, double v=0.0, double x=0.0) -{ - return DERR; } #endif @@ -102,25 +58,41 @@ extern "C" { int ct_appdelete() { - appdelete(); - return 0; + try { + appdelete(); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } //--------------- Phase ---------------------// size_t phase_nElements(int n) { - return ThermoCabinet::item(n).nElements(); + try { + return ThermoCabinet::item(n).nElements(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t phase_nSpecies(int n) { - return ThermoCabinet::item(n).nSpecies(); + try { + return ThermoCabinet::item(n).nSpecies(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } doublereal phase_temperature(int n) { - return ThermoCabinet::item(n).temperature(); + try { + return ThermoCabinet::item(n).temperature(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int phase_setTemperature(int n, double t) @@ -135,7 +107,11 @@ extern "C" { doublereal phase_density(int n) { - return ThermoCabinet::item(n).density(); + try { + return ThermoCabinet::item(n).density(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int phase_setDensity(int n, double rho) @@ -143,13 +119,21 @@ extern "C" { if (rho < 0.0) { return -1; } - ThermoCabinet::item(n).setDensity(rho); + try { + ThermoCabinet::item(n).setDensity(rho); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } doublereal phase_molarDensity(int n) { - return ThermoCabinet::item(n).molarDensity(); + try { + return ThermoCabinet::item(n).molarDensity(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int phase_setMolarDensity(int n, double ndens) @@ -157,71 +141,107 @@ extern "C" { if (ndens < 0.0) { return -1; } - ThermoCabinet::item(n).setMolarDensity(ndens); + try { + ThermoCabinet::item(n).setMolarDensity(ndens); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } doublereal phase_meanMolecularWeight(int n) { - return ThermoCabinet::item(n).meanMolecularWeight(); + try { + return ThermoCabinet::item(n).meanMolecularWeight(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } size_t phase_elementIndex(int n, char* nm) { - string elnm = string(nm); - return ThermoCabinet::item(n).elementIndex(elnm); + try { + string elnm = string(nm); + return ThermoCabinet::item(n).elementIndex(elnm); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t phase_speciesIndex(int n, char* nm) { - string spnm = string(nm); - return ThermoCabinet::item(n).speciesIndex(spnm); + try { + string spnm = string(nm); + return ThermoCabinet::item(n).speciesIndex(spnm); + } catch (...) { + return handleAllExceptions(npos, npos); + } } int phase_getMoleFractions(int n, size_t lenx, double* x) { - ThermoPhase& p = ThermoCabinet::item(n); - if (lenx >= p.nSpecies()) { - p.getMoleFractions(x); - return 0; - } else { - return -1; + try { + ThermoPhase& p = ThermoCabinet::item(n); + if (lenx >= p.nSpecies()) { + p.getMoleFractions(x); + return 0; + } else { + return -1; + } + } catch (...) { + return handleAllExceptions(-1, ERR); } } doublereal phase_moleFraction(int n, size_t k) { - return ThermoCabinet::item(n).moleFraction(k); + try { + return ThermoCabinet::item(n).moleFraction(k); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int phase_getMassFractions(int n, size_t leny, double* y) { - ThermoPhase& p = ThermoCabinet::item(n); - if (leny >= p.nSpecies()) { - p.getMassFractions(y); - return 0; - } else { - return -1; + try { + ThermoPhase& p = ThermoCabinet::item(n); + if (leny >= p.nSpecies()) { + p.getMassFractions(y); + return 0; + } else { + return -1; + } + } catch (...) { + return handleAllExceptions(-1, ERR); } } doublereal phase_massFraction(int n, size_t k) { - return ThermoCabinet::item(n).massFraction(k); + try { + return ThermoCabinet::item(n).massFraction(k); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int phase_setMoleFractions(int n, size_t lenx, double* x, int norm) { - ThermoPhase& p = ThermoCabinet::item(n); - if (lenx >= p.nSpecies()) { - if (norm) { - p.setMoleFractions(x); + try { + ThermoPhase& p = ThermoCabinet::item(n); + if (lenx >= p.nSpecies()) { + if (norm) { + p.setMoleFractions(x); + } else { + p.setMoleFractions_NoNorm(x); + } + return 0; } else { - p.setMoleFractions_NoNorm(x); + return -1; } - return 0; - } else { - return -1; + } catch (...) { + return handleAllExceptions(-1, ERR); } } @@ -245,16 +265,20 @@ extern "C" { int phase_setMassFractions(int n, size_t leny, double* y, int norm) { - ThermoPhase& p = ThermoCabinet::item(n); - if (leny >= p.nSpecies()) { - if (norm) { - p.setMassFractions(y); + try { + ThermoPhase& p = ThermoCabinet::item(n); + if (leny >= p.nSpecies()) { + if (norm) { + p.setMassFractions(y); + } else { + p.setMassFractions_NoNorm(y); + } + return 0; } else { - p.setMassFractions_NoNorm(y); + return -10; } - return 0; - } else { - return -10; + } catch (...) { + return handleAllExceptions(-1, ERR); } } @@ -275,46 +299,60 @@ extern "C" { } } - int phase_getAtomicWeights(int n, - size_t lenm, double* atw) + int phase_getAtomicWeights(int n, size_t lenm, double* atw) { - ThermoPhase& p = ThermoCabinet::item(n); - if (lenm >= p.nElements()) { - const vector_fp& wt = p.atomicWeights(); - copy(wt.begin(), wt.end(), atw); - return 0; - } else { - return -10; + try { + ThermoPhase& p = ThermoCabinet::item(n); + if (lenm >= p.nElements()) { + const vector_fp& wt = p.atomicWeights(); + copy(wt.begin(), wt.end(), atw); + return 0; + } else { + return -10; + } + } catch (...) { + return handleAllExceptions(-1, ERR); } } - int phase_getMolecularWeights(int n, - size_t lenm, double* mw) + int phase_getMolecularWeights(int n, size_t lenm, double* mw) { - ThermoPhase& p = ThermoCabinet::item(n); - if (lenm >= p.nSpecies()) { - const vector_fp& wt = p.molecularWeights(); - copy(wt.begin(), wt.end(), mw); - return 0; - } else { - return -10; + try { + ThermoPhase& p = ThermoCabinet::item(n); + if (lenm >= p.nSpecies()) { + const vector_fp& wt = p.molecularWeights(); + copy(wt.begin(), wt.end(), mw); + return 0; + } else { + return -10; + } + } catch (...) { + return handleAllExceptions(-1, ERR); } } int phase_getName(int n, size_t lennm, char* nm) { - string name = ThermoCabinet::item(n).name(); - size_t lout = min(lennm, name.size()); - copy(name.c_str(), name.c_str() + lout, nm); - nm[lout] = '\0'; - return 0; + try { + string name = ThermoCabinet::item(n).name(); + size_t lout = min(lennm, name.size()); + copy(name.c_str(), name.c_str() + lout, nm); + nm[lout] = '\0'; + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int phase_setName(int n, const char* nm) { - string name = string(nm); - ThermoCabinet::item(n).setName(name); - return 0; + try { + string name = string(nm); + ThermoCabinet::item(n).setName(name); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int phase_getSpeciesName(int n, size_t k, size_t lennm, char* nm) @@ -363,26 +401,6 @@ extern "C" { } } - // int phase_addSpecies(int n, char* name, int phase, - // int ncomp, doublereal* comp, int thermoType, int ncoeffs, - // double* coeffs, double minTemp, double maxTemp, double refPressure, - // doublereal charge, doublereal weight) { - // try { - // vector_fp cmp(ncomp); - // copy(comp, comp + ncomp, cmp.begin()); - // vector_fp c(ncoeffs); - // copy(coeffs, coeffs + ncoeffs, c.begin()); - // ph(n)->addSpecies(string(name), phase, cmp, - // thermoType, c, minTemp, maxTemp, refPressure, - // charge, weight); - // return 0; - // } - // catch (CanteraError) { return -1; } - // catch (...) {return ERR;} - // } - - - //-------------- Thermo --------------------// size_t newThermoFromXML(int mxml) @@ -398,12 +416,20 @@ extern "C" { size_t th_nSpecies(size_t n) { - return ThermoCabinet::item(n).nSpecies(); + try { + return ThermoCabinet::item(n).nSpecies(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } int th_eosType(int n) { - return ThermoCabinet::item(n).eosType(); + try { + return ThermoCabinet::item(n).eosType(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } double th_enthalpy_mole(int n) @@ -534,26 +560,34 @@ extern "C" { int th_chemPotentials(int n, size_t lenm, double* murt) { - ThermoPhase& thrm = ThermoCabinet::item(n); - size_t nsp = thrm.nSpecies(); - if (lenm >= nsp) { - thrm.getChemPotentials(murt); - return 0; - } else { - return -10; + try { + ThermoPhase& thrm = ThermoCabinet::item(n); + size_t nsp = thrm.nSpecies(); + if (lenm >= nsp) { + thrm.getChemPotentials(murt); + return 0; + } else { + return -10; + } + } catch (...) { + return handleAllExceptions(-1, ERR); } } int th_elementPotentials(int n, size_t lenm, double* lambda) { - ThermoPhase& thrm = ThermoCabinet::item(n); - size_t nel = thrm.nElements(); - if (lenm >= nel) { - equilibrate(thrm, "TP", 0); - thrm.getElementPotentials(lambda); - return 0; - } else { - return -10; + try { + ThermoPhase& thrm = ThermoCabinet::item(n); + size_t nel = thrm.nElements(); + if (lenm >= nel) { + equilibrate(thrm, "TP", 0); + thrm.getElementPotentials(lambda); + return 0; + } else { + return -10; + } + } catch (...) { + return handleAllExceptions(-1, ERR); } } @@ -635,17 +669,29 @@ extern "C" { doublereal th_refPressure(int n) { - return ThermoCabinet::item(n).refPressure(); + try { + return ThermoCabinet::item(n).refPressure(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } doublereal th_minTemp(int n, int k) { - return ThermoCabinet::item(n).minTemp(k); + try { + return ThermoCabinet::item(n).minTemp(k); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } doublereal th_maxTemp(int n, int k) { - return ThermoCabinet::item(n).maxTemp(k); + try { + return ThermoCabinet::item(n).maxTemp(k); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } @@ -699,8 +745,12 @@ extern "C" { int th_setElectricPotential(int n, double v) { - ThermoCabinet::item(n).setElectricPotential(v); - return 0; + try { + ThermoCabinet::item(n).setElectricPotential(v); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } //-------------- pure fluids ---------------// @@ -708,22 +758,38 @@ extern "C" { double th_critTemperature(int n) { - return pfprop(n,0); + try { + return purefluid(n)->critTemperature(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double th_critPressure(int n) { - return purefluid(n)->critPressure(); + try { + return purefluid(n)->critPressure(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double th_critDensity(int n) { - return purefluid(n)->critDensity(); + try { + return purefluid(n)->critDensity(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double th_vaporFraction(int n) { - return purefluid(n)->vaporFraction(); + try { + return purefluid(n)->vaporFraction(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double th_satTemperature(int n, double p) @@ -858,65 +924,110 @@ extern "C" { //------------------------------------- int kin_type(int n) { - return KineticsCabinet::item(n).type(); + try { + return KineticsCabinet::item(n).type(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } size_t kin_start(int n, int p) { - return KineticsCabinet::item(n).kineticsSpeciesIndex(0,p); + try { + return KineticsCabinet::item(n).kineticsSpeciesIndex(0,p); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t kin_speciesIndex(int n, const char* nm, const char* ph) { - return KineticsCabinet::item(n).kineticsSpeciesIndex(string(nm), string(ph)); + try { + return KineticsCabinet::item(n).kineticsSpeciesIndex(string(nm), + string(ph)); + } catch (...) { + return handleAllExceptions(npos, npos); + } } //--------------------------------------- size_t kin_nSpecies(int n) { - return KineticsCabinet::item(n).nTotalSpecies(); + try { + return KineticsCabinet::item(n).nTotalSpecies(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t kin_nReactions(int n) { - return KineticsCabinet::item(n).nReactions(); + try { + return KineticsCabinet::item(n).nReactions(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t kin_nPhases(int n) { - return KineticsCabinet::item(n).nPhases(); + try { + return KineticsCabinet::item(n).nPhases(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t kin_phaseIndex(int n, char* ph) { - return KineticsCabinet::item(n).phaseIndex(string(ph)); + try { + return KineticsCabinet::item(n).phaseIndex(string(ph)); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t kin_reactionPhaseIndex(int n) { - return KineticsCabinet::item(n).reactionPhaseIndex(); + try { + return KineticsCabinet::item(n).reactionPhaseIndex(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } double kin_reactantStoichCoeff(int n, int k, int i) { - return KineticsCabinet::item(n).reactantStoichCoeff(k,i); + try { + return KineticsCabinet::item(n).reactantStoichCoeff(k,i); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double kin_productStoichCoeff(int n, int k, int i) { - return KineticsCabinet::item(n).productStoichCoeff(k,i); + try { + return KineticsCabinet::item(n).productStoichCoeff(k,i); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int kin_reactionType(int n, int i) { - return KineticsCabinet::item(n).reactionType(i); + try { + return KineticsCabinet::item(n).reactionType(i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int kin_getFwdRatesOfProgress(int n, size_t len, double* fwdROP) { - Kinetics& k = KineticsCabinet::item(n); try { + Kinetics& k = KineticsCabinet::item(n); if (len >= k.nReactions()) { k.getFwdRatesOfProgress(fwdROP); return 0; @@ -930,8 +1041,8 @@ extern "C" { int kin_getRevRatesOfProgress(int n, size_t len, double* revROP) { - Kinetics& k = KineticsCabinet::item(n); try { + Kinetics& k = KineticsCabinet::item(n); if (len >= k.nReactions()) { k.getRevRatesOfProgress(revROP); return 0; @@ -945,7 +1056,11 @@ extern "C" { int kin_isReversible(int n, int i) { - return (int) KineticsCabinet::item(n).isReversible(i); + try { + return (int) KineticsCabinet::item(n).isReversible(i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int kin_getNetRatesOfProgress(int n, size_t len, double* netROP) @@ -1134,12 +1249,20 @@ extern "C" { double kin_multiplier(int n, int i) { - return KineticsCabinet::item(n).multiplier(i); + try { + return KineticsCabinet::item(n).multiplier(i); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } size_t kin_phase(int n, size_t i) { - return ThermoCabinet::index(KineticsCabinet::item(n).thermo(i)); + try { + return ThermoCabinet::index(KineticsCabinet::item(n).thermo(i)); + } catch (...) { + return handleAllExceptions(npos, npos); + } } int kin_getEquilibriumConstants(int n, size_t len, double* kc) @@ -1206,9 +1329,9 @@ extern "C" { size_t newTransport(char* model, int ith, int loglevel) { - string mstr = string(model); - ThermoPhase& t = ThermoCabinet::item(ith); try { + string mstr = string(model); + ThermoPhase& t = ThermoCabinet::item(ith); Transport* tr = newTransportMgr(mstr, &t, loglevel); return TransportCabinet::add(tr); } catch (...) { @@ -1310,10 +1433,10 @@ extern "C" { int import_phase(int nth, int nxml, char* id) { - ThermoPhase& thrm = ThermoCabinet::item(nth); - XML_Node& node = XmlCabinet::item(nxml); - string idstr = string(id); try { + ThermoPhase& thrm = ThermoCabinet::item(nth); + XML_Node& node = XmlCabinet::item(nxml); + string idstr = string(id); importPhase(node, &thrm); return 0; } catch (...) { @@ -1324,14 +1447,14 @@ extern "C" { int import_kinetics(int nxml, char* id, int nphases, integer* ith, int nkin) { - vector phases; - for (int i = 0; i < nphases; i++) { - phases.push_back(&ThermoCabinet::item(ith[i])); - } - XML_Node& node = XmlCabinet::item(nxml); - Kinetics& k = KineticsCabinet::item(nkin); - string idstr = string(id); try { + vector phases; + for (int i = 0; i < nphases; i++) { + phases.push_back(&ThermoCabinet::item(ith[i])); + } + XML_Node& node = XmlCabinet::item(nxml); + Kinetics& k = KineticsCabinet::item(nkin); + string idstr = string(id); importKinetics(node, phases, &k); return 0; } catch (...) { @@ -1370,56 +1493,80 @@ extern "C" { int write_HTML_log(char* file) { - write_logfile(string(file)); - return 0; + try { + write_logfile(string(file)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int getCanteraError(int buflen, char* buf) { - string e; - e = lastErrorMessage(); - if (buflen > 0) { - int n = min(static_cast(e.size()), buflen-1); - copy(e.begin(), e.begin() + n, buf); - buf[min(n, buflen-1)] = '\0'; + try { + string e; + e = lastErrorMessage(); + if (buflen > 0) { + int n = min(static_cast(e.size()), buflen-1); + copy(e.begin(), e.begin() + n, buf); + buf[min(n, buflen-1)] = '\0'; + } + return int(e.size()); + } catch (...) { + return handleAllExceptions(-1, ERR); } - return int(e.size()); } int showCanteraErrors() { - showErrors(); - return 0; + try { + showErrors(); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int addCanteraDirectory(size_t buflen, char* buf) { - addDirectory(string(buf)); - return 0; + try { + addDirectory(string(buf)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int setLogWriter(void* logger) { - Logger* logwriter = (Logger*)logger; - setLogger(logwriter); - return 0; + try { + Logger* logwriter = (Logger*)logger; + setLogger(logwriter); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int readlog(int n, char* buf) { - string s; - writelog("function readlog is deprecated!"); - //getlog(s); - int nlog = static_cast(s.size()); - if (n < 0) { - return nlog; + try { + string s; + writelog("function readlog is deprecated!"); + //getlog(s); + int nlog = static_cast(s.size()); + if (n < 0) { + return nlog; + } + int nn = min(n-1, nlog); + copy(s.begin(), s.begin() + nn, + buf); + buf[min(nlog, n-1)] = '\0'; + //clearlog(); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - int nn = min(n-1, nlog); - copy(s.begin(), s.begin() + nn, - buf); - buf[min(nlog, n-1)] = '\0'; - //clearlog(); - return 0; } int clearStorage() @@ -1446,66 +1593,82 @@ extern "C" { int delKinetics(int n) { - KineticsCabinet::del(n); - return 0; + try { + KineticsCabinet::del(n); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int delTransport(int n) { - TransportCabinet::del(n); - return 0; + try { + TransportCabinet::del(n); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int buildSolutionFromXML(char* src, int ixml, char* id, int ith, int ikin) { + try { + XML_Node* root = 0; + if (ixml > 0) { + root = &XmlCabinet::item(ixml); + } - XML_Node* root = 0; - if (ixml > 0) { - root = &XmlCabinet::item(ixml); - } - - ThermoPhase& t = ThermoCabinet::item(ith); - Kinetics& kin = KineticsCabinet::item(ikin); - XML_Node* x, *r=0; - if (root) { - r = &root->root(); - } - x = get_XML_Node(string(src), r); - //x = find_XML(string(src), r, string(id), "", "phase"); - if (!x) { - return false; - } - importPhase(*x, &t); - kin.addPhase(t); - kin.init(); - installReactionArrays(*x, kin, x->id()); - t.setState_TP(300.0, OneAtm); - if (r) { - if (&x->root() != &r->root()) { + ThermoPhase& t = ThermoCabinet::item(ith); + Kinetics& kin = KineticsCabinet::item(ikin); + XML_Node* x, *r=0; + if (root) { + r = &root->root(); + } + x = get_XML_Node(string(src), r); + //x = find_XML(string(src), r, string(id), "", "phase"); + if (!x) { + return false; + } + importPhase(*x, &t); + kin.addPhase(t); + kin.init(); + installReactionArrays(*x, kin, x->id()); + t.setState_TP(300.0, OneAtm); + if (r) { + if (&x->root() != &r->root()) { + delete &x->root(); + } + } else { delete &x->root(); } - } else { - delete &x->root(); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - return 0; } - int ck_to_cti(char* in_file, char* db_file, char* tr_file, char* id_tag, int debug, int validate) { - bool dbg = (debug != 0); - bool val = (validate != 0); - return pip::convert_ck(in_file, db_file, tr_file, id_tag, dbg, val); + try { + bool dbg = (debug != 0); + bool val = (validate != 0); + return pip::convert_ck(in_file, db_file, tr_file, id_tag, dbg, val); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } - int writelogfile(char* logfile) { - write_logfile(string(logfile)); - return 0; + try { + write_logfile(string(logfile)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } - } diff --git a/src/clib/ctbdry.cpp b/src/clib/ctbdry.cpp index cd2b40f66..b2878afe9 100644 --- a/src/clib/ctbdry.cpp +++ b/src/clib/ctbdry.cpp @@ -20,36 +20,48 @@ extern "C" { int bndry_new(int itype) { - Bdry1D* s; - switch (itype) { - case 1: - s = new Inlet1D(); - break; - case 2: - s = new Symm1D(); - break; - case 3: - s = new Surf1D(); - break; - case 4: - s = new ReactingSurf1D(); - break; - default: - return -2; + try { + Bdry1D* s; + switch (itype) { + case 1: + s = new Inlet1D(); + break; + case 2: + s = new Symm1D(); + break; + case 3: + s = new Surf1D(); + break; + case 4: + s = new ReactingSurf1D(); + break; + default: + return -2; + } + int i = BoundaryCabinet::add(s); + return i; + } catch (...) { + return handleAllExceptions(-1, ERR); } - int i = BoundaryCabinet::add(s); - return i; } int bndry_del(int i) { - BoundaryCabinet::del(i); - return 0; + try { + BoundaryCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } double bndry_temperature(int i) { - return BoundaryCabinet::item(i).temperature(); + try { + return BoundaryCabinet::item(i).temperature(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int bndry_settemperature(int i, double t) @@ -65,7 +77,7 @@ extern "C" { double bndry_spreadrate(int i) { try { - return dynamic_cast(&BoundaryCabinet::item(i))->spreadRate(); + return dynamic_cast(BoundaryCabinet::item(i)).spreadRate(); } catch (...) { return Cantera::handleAllExceptions(-1, ERR); } @@ -75,7 +87,7 @@ extern "C" { int bndry_setSpreadRate(int i, double v) { try { - dynamic_cast(&BoundaryCabinet::item(i))->setSpreadRate(v); + dynamic_cast(BoundaryCabinet::item(i)).setSpreadRate(v); } catch (...) { return Cantera::handleAllExceptions(-1, ERR); } @@ -92,10 +104,13 @@ extern "C" { return 0; } - double bndry_mdot(int i) { - return BoundaryCabinet::item(i).mdot(); + try { + return BoundaryCabinet::item(i).mdot(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int bndry_setxin(int i, double* xin) @@ -121,11 +136,11 @@ extern "C" { int surf_setkinetics(int i, int j) { try { - ReactingSurf1D* srf = - dynamic_cast(&BoundaryCabinet::item(i)); - InterfaceKinetics* k = - dynamic_cast(&Cabinet::item(j)); - srf->setKineticsMgr(k); + ReactingSurf1D& srf = + dynamic_cast(BoundaryCabinet::item(i)); + InterfaceKinetics& k = + dynamic_cast(Cabinet::item(j)); + srf.setKineticsMgr(&k); } catch (...) { return Cantera::handleAllExceptions(-1, ERR); } diff --git a/src/clib/ctfunc.cpp b/src/clib/ctfunc.cpp index 73398c053..ab297cc02 100644 --- a/src/clib/ctfunc.cpp +++ b/src/clib/ctfunc.cpp @@ -24,9 +24,9 @@ extern "C" { int func_new(int type, size_t n, size_t lenp, double* params) { - func_t* r=0; - size_t m = lenp; try { + func_t* r=0; + size_t m = lenp; if (type == SinFuncType) { r = new Sin1(params[0]); } else if (type == CosFuncType) { @@ -95,37 +95,61 @@ extern "C" { int func_del(int i) { - FuncCabinet::del(i); - return 0; + try { + FuncCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int func_copy(int i) { - return FuncCabinet::newCopy(i); + try { + return FuncCabinet::newCopy(i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int func_assign(int i, int j) { - return FuncCabinet::assign(i,j); + try { + return FuncCabinet::assign(i,j); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } double func_value(int i, double t) { - return FuncCabinet::item(i).eval(t); + try { + return FuncCabinet::item(i).eval(t); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int func_derivative(int i) { - func_t* r = 0; - r = &FuncCabinet::item(i).derivative(); - return FuncCabinet::add(r); + try { + func_t* r = 0; + r = &FuncCabinet::item(i).derivative(); + return FuncCabinet::add(r); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int func_duplicate(int i) { - func_t* r = 0; - r = &FuncCabinet::item(i).duplicate(); - return FuncCabinet::add(r); + try { + func_t* r = 0; + r = &FuncCabinet::item(i).duplicate(); + return FuncCabinet::add(r); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int func_write(int i, size_t lennm, const char* arg, char* nm) diff --git a/src/clib/ctmultiphase.cpp b/src/clib/ctmultiphase.cpp index dbcf81fb9..b6ba554c6 100644 --- a/src/clib/ctmultiphase.cpp +++ b/src/clib/ctmultiphase.cpp @@ -53,100 +53,151 @@ static bool checkPhase(int i, int n) } } -namespace Cantera -{ -int _equilflag(const char* xy); -} - extern "C" { int mix_new() { - MultiPhase* m = new MultiPhase; - return mixCabinet::add(m); + try { + MultiPhase* m = new MultiPhase; + return mixCabinet::add(m); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int mix_del(int i) { - mixCabinet::del(i); - return 0; + try { + mixCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int mix_copy(int i) { - return mixCabinet::newCopy(i); + try { + return mixCabinet::newCopy(i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int mix_assign(int i, int j) { - return mixCabinet::assign(i,j); + try { + return mixCabinet::assign(i,j); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int mix_addPhase(int i, int j, double moles) { - mixCabinet::item(i).addPhase(&Cabinet::item(j), moles); - return 0; + try { + mixCabinet::item(i).addPhase(&Cabinet::item(j), moles); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int mix_init(int i) { - mixCabinet::item(i).init(); - return 0; + try { + mixCabinet::item(i).init(); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } size_t mix_nElements(int i) { - return mixCabinet::item(i).nElements(); + try { + return mixCabinet::item(i).nElements(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t mix_elementIndex(int i, char* name) { - return mixCabinet::item(i).elementIndex(string(name)); + try { + return mixCabinet::item(i).elementIndex(string(name)); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t mix_nSpecies(int i) { - return mixCabinet::item(i).nSpecies(); + try { + return mixCabinet::item(i).nSpecies(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t mix_speciesIndex(int i, int k, int p) { - return mixCabinet::item(i).speciesIndex(k, p); + try { + return mixCabinet::item(i).speciesIndex(k, p); + } catch (...) { + return handleAllExceptions(npos, npos); + } } doublereal mix_nAtoms(int i, int k, int m) { - bool ok = (checkSpecies(i,k) && checkElement(i,m)); - if (ok) { - return mixCabinet::item(i).nAtoms(k,m); - } else { - return DERR; + try { + bool ok = (checkSpecies(i,k) && checkElement(i,m)); + if (ok) { + return mixCabinet::item(i).nAtoms(k,m); + } else { + return DERR; + } + } catch (...) { + return handleAllExceptions(DERR, DERR); } } size_t mix_nPhases(int i) { - return mixCabinet::item(i).nPhases(); + try { + return mixCabinet::item(i).nPhases(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } doublereal mix_phaseMoles(int i, int n) { - if (!checkPhase(i, n)) { - return DERR; + try { + if (!checkPhase(i, n)) { + return DERR; + } + return mixCabinet::item(i).phaseMoles(n); + } catch (...) { + return handleAllExceptions(DERR, DERR); } - return mixCabinet::item(i).phaseMoles(n); } int mix_setPhaseMoles(int i, int n, double v) { - if (!checkPhase(i, n)) { - return ERR; + try { + if (!checkPhase(i, n)) { + return ERR; + } + if (v < 0.0) { + return -1; + } + mixCabinet::item(i).setPhaseMoles(n, v); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - if (v < 0.0) { - return -1; - } - mixCabinet::item(i).setPhaseMoles(n, v); - return 0; } int mix_setMoles(int i, size_t nlen, double* n) @@ -175,69 +226,109 @@ extern "C" { int mix_setTemperature(int i, double t) { - if (t < 0.0) { - return -1; + try { + if (t < 0.0) { + return -1; + } + mixCabinet::item(i).setTemperature(t); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - mixCabinet::item(i).setTemperature(t); - return 0; } doublereal mix_temperature(int i) { - return mixCabinet::item(i).temperature(); + try { + return mixCabinet::item(i).temperature(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } doublereal mix_minTemp(int i) { - return mixCabinet::item(i).minTemp(); + try { + return mixCabinet::item(i).minTemp(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } doublereal mix_maxTemp(int i) { - return mixCabinet::item(i).maxTemp(); + try { + return mixCabinet::item(i).maxTemp(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } doublereal mix_charge(int i) { - return mixCabinet::item(i).charge(); + try { + return mixCabinet::item(i).charge(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } doublereal mix_phaseCharge(int i, int p) { - if (!checkPhase(i,p)) { - return DERR; + try { + if (!checkPhase(i,p)) { + return DERR; + } + return mixCabinet::item(i).phaseCharge(p); + } catch (...) { + return handleAllExceptions(DERR, DERR); } - return mixCabinet::item(i).phaseCharge(p); } int mix_setPressure(int i, double p) { - if (p < 0.0) { - return -1; + try { + if (p < 0.0) { + return -1; + } + mixCabinet::item(i).setPressure(p); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - mixCabinet::item(i).setPressure(p); - return 0; } doublereal mix_pressure(int i) { - return mixCabinet::item(i).pressure(); + try { + return mixCabinet::item(i).pressure(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } doublereal mix_speciesMoles(int i, int k) { - if (!checkSpecies(i,k)) { - return DERR; + try { + if (!checkSpecies(i,k)) { + return DERR; + } + return mixCabinet::item(i).speciesMoles(k); + } catch (...) { + return handleAllExceptions(DERR, DERR); } - return mixCabinet::item(i).speciesMoles(k); } doublereal mix_elementMoles(int i, int m) { - if (!checkElement(i,m)) { - return DERR; + try { + if (!checkElement(i,m)) { + return DERR; + } + return mixCabinet::item(i).elementMoles(m); + } catch (...) { + return handleAllExceptions(DERR, DERR); } - return mixCabinet::item(i).elementMoles(m); } @@ -291,8 +382,8 @@ extern "C" { int mix_getValidChemPotentials(int i, double bad_mu, int standard, size_t lenmu, double* mu) { - bool st = (standard == 1); try { + bool st = (standard == 1); if (lenmu < mixCabinet::item(i).nSpecies()) { throw CanteraError("getChemPotentials","array too small"); } @@ -305,36 +396,64 @@ extern "C" { double mix_enthalpy(int i) { - return mixCabinet::item(i).enthalpy(); + try { + return mixCabinet::item(i).enthalpy(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double mix_entropy(int i) { - return mixCabinet::item(i).entropy(); + try { + return mixCabinet::item(i).entropy(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double mix_gibbs(int i) { - return mixCabinet::item(i).gibbs(); + try { + return mixCabinet::item(i).gibbs(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double mix_cp(int i) { - return mixCabinet::item(i).cp(); + try { + return mixCabinet::item(i).cp(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double mix_volume(int i) { - return mixCabinet::item(i).volume(); + try { + return mixCabinet::item(i).volume(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } size_t mix_speciesPhaseIndex(int i, int k) { - return mixCabinet::item(i).speciesPhaseIndex(k); + try { + return mixCabinet::item(i).speciesPhaseIndex(k); + } catch (...) { + return handleAllExceptions(npos, npos); + } } double mix_moleFraction(int i, int k) { - return mixCabinet::item(i).moleFraction(k); + try { + return mixCabinet::item(i).moleFraction(k); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } } diff --git a/src/clib/ctonedim.cpp b/src/clib/ctonedim.cpp index db9f845ae..a5068f082 100644 --- a/src/clib/ctonedim.cpp +++ b/src/clib/ctonedim.cpp @@ -59,28 +59,48 @@ extern "C" { int domain_del(int i) { - DomainCabinet::del(i); - return 0; + try { + DomainCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1 , ERR); + } } int domain_type(int i) { - return DomainCabinet::item(i).domainType(); + try { + return DomainCabinet::item(i).domainType(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } size_t domain_index(int i) { - return DomainCabinet::item(i).domainIndex(); + try { + return DomainCabinet::item(i).domainIndex(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t domain_nComponents(int i) { - return DomainCabinet::item(i).nComponents(); + try { + return DomainCabinet::item(i).nComponents(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } size_t domain_nPoints(int i) { - return DomainCabinet::item(i).nPoints(); + try { + return DomainCabinet::item(i).nPoints(); + } catch (...) { + return handleAllExceptions(npos, npos); + } } int domain_componentName(int i, int n, int sz, char* buf) @@ -386,11 +406,11 @@ extern "C" { int stflow_setTransport(int i, int itr, int iSoret) { - bool withSoret = false; - if (iSoret > 0) { - withSoret = true; - } try { + bool withSoret = false; + if (iSoret > 0) { + withSoret = true; + } _stflow(i)->setTransport(TransportCabinet::item(itr), withSoret); return 0; } catch (...) { @@ -400,11 +420,11 @@ extern "C" { int stflow_enableSoret(int i, int iSoret) { - bool withSoret = false; - if (iSoret > 0) { - withSoret = true; - } try { + bool withSoret = false; + if (iSoret > 0) { + withSoret = true; + } _stflow(i)->enableSoret(withSoret); return 0; } catch (...) { @@ -473,17 +493,12 @@ extern "C" { int sim1D_new(size_t nd, int* domains) { - vector d; try { - // cout << "nd = " << nd << endl; + vector d; for (size_t n = 0; n < nd; n++) { - //writelog("n = "+int2str(n)+"\n"); - //writelog("dom = "+int2str(domains[n])+"\n"); d.push_back(&DomainCabinet::item(domains[n])); } - //writelog("in sim1D_new, calling new Sim1D\n"); Sim1D* s = new Sim1D(d); - //writelog("in sim1D_new, ret Sim1D\n"); return SimCabinet::add(s); } catch (...) { return handleAllExceptions(-1, ERR); @@ -502,8 +517,12 @@ extern "C" { int sim1D_del(int i) { - SimCabinet::del(i); - return 0; + try { + SimCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int sim1D_setValue(int i, int dom, int comp, @@ -545,15 +564,19 @@ extern "C" { int sim1D_showSolution(int i, char* fname) { - string fn = string(fname); - if (fn == "-") { - SimCabinet::item(i).showSolution(); - } else { - ofstream fout(fname); - SimCabinet::item(i).showSolution(fout); - fout.close(); + try { + string fn = string(fname); + if (fn == "-") { + SimCabinet::item(i).showSolution(); + } else { + ofstream fout(fname); + SimCabinet::item(i).showSolution(fout); + fout.close(); + } + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - return 0; } int sim1D_setTimeStep(int i, double stepsize, size_t ns, integer* nsteps) diff --git a/src/clib/ctreactor.cpp b/src/clib/ctreactor.cpp index 93d43d4b7..feb10187a 100644 --- a/src/clib/ctreactor.cpp +++ b/src/clib/ctreactor.cpp @@ -36,69 +36,105 @@ extern "C" { int reactor_new(int type) { - ReactorBase* r=0; - if (type == ReactorType) { - r = new Reactor(); - } else if (type == FlowReactorType) { - r = new FlowReactor(); - } else if (type == ConstPressureReactorType) { - r = new ConstPressureReactor(); - } else if (type == ReservoirType) { - r = new Reservoir(); - } else { - r = new ReactorBase(); + try { + ReactorBase* r=0; + if (type == ReactorType) { + r = new Reactor(); + } else if (type == FlowReactorType) { + r = new FlowReactor(); + } else if (type == ConstPressureReactorType) { + r = new ConstPressureReactor(); + } else if (type == ReservoirType) { + r = new Reservoir(); + } else { + r = new ReactorBase(); + } + return ReactorCabinet::add(r); + } catch (...) { + return handleAllExceptions(-1, ERR); } - return ReactorCabinet::add(r); } int reactor_del(int i) { - ReactorCabinet::del(i); - return 0; + try { + ReactorCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactor_copy(int i) { - return ReactorCabinet::newCopy(i); + try { + return ReactorCabinet::newCopy(i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactor_assign(int i, int j) { - return ReactorCabinet::assign(i,j); + try { + return ReactorCabinet::assign(i,j); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactor_setInitialVolume(int i, double v) { - ReactorCabinet::item(i).setInitialVolume(v); - return 0; + try { + ReactorCabinet::item(i).setInitialVolume(v); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactor_setInitialTime(int i, double t) { - ReactorCabinet::item(i).setInitialTime(t); - return 0; + try { + ReactorCabinet::item(i).setInitialTime(t); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactor_setThermoMgr(int i, int n) { - ReactorCabinet::item(i).setThermoMgr(ThermoCabinet::item(n)); - return 0; + try { + ReactorCabinet::item(i).setThermoMgr(ThermoCabinet::item(n)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactor_setKineticsMgr(int i, int n) { - ReactorBase* r = &ReactorCabinet::item(i); - if (r->type() >= ReactorType) { - ((Reactor*)r)->setKineticsMgr(KineticsCabinet::item(n)); + try { + ReactorBase* r = &ReactorCabinet::item(i); + if (r->type() >= ReactorType) { + ((Reactor*)r)->setKineticsMgr(KineticsCabinet::item(n)); + } + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - return 0; } int reactor_advance(int i, double t) { try { - ReactorCabinet::item(i).advance(t); - return 0; + try { + ReactorCabinet::item(i).advance(t); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } catch (...) { return handleAllExceptions(-1, ERR); } @@ -106,88 +142,144 @@ extern "C" { double reactor_step(int i, double t) { - return ReactorCabinet::item(i).step(t); + try { + return ReactorCabinet::item(i).step(t); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double reactor_time(int i) { - return ReactorCabinet::item(i).time(); + try { + return ReactorCabinet::item(i).time(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double reactor_mass(int i) { - return ReactorCabinet::item(i).mass(); + try { + return ReactorCabinet::item(i).mass(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double reactor_volume(int i) { - return ReactorCabinet::item(i).volume(); + try { + return ReactorCabinet::item(i).volume(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double reactor_density(int i) { - return ReactorCabinet::item(i).density(); + try { + return ReactorCabinet::item(i).density(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double reactor_temperature(int i) { - return ReactorCabinet::item(i).temperature(); + try { + return ReactorCabinet::item(i).temperature(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double reactor_enthalpy_mass(int i) { - return ReactorCabinet::item(i).enthalpy_mass(); + try { + return ReactorCabinet::item(i).enthalpy_mass(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double reactor_intEnergy_mass(int i) { - return ReactorCabinet::item(i).intEnergy_mass(); + try { + return ReactorCabinet::item(i).intEnergy_mass(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double reactor_pressure(int i) { - return ReactorCabinet::item(i).pressure(); + try { + return ReactorCabinet::item(i).pressure(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double reactor_massFraction(int i, int k) { - return ReactorCabinet::item(i).massFraction(k); + try { + return ReactorCabinet::item(i).massFraction(k); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int reactor_setEnergy(int i, int eflag) { - ReactorBase* r = &ReactorCabinet::item(i); - if (r->type() >= ReactorType) { - ((Reactor*)r)->setEnergy(eflag); + try { + ReactorBase* r = &ReactorCabinet::item(i); + if (r->type() >= ReactorType) { + ((Reactor*)r)->setEnergy(eflag); + } + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - return 0; } int flowReactor_setMassFlowRate(int i, double mdot) { - ReactorBase* r = &ReactorCabinet::item(i); - if (r->type() >= ReactorType) { - ((FlowReactor*)r)->setMassFlowRate(mdot); + try { + ReactorBase* r = &ReactorCabinet::item(i); + if (r->type() >= ReactorType) { + ((FlowReactor*)r)->setMassFlowRate(mdot); + } + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - return 0; } size_t reactor_nSensParams(int i) { - ReactorBase* r = &ReactorCabinet::item(i); - if (r->type() >= ReactorType) { - return ((Reactor*)r)->nSensParams(); - } else { - std::cout << "type problem..." << r->type() << std::endl; - return 0; + try { + ReactorBase* r = &ReactorCabinet::item(i); + if (r->type() >= ReactorType) { + return ((Reactor*)r)->nSensParams(); + } else { + std::cout << "type problem..." << r->type() << std::endl; + return 0; + } + } catch (...) { + return handleAllExceptions(npos, npos); } } int reactor_addSensitivityReaction(int i, int rxn) { - ReactorBase* r = &ReactorCabinet::item(i); - ((Reactor*)r)->addSensitivityReaction(rxn); - return 0; + try { + ReactorBase* r = &ReactorCabinet::item(i); + ((Reactor*)r)->addSensitivityReaction(rxn); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } @@ -195,8 +287,12 @@ extern "C" { int reactornet_new() { - ReactorNet* r = new ReactorNet(); - return NetworkCabinet::add(r); + try { + ReactorNet* r = new ReactorNet(); + return NetworkCabinet::add(r); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactornet_del(int i) @@ -205,42 +301,66 @@ extern "C" { NetworkCabinet::del(i); return 0; } catch (...) { - return -1; + return handleAllExceptions(-1, ERR); } } int reactornet_copy(int i) { - return NetworkCabinet::newCopy(i); + try { + return NetworkCabinet::newCopy(i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactornet_assign(int i, int j) { - return NetworkCabinet::assign(i,j); + try { + return NetworkCabinet::assign(i,j); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactornet_setInitialTime(int i, double t) { - NetworkCabinet::item(i).setInitialTime(t); - return 0; + try { + NetworkCabinet::item(i).setInitialTime(t); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactornet_setMaxTimeStep(int i, double maxstep) { - NetworkCabinet::item(i).setMaxTimeStep(maxstep); - return 0; + try { + NetworkCabinet::item(i).setMaxTimeStep(maxstep); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactornet_setTolerances(int i, double rtol, double atol) { - NetworkCabinet::item(i).setTolerances(rtol, atol); - return 0; + try { + NetworkCabinet::item(i).setTolerances(rtol, atol); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactornet_setSensitivityTolerances(int i, double rtol, double atol) { - NetworkCabinet::item(i).setSensitivityTolerances(rtol, atol); - return 0; + try { + NetworkCabinet::item(i).setSensitivityTolerances(rtol, atol); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int reactornet_addreactor(int i, int n) @@ -274,50 +394,73 @@ extern "C" { double reactornet_time(int i) { - return NetworkCabinet::item(i).time(); + try { + return NetworkCabinet::item(i).time(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } double reactornet_rtol(int i) { - return NetworkCabinet::item(i).rtol(); + try { + return NetworkCabinet::item(i).rtol(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } double reactornet_atol(int i) { - return NetworkCabinet::item(i).atol(); + try { + return NetworkCabinet::item(i).atol(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } double reactornet_sensitivity(int i, char* v, int p, int r) { - return NetworkCabinet::item(i).sensitivity(v, p, r); + try { + return NetworkCabinet::item(i).sensitivity(v, p, r); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } - // flow devices int flowdev_new(int type) { - FlowDevice* r; - switch (type) { - case MFC_Type: - r = new MassFlowController(); - break; - case PressureController_Type: - r = new PressureController(); - break; - case Valve_Type: - r = new Valve(); - break; - default: - r = new FlowDevice(); + try { + FlowDevice* r; + switch (type) { + case MFC_Type: + r = new MassFlowController(); + break; + case PressureController_Type: + r = new PressureController(); + break; + case Valve_Type: + r = new Valve(); + break; + default: + r = new FlowDevice(); + } + return FlowDeviceCabinet::add(r); + } catch (...) { + return handleAllExceptions(-1, ERR); } - return FlowDeviceCabinet::add(r); } int flowdev_del(int i) { - FlowDeviceCabinet::del(i); - return 0; + try { + FlowDeviceCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int flowdev_install(int i, int n, int m) @@ -336,43 +479,67 @@ extern "C" { int flowdev_setMaster(int i, int n) { - if (FlowDeviceCabinet::item(i).type() == PressureController_Type) { - dynamic_cast(FlowDeviceCabinet::item(i)).setMaster( - &FlowDeviceCabinet::item(n)); + try { + if (FlowDeviceCabinet::item(i).type() == PressureController_Type) { + dynamic_cast(FlowDeviceCabinet::item(i)).setMaster( + &FlowDeviceCabinet::item(n)); + } + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - return 0; } double flowdev_massFlowRate(int i, double time) { - return FlowDeviceCabinet::item(i).massFlowRate(time); + try { + return FlowDeviceCabinet::item(i).massFlowRate(time); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int flowdev_setMassFlowRate(int i, double mdot) { - FlowDeviceCabinet::item(i).setMassFlowRate(mdot); - return 0; + try { + FlowDeviceCabinet::item(i).setMassFlowRate(mdot); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int flowdev_setParameters(int i, int n, double* v) { - FlowDeviceCabinet::item(i).setParameters(n, v); - return 0; + try { + FlowDeviceCabinet::item(i).setParameters(n, v); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int flowdev_setFunction(int i, int n) { - FlowDeviceCabinet::item(i).setFunction(&FuncCabinet::item(n)); - return 0; + try { + FlowDeviceCabinet::item(i).setFunction(&FuncCabinet::item(n)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int flowdev_ready(int i) { - bool ok = FlowDeviceCabinet::item(i).ready(); - if (ok) { - return 1; + try { + bool ok = FlowDeviceCabinet::item(i).ready(); + if (ok) { + return 1; + } + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - return 0; } @@ -381,118 +548,190 @@ extern "C" { int wall_new(int type) { - Wall* r; - r = new Wall(); - return WallCabinet::add(r); + try { + Wall* r; + r = new Wall(); + return WallCabinet::add(r); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_del(int i) { - WallCabinet::del(i); - return 0; + try { + WallCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_copy(int i) { - return WallCabinet::newCopy(i); + try { + return WallCabinet::newCopy(i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_assign(int i, int j) { - return WallCabinet::assign(i,j); + try { + return WallCabinet::assign(i,j); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_install(int i, int n, int m) { - WallCabinet::item(i).install(ReactorCabinet::item(n), - ReactorCabinet::item(m)); - return 0; + try { + WallCabinet::item(i).install(ReactorCabinet::item(n), + ReactorCabinet::item(m)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_setkinetics(int i, int n, int m) { - Kinetics* left=0, *right=0; - if (n > 0) - if (KineticsCabinet::item(n).type() == cInterfaceKinetics) { - left = &KineticsCabinet::item(n); - } - if (m > 0) - if (KineticsCabinet::item(m).type() == cInterfaceKinetics) { - right = &KineticsCabinet::item(m); - } - WallCabinet::item(i).setKinetics(left, right); - return 0; + try { + Kinetics* left=0, *right=0; + if (n > 0) + if (KineticsCabinet::item(n).type() == cInterfaceKinetics) { + left = &KineticsCabinet::item(n); + } + if (m > 0) + if (KineticsCabinet::item(m).type() == cInterfaceKinetics) { + right = &KineticsCabinet::item(m); + } + WallCabinet::item(i).setKinetics(left, right); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } double wall_vdot(int i, double t) { - return WallCabinet::item(i).vdot(t); + try { + return WallCabinet::item(i).vdot(t); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double wall_Q(int i, double t) { - return WallCabinet::item(i).Q(t); + try { + return WallCabinet::item(i).Q(t); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } double wall_area(int i) { - return WallCabinet::item(i).area(); + try { + return WallCabinet::item(i).area(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int wall_setArea(int i, double v) { - WallCabinet::item(i).setArea(v); - return 0; + try { + WallCabinet::item(i).setArea(v); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_setThermalResistance(int i, double rth) { - WallCabinet::item(i).setThermalResistance(rth); - return 0; + try { + WallCabinet::item(i).setThermalResistance(rth); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_setHeatTransferCoeff(int i, double u) { - WallCabinet::item(i).setHeatTransferCoeff(u); - return 0; + try { + WallCabinet::item(i).setHeatTransferCoeff(u); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_setHeatFlux(int i, int n) { - WallCabinet::item(i).setHeatFlux(&FuncCabinet::item(n)); - return 0; + try { + WallCabinet::item(i).setHeatFlux(&FuncCabinet::item(n)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_setExpansionRateCoeff(int i, double k) { - WallCabinet::item(i).setExpansionRateCoeff(k); - return 0; + try { + WallCabinet::item(i).setExpansionRateCoeff(k); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_setVelocity(int i, int n) { - WallCabinet::item(i).setVelocity(&FuncCabinet::item(n)); - return 0; + try { + WallCabinet::item(i).setVelocity(&FuncCabinet::item(n)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_setEmissivity(int i, double epsilon) { - WallCabinet::item(i).setEmissivity(epsilon); - return 0; + try { + WallCabinet::item(i).setEmissivity(epsilon); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int wall_ready(int i) { - if (WallCabinet::item(i).ready()) { - return 1; - } else { - return 0; + try { + if (WallCabinet::item(i).ready()) { + return 1; + } else { + return 0; + } + } catch (...) { + return handleAllExceptions(-1, ERR); } } int wall_addSensitivityReaction(int i, int lr, int rxn) { - WallCabinet::item(i).addSensitivityReaction(lr, rxn); - return 0; + try { + WallCabinet::item(i).addSensitivityReaction(lr, rxn); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } } diff --git a/src/clib/ctrpath.cpp b/src/clib/ctrpath.cpp index db8f29712..aff42f89f 100644 --- a/src/clib/ctrpath.cpp +++ b/src/clib/ctrpath.cpp @@ -22,181 +22,288 @@ extern "C" { int rdiag_new() { - ReactionPathDiagram* d = new ReactionPathDiagram(); - return DiagramCabinet::add(d); + try { + ReactionPathDiagram* d = new ReactionPathDiagram(); + return DiagramCabinet::add(d); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_del(int i) { - DiagramCabinet::del(i); - return 0; + try { + DiagramCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_copy(int i) { - return DiagramCabinet::newCopy(i); + try { + return DiagramCabinet::newCopy(i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_assign(int i, int j) { - return DiagramCabinet::assign(i,j); + try { + return DiagramCabinet::assign(i,j); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_detailed(int i) { - DiagramCabinet::item(i).show_details = true; - return 0; + try { + DiagramCabinet::item(i).show_details = true; + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_brief(int i) { - DiagramCabinet::item(i).show_details = false; - return 0; + try { + DiagramCabinet::item(i).show_details = false; + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setThreshold(int i, double v) { - DiagramCabinet::item(i).threshold = v; - return 0; + try { + DiagramCabinet::item(i).threshold = v; + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setBoldColor(int i, char* color) { - DiagramCabinet::item(i).bold_color = string(color); - return 0; + try { + DiagramCabinet::item(i).bold_color = string(color); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setNormalColor(int i, char* color) { - DiagramCabinet::item(i).normal_color = string(color); - return 0; + try { + DiagramCabinet::item(i).normal_color = string(color); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setDashedColor(int i, char* color) { - DiagramCabinet::item(i).dashed_color = string(color); - return 0; + try { + DiagramCabinet::item(i).dashed_color = string(color); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setDotOptions(int i, char* opt) { - DiagramCabinet::item(i).dot_options = string(opt); - return 0; + try { + DiagramCabinet::item(i).dot_options = string(opt); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setFont(int i, char* font) { - DiagramCabinet::item(i).setFont(string(font)); - return 0; + try { + DiagramCabinet::item(i).setFont(string(font)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setBoldThreshold(int i, double v) { - DiagramCabinet::item(i).bold_min = v; - return 0; + try { + DiagramCabinet::item(i).bold_min = v; + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setNormalThreshold(int i, double v) { - DiagramCabinet::item(i).dashed_max = v; - return 0; + try { + DiagramCabinet::item(i).dashed_max = v; + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setLabelThreshold(int i, double v) { - DiagramCabinet::item(i).label_min = v; - return 0; + try { + DiagramCabinet::item(i).label_min = v; + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setScale(int i, double v) { - DiagramCabinet::item(i).scale = v; - return 0; + try { + DiagramCabinet::item(i).scale = v; + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setFlowType(int i, int iflow) { - if (iflow == 0) { - DiagramCabinet::item(i).flow_type = OneWayFlow; - } else { - DiagramCabinet::item(i).flow_type = NetFlow; + try { + if (iflow == 0) { + DiagramCabinet::item(i).flow_type = OneWayFlow; + } else { + DiagramCabinet::item(i).flow_type = NetFlow; + } + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - return 0; } int rdiag_setArrowWidth(int i, double v) { - DiagramCabinet::item(i).arrow_width = v; - return 0; + try { + DiagramCabinet::item(i).arrow_width = v; + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_setTitle(int i, char* title) { - DiagramCabinet::item(i).title = string(title); - return 0; + try { + DiagramCabinet::item(i).title = string(title); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_add(int i, int n) { - DiagramCabinet::item(i).add(DiagramCabinet::item(n)); - return 0; + try { + DiagramCabinet::item(i).add(DiagramCabinet::item(n)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_findMajor(int i, double threshold, size_t lda, double* a) { - DiagramCabinet::item(i).findMajorPaths(threshold, lda, a); - return 0; + try { + DiagramCabinet::item(i).findMajorPaths(threshold, lda, a); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rdiag_write(int i, int fmt, char* fname) { - ofstream f(fname); - if (fmt == 0) { - DiagramCabinet::item(i).exportToDot(f); - } else { - DiagramCabinet::item(i).writeData(f); + try { + ofstream f(fname); + if (fmt == 0) { + DiagramCabinet::item(i).exportToDot(f); + } else { + DiagramCabinet::item(i).writeData(f); + } + f.close(); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - f.close(); - return 0; } int rdiag_displayOnly(int i, int k) { - DiagramCabinet::item(i).displayOnly(k); - return 0; + try { + DiagramCabinet::item(i).displayOnly(k); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rbuild_new() { - ReactionPathBuilder* d = new ReactionPathBuilder(); - return BuilderCabinet::add(d); + try { + ReactionPathBuilder* d = new ReactionPathBuilder(); + return BuilderCabinet::add(d); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rbuild_del(int i) { - BuilderCabinet::del(i); - return 0; + try { + BuilderCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rbuild_init(int i, char* logfile, int k) { - ofstream flog(logfile); - BuilderCabinet::item(i).init(flog, KineticsCabinet::item(k)); - return 0; + try { + ofstream flog(logfile); + BuilderCabinet::item(i).init(flog, KineticsCabinet::item(k)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int rbuild_build(int i, int k, char* el, char* dotfile, int idiag, int iquiet) { - ofstream fdot(dotfile); - bool quiet = false; - if (iquiet > 0) { - quiet = true; + try { + ofstream fdot(dotfile); + bool quiet = false; + if (iquiet > 0) { + quiet = true; + } + BuilderCabinet::item(i).build(KineticsCabinet::item(k), string(el), fdot, + DiagramCabinet::item(idiag), quiet); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); } - BuilderCabinet::item(i).build(KineticsCabinet::item(k), string(el), fdot, - DiagramCabinet::item(idiag), quiet); - return 0; } - } diff --git a/src/clib/ctsurf.cpp b/src/clib/ctsurf.cpp index 9415fb9f5..8e064d290 100644 --- a/src/clib/ctsurf.cpp +++ b/src/clib/ctsurf.cpp @@ -14,61 +14,84 @@ using namespace std; using namespace Cantera; -inline SurfPhase* _surfphase(int n) +inline SurfPhase& _surfphase(int n) { - return dynamic_cast(&Cabinet::item(n)); + return dynamic_cast(Cabinet::item(n)); } -inline InterfaceKinetics* _surfkin(int n) +inline InterfaceKinetics& _surfkin(int n) { - return dynamic_cast(&Cabinet::item(n)); + return dynamic_cast(Cabinet::item(n)); } extern "C" { int surf_setsitedensity(int i, double s0) { - _surfphase(i)->setSiteDensity(s0); - return 0; + try { + _surfphase(i).setSiteDensity(s0); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } double surf_sitedensity(int i) { - return _surfphase(i)->siteDensity(); + try { + return _surfphase(i).siteDensity(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } int surf_setcoverages(int i, double* c) { - _surfphase(i)->setCoverages(c); - return 0; + try { + _surfphase(i).setCoverages(c); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int surf_setcoveragesbyname(int i, char* c) { - _surfphase(i)->setCoveragesByName(string(c)); - return 0; + try { + _surfphase(i).setCoveragesByName(string(c)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int surf_getcoverages(int i, double* c) { - _surfphase(i)->getCoverages(c); + try { + _surfphase(i).getCoverages(c); return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int surf_setconcentrations(int i, double* c) { - _surfphase(i)->setConcentrations(c); - return 0; + try { + _surfphase(i).setConcentrations(c); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int surf_getconcentrations(int i, double* c) { - _surfphase(i)->getConcentrations(c); - return 0; + try { + _surfphase(i).getConcentrations(c); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } - - // int surface_setcoverages(int i, double* c) { - // _surface(i)->setCoverages(c); - // return 0; - // } } diff --git a/src/clib/ctxml.cpp b/src/clib/ctxml.cpp index e3e33b072..54e73e4de 100644 --- a/src/clib/ctxml.cpp +++ b/src/clib/ctxml.cpp @@ -21,13 +21,17 @@ extern "C" { int xml_new(const char* name = 0) { - XML_Node* x; - if (!name) { - x = new XML_Node; - } else { - x = new XML_Node(name); - } - return XmlCabinet::add(x); + try { + XML_Node* x; + if (!name) { + x = new XML_Node; + } else { + x = new XML_Node(name); + } + return XmlCabinet::add(x); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int xml_get_XML_File(const char* file, int debug) @@ -53,24 +57,40 @@ extern "C" { int xml_del(int i) { - XmlCabinet::del(i); - return 0; + try { + XmlCabinet::del(i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int xml_removeChild(int i, int j) { - XmlCabinet::item(i).removeChild(&XmlCabinet::item(j)); - return 0; + try { + XmlCabinet::item(i).removeChild(&XmlCabinet::item(j)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int xml_copy(int i) { - return XmlCabinet::newCopy(i); + try { + return XmlCabinet::newCopy(i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int xml_assign(int i, int j) { - return XmlCabinet::assign(i,j); + try { + return XmlCabinet::assign(i,j); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int xml_build(int i, const char* file) @@ -101,8 +121,6 @@ extern "C" { } } - - int xml_attrib(int i, const char* key, char* value) { try { @@ -230,10 +248,9 @@ extern "C" { try { XML_Node& node = XmlCabinet::item(i); return (int) node.nChildren(); - } catch (CanteraError& err) { - err.save(); - return -1; - } + } catch (...) { + return handleAllExceptions(-1, ERR); + } } int xml_addChild(int i, const char* name, const char* value) diff --git a/src/fortran/fct.cpp b/src/fortran/fct.cpp index edd4f22e4..339e201e7 100644 --- a/src/fortran/fct.cpp +++ b/src/fortran/fct.cpp @@ -71,12 +71,6 @@ std::string f2string(const char* s, ftnlen n) return ss; } -static void handleError(CanteraError& err) -{ - err.save(); - error(lastErrorMessage()); -} - /** * Exported functions. */ @@ -85,13 +79,9 @@ extern "C" { status_t cantera_error_(const char* proc, const char* msg, ftnlen proclen, ftnlen msglen) { - try { - std::string sproc = f2string(proc, proclen); - std::string smsg = f2string(msg, msglen); - throw CanteraError(sproc, smsg); - } catch (...) { - return handleAllExceptions(-1, ERR); - } + std::string sproc = f2string(proc, proclen); + std::string smsg = f2string(msg, msglen); + throw CanteraError(sproc, smsg); return -1; } @@ -107,96 +97,157 @@ extern "C" { for (int nn = lout; nn < lennm; nn++) { nm[nn] = ' '; } - return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } + return 0; + } + + integer phase_nelements_(const integer* n) + { + try { + return _fph(n)->nElements(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } + } + + integer phase_nspecies_(const integer* n) + { + try { + return _fph(n)->nSpecies(); } catch (...) { return handleAllExceptions(-1, ERR); } } - integer phase_nelements_(const integer* n) - { - return _fph(n)->nElements(); - } - - integer phase_nspecies_(const integer* n) - { - return _fph(n)->nSpecies(); - } - doublereal phase_temperature_(const integer* n) { - return _fph(n)->temperature(); + try { + return _fph(n)->temperature(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } status_t phase_settemperature_(const integer* n, doublereal* t) { - _fph(n)->setTemperature(*t); + try { + _fph(n)->setTemperature(*t); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } doublereal phase_density_(const integer* n) { - return _fph(n)->density(); + try { + return _fph(n)->density(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } status_t phase_setdensity_(const integer* n, doublereal* rho) { - _fph(n)->setDensity(*rho); + try { + _fph(n)->setDensity(*rho); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } doublereal phase_molardensity_(const integer* n) { - return _fph(n)->molarDensity(); + try { + return _fph(n)->molarDensity(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } doublereal phase_meanmolecularweight_(const integer* n) { - return _fph(n)->meanMolecularWeight(); + try { + return _fph(n)->meanMolecularWeight(); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } integer phase_elementindex_(const integer* n, char* nm, ftnlen lennm) { - std::string elnm = f2string(nm, lennm); - return _fph(n)->elementIndex(elnm) + 1; + try { + std::string elnm = f2string(nm, lennm); + return _fph(n)->elementIndex(elnm) + 1; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } integer phase_speciesindex_(const integer* n, char* nm, ftnlen lennm) { - std::string spnm = f2string(nm, lennm); - return _fph(n)->speciesIndex(spnm) + 1; + try { + std::string spnm = f2string(nm, lennm); + return _fph(n)->speciesIndex(spnm) + 1; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } status_t phase_getmolefractions_(const integer* n, doublereal* x) { - _fph(n)->getMoleFractions(x); + try { + _fph(n)->getMoleFractions(x); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } doublereal phase_molefraction_(const integer* n, integer* k) { - return _fph(n)->moleFraction(*k-1); + try { + return _fph(n)->moleFraction(*k-1); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } + } status_t phase_getmassfractions_(const integer* n, doublereal* y) { - ThermoPhase* p = _fph(n); - p->getMassFractions(y); - return 0; + try { + ThermoPhase* p = _fph(n); + p->getMassFractions(y); + } catch (...) { + return handleAllExceptions(-1, ERR); + } + return 0; } doublereal phase_massfraction_(const integer* n, integer* k) { - return _fph(n)->massFraction(*k-1); + try { + return _fph(n)->massFraction(*k-1); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } status_t phase_setmolefractions_(const integer* n, double* x, const integer* norm) { - ThermoPhase* p = _fph(n); - if (*norm) { - p->setMoleFractions(x); - } else { - p->setMoleFractions_NoNorm(x); + try { + ThermoPhase* p = _fph(n); + if (*norm) { + p->setMoleFractions(x); + } else { + p->setMoleFractions_NoNorm(x); + } + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -212,19 +263,23 @@ extern "C" { } parseCompString(f2string(x, lx), xx); p->setMoleFractionsByName(xx); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t phase_setmassfractions_(const integer* n, doublereal* y, const integer* norm) { - ThermoPhase* p = _fph(n); - if (*norm) { - p->setMassFractions(y); - } else { - p->setMassFractions_NoNorm(y); + try { + ThermoPhase* p = _fph(n); + if (*norm) { + p->setMassFractions(y); + } else { + p->setMassFractions_NoNorm(y); + } + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -240,29 +295,36 @@ extern "C" { } parseCompString(f2string(y, leny), yy); p->setMassFractionsByName(yy); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t phase_getatomicweights_(const integer* n, doublereal* atw) { - ThermoPhase* p = _fph(n); - const vector_fp& wt = p->atomicWeights(); - copy(wt.begin(), wt.end(), atw); + try { + ThermoPhase* p = _fph(n); + const vector_fp& wt = p->atomicWeights(); + copy(wt.begin(), wt.end(), atw); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } status_t phase_getmolecularweights_(const integer* n, doublereal* mw) { - ThermoPhase* p = _fph(n); - const vector_fp& wt = p->molecularWeights(); - copy(wt.begin(), wt.end(), mw); + try { + ThermoPhase* p = _fph(n); + const vector_fp& wt = p->molecularWeights(); + copy(wt.begin(), wt.end(), mw); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } - status_t phase_getspeciesname_(const integer* n, integer* k, char* nm, ftnlen lennm) { try { @@ -272,10 +334,10 @@ extern "C" { for (int nn = lout; nn < lennm; nn++) { nm[nn] = ' '; } - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t phase_getelementname_(const integer* n, integer* m, char* nm, ftnlen lennm) @@ -287,10 +349,10 @@ extern "C" { for (int nn = lout; nn < lennm; nn++) { nm[nn] = ' '; } - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } @@ -303,9 +365,6 @@ extern "C" { } } - - - //-------------- Thermo --------------------// integer newthermofromxml_(integer* mxml) @@ -321,12 +380,20 @@ extern "C" { integer th_nspecies_(const integer* n) { - return _fth(n)->nSpecies(); + try { + return _fth(n)->nSpecies(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } integer th_eostype_(const integer* n) { - return _fth(n)->eosType(); + try { + return _fth(n)->eosType(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } doublereal th_enthalpy_mole_(const integer* n) @@ -448,8 +515,12 @@ extern "C" { status_t th_chempotentials_(const integer* n, doublereal* murt) { - thermo_t* thrm = _fth(n); - thrm->getChemPotentials(murt); + try { + thermo_t* thrm = _fth(n); + thrm->getChemPotentials(murt); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } @@ -457,96 +528,120 @@ extern "C" { { try { _fth(n)->setPressure(*p); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t th_set_hp_(const integer* n, doublereal* v1, doublereal* v2) { try { _fth(n)->setState_HP(*v1, *v2); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t th_set_uv_(const integer* n, doublereal* v1, doublereal* v2) { try { _fth(n)->setState_UV(*v1, *v2); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t th_set_sv_(const integer* n, doublereal* v1, doublereal* v2) { try { _fth(n)->setState_SV(*v1, *v2); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t th_set_sp_(const integer* n, doublereal* v1, doublereal* v2) { try { _fth(n)->setState_SP(*v1, *v2); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t th_equil_(const integer* n, char* XY, ftnlen lenxy) { try { equilibrate(*_fth(n), f2string(XY,lenxy).c_str()); - return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } + return 0; + } + + doublereal th_refpressure_(const integer* n) + { + try { + return _fth(n)->refPressure(); } catch (...) { return handleAllExceptions(-1, ERR); } } - doublereal th_refpressure_(const integer* n) - { - return _fth(n)->refPressure(); - } - doublereal th_mintemp_(const integer* n, integer* k) { - return _fth(n)->minTemp(*k-1); + try { + return _fth(n)->minTemp(*k-1); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } doublereal th_maxtemp_(const integer* n, integer* k) { - return _fth(n)->maxTemp(*k-1); + try { + return _fth(n)->maxTemp(*k-1); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } status_t th_getenthalpies_rt_(const integer* n, doublereal* h_rt) { - thermo_t* thrm = _fth(n); - thrm->getEnthalpy_RT(h_rt); + try { + thermo_t* thrm = _fth(n); + thrm->getEnthalpy_RT(h_rt); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } status_t th_getentropies_r_(const integer* n, doublereal* s_r) { - thermo_t* thrm = _fth(n); - thrm->getEntropy_R(s_r); + try { + thermo_t* thrm = _fth(n); + thrm->getEntropy_R(s_r); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } status_t th_getcp_r_(const integer* n, integer* lenm, doublereal* cp_r) { - thermo_t* thrm = _fth(n); - thrm->getCp_R(cp_r); + try { + thermo_t* thrm = _fth(n); + thrm->getCp_R(cp_r); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } @@ -585,98 +680,130 @@ extern "C" { } } - // status_t installRxnArrays_(integer* pxml, integer* ikin, - // char* default_phase) { - // try { - // XML_Node* p = _xml(pxml); - // kinetics_t* k = kin(ikin); - // string defphase = string(default_phase); - // installReactionArrays(*p, *k, defphase); - // return 0; - // } - // catch (CanteraError) { handleError(); return -1; } - // } - //------------------------------------- integer kin_type_(const integer* n) { - return _fkin(n)->type(); + try { + return _fkin(n)->type(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } integer kin_start_(const integer* n, integer* p) { - return _fkin(n)->start(*p)+1; + try { + return _fkin(n)->start(*p)+1; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } integer kin_speciesindex_(const integer* n, const char* nm, const char* ph, ftnlen lennm, ftnlen lenph) { - return _fkin(n)->kineticsSpeciesIndex(f2string(nm, lennm), f2string(ph, lenph))+1; + try { + return _fkin(n)->kineticsSpeciesIndex(f2string(nm, lennm), + f2string(ph, lenph)) + 1; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } //--------------------------------------- integer kin_ntotalspecies_(const integer* n) { - return _fkin(n)->nTotalSpecies(); + try { + return _fkin(n)->nTotalSpecies(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } integer kin_nreactions_(const integer* n) { - return _fkin(n)->nReactions(); + try { + return _fkin(n)->nReactions(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } integer kin_nphases_(const integer* n) { - return _fkin(n)->nPhases(); + try { + return _fkin(n)->nPhases(); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } - integer kin_phaseindex_(const integer* n, const char* ph, - ftnlen lenph) + integer kin_phaseindex_(const integer* n, const char* ph, ftnlen lenph) { - return _fkin(n)->phaseIndex(f2string(ph, lenph)); + try { + return _fkin(n)->phaseIndex(f2string(ph, lenph)); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } doublereal kin_reactantstoichcoeff_(const integer* n, integer* k, integer* i) { - return _fkin(n)->reactantStoichCoeff(*k-1,*i-1); + try { + return _fkin(n)->reactantStoichCoeff(*k-1,*i-1); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } doublereal kin_productstoichcoeff_(const integer* n, integer* k, integer* i) { - return _fkin(n)->productStoichCoeff(*k-1,*i-1); + try { + return _fkin(n)->productStoichCoeff(*k-1,*i-1); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } integer kin_reactiontype_(const integer* n, integer* i) { - return _fkin(n)->reactionType(*i-1); + try { + return _fkin(n)->reactionType(*i-1); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } status_t kin_getfwdratesofprogress_(const integer* n, doublereal* fwdROP) { - Kinetics* k = _fkin(n); try { + Kinetics* k = _fkin(n); k->getFwdRatesOfProgress(fwdROP); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t kin_getrevratesofprogress_(const integer* n, doublereal* revROP) { - Kinetics* k = _fkin(n); try { + Kinetics* k = _fkin(n); k->getRevRatesOfProgress(revROP); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } integer kin_isreversible_(const integer* n, integer* i) { - return (int)_fkin(n)->isReversible(*i); + try { + return (int)_fkin(n)->isReversible(*i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } status_t kin_getnetratesofprogress_(const integer* n, doublereal* netROP) @@ -684,10 +811,10 @@ extern "C" { try { Kinetics* k = _fkin(n); k->getNetRatesOfProgress(netROP); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t kin_getcreationrates_(const integer* n, doublereal* cdot) @@ -695,10 +822,10 @@ extern "C" { try { Kinetics* k = _fkin(n); k->getCreationRates(cdot); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t kin_getdestructionrates_(const integer* n, doublereal* ddot) @@ -706,10 +833,10 @@ extern "C" { try { Kinetics* k = _fkin(n); k->getDestructionRates(ddot); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t kin_getnetproductionrates_(const integer* n, doublereal* wdot) @@ -717,15 +844,19 @@ extern "C" { try { Kinetics* k = _fkin(n); k->getNetProductionRates(wdot); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } doublereal kin_multiplier_(const integer* n, integer* i) { - return _fkin(n)->multiplier(*i); + try { + return _fkin(n)->multiplier(*i); + } catch (...) { + return handleAllExceptions(DERR, DERR); + } } status_t kin_getequilibriumconstants_(const integer* n, doublereal* kc) @@ -733,10 +864,10 @@ extern "C" { try { Kinetics* k = _fkin(n); k->getEquilibriumConstants(kc); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t kin_getreactionstring_(const integer* n, integer* i, char* buf, ftnlen lenbuf) @@ -749,20 +880,20 @@ extern "C" { for (int nn = lout; nn < lenbuf; nn++) { buf[nn] = ' '; } - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t kin_setmultiplier_(const integer* n, integer* i, doublereal* v) { try { _fkin(n)->setMultiplier(*i-1,*v); - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } status_t kin_advancecoverages_(const integer* n, doublereal* tstep) @@ -775,20 +906,20 @@ extern "C" { throw CanteraError("kin_advanceCoverages", "wrong kinetics manager type"); } - return 0; } catch (...) { return handleAllExceptions(-1, ERR); } + return 0; } //------------------- Transport --------------------------- - integer newtransport_(char* model, - integer* ith, integer* loglevel, ftnlen lenmodel) + integer newtransport_(char* model, integer* ith, + integer* loglevel, ftnlen lenmodel) { - std::string mstr = f2string(model, lenmodel); - thermo_t* t = _fth(ith); try { + std::string mstr = f2string(model, lenmodel); + thermo_t* t = _fth(ith); Transport* tr = newTransportMgr(mstr, t, *loglevel); return TransportCabinet::add(tr); } catch (...) { @@ -866,34 +997,6 @@ extern "C" { //-------------------- Functions --------------------------- - // status_t import_phase_(const integer* nth, const integer* nxml, char* id, ftnlen lenid) { - // thermo_t* thrm = th(nth); - // XML_Node* node = _xml(nxml); - // string idstr = f2string(id, lenid); - // try { - // importPhase(*node, thrm); - // return 0; - // } - // catch (CanteraError) { handleError(); return -1; } - // } - - // status_t import_kinetics_(const integer* nxml, char* id, - // const integer* nphases, integer* ith, const integer* nkin, ftnlen lenid) { - // vector phases; - // for (int i = 0; i < nphases; i++) { - // phases.push_back(th(ith[i])); - // } - // XML_Node* node = _xml(nxml); - // Kinetics* k = kin(nkin); - // string idstr = f2string(id, lenid); - // try { - // importKinetics(*node, phases, k); - // return 0; - // } - // catch (CanteraError) { handleError(); return -1; } - // } - - status_t ctphase_report_(const integer* nth, char* buf, integer* show_thermo, ftnlen buflen) { @@ -915,20 +1018,28 @@ extern "C" { status_t ctgetcanteraerror_(char* buf, ftnlen buflen) { - std::string e; // = ""; - //if (nErrors() > 0) - e = lastErrorMessage(); - int n = std::min((int) e.size(), buflen-1); - copy(e.begin(), e.begin() + n, buf); - for (int nn = n; nn < buflen; nn++) { - buf[nn] = ' '; + try { + std::string e; // = ""; + //if (nErrors() > 0) + e = lastErrorMessage(); + int n = std::min((int) e.size(), buflen-1); + copy(e.begin(), e.begin() + n, buf); + for (int nn = n; nn < buflen; nn++) { + buf[nn] = ' '; + } + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } status_t ctaddcanteradirectory_(integer* buflen, char* buf) { - addDirectory(std::string(buf)); + try { + addDirectory(std::string(buf)); + } catch (...) { + return handleAllExceptions(-1, ERR); + } return 0; } @@ -936,42 +1047,44 @@ extern "C" { status_t ctbuildsolutionfromxml(char* src, integer* ixml, char* id, integer* ith, integer* ikin, ftnlen lensrc, ftnlen lenid) { + try { + XML_Node* root = 0; + if (*ixml > 0) { + root = _xml(ixml); + } - XML_Node* root = 0; - if (*ixml > 0) { - root = _xml(ixml); - } + thermo_t* t = _fth(ith); + kinetics_t* k = _fkin(ikin); - thermo_t* t = _fth(ith); - kinetics_t* k = _fkin(ikin); - - Kinetics& kin = *k; - XML_Node* x, *r=0; - if (root) { - r = &root->root(); - } - std::string srcS = f2string(src, lensrc); - std::string idS = f2string(id, lenid); - if (srcS != "") { - x = get_XML_Node(srcS, r); - } else { - x = get_XML_Node(idS, r); - } - // x = find_XML(f2string(src, lensrc), r, f2string(id,lenid), "", "phase"); - if (!x) { - return 0; - } - importPhase(*x, t); - kin.addPhase(*t); - kin.init(); - installReactionArrays(*x, kin, x->id()); - t->setState_TP(300.0, OneAtm); - if (r) { - if (&x->root() != &r->root()) { + Kinetics& kin = *k; + XML_Node* x, *r=0; + if (root) { + r = &root->root(); + } + std::string srcS = f2string(src, lensrc); + std::string idS = f2string(id, lenid); + if (srcS != "") { + x = get_XML_Node(srcS, r); + } else { + x = get_XML_Node(idS, r); + } + if (!x) { + return 0; + } + importPhase(*x, t); + kin.addPhase(*t); + kin.init(); + installReactionArrays(*x, kin, x->id()); + t->setState_TP(300.0, OneAtm); + if (r) { + if (&x->root() != &r->root()) { + delete &x->root(); + } + } else { delete &x->root(); } - } else { - delete &x->root(); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } diff --git a/src/fortran/fctxml.cpp b/src/fortran/fctxml.cpp index dcf964eb5..b621930e4 100644 --- a/src/fortran/fctxml.cpp +++ b/src/fortran/fctxml.cpp @@ -14,6 +14,7 @@ using namespace ctml; using namespace std; using Cantera::XML_Node; using Cantera::CanteraError; +using Cantera::handleAllExceptions; #include "clib/Cabinet.h" @@ -27,25 +28,23 @@ inline XML_Node* _xml(const integer* i) return &XmlCabinet::item(*i); } -static void handleError(CanteraError& err) -{ - err.save(); - Cantera::error(Cantera::lastErrorMessage()); -} - std::string f2string(const char* s, ftnlen n); extern "C" { integer fxml_new_(const char* name, ftnlen namelen) { - XML_Node* x; - if (!name) { - x = new XML_Node; - } else { - x = new XML_Node(f2string(name, namelen), 0); + try { + XML_Node* x; + if (!name) { + x = new XML_Node; + } else { + x = new XML_Node(f2string(name, namelen), 0); + } + return XmlCabinet::add(x); + } catch (...) { + return handleAllExceptions(-1, ERR); } - return XmlCabinet::add(x); } status_t fxml_get_xml_file_(const char* file, ftnlen filelen) @@ -54,9 +53,8 @@ extern "C" { XML_Node* x = Cantera::get_XML_File(f2string(file, filelen)); int ix = XmlCabinet::add(x); return ix; - } catch (CanteraError& err) { - handleError(err); - return -1; + } catch (...) { + return handleAllExceptions(-1, ERR); } } @@ -66,36 +64,51 @@ extern "C" { XmlCabinet::clear(); Cantera::close_XML_File("all"); return 0; - } catch (CanteraError& err) { - handleError(err); - return -1; + } catch (...) { + return handleAllExceptions(-1, ERR); } } status_t fxml_del_(const integer* i) { - XmlCabinet::del(*i); - return 0; + try { + XmlCabinet::del(*i); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } status_t fxml_removechild_(const integer* i, const integer* j) { - _xml(i)->removeChild(_xml(j)); - return 0; + try { + _xml(i)->removeChild(_xml(j)); + return 0; + } catch (...) { + return handleAllExceptions(-1, ERR); + } } status_t fxml_copy_(const integer* i) { - return XmlCabinet::newCopy(*i); + try { + return XmlCabinet::newCopy(*i); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } status_t fxml_assign_(const integer* i, const integer* j) { - return XmlCabinet::assign(*i,*j); + try { + return XmlCabinet::assign(*i,*j); + } catch (...) { + return handleAllExceptions(-1, ERR); + } } - status_t fxml_attrib_(const integer* i, const char* key, - char* value, ftnlen keylen, ftnlen valuelen) + status_t fxml_attrib_(const integer* i, const char* key, char* value, + ftnlen keylen, ftnlen valuelen) { try { std::string ky = f2string(key, keylen); @@ -106,35 +119,35 @@ extern "C" { } else throw CanteraError("fxml_attrib","node " " has no attribute '"+ky+"'"); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } - status_t fxml_addattrib_(const integer* i, - const char* key, const char* value, ftnlen keylen, ftnlen valuelen) + status_t fxml_addattrib_(const integer* i, const char* key, + const char* value, ftnlen keylen, ftnlen valuelen) { try { std::string ky = f2string(key, keylen); std::string val = f2string(value, valuelen); XML_Node& node = *_xml(i); node.addAttribute(ky, val); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } status_t fxml_addcomment_(const integer* i, const char* comment, - ftnlen commentlen) + ftnlen commentlen) { try { std::string c = f2string(comment, commentlen); XML_Node& node = *_xml(i); node.addComment(c); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -145,8 +158,8 @@ extern "C" { XML_Node& node = *_xml(i); const std::string v = node.name(); strncpy(tag, v.c_str(), taglen); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -157,8 +170,8 @@ extern "C" { XML_Node& node = *_xml(i); const std::string v = node.value(); strncpy(value, v.c_str(), valuelen); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -169,10 +182,9 @@ extern "C" { XML_Node& node = *_xml(i); XML_Node& c = node.child(f2string(loc, loclen)); return XmlCabinet::add(&c); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } - return 0; } status_t fxml_child_bynumber_(const integer* i, const integer* m) @@ -181,8 +193,8 @@ extern "C" { XML_Node& node = *_xml(i); XML_Node& c = node.child(*m); return XmlCabinet::add(&c); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -197,8 +209,8 @@ extern "C" { } else { throw CanteraError("fxml_find_id","id not found: "+f2string(id, idlen)); } - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -213,8 +225,8 @@ extern "C" { } else throw CanteraError("fxml_findByName","name "+f2string(nm, nmlen) +" not found"); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -224,22 +236,22 @@ extern "C" { try { XML_Node& node = *_xml(i); return node.nChildren(); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } status_t fxml_addchild_(const integer* i, const char* name, - const char* value, ftnlen namelen, ftnlen valuelen) + const char* value, ftnlen namelen, ftnlen valuelen) { try { XML_Node& node = *_xml(i); XML_Node& c = node.addChild(f2string(name, namelen), f2string(value,valuelen)); return XmlCabinet::add(&c); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -251,8 +263,8 @@ extern "C" { XML_Node& chld = *_xml(j); XML_Node& c = node.addChild(chld); return XmlCabinet::add(&c); - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } @@ -270,14 +282,14 @@ extern "C" { "file "+f2string(file, filelen)+" not found."); } return 0; - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; } status_t ctml_getfloatarray_(const integer* i, const integer* n, - doublereal* data, const integer* iconvert) + doublereal* data, const integer* iconvert) { try { XML_Node& node = *_xml(i); @@ -299,8 +311,8 @@ extern "C" { data[i] = v[i]; } //n = nv; - } catch (CanteraError& err) { - handleError(err); + } catch (...) { + return handleAllExceptions(-1, ERR); } return 0; }