[petsc-users] *****SPAM*****Create vector with evenly spaced values

hong at aspiritech.org hong at aspiritech.org
Fri Aug 29 09:29:21 CDT 2014


Florian :
>>
>> is there a way to create a vector with evenly spaced values in an
>> interval, like the numpy.arange function?
>> http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html
>> or the linspace function?
>
>
> Ok, I gave it as try. Do I need the VecAssembly the vector?

No, but you need VecResetArray() when vector is no longer used.
Hong
>
>
> void arange(size_t start, size_t stop)
>     {
>       // given a vector of appropriate size
>       PetscScalar ierr;
>       PetscScalar *a;
>       ierr = PetscMalloc(sizeof(PetscScalar) * (stop-start), &a);
> CHKERRV(ierr);
>       size_t pos = 0;
>       for (size_t i = start; i < stop; i++) {
>         a[pos] = i;
>         pos++;
>       }
>       cout << "Array filled." << endl;
>       VecReplaceArray(vector, a);
>       // VecAssemblyBegin(vector); VecAssemblyEnd(vector);  Do I need that?
>     }
>
> Any comments?
>
> Thanks,
> Florian


More information about the petsc-users mailing list