void TApp::Init() { fInit = true; for( int i = 0; i < AppList.Count(); i++ ) (*AppList.Values(i))->Init(); initRnd(); }
PyObject * smds_core_sRnd(PyObject *self, PyObject *args) { if (PyTuple_Size(args) != 1) { initRnd(); } else { int seed; if (!PyArg_ParseTuple(args, "k", &seed)) return NULL; seedRnd(seed, NULL); } Py_RETURN_NONE; }
int main() { uchar i; uint16_t b; DDRB = 1; // PB0 as output wdt_enable(WDTO_1S); // enable 1s watchdog timer usbInit(); initRnd(); usbDeviceDisconnect(); // enforce re-enumeration for(i=0; i < 250; i++) { // wait 500 ms wdt_reset(); // keep the watchdog happy _delay_ms(2); } usbDeviceConnect(); sei(); // Enable interrupts after re-enumeration while(1) { wdt_reset(); // keep the watchdog happy usbPoll(); // PORTB = PINC & 1; if(clear) { replyBuf[0] = 0; clear = 0; PORTB = PINB ^ 1; } b = getRnd(); if(b != 0x100) { uint8_t len = replyBuf[0]; if(len < sizeof(replyBuf)-1) { replyBuf[len+1] = b; replyBuf[0]++; } } } return 0; }
// takes three arguments: numbins, array[short], pos // PERFORMS NO CHECKING OF PARAMETERS! PyObject * smds_core_run(PyObject *self, PyObject *args) { smds_core *s = (smds_core*)self; PyArrayObject *o; Int16 *iData; double *data; int dur, pos, i; smds_molec *m; int size, minX, maxX; double a; #ifdef CORE_INTENS double *intens = (double*)(s->intens->data); double sc_x; int numBins_x = s->numBins_x; #ifdef CORE_3D double sc_z; int numBins_z = s->numBins_z; #endif #else int r; int threshold; double b; #ifdef CORE_3D int threshold_z; double c; #endif #endif double flow_x; int dir_flow_x = s->dir_flow_x; double t_insert_avg = s->t_insert_avg; unsigned long int t_insert = s->t_insert; int t, dir, rndCounter = 0; unsigned int rnd = 0; double *binCount; double I; if (!PyArg_ParseTuple(args, "iO!i", &dur, &PyArray_Type, &o, &pos)) return NULL; s->dur += dur; data = (double*)malloc(sizeof(double)*dur); if (!data) { PyErr_SetString(PyExc_MemoryError, "Unable to allocate working space."); return NULL; } Py_BEGIN_ALLOW_THREADS if (s->bkg > 0.0) for (i=0; i < dur; i++) data[i] = s->bkg; else for (i=0; i < dur; i++) data[i] = 0.0; binCount = data; for (i=dur; i; i--) { for (t=s->steps; t; t--, t_insert--) { /* Create new molecules */ while (!t_insert) { if (s->avail) { m = s->avail; s->avail = s->avail->next; } else { m = (smds_molec*)malloc(sizeof(smds_molec)); } m->prev = NULL; smds_core_create_molec(m, s, 1); if (s->molecs) s->molecs->prev = m; m->next = s->molecs; s->molecs = m; t_insert = (int)(log(DRandom(&s->rs))*t_insert_avg); } m = s->molecs; while (m) { size = s->size[m->species]; minX = s->minX[m->species]; maxX = s->maxX[m->species]; a = s->a[m->species]; #ifdef CORE_INTENS sc_x = s->sc_x[m->species]; #ifdef CORE_3D sc_z = s->sc_z[m->species]; #endif #else threshold = s->threshold[m->species]; b = s->b[m->species]; #ifdef CORE_3D threshold_z = s->threshold_z[m->species]; c = s->c[m->species]; #endif #endif flow_x = s->flow_x[m->species]; /* Flow */ if ( ((dir_flow_x > 0) && ((m->x += flow_x) >= maxX)) || ((dir_flow_x < 0) && ((m->x -= flow_x) < minX)) ) { smds_molec *p = m; m = m->next; if (p->prev) p->prev->next = p->next; else s->molecs = p->next; // head of list if (p->next) p->next->prev = p->prev; // p->prev = NULL; s->avail is singly-linked. p->next = s->avail; s->avail = p; continue; } /* Reposition */ #ifdef CORE_3D do { if (!rndCounter) { rnd = Random(&s->rs); rndCounter = 10; } dir = rnd & 0x7; rndCounter--; rnd >>= 3; } while (dir == 6 || dir == 7); #else if (!rndCounter) { rnd = Random(&s->rs); rndCounter = 16; } dir = rnd & 0x3; rndCounter--; rnd >>= 2; #endif switch (dir) { case 0: m->x++; if (m->x >= maxX) { m->x = minX; } break; case 1: m->x--; if (m->x < minX) { m->x = maxX-1; } break; case 2: m->y++; if (m->y >= size) { m->y = -size; } break; case 3: m->y--; if (m->y < -size) { m->y = size-1; } break; #ifdef CORE_3D case 4: m->z++; if (m->z >= size) { m->z = -size; } break; case 5: m->z--; if (m->z < -size) { m->z = size-1; } break; #endif } /* Calculate Intensity */ #ifdef CORE_INTENS #ifdef CORE_3D if (abs((int)(m->x*sc_x)) <= numBins_x && abs((int)(m->y*sc_x)) <= numBins_x && abs((int)(m->z*sc_z)) <= numBins_z) { I = a * intens[ (2*numBins_x+1)*(2*numBins_x+1)*(int)(numBins_z + m->z * sc_z) + (2*numBins_x+1) * (int)(numBins_x + m->y * sc_x) + (int)(numBins_x + m->x * sc_x) ]; #else if (abs((int)(x*sc_x)) <= numBins_x && abs((int)(y*sc_x)) <= numBins_x) { I = a * intens[ (2*numBins_x+1) * (int)(numBins_x + m->y * sc_x) + (int)(numBins_x + m->x * sc_x) ]; #endif #else // not INTENS #ifdef CORE_3D if ( (r = m->x*m->x + m->y*m->y) < threshold && abs(m->z) < threshold_z) { I = a * exp(b*(double)r + c*(double)(m->z*m->z)); #else if ( (r = m->x*m->x + m->y*m->y) < threshold) { I = a * exp(b * (double)r); #endif #endif #ifdef CORE_BLEACH if ((m->photonTolerance -= I) < 0) { smds_molec *p = m; I += m->photonTolerance; s->numBleached[m->species]++; *binCount += I; m = m->next; if (p->prev) p->prev->next = p->next; else s->molecs = p->next; // head of list if (p->next) p->next->prev = p->prev; p->prev = NULL; p->next = s->avail; s->avail = p; continue; } #endif *binCount += I; } // in threshold m = m->next; } // each molecule } // each step binCount++; } // each bin s->t_insert = t_insert; iData = (Int16*)o->data + pos; for (i=0; i < dur; i++) s->I += (double)(iData[i] = Poisson(data[i], &s->rs)); Py_END_ALLOW_THREADS free(data); Py_RETURN_NONE; } PyObject * smds_core_getResults(PyObject *self) { smds_core *c = (smds_core*)self; PyObject *r, *o, *l = NULL; int i; if (!ResultsType) { PyErr_SetString(PyExc_SystemError, "Couldn't find ResultsType."); return NULL; } r = PyInstance_New(ResultsType, NULL, NULL); if (!r) return NULL; o = PyFloat_FromDouble(c->I/(double)c->dur/c->bw); if (!o) goto bail; if (PyObject_SetAttrString(r, "avg_I", o) == -1) goto bail; Py_DECREF(o); o = PyFloat_FromDouble((double)c->dur*c->bw*1e-3); if (!o) goto bail; if (PyObject_SetAttrString(r, "length", o) == -1) goto bail; Py_DECREF(o); l = PyList_New(c->numSpecies); if (!l) goto bail; for(i=0; i < c->numSpecies; i++) { o = PyInt_FromLong(c->counts[i]); if (!o) goto bail; if (PyList_SetItem(l, i, o)) goto bail; // steals reference } o = NULL; if (PyObject_SetAttrString(r, "counts", l) == -1) goto bail; Py_DECREF(l); #ifdef CORE_BLEACH l = PyList_New(c->numSpecies); if (!l) goto bail; for(i=0; i < c->numSpecies; i++) { o = PyInt_FromLong(c->numBleached[i]); if (!o) goto bail; if (PyList_SetItem(l, i, o)) goto bail; // steals reference } o = NULL; if (PyObject_SetAttrString(r, "bleached", l) == -1) goto bail; Py_DECREF(l); #endif return r; bail: Py_DECREF(r); if (o) { Py_DECREF(o); } if (l) { Py_DECREF(l); } return NULL; } void smds_core_free(smds_core *p) { smds_molec *m; if (!p) return; free(p->X); free(p->a); #ifdef CORE_BLEACH free(p->tol); #endif free(p->size); free(p->minX); free(p->maxX); #ifdef CORE_INTENS if (p->intens) { Py_DECREF((PyObject*)p->intens); } free(p->sc_x); #ifdef CORE_3D free(p->sc_z); #endif #else free(p->threshold); free(p->b); #ifdef CORE_3D free(p->c); free(p->threshold_z); #endif #endif free(p->counts); #ifdef CORE_BLEACH free(p->numBleached); #endif free(p->flow_x); while (p->molecs) { m = p->molecs; p->molecs = p->molecs->next; free(m); } while (p->avail) { m = p->avail; p->avail = p->avail->next; free(m); } p->ob_type->tp_free((PyObject*)p); } PyObject * smds_core_getParamsType(PyObject *cls) { if (ParamsType) { Py_INCREF(ParamsType); } return ParamsType; } PyObject * smds_core_getResultsType(PyObject *cls) { if (ResultsType) { Py_INCREF(ResultsType); } return ResultsType; } PyObject * smds_core_getName(PyObject *cls) { return PyString_FromString(str(CORE_NAME)); } PyObject * smds_core_sRnd(PyObject *self, PyObject *args) { if (PyTuple_Size(args) != 1) { initRnd(); } else { int seed; if (!PyArg_ParseTuple(args, "k", &seed)) return NULL; seedRnd(seed, NULL); } Py_RETURN_NONE; }
void GradFragment::createFeatures(InstInfo& t, bool forceRnd) { // from the current world state, produce pattern int method = static_cast<GradHarness*>(_parHarness)->_gradCreateMethod; if (method == gradThresh) initThresh(t, forceRnd); else if (method == gradRandom) initRnd(t); }