Example #1
0
// Take a copy of the current error message
void VapourSynther::setError(const char *_text, const wchar_t *alt) {

    if (errText)
        free(errText);

    const int maxLength = 2046;
    char text[maxLength+2];
    int i = 0;

    // Exception protected copy error message,
    // reformat \r and \n for windows text output.
    __try {
        // We are accessing a string pointer that can easily point to an invalid
        // memory address or fail to have the expected null string terminator.
        // Do not call any routines inside the __try { } block.
        for (i=0; i<maxLength; i++) {
            char ch = *_text++;
            while (ch == '\r') ch = *_text++;
            text[i] = ch;
            if (ch == '\0') break;
            if (ch == '\n') {
                text[i++] = '\r';
                text[i]   = '\n';
            }
        }
    }
    __except (EXCEPTION_EXECUTE_HANDLER) {
        text[i] = '\0';
        //    trace->printf("setError: Trap accessing 0x%08X current contents :-\n%s\n",
        //	              _text, text);
        if (alt) {
            errText = ssdup(alt);
        }
        else {
            errText = ssdup(L"Error message corrupted.");
        }
        return;
    }
    text[maxLength+1] = '\0';
    errText = ssconvalloc(text);
    //  trace->printf("%s\n",text);
}
/* at_cfgopen_low: initialize members of at_t from configuration
 * file `cfg', or if unavailable, from default values */
static int at_cfgopen_low(at_t *at, cfgdata_t *cfg, double tmstep)
{
   char buf1[100], buf2[100];
   char suf[2];
   suf[0] = at->suffix;
   suf[1] = '\0';
   /* NOTE: only at->beta won't be initialized in this function */

   die_if((at == NULL),"null pointer to at_t\n");
   die_if((cfg == NULL),"cannot load *.cfg file\n");

   /* bmin: minimal beta (highest temperature) */
   die_if(cfgget(cfg, &at->bmin, "beta_min", "%lf"), "missing var: at->bmin, key: beta_min, fmt: %%lf\n");
   die_if((at->bmin <= 0.0), "at->bmin should be positive!\n");

   /* bmax: maximum beta (lowest temperature) */
   die_if(cfgget(cfg, &at->bmax, "beta_max", "%lf"), "missing var: at->bmax, key: beta_min, fmt: %%lf\n");
   die_if((at->bmax <= 0.0), "at->bmax: should be positive!\n");

   die_if(!(at->bmax > at->bmin), "at->bmax: failed validation: at->bmax > at->bmin\n");

   /* T0: thermostat temperature */
   at->T0 = 300.0;
   if (cfgget(cfg, &at->T0, "T0", "%lf"))
      fprintf(stderr, "assuming default: at->T0 = 300.0, key: T0\n");

   /* nsttemp: frequency of tempering, 0: disable, -1: only ns */
   at->nsttemp = -1;
   if (cfgget(cfg, &at->nsttemp, "nsttemp", "%d"))
      fprintf(stderr, "assuming default: at->nsttemp = -1, key: nsttemp\n");

   /* mvreps: number of repeating Langevin eq */
   at->mvreps = 1;
   if (cfgget(cfg, &at->mvreps, "move_repeats", "%d"))
      fprintf(stderr, "assuming default: at->mvreps = 1, key: move_repeats\n");

   /* tmstep: MD integration step, for convenience */
   at->tmstep = tmstep;

   /* nsttrace: interval of writing trace file; -1: only when doing neighbor search, 0: disable */
   at->nsttrace = -1;
   if (cfgget(cfg, &at->nsttrace, "nsttrace", "%d"))
      fprintf(stderr, "assuming default: at->nsttrace = -1, key: nsttrace\n");

   /* grand: function pointer to a gaussian random number generator */
   at->grand = &grand0;

   /* rng_file: file name of random number state */
   if (cfgget(cfg, &buf1, "rng_file", "%s"))
   {
      fprintf(stderr, "assuming default: at->rng_file = \"MTSEED\", key: rng_file\n");
      strcpy(buf1, "MTSEED");
   }
   strcat(buf1, suf);
   at->rng_file = ssdup(buf1);

   /* trace_file: name of trace file */
   if (cfgget(cfg, &buf2, "trace_file", "%s"))
   {
      fprintf(stderr, "assuming default: at->trace_file = \"TRACE\", key: trace_file\n");
      strcpy(buf2, "TRACE");
   }
   strcat(buf2, suf);
   at->trace_file = ssdup(buf2);

   /* log: logfile */
   at->log = NULL;

   /* bTH : 0: disable; 1:enable */
   at->bTH = 0;
   if (cfgget(cfg, &at->bTH, "boost_mode", "%d")) 
      fprintf(stderr, "assuming default: at->th_mode = 0, key: boost_mode\n");

   /* TH_Tref */
   at->TH_Tref = 300.0;
   if (at->bTH)
      if (cfgget(cfg, &at->TH_Tref, "boost_Tref", "%lf"))
	 fprintf(stderr, "assuming default: at->th_Tref = 300.0, key: boost_Tref\n");

   /* kappa0 */
   at->kappa0 = 1.0;
   if (at->bTH) 
      if (cfgget(cfg, &at->kappa0, "kappa0", "%lf"))
	 fprintf(stderr, "assuming default: at->kappa0 = 1.0, key: kappa0\n");

   /* epsilon0 */
   at->epsilon0 = 0.0;
   if (at->bTH)
      if (cfgget(cfg, &at->epsilon0, "epsilon0", "%lf"))
	 fprintf(stderr, "assuming default: at->epsilon0 = 0.0, key: epsilon0\n");

   /* mb: handle for multiple-bin estimator */
   at->mb = mb_cfgopen(cfg, at->bmin, at->bmax, at->suffix);
   die_if((at->mb == NULL), "failed to initialize at->mb\n\n");

   /* Ea: total potential energy */
   at->Ea = 0.0;

   return 1;
}