Example #1
0
/*@
  PetscRandomSetFromOptions - Configures the random number generator from the options database.

  Collective on PetscRandom

  Input Parameter:
. rnd - The random number generator context

  Options Database:
+ -random_seed <integer> - provide a seed to the random number generater
- -random_no_imaginary_part - makes the imaginary part of the random number zero, this is useful when you want the
                              same code to produce the same result when run with real numbers or complex numbers for regression testing purposes

  Notes:  To see all options, run your program with the -help option.
          Must be called after PetscRandomCreate() but before the rnd is used.

  Level: beginner

.keywords: PetscRandom, set, options, database
.seealso: PetscRandomCreate(), PetscRandomSetType()
@*/
PetscErrorCode  PetscRandomSetFromOptions(PetscRandom rnd)
{
    PetscErrorCode ierr;
    PetscBool      set,noimaginary = PETSC_FALSE;
    PetscInt       seed;

    PetscFunctionBegin;
    PetscValidHeaderSpecific(rnd,PETSC_RANDOM_CLASSID,1);

    ierr = PetscObjectOptionsBegin((PetscObject)rnd);
    CHKERRQ(ierr);

    /* Handle PetscRandom type options */
    ierr = PetscRandomSetTypeFromOptions_Private(PetscOptionsObject,rnd);
    CHKERRQ(ierr);

    /* Handle specific random generator's options */
    if (rnd->ops->setfromoptions) {
        ierr = (*rnd->ops->setfromoptions)(PetscOptionsObject,rnd);
        CHKERRQ(ierr);
    }
    ierr = PetscOptionsInt("-random_seed","Seed to use to generate random numbers","PetscRandomSetSeed",0,&seed,&set);
    CHKERRQ(ierr);
    if (set) {
        ierr = PetscRandomSetSeed(rnd,(unsigned long int)seed);
        CHKERRQ(ierr);
        ierr = PetscRandomSeed(rnd);
        CHKERRQ(ierr);
    }
    ierr = PetscOptionsBool("-random_no_imaginary_part","The imaginary part of the random number will be zero","PetscRandomSetInterval",noimaginary,&noimaginary,&set);
    CHKERRQ(ierr);
#if defined(PETSC_HAVE_COMPLEX)
    if (set) {
        if (noimaginary) {
            PetscScalar low,high;
            ierr = PetscRandomGetInterval(rnd,&low,&high);
            CHKERRQ(ierr);
            low  = low - PetscImaginaryPart(low);
            high = high - PetscImaginaryPart(high);
            ierr = PetscRandomSetInterval(rnd,low,high);
            CHKERRQ(ierr);
        }
    }
#endif
    ierr = PetscOptionsEnd();
    CHKERRQ(ierr);
    ierr = PetscRandomViewFromOptions(rnd,NULL, "-random_view");
    CHKERRQ(ierr);
    PetscFunctionReturn(0);
}
Example #2
0
PetscErrorCode VecSetRandom_SAMRAI(Vec x, PetscRandom rctx)
{
    IBTK_TIMER_START(t_vec_set_random);
#if !defined(NDEBUG)
    TBOX_ASSERT(x);
#endif
    PetscScalar lo, hi;
    int ierr;
    ierr = PetscRandomGetInterval(rctx, &lo, &hi);
    IBTK_CHKERRQ(ierr);
    PSVR_CAST2(x)->setRandomValues(hi - lo, lo);
    ierr = PetscObjectStateIncrease(reinterpret_cast<PetscObject>(x));
    IBTK_CHKERRQ(ierr);
    IBTK_TIMER_STOP(t_vec_set_random);
    PetscFunctionReturn(0);
} // VecSetRandom