示例#1
0
int main()
{
    reactor* r = reactor_new();
    event_handler* ev = fire_n_handler_new(-1, handle);
    fire_n_handler_repeat((fire_n_handler*)ev, 3);
    reactor_set_timeout(r, 1000, ev);
    reactor_run(r);
}
示例#2
0
int main()
{
    reactor* r  = reactor_new();
    event_handler* ev = spawn_handler_new(parent, child);

    void quit() {
        reactor_quit(r);
    }
示例#3
0
static PyObject*
py_reactor_new(PyObject* self, PyObject* args)
{
    int type;
    if (!PyArg_ParseTuple(args, "i:reactor_new", &type)) {
        return NULL;
    }
    int n = reactor_new(type);
    return Py_BuildValue("i",n);
}
示例#4
0
thread_t *thread_new_sized(const char *name, size_t work_queue_capacity) {
  assert(name != NULL);
  assert(work_queue_capacity != 0);

  thread_t *ret = osi_calloc(sizeof(thread_t));
  if (!ret)
    goto error;

  ret->reactor = reactor_new();
  if (!ret->reactor)
    goto error;

  ret->work_queue = fixed_queue_new(work_queue_capacity);
  if (!ret->work_queue)
    goto error;

  // Start is on the stack, but we use a semaphore, so it's safe
  struct start_arg start;
  start.start_sem = semaphore_new(0);
  if (!start.start_sem)
    goto error;

  strncpy(ret->name, name, THREAD_NAME_MAX);
  start.thread = ret;
  start.error = 0;
  pthread_create(&ret->pthread, NULL, run_thread, &start);
  semaphore_wait(start.start_sem);
  semaphore_free(start.start_sem);

  if (start.error)
    goto error;

  return ret;

error:;
  if (ret) {
    fixed_queue_free(ret->work_queue, osi_free);
    reactor_free(ret->reactor);
  }
  osi_free(ret);
  return NULL;
}
thread_t *thread_new(const char *name) {
  assert(name != NULL);

  // Start is on the stack, but we use a semaphore, so it's safe
  thread_t *ret = calloc(1, sizeof(thread_t));
  if (!ret)
    goto error;

  ret->reactor = reactor_new();
  if (!ret->reactor)
    goto error;

  ret->work_queue = fixed_queue_new(WORK_QUEUE_CAPACITY);
  if (!ret->work_queue)
    goto error;

  struct start_arg start;
  start.start_sem = semaphore_new(0);
  if (!start.start_sem)
    goto error;

  strncpy(ret->name, name, THREAD_NAME_MAX);
  start.thread = ret;
  start.error = 0;
  pthread_create(&ret->pthread, NULL, run_thread, &start);
  semaphore_wait(start.start_sem);
  semaphore_free(start.start_sem);
  if (start.error)
    goto error;
  return ret;

error:;
  if (ret) {
    fixed_queue_free(ret->work_queue, free);
    reactor_free(ret->reactor);
  }
  free(ret);
  return NULL;
}
示例#6
0
int main(int argc, char** argv)
{
    int ret;
    int xml_file = xml_get_XML_File("gri30.xml", 0);
    assert(xml_file > 0);

    int phase_node = xml_findID(xml_file, "gri30_mix");
    assert(phase_node > 0);

    int thermo = thermo_newFromXML(phase_node);
    assert(thermo > 0);
    int nsp = thermo_nSpecies(thermo);
    assert(nsp == 53);

    ret = thermo_setTemperature(thermo, 500);
    assert(ret == 0);
    ret = thermo_setPressure(thermo, 5 * 101325);
    assert(ret == 0);
    ret = thermo_setMoleFractionsByName(thermo, "CH4:1.0, O2:2.0, N2:7.52");
    assert(ret == 0);

    ret = thermo_equilibrate(thermo, "HP", 0, 1e-9, 50000, 1000, 0);
    assert(ret == 0);
    double T = thermo_temperature(thermo);
    assert(T > 2200 && T < 2300);

    ret = thermo_print(thermo, 1, 0);
    assert(ret == 0);

    int kin = kin_newFromXML(phase_node, thermo, 0, 0, 0, 0);
    assert(kin > 0);

    size_t nr = kin_nReactions(kin);
    assert(nr == 325 );

    ret = thermo_setTemperature(thermo, T - 200);
    assert(ret == 0);

    char buf [1000];
    double ropf[325];
    printf("\n                   Reaction           Forward ROP\n");
    kin_getFwdRatesOfProgress(kin, 325, ropf);
    size_t n; // declare this here for C89 compatibility
    for (n = 0; n < nr; n++) {
        kin_getReactionString(kin, n, 1000, buf);
        printf("%35s   %8.6e\n", buf, ropf[n]);
    }

    printf("\n  Species    Mix diff coeff\n");
    int tran = trans_new("Mix", thermo, 0);
    double dkm[53];
    trans_getMixDiffCoeffs(tran, 53, dkm);
    int k; // declare this here for C89 compatibility
    for (k = 0; k < nsp; k++) {
        thermo_getSpeciesName(thermo, k, 1000, buf);
        printf("%10s   %8.6e\n", buf, dkm[k]);
    }

    ret = thermo_setTemperature(thermo, 1050);
    assert(ret == 0);
    ret = thermo_setPressure(thermo, 5 * 101325);
    assert(ret == 0);
    ret = thermo_setMoleFractionsByName(thermo, "CH4:1.0, O2:2.0, N2:7.52");
    assert(ret == 0);

    printf("\ntime       Temperature\n");
    int reactor = reactor_new(5);
    int net = reactornet_new();
    ret = reactor_setThermoMgr(reactor, thermo);
    assert(ret == 0);
    ret = reactor_setKineticsMgr(reactor, kin);
    assert(ret == 0);
    ret = reactornet_addreactor(net, reactor);
    assert(ret == 0);

    double t = 0.0;
    while (t < 0.1 && ret == 0) {
        double T = reactor_temperature(reactor);
        t = reactornet_time(net);
        printf("%.2e   %.3f\n", t, T);
        ret = reactornet_advance(net, t + 5e-3);
        assert(ret == 0);
    }
    ct_appdelete();
    return 0;
}
void reactormethods(int nlhs, mxArray* plhs[],
                    int nrhs, const mxArray* prhs[])
{
    int iok = 0, n;
    int job = getInt(prhs[1]);
    int i = getInt(prhs[2]);
    double r = Undef;
    double v = Undef;
    if (nrhs > 3) {
        v = getDouble(prhs[3]);
    }

    // constructor
    if (job == 0) {
        n = reactor_new(i);
        plhs[0] = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL);
        double* h = mxGetPr(plhs[0]);
        *h = double(n);
        if (n < 0) {
            reportError();
        }
        return;
    }

    // options that do not return a value
    if (job < 20) {
        switch (job) {
        case 1:
            iok = reactor_del(i);
            break;
        case 2:
            iok = reactor_copy(i);
            break;
        case 4:
            iok = reactor_setInitialVolume(i, v);
            break;
        case 6:
            iok = reactor_setThermoMgr(i, int(v));
            break;
        case 7:
            iok = reactor_setKineticsMgr(i, int(v));
            break;
        case 8:
            iok = reactor_setChemistry(i, bool(v));
            break;
        case 9:
            iok = reactor_setEnergy(i, int(v));
            break;
        case 10:
            iok = flowReactor_setMassFlowRate(i, v);
            break;
        default:
            mexErrMsgTxt("unknown job parameter");
        }
        plhs[0] = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL);
        double* h = mxGetPr(plhs[0]);
        *h = double(iok);
        if (iok < 0) {
            reportError();
        }
        return;
    } else if (job < 40) {
        // options that return a value of type 'double'
        switch (job) {
        case 23:
            r = reactor_mass(i);
            break;
        case 24:
            r = reactor_volume(i);
            break;
        case 25:
            r = reactor_density(i);
            break;
        case 26:
            r = reactor_temperature(i);
            break;
        case 27:
            r = reactor_enthalpy_mass(i);
            break;
        case 28:
            r = reactor_intEnergy_mass(i);
            break;
        case 29:
            r = reactor_pressure(i);
            break;
        case 30:
            r = reactor_massFraction(i, int(v));
            break;
        default:
            mexErrMsgTxt("unknown job parameter");
        }
        plhs[0] = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL);
        double* h = mxGetPr(plhs[0]);
        *h = r;
        if (r == DERR) {
            reportError();
        }
        return;
    }
}