Ejemplo n.º 1
0
void gen_bulge(int nptcl, int myseed, float *buffer, int verbose)
{
	int i, j, k, nobj=10000;
	int seed= -123;
	int icofm=1;
	float q=0.9, rtrunc=5.0;
	float rhoran, massapp;
	float x, y, z, vx, vy, v, v2, R, vmax, vmax2;
	float phi, cph, sph, cth, sth, vR, vp, vz;
	float E, Lz, rad, rad2;
	float f0, frand, fmax, psi;
	float t, mass;
	float dr;
	float u1, v1, u1max, v1max;
	float xcm, ycm, zcm, vxcm, vycm, vzcm;
	float zip = 0.0, psicut;
	float rhomax=0.15, rhomin, rhotst;
	float rhomax1;
	float stream=0.5;
	float bulgedens_(), pot_(), dfbulge_();
	float ran1();
	char harmfile[80];
	float fcut, fac, ebh, psic_bh;
	float dfmax, emax, psibh;
	float b, c, d, int_const, sig2, aeff;
	float v02, vb2, coef1, coef2;
	float arg;
	float psi_n, psic_bulge_n;
	int   itest;

  if (verbose)
    fprintf(stderr,"The Bulge\n");
	(void) strcpy(harmfile,"dbh.dat");
  stream = 0.5;
  nobj = nptcl;
  seed = myseed;
  icofm = 1;
  mysrand(myseed);
  if (nobj <= 0)
  {
    fquery("Enter streaming fraction",&stream);
    iquery("Enter the number of particles",&nobj);
    iquery("Enter negative integer seed",&seed);
    iquery("Center particles (0=no, 1=yes)",&icofm);
    cquery("Enter harmonics file",harmfile);
  }

	readmassrad(); /* reads in mass and truncation radius of components */
	readharmfile_(harmfile,&gparam,&cparam,&bparam,&flags);
	itest = flags.ibulgeflag;
	if (itest == 0) {return 0;}
	readdenspsibulge_();

	c = gparam.c; v0 = gparam.v0; a = gparam.a; 
	abulge = gparam.abulge; 
	cbulge = gparam.cbulge; v0bulge=gparam.v0bulge;
	psi0 = gparam.psi0; bulgeconst = gparam.bulgeconst;

	psic_bulge = cparam.psic_bulge; 
	psic_halo=cparam.psic_halo;
	bhmass = bparam.bhmass; 

	rtrunc = bulgeedge;
	mass = bulgemass/nobj; 
	sig2 = -psi0;
	u1max = rtrunc;
	v1max = 0.5*M_PI;
	psic_bulge_n = -psic_bulge;
	fcut = dfbulge_(&psic_bulge_n);
  	dfmaximumbulge_(&emax,&dfmax); 
	r = (phase *) calloc(nobj,sizeof(phase));

	if(bhmass != 0 ) {
	  psic_bh = bhmass/rtrunc;
	  effectivescalelength_(&aeff);
	  d = bhmass/aeff;
	  int_const = d/(1-psic_bulge_n/sig2)-psic_bh; 
	} else {
	  psic_bh = 0.;
	  d = 0.;
	  int_const = 0.;
	}
/* Find maximum of rho*r^2 */

	dr = rtrunc/100;
	rhomax1 = 0.0;
	for(i=0; i<1000; i++) {
		float z, rcyl, rhocur;
        rcyl = i*dr;
        z = 0;
		rhocur = bulgedens_(&rcyl,&z);

/*
fprintf(stdout,"%g %g %g\n",i*dr,rhocur,pot_(&rcyl,&z));
*/
        rhocur *= (rcyl*rcyl);
		if( rhocur > rhomax1  )
			rhomax1 = rhocur;
  }
  if (verbose)
    fprintf(stderr,"rhomax1 = %g\n",rhomax1);
  rhomax = 1.5*rhomax1;
  rhomin = 1.0e-10*rhomax;  /* define density contour cutoff */

  if (verbose)  
    fprintf(stderr,"Calculating bulge particles positions and velocities\n");
  for(i=0; i<nobj;) {

    u1 = u1max*ran1(&seed);
    v1 = 2.0*v1max*(ran1(&seed) - 0.5);
    R = u1;
    z = R*tan(v1);


    rhotst = bulgedens_(&R,&z);
    rhotst = rhotst*(R*R + z*z);

    j++;
    if( rhotst < rhomin )
      continue;

    rhoran = (rhomax - rhomin)*ran1(&seed);
    if( rhoran > rhotst )
      continue;
    phi = 2.0*M_PI*ran1(&seed);
    x = R*cos(phi);
    y = R*sin(phi);

    psi = pot_(&R,&z);
    psi_n = -psi;
    psibh = bhmass/sqrt(R*R+z*z);
    vmax2 = 2.0*(psi_n + psibh - psic_bulge_n - psic_bh);
    vmax = sqrt(vmax2);
    if( psi_n > emax ) {
      fmax = dfmax;	
    }
    else {
      dfcorrectionbulge_(&psi_n,&fac);
      fmax = (dfbulge_(&psi_n)-fcut)/fac;
    } 
    f0 = 0.0; frand = 1.0; /* dummy starters */
    j = 0;
    while( frand > f0 ) {
      /* select a random speed < the escape speed */

      v2 = 1.1*vmax2;
      while( v2 > vmax2 ) {
        vx = 2*vmax*(ran1(&seed) - 0.5);
        vy = 2*vmax*(ran1(&seed) - 0.5);
        vz = 2*vmax*(ran1(&seed) - 0.5);
        v2 = vx*vx + vy*vy + vz*vz;
      }
      ebh = psi_n + psibh - 0.5*v2;
      b = sig2 + ebh + int_const;
      c = sig2 - ebh - int_const; 
      E = 0.5*(b-sqrt(c*c+4.*d));
      dfcorrectionbulge_(&E,&fac);
      f0 = (dfbulge_(&E)-fcut)/fac;
      frand = fmax*ran1(&seed);
      j++;
    }

    /* Streaming of the bulge */
    vR = (vx*x + vy*y)/R;
    vp = (-vx*y + vy*x)/R;
    if( ran1(&seed) < stream )
      vp = fabs(vp);
    else
      vp = -fabs(vp);
    vx = (vR*x - vp*y)/R;
    vy = (vR*y + vp*x)/R;

    /* sanity check */
    if (isnan(x)) continue;
    if (isnan(y)) continue;
    if (isnan(z)) continue;
    if (isnan(vx)) continue;
    if (isnan(vy)) continue;
    if (isnan(vz)) continue;
    const float RMAX = 200;
    const float VMAX = 10;
    if (abs(x) > RMAX) continue;
    if (abs(y) > RMAX) continue;
    if (abs(z) > RMAX) continue;
    if (abs(vx) > VMAX) continue;
    if (abs(vy) > VMAX) continue;
    if (abs(vz) > VMAX) continue;

    r[i].x = (float) x;
    r[i].y = (float) y;
    r[i].z = (float) z;
    r[i].vx = (float)vx;
    r[i].vy = (float)vy;
    r[i].vz = (float)vz;
    i++;
    if (verbose)
      if( i % 1000 == 0 ) fprintf(stderr,".");
  }
  if (verbose)
    fprintf(stderr,"\n");

  if( icofm ) {
    xcm = ycm =zcm = vxcm =vycm =vzcm = 0;
    for(i=0; i<nobj; i++) {
      xcm += r[i].x;
      ycm += r[i].y;
      zcm += r[i].z;
      vxcm += r[i].vx;
      vycm += r[i].vy;
      vzcm += r[i].vz;
    }
    xcm /= nobj; ycm /=nobj; zcm /= nobj;
    vxcm /= nobj; vycm /=nobj; vzcm /= nobj;

    for(i=0; i<nobj; i++) {
      r[i].x -= xcm;
      r[i].y -= ycm;
      r[i].z -= zcm;
      r[i].vx -= vxcm;
      r[i].vy -= vycm;
      r[i].vz -= vzcm;
    }
  }

  if (buffer == NULL)
  {
#ifdef ASCII
    t = 0.0;
    fprintf(stdout,"%d\n",nobj);
    for(i=0; i<nobj; i++) {
      fprintf(stdout,"% 15.7e % 15.7e % 15.7e % 15.7e % 15.7e % 15.7e % 15.7e\n", mass, r[i].x, r[i].y, r[i].z, r[i].vx, r[i].vy, r[i].vz);
    }
#else
    for(i=0; i<nobj; i++) {
      fwrite(&mass,sizeof(float),1,stdout);
      fwrite(r+i,sizeof(phase),1,stdout);
    }
#endif
  }
  else
  {
    const int min_bulge = 100000000;
    const int max_bulge = 200000000;
    int NEL = 8;
    int i,pc;
    for (i = 0, pc= 0; i < nobj; i++, pc += NEL)
    {
      *((int*)&buffer[pc]) = min_bulge + (i%(max_bulge-min_bulge));
      buffer[pc+1] = mass;
      buffer[pc+2] = r[i].x;
      buffer[pc+3] = r[i].y;
      buffer[pc+4] = r[i].z;
      buffer[pc+5] = r[i].vx;
      buffer[pc+6] = r[i].vy;
      buffer[pc+7] = r[i].vz;
    }
  }
}
Ejemplo n.º 2
0
// import old diseqc configuration into tree
bool convert_diseqc_db(void)
{
    MSqlQuery cquery(MSqlQuery::InitCon());
    cquery.prepare(
        "SELECT cardid, dvb_diseqc_type "
        "FROM capturecard"
        "WHERE dvb_diseqc_type IS NOT NULL AND "
        "      diseqcid IS NULL");

    // iterate through cards
    if (!cquery.exec())
        return false;

    MSqlQuery iquery(MSqlQuery::InitCon());
    iquery.prepare(
        "SELECT cardinputid,    diseqc_port, diseqc_pos, "
        "       lnb_lof_switch, lnb_lof_hi,  lnb_lof_lo  "
        "FROM cardinput "
        "WHERE cardinput.cardid = :CARDID");

    while (cquery.next())
    {
        uint cardid = cquery.value(0).toUInt();
        OLD_DISEQC_TYPES type = (OLD_DISEQC_TYPES) cquery.value(1).toUInt();

        DiSEqCDevTree    tree;
        DiSEqCDevDevice *root     = NULL;
        uint             add_lnbs = 0;
        DiSEqCDevLNB::dvbdev_lnb_t lnb_type =
            DiSEqCDevLNB::kTypeVoltageAndToneControl;

        // create root of tree
        switch (type)
        {
            case DISEQC_SINGLE:
            {
                // single LNB
                root = DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeLNB);
                break;
            }

            case DISEQC_MINI_2:
            {
                // tone switch + 2 LNBs
                root = DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeSwitch);
                DiSEqCDevSwitch *sw = dynamic_cast<DiSEqCDevSwitch*>(root);
                if (sw)
                {
                    sw->SetType(DiSEqCDevSwitch::kTypeTone);
                    sw->SetNumPorts(2);
                    add_lnbs = 2;
                }
                break;
            }

            case DISEQC_SWITCH_2_1_0:
            case DISEQC_SWITCH_2_1_1:
            {
                // 2 port diseqc + 2 LNBs
                root = DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeSwitch);
                DiSEqCDevSwitch *sw = dynamic_cast<DiSEqCDevSwitch*>(root);
                if (sw)
                {
                    sw->SetType(DiSEqCDevSwitch::kTypeDiSEqCCommitted);
                    sw->SetAddress(0x10);
                    sw->SetNumPorts(2);
                    add_lnbs = 2;
                }
                break;
            }

            case DISEQC_SWITCH_4_1_0:
            case DISEQC_SWITCH_4_1_1:
            {
                // 4 port diseqc + 4 LNBs
                root = DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeSwitch);
                DiSEqCDevSwitch *sw = dynamic_cast<DiSEqCDevSwitch*>(root);
                if (sw)
                {
                    sw->SetType(DiSEqCDevSwitch::kTypeDiSEqCCommitted);
                    sw->SetAddress(0x10);
                    sw->SetNumPorts(4);
                    add_lnbs = 4;
                }
                break;
            }

            case DISEQC_POSITIONER_1_2:
            {
                // non-usals positioner + LNB
                root = DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeRotor);
                DiSEqCDevRotor *rotor = dynamic_cast<DiSEqCDevRotor*>(root);
                if (rotor)
                {
                    rotor->SetType(DiSEqCDevRotor::kTypeDiSEqC_1_2);
                    add_lnbs = 1;
                }
                break;
            }

            case DISEQC_POSITIONER_X:
            {
                // usals positioner + LNB (diseqc_pos)
                root = DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeRotor);
                DiSEqCDevRotor *rotor = dynamic_cast<DiSEqCDevRotor*>(root);
                if (rotor)
                {
                    rotor->SetType(DiSEqCDevRotor::kTypeDiSEqC_1_3);
                    add_lnbs = 1;
                }
                break;
            }

            case DISEQC_POSITIONER_1_2_SWITCH_2:
            {
                // 10 port uncommitted switch + 10 LNBs
                root = DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeSwitch);
                DiSEqCDevSwitch *sw = dynamic_cast<DiSEqCDevSwitch*>(root);
                if (sw)
                {
                    sw->SetType(DiSEqCDevSwitch::kTypeDiSEqCUncommitted);
                    sw->SetNumPorts(10);
                    add_lnbs = 10;
                }
                break;
            }

            case DISEQC_SW21:
            {
                // legacy SW21 + 2 fixed lnbs
                root = DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeSwitch);
                DiSEqCDevSwitch *sw = dynamic_cast<DiSEqCDevSwitch*>(root);
                if (sw)
                {
                    sw->SetType(DiSEqCDevSwitch::kTypeLegacySW21);
                    sw->SetNumPorts(2);
                    add_lnbs = 2;
                    lnb_type = DiSEqCDevLNB::kTypeFixed;
                }
                break;
            }

            case DISEQC_SW64:
            {
                // legacy SW64 + 3 fixed lnbs
                root = DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeSwitch);
                DiSEqCDevSwitch *sw = dynamic_cast<DiSEqCDevSwitch*>(root);
                if (sw)
                {
                    sw->SetType(DiSEqCDevSwitch::kTypeLegacySW64);
                    sw->SetNumPorts(3);
                    add_lnbs = 3;
                    lnb_type = DiSEqCDevLNB::kTypeFixed;
                }
                break;
            }

            default:
            {
                LOG(VB_GENERAL, LOG_ERR, "Unknown DiSEqC device type " +
                        QString("%1 ignoring card %2").arg(type).arg(cardid));
                break;
            }
        }

        if (!root)
            continue;

        tree.SetRoot(root);

        // create LNBs
        for (uint i = 0; i < add_lnbs; i++)
        {
            DiSEqCDevLNB *lnb = dynamic_cast<DiSEqCDevLNB*>
                (DiSEqCDevDevice::CreateByType(
                    tree, DiSEqCDevDevice::kTypeLNB));
            if (lnb)
            {
                lnb->SetType(lnb_type);
                lnb->SetDescription(QString("LNB #%1").arg(i+1));
                if (!root->SetChild(i, lnb))
                    delete lnb;
            }
        }

        // save the tree to get real device ids
        tree.Store(cardid);

        // iterate inputs
        DiSEqCDevSettings set;
        iquery.bindValue(":CARDID", cardid);

        if (!iquery.exec())
            return false;

        while (iquery.next())
        {
            uint inputid = iquery.value(0).toUInt();
            uint port = iquery.value(1).toUInt();
            double pos = iquery.value(2).toDouble();
            DiSEqCDevLNB *lnb = NULL;

            // configure LNB and settings
            switch (type)
            {
                case DISEQC_SINGLE:
                    lnb = dynamic_cast<DiSEqCDevLNB*>(root);
                    break;

                case DISEQC_MINI_2:
                case DISEQC_SWITCH_2_1_0:
                case DISEQC_SWITCH_2_1_1:
                case DISEQC_SWITCH_4_1_0:
                case DISEQC_SWITCH_4_1_1:
                case DISEQC_SW21:
                case DISEQC_SW64:
                case DISEQC_POSITIONER_1_2_SWITCH_2:
                    lnb = dynamic_cast<DiSEqCDevLNB*>(root->GetChild(port));
                    set.SetValue(root->GetDeviceID(), port);
                    break;

                case DISEQC_POSITIONER_1_2:
                case DISEQC_POSITIONER_X:
                    lnb = dynamic_cast<DiSEqCDevLNB*>(root->GetChild(0));
                    set.SetValue(root->GetDeviceID(), pos);
                    break;

                default:
                    break;
            }

            // configure lnb
            if (lnb)
            {
                lnb->SetLOFSwitch(iquery.value(3).toUInt());
                lnb->SetLOFHigh(iquery.value(4).toUInt());
                lnb->SetLOFLow(iquery.value(5).toUInt());
            }

            // save settings
            set.Store(inputid);
        }

        // save any LNB changes
        tree.Store(cardid);

        // invalidate cached devices
        DiSEqCDev trees;
        trees.InvalidateTrees();
    }

    return true;
}
Ejemplo n.º 3
0
static unsigned long Evaluate_Expression(struct expValueTable_data *vtable_data)
{
    struct header_complex_index *hcindex;
    struct expObjectTable_data *objstorage, *objfound;
    struct expValueTable_data *const valstorage = vtable_data;
    const char     *expression;
    char           *result, *resultbak;
    char           *temp, *tempbak;
    int             i = 0, j, l;
    unsigned long   result_u_long = 0;
    static int      level;

    temp = malloc(100);
    result = malloc(100);
    tempbak = temp;
    memset(result, 0, 100);
    *result = '\0';
    resultbak = result;

    level++;

    if (level > 1) {
        snmp_log(LOG_ERR, "%s: detected recursion\n", __func__);
        goto out;
    }

    expression = vtable_data->expression_data->expExpression;

    DEBUGMSGTL(("expValueTable", "%s(%s.%s): evaluating %s\n", __func__,
                valstorage->expExpressionOwner, valstorage->expExpressionName,
                expression));

    while (*expression != '\0') {
        if (*expression == '$') {
            objfound = NULL;
            i++;
            for (j = 1; j < 100; j++) {
                if ((*(expression + j) == '+') ||
                    (*(expression + j) == '-') ||
                    (*(expression + j) == '*') ||
                    (*(expression + j) == '/') ||
                    (*(expression + j) == '(') ||
                    (*(expression + j) == ')') ||
                    *(expression + j) == '\0') {
                    break;
                }
            }
            sprintf(temp, "%.*s", j - 1, expression + 1);
            l = atoi(temp);
            expression = expression + j;
            /*
             *   here use snmpget to get value
             */
            for (hcindex = expObjectTableStorage; hcindex != NULL;
                 hcindex = hcindex->next) {
                objstorage = (struct expObjectTable_data *) hcindex->data;
                if (!strcmp
                    (objstorage->expExpressionOwner,
                     valstorage->expExpressionOwner)
                    && (objstorage->expExpressionOwnerLen ==
                        valstorage->expExpressionOwnerLen)
                    && !strcmp(objstorage->expExpressionName,
                               valstorage->expExpressionName)
                    && (objstorage->expExpressionNameLen ==
                        valstorage->expExpressionNameLen)
                    && (l == objstorage->expObjectIndex)) {
                    objfound = objstorage;
                    break;
                }
            }

            if (!objfound) {
                snmp_log(LOG_ERR, "%s: lookup of expression %s.%s failed\n",
                         __func__, valstorage->expExpressionOwner,
                         valstorage->expExpressionName);
                goto out;
            }

            DEBUGMSGTL(("expValueTable", "%s: Found OID ", __func__));
            DEBUGMSGOID(("expValueTable", objfound->expObjectID,
                         objfound->expObjectIDLen));
            DEBUGMSG(("expValueTable", "%s\n",
                      objfound->expObjectIDWildcard ==
                      EXPOBJCETIDWILDCARD_TRUE ? "(wildcard)" : ""));

            oid             anOID[MAX_OID_LEN];
            size_t          anOID_len;

            memcpy(anOID, objfound->expObjectID,
                   objfound->expObjectIDLen * sizeof(oid));
            anOID_len = objfound->expObjectIDLen;
            if (objfound->expObjectIDWildcard == EXPOBJCETIDWILDCARD_TRUE) {
                anOID_len =
                    anOID_len + valstorage->expValueInstanceLen - 2;
                memcpy(anOID + objfound->expObjectIDLen,
                       valstorage->expValueInstance + 2,
                       (valstorage->expValueInstanceLen -
                        2) * sizeof(oid));
            }

            struct variable_list *vars;
            int             rc;

            rc = iquery(&vars,
                        (char *)vtable_data->expression_data->pdu_community,
                        vtable_data->expression_data->pdu_version,
                        anOID, anOID_len);
            if (rc != SNMP_ERR_NOERROR)
                snmp_log(LOG_ERR, "Error in packet: %s\n", snmp_errstring(rc));
            sprintf(result, "%lu", rc == SNMP_ERR_NOERROR ?
                    *(vars->val.integer) : 0);
            result += strlen(result);
        } else {
            *result++ = *expression++;
        }
    }
    result_u_long = get_result(resultbak);
    DEBUGMSGTL(("expValueTable", "%s(%s.%s): evaluated %s into %ld\n", __func__,
                valstorage->expExpressionOwner, valstorage->expExpressionName,
                resultbak, result_u_long));

out:
    free(tempbak);
    free(resultbak);
    level--;
    return result_u_long;
}
Ejemplo n.º 4
0
static void build_valuetable(void)
{
    struct expExpressionTable_data *expstorage;
    struct expObjectTable_data *objstorage, *objfound = NULL;
    struct header_complex_index *hcindex, *object_hcindex;
    const char     *expression;
    oid            *index;
    int             i = 0, j, l;

    DEBUGMSGTL(("expValueTable", "building valuetable...  \n"));

    for (hcindex = expExpressionTableStorage; hcindex != NULL;
         hcindex = hcindex->next) {
        expstorage = (struct expExpressionTable_data *) hcindex->data;
        if (expstorage->expExpressionEntryStatus == RS_ACTIVE) {
            expression = expstorage->expExpression;
            while (*expression != '\0') {
                if (*expression == '$') {
                    i++;
                    for (j = 1; j < 100; j++) {
                        if ((*(expression + j) == '+') ||
                            (*(expression + j) == '-') ||
                            (*(expression + j) == '*') ||
                            (*(expression + j) == '/') ||
                            (*(expression + j) == '(') ||
                            (*(expression + j) == ')') ||
                            *(expression + j) == '\0') {
                            break;
                        }
                    }
                    {
                        char temp[100];

                        sprintf(temp, "%.*s", j - 1, expression + 1);
                        l = atoi(temp);
                    }
                    for (object_hcindex = expObjectTableStorage;
                         object_hcindex != NULL;
                         object_hcindex = object_hcindex->next) {
                        objstorage =
                            (struct expObjectTable_data *) object_hcindex->
                            data;
                        if (!strcmp
                            (objstorage->expExpressionOwner,
                             expstorage->expExpressionOwner)
                            && (objstorage->expExpressionOwnerLen ==
                                expstorage->expExpressionOwnerLen)
                            && !strcmp(objstorage->expExpressionName,
                                       expstorage->expExpressionName)
                            && (objstorage->expExpressionNameLen ==
                                expstorage->expExpressionNameLen)
                            && (l == objstorage->expObjectIndex)) {
                            if (objfound == NULL) {
                                objfound = objstorage;
                            }
                            if (objstorage->expObjectIDWildcard ==
                                EXPOBJCETIDWILDCARD_TRUE)
                                objfound = objstorage;
                        }
                    }
                    expression = expression + j;
                } else {
                    expression++;
                }
            };
        }

        if (!objfound) {
            continue;
        }
        if (objfound->expObjectIDWildcard == EXPOBJCETIDWILDCARD_FALSE) {
            index = calloc(1, MAX_OID_LEN);
            *index = 0;
            *(index + 1) = 0;
            *(index + 2) = 0;
            expValueTable_set(expstorage, objfound->expExpressionOwner,
                              objfound->expExpressionOwnerLen,
                              objfound->expExpressionName,
                              objfound->expExpressionNameLen, index, 3);
        } else {
            oid            *targetOID = objfound->expObjectID;
            size_t          taggetOID_len = objfound->expObjectIDLen;
            oid            *next_OID;
            size_t          next_OID_len;
            struct variable_list *vars;
            int             rc;

            next_OID = targetOID;
            next_OID_len = taggetOID_len;
            do {
                index = calloc(1, MAX_OID_LEN);

                rc = iquery(&vars, (char *)expstorage->pdu_community,
                            expstorage->pdu_version, next_OID, next_OID_len);
                if (rc == SNMP_ERR_NOERROR) {
                    if (((vars->type >= SNMP_NOSUCHOBJECT &&
                          vars->type <= SNMP_ENDOFMIBVIEW)
                         || snmp_oid_compare(targetOID, taggetOID_len,
                                             vars->name,
                                             taggetOID_len) != 0)) {
                        break;
                    }
                    /* add to expValueTable */

                    *index = 0;
                    *(index + 1) = 0;
                    memcpy(index + 2, vars->name + taggetOID_len,
                           (vars->name_length - taggetOID_len) * sizeof(oid));
                    expValueTable_set(expstorage,
                                      objfound->expExpressionOwner,
                                      objfound->expExpressionOwnerLen,
                                      objfound->expExpressionName,
                                      objfound->expExpressionNameLen,
                                      index,
                                      vars->name_length -
                                      taggetOID_len + 2);

                    next_OID = vars->name;
                    next_OID_len = vars->name_length;
                } else {
                    snmp_log(LOG_ERR, "Error in packet: %s\n",
                             snmp_errstring(rc));
                }
            } while (TRUE);
        }
    }
}
Ejemplo n.º 5
0
void gen_disk(int nptcl, int myseed, float *buffer, int verbose)
{
  dotalk = verbose;
	int i, j, k, nobj=10000;
	int seed= -123;
	int icofm=1;
	float q=0.9, rtrunc=5.0;
	float rhoran, massapp;
	float x, y, z, vx, vy, v, v2, R, vmax, vmax2;
	float phi, cph, sph, cth, sth, vR, vp, vz;
	float E, Lz, rad, rad2;
	float f0, frand, fmax, fmax1, vphimax1, psi, fnorm;
	float Fdisk();
	float ran1();
	float invu();
	float dr, rhomax1;
	float t, mass;
	float u1, v1, u1max, v1max;
	float xcm, ycm, zcm, vxcm, vycm, vzcm;
	float zip = 0.0, psi0;
	float rhomax=0.15, rhomin, rhotst;
	float rhoguess, zcon;
	float stream=0.5;
	float con, outdisk, drtrunc;
	float omega, kappa;
	float dv, v0;
	float broadcon=1.0;
	float vfacR, vfacz;
	float f1;
	float gr, gp, gz, g2;
	float vsigmax, vmean, vsig2;
	float gasdev();
	float diskdensf_(), sigr2_(), sigz2_();
	float FindMax(), FindMax1(), Fmax();
	float simpson(), massring();
	float velocityfactors_();
//	FILE *rhoout;
//	FILE *errfile;
	char harmfile[80];

//	errfile = fopen("errmsg.dat","w"); 
  if(verbose)
    fprintf(stderr,"The Disk\n");
	strcpy(harmfile,"dbh.dat");
  nobj = nptcl;
  seed = myseed;
  mysrand(myseed);
  icofm = 1;
  if (nobj <= 0)
  {
    iquery("Enter the number of particles",&nobj);
    iquery("Enter negative integer seed",&seed);
    iquery("Center the simulation (0=no,1=yes)",&icofm);
    cquery("Enter harmonics file",harmfile);
  }
  

	r = (phase *) calloc(nobj,sizeof(phase));

	readdiskdf_(harmfile,&gparam);
	mdisk = gparam.mdisk; rdisk = gparam.rdisk; zdisk = gparam.zdisk;
	outdisk = gparam.outdisk; drtrunc = gparam.drtrunc;
	rd = 1.2*rdisk;
	zd = 1.2*zdisk;
	rtrunc = (outdisk + 2.0*drtrunc);
	diskmass = 4.0*M_PI*simpson(massring,0.0,rtrunc,128);
	mass   = diskmass/nobj;
	dr = rtrunc/100.;
	rhomax = 0.0;
//	rhoout = fopen("rhotst.dat","w");
	for(i=0; i<100; i++) {
	  R = i*dr;
	  z = 0.0;
	  rhoguess = exp(-R/rd);
	  rhotst = diskdensf_(&R,&z);
	  rhotst /= rhoguess;
	  if( rhotst > rhomax ) rhomax = rhotst;
//	  fprintf(rhoout,"%g %g\n",R, rhotst);
	}
	rhomin = 0.0;
	rhomax *=1.2;
#if 0
	{
		FILE *disksig;
		float rad, vsigR, vsigp;;

		disksig = fopen("disksig.dat","w");
		for(rad=0.1; rad<30.0; rad+=0.1) {
			omekap_(&rad,&omega,&kappa);
			vsigR = sqrt(sigr2_(&rad));
			vsigp = kappa/(2.0*omega)*vsigR;
			fprintf(disksig,"%g %g %g %g %g\n",rad,vsigp,vsigR,omega,kappa);
		}
		fclose(disksig);
	}
#endif

/*
	tstFdisk();
*/
  if (verbose)
    fprintf(stderr,"Calculating disk positions and velocities\n");
	vmean = 0;  vsig2 = 0;
	for(i=0, j=0, k=0; i<nobj;) {

	  u1 = -ran1(&seed);
	  v1 = 2.0*(ran1(&seed) - 0.5);
	  R = rd*invu(u1);
	  z = zd*atanh(v1);
	  
	  zcon = cosh(z/zd);
	  rhoguess = exp(-R/rd)/(zcon*zcon);
	  /* Guess at the approximate functional form of the density */
	  
	  rhotst = diskdensf_(&R,&z);
	  rhotst /= rhoguess;
	  
	  k++;
	  if( rhotst < rhomin )
	    continue;
	  
	  rhoran = (rhomax - rhomin)*ran1(&seed);
	  if( rhoran > rhotst )
	    continue;
	  phi = 2.0*M_PI*ran1(&seed);
	  x = R*cos(phi);
	  y = R*sin(phi);
	  omekap_(&R, &omega, &kappa);
	  vphimax = omega*R;	  
	  vsigR = sqrt(sigr2_(&R));
	  vsigp = kappa/(2.0*omega)*vsigR;
	  vsigz = sqrt(sigz2_(&R));
	  vsigmax = vsigR;
	  if( vsigp > vsigmax ) vsigmax = vsigp;
	  if( vsigz > vsigmax ) vsigmax = vsigz;


	  fmax = 1.1*FindMax(R,z,&vphimax);
/*
	  fmax1 = 1.1*FindMax1(R,z,&vphimax1);
	  fprintf(stderr,"fmax %g fmax1 %g vphimax %g vphimax1 %g\n",
			fmax, fmax1, vphimax, vphimax1);
*/
	  f0 = 0.0; frand = 1.0; /* dummy starters */
	  while( frand > f0 ) {
			  /*
		g2 = 999.;
	    while( g2 > 1.0) {
	      gr = 2.*(ran1(&seed) - 0.5);
	      gp = 2.*(ran1(&seed) - 0.5);
	      gz = 2.*(ran1(&seed) - 0.5);
	      g2 = (gr*gr + gp*gp + gz*gz);
	    }
		*/

		gr = 6.0*(ran1(&seed) - 0.5)*vsigR;
	    gp = 6.0*(ran1(&seed) - 0.5)*vsigp;
		gz = 6.0*(ran1(&seed) - 0.5)*vsigz;
/*
	    gp = (vphimax + 3.0*vsigp)*ran1(&seed) - vphimax;
*/
		/*
		gr = 3.0*vsigmax;
		gp = 3.0*vsigmax;
		gz = 3.0*vsigmax;
		*/
	    
	    vR = gr;
	    vp = vphimax + gp;
	    vz = gz;

	    f0 = Fdisk(vR, vp, vz, R, z);
	    frand = fmax*ran1(&seed);
#if 0
	    if( f0 > fmax ) {
	      float vpmax;
	      fprintf(errfile,"f0 > fmax at R=%g z=%g\nvr=%g vp=%g, vz=%g vphimax=%g f0=%g, fmax=%g\n", 
		      R,z, 
		      vR*broadcon/vsigR, 
		      (vp - vphimax)*broadcon/vsigp, 
		      vz*broadcon/vsigz, vphimax, f0, fmax);
	      fflush(errfile);
	    }
#endif
	    j++;
	  }
	  
	  velocityfactors_(&R, &z, &vfacR, &vfacz);
	  vphimax = vp - gp;
	  vphimax *= vfacR;
	  vp = vphimax + gp;
	  vz *= vfacz;
	  /*
	  fprintf(stdout,"%g %g\n",vp,frand);
	  */

	  cph = x/R; sph = y/R;
	  vx = vR*cph - vp*sph;
	  vy = vR*sph + vp*cph;
	  /*  vz stays the same */

    /* sanity check */
    if (isnan(x)) continue;
    if (isnan(y)) continue;
    if (isnan(z)) continue;
    if (isnan(vx)) continue;
    if (isnan(vy)) continue;
    if (isnan(vz)) continue;
    const float RMAX = 200;
    const float VMAX = 10;
    if (abs(x) > RMAX) continue;
    if (abs(y) > RMAX) continue;
    if (abs(z) > RMAX) continue;
    if (abs(vx) > VMAX) continue;
    if (abs(vy) > VMAX) continue;
    if (abs(vz) > VMAX) continue;

	  r[i].x = (float) x;
	  r[i].y = (float) y;
	  r[i].z = (float) z;
	  r[i].vx = (float)vx;
	  r[i].vy = (float)vy;
	  r[i].vz = (float)vz;
	  i++;
    if (verbose)
      if( i % 1000 == 0 ) {
        fprintf(stderr,".");
        fflush(stderr);
      }
  }
  if (verbose)
  {
    fprintf(stderr,"\n");
    fprintf(stderr,"number of density trials %d\n",k);
    fprintf(stderr,"number of velocity trials %d\n",j);
  }

  if( icofm ) {
    xcm = ycm =zcm = vxcm =vycm =vzcm = 0;
    for(i=0; i<nobj; i++) {
      xcm += r[i].x;
      ycm += r[i].y;
      zcm += r[i].z;
      vxcm += r[i].vx;
      vycm += r[i].vy;
      vzcm += r[i].vz;
    }
    xcm /= nobj; ycm /=nobj; zcm /= nobj;
    vxcm /= nobj; vycm /=nobj; vzcm /= nobj;

    for(i=0; i<nobj; i++) {
      r[i].x -= xcm;
      r[i].y -= ycm;
      r[i].z -= zcm;
      r[i].vx -= vxcm;
      r[i].vy -= vycm;
      r[i].vz -= vzcm;
    }
  }


  if (buffer == NULL)
  {
    t = 0.0;
#ifdef ASCII
    fprintf(stdout,"%d\n",nobj);
    for(i=0; i<nobj; i++) {
      fprintf(stdout,"% 15.7e % 15.7e % 15.7e % 15.7e % 15.7e % 15.7e % 15.7e\n", mass, r[i].x, r[i].y, r[i].z, r[i].vx, r[i].vy, r[i].vz);
    }
#else
    for(i=0; i<nobj; i++)  {
      fwrite(&mass,sizeof(float),1,stdout);
      fwrite(r+i,sizeof(phase),1,stdout);
    }
#endif
  }
  else
  {
    const int min_disk = 000000000;
    const int max_disk = 100000000;
    int NEL = 8;
    int i,pc;
    for (i = 0, pc= 0; i < nobj; i++, pc += NEL)
    {
      *((int*)&buffer[pc]) = min_disk + (i%(max_disk-min_disk));
      buffer[pc+1] = mass;
      buffer[pc+2] = r[i].x;
      buffer[pc+3] = r[i].y;
      buffer[pc+4] = r[i].z;
      buffer[pc+5] = r[i].vx;
      buffer[pc+6] = r[i].vy;
      buffer[pc+7] = r[i].vz;
    }
  }
}