Subversion Repositories slepc-dev

Rev

Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2340 jroman 1
/*
2
   Miscellaneous Vec-related functions.
3
 
4
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5
   SLEPc - Scalable Library for Eigenvalue Problem Computations
2575 eromero 6
   Copyright (c) 2002-2011, Universitat Politecnica de Valencia, Spain
2340 jroman 7
 
8
   This file is part of SLEPc.
9
 
10
   SLEPc is free software: you can redistribute it and/or modify it under  the
11
   terms of version 3 of the GNU Lesser General Public License as published by
12
   the Free Software Foundation.
13
 
14
   SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
15
   WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
16
   FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
17
   more details.
18
 
19
   You  should have received a copy of the GNU Lesser General  Public  License
20
   along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
21
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22
*/
23
 
2729 jroman 24
#include <slepc-private/vecimplslepc.h>            /*I "slepcvec.h" I*/
2345 jroman 25
#include <slepcsys.h>
2340 jroman 26
 
27
#undef __FUNCT__  
28
#define __FUNCT__ "SlepcVecSetRandom"
29
/*@
30
   SlepcVecSetRandom - Sets all components of a vector to random numbers.
31
 
32
   Logically Collective on Vec
33
 
34
   Input/Output Parameter:
35
.  x  - the vector
36
 
37
   Input Parameter:
38
-  rctx - the random number context, formed by PetscRandomCreate(), or PETSC_NULL and
39
          it will create one internally.
40
 
41
   Note:
42
   This operation is equivalent to VecSetRandom - the difference is that the
43
   vector generated by SlepcVecSetRandom is the same irrespective of the size
2691 jroman 44
   of the communicator (if all processes pass a PetscRandom context initialized
45
   with the same seed).
2340 jroman 46
 
47
   Level: developer
48
@*/
49
PetscErrorCode SlepcVecSetRandom(Vec x,PetscRandom rctx)
50
{
51
  PetscErrorCode ierr;
52
  PetscRandom    randObj = PETSC_NULL;
53
  PetscInt       i,n,low,high;
54
  PetscScalar    *px,t;
55
  MPI_Comm       comm;
56
 
57
  PetscFunctionBegin;
58
  PetscValidHeaderSpecific(x,VEC_CLASSID,1);
59
  if (rctx) PetscValidHeaderSpecific(rctx,PETSC_RANDOM_CLASSID,2);
60
  else {
61
    ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
62
    ierr = PetscRandomCreate(comm,&randObj);CHKERRQ(ierr);
2691 jroman 63
    ierr = PetscRandomSetSeed(randObj,0x12345678);CHKERRQ(ierr);
2340 jroman 64
    ierr = PetscRandomSetFromOptions(randObj);CHKERRQ(ierr);
65
    rctx = randObj;
66
  }
67
 
68
  ierr = VecGetSize(x,&n);CHKERRQ(ierr);
69
  ierr = VecGetOwnershipRange(x,&low,&high);CHKERRQ(ierr);
70
  ierr = VecGetArray(x,&px);CHKERRQ(ierr);
71
  for (i=0;i<n;i++) {
72
    ierr = PetscRandomGetValue(rctx,&t);CHKERRQ(ierr);
73
    if (i>=low && i<high) px[i-low] = t;
74
  }
75
  ierr = VecRestoreArray(x,&px);CHKERRQ(ierr);
76
  ierr = PetscRandomDestroy(&randObj);CHKERRQ(ierr);
77
  ierr = PetscObjectStateIncrease((PetscObject)x);CHKERRQ(ierr);
78
  PetscFunctionReturn(0);
79
}
80
 
81
#undef __FUNCT__  
82
#define __FUNCT__ "SlepcVecNormalize"
83
/*@C
84
   SlepcVecNormalize - Normalizes a possibly complex vector by the 2-norm.
85
 
86
   Collective on Vec
87
 
88
   Input parameters:
89
+  xr         - the real part of the vector (overwritten on output)
2564 eromero 90
.  xi         - the imaginary part of the vector (not referenced if iscomplex is false)
2340 jroman 91
-  iscomplex - a flag that indicating if the vector is complex
92
 
93
   Output parameter:
94
.  norm      - the vector norm before normalization (can be set to PETSC_NULL)
95
 
96
   Level: developer
97
 
98
@*/
99
PetscErrorCode SlepcVecNormalize(Vec xr,Vec xi,PetscBool iscomplex,PetscReal *norm)
100
{
101
  PetscErrorCode ierr;
102
#if !defined(PETSC_USE_COMPLEX)
103
  PetscReal      normr,normi,alpha;
104
#endif
105
 
106
  PetscFunctionBegin;
2349 jroman 107
  PetscValidHeaderSpecific(xr,VEC_CLASSID,1);
2541 jroman 108
#if !defined(PETSC_USE_COMPLEX)
2340 jroman 109
  if (iscomplex) {
2564 eromero 110
    PetscValidHeaderSpecific(xi,VEC_CLASSID,2);
111
    ierr = VecNormBegin(xr,NORM_2,&normr);CHKERRQ(ierr);
112
    ierr = VecNormBegin(xi,NORM_2,&normi);CHKERRQ(ierr);
113
    ierr = VecNormEnd(xr,NORM_2,&normr);CHKERRQ(ierr);
114
    ierr = VecNormEnd(xi,NORM_2,&normi);CHKERRQ(ierr);
2340 jroman 115
    alpha = SlepcAbsEigenvalue(normr,normi);
116
    if (norm) *norm = alpha;
117
    alpha = 1.0 / alpha;
118
    ierr = VecScale(xr,alpha);CHKERRQ(ierr);
119
    ierr = VecScale(xi,alpha);CHKERRQ(ierr);
120
  } else
121
#endif
122
  {
123
    ierr = VecNormalize(xr,norm);CHKERRQ(ierr);
124
  }
125
  PetscFunctionReturn(0);
126
}
127