[petsc-users] unexpected ordering when VecSetValues set multiples values
Matteo Parsani
parsani.matteo at gmail.com
Fri Apr 26 10:20:21 CDT 2013
Hello,
I have some problem when I try to set multiple values to a PETSc vector
that I will use later on with SNES. I am using Fortran 90.
Here the problem and two fixes that however are not so good for
performances reasons. The code is very simple.
*Standard approach that does not work correctly: (I am probably doing
something wrong)
*m = 1
! Loop over all elements
do ielem = elem_low, elem_high
! Loop over all nodes in the element
do inode = 1, nodesperelem
!Loop over all equations
do ieq = 1, nequations
! Add element to x_vec_local
x_vec_local(m) = ug(ieq,inode,ielem)
! Add element to index list
ind(m) = (elem_low-1)*nodesperelem*nequations+m-1
! Update m index
m = m+1
end do
end do
end do
! Set values in the portion of the vector owned by the process
call
VecSetValues(x_vec_in,len_local,index_list,x_vec_local,INSERT_VALUES,&
& ierr_local)
! Assemble initial guess
call VecAssemblyBegin(x_vec_in,ierr_local)
call VecAssemblyEnd(x_vec_in,ierr_local)
Then I print my expected values and the values contained in the PETSc
vector to a file. See attachment. I am running in serial for the moment BUT
strangely if you look at the file I have attached the first 79 DOFs values
have a wrong ordering and the remaining 80 are zero.
*1st approach: set just one value at the time inside the loop*.
m = 1
! Loop over all elements
do ielem = elem_low, elem_high
! Loop over all nodes in the element
do inode = 1, nodesperelem
!Loop over all equations
do ieq = 1, nequations
! Add element to x_vec_local
value = ug(ieq,inode,ielem)
! Add element to index list
ind = (elem_low-1)*nodesperelem*nequations+m-1
call VecSetValues(x_vec_in,1,ind,value,INSERT_VALUES,&
& ierr_local)
! Update m index
m = m+1
end do
end do
end do
*This works fine*. As you can see I am using the same expression used in
the previous loop to compute the index of the element that I have to add in
the x_vec_in, i.e.
ind = (elem_low-1)*nodesperelem*nequations+m-1
Thus I cannot see which is the problem.
*2nd approach: get the pointer to the local part of the global vector and
use it to set the values in the global vector
*m = 1
! Loop over all elements
do ielem = elem_low, elem_high
! Loop over all nodes in the element
do inode = 1, nodesperelem
!Loop over all equations
do ieq = 1, nequations
! Add element to x_vec_local
tmp(m) = ug(ieq,inode,ielem)
! Update m index
m = m+1
end do
end do
end do*
*
*This works fine too*.
Jut to be complete. I use the following two approaches to view the vector:
call VecView(x_vec_in,PETSC_VIEWER_STDOUT_WORLD,ierr_local)
and
call VecGetArrayF90(x_vec_in,tmp,ierr_local)
m = 1
! Loop over all elements
do ielem = elem_low, elem_high
! Loop over all nodes in the element
do inode = 1, nodesperelem
!Loop over all equations
do ieq = 1, nequations
write(*,*) m,index_list(m),x_vec_local(m),tmp(m)
! Update m index
m = m+1
end do
end do
end do
Thank you.
--
Matteo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20130426/49720a51/attachment-0001.html>
-------------- next part --------------
Fortran index PETSc index Expected values wrong values stored in PETSc vec (x_vec_in)
1 0 0.93948776058346750 0.73888153309055049
2 1 0.55294328167143081 0.98109660706253266
3 2 0.98109660706253266 0.72201428570658543
4 3 0.0000000000000000 0.37500572347501365
5 4 0.72201428570658543 0.0000000000000000
6 5 0.87455684095044839 0.92271262338061255
7 6 0.37500572347501365 1.0498576790186152
8 7 0.74592494251227448 0.73867698789744696
9 8 0.0000000000000000 0.79005016762214764
10 9 0.63192361099133243 0.0000000000000000
11 10 0.92271262338061255 0.87455684095044839
12 11 0.86706624329676873 0.74592494251227448
13 12 1.0498576790186152 0.63192361099133243
14 13 0.0000000000000000 0.30331870059955296
15 14 0.73867698789744696 0.0000000000000000
16 15 0.84075382752435801 0.84075382752435801
17 16 0.79005016762214764 0.77428621161408018
18 17 0.77428621161408018 0.63305806072430315
19 18 0.0000000000000000 0.75036023117937511
20 19 0.63305806072430315 0.0000000000000000
21 20 0.87455684095044839 0.84075382752435801
22 21 0.37500572347501365 0.28755474459148550
23 22 0.74592494251227448 0.57067306732654188
24 23 0.0000000000000000 0.37500572347501365
25 24 0.63192361099133243 0.0000000000000000
26 25 0.84075382752435801 0.79851667937097781
27 26 0.30331870059955296 0.27310878912639885
28 27 0.28755474459148550 0.56120402227094435
29 28 0.0000000000000000 0.79005016762214764
30 29 0.57067306732654188 0.0000000000000000
31 30 0.84075382752435801 0.87455684095044839
32 31 0.79005016762214764 -0.14769283033564165
33 32 0.77428621161408018 0.60136008311655142
34 33 0.0000000000000000 0.55294328167143081
35 34 0.63305806072430315 0.0000000000000000
36 35 0.79851667937097781 0.84075382752435801
37 36 0.75036023117937511 -0.19917672243110918
38 37 0.27310878912639885 0.59976366750186694
39 38 0.0000000000000000 0.86706624329676873
40 39 0.56120402227094435 0.0000000000000000
41 40 0.84075382752435801 0.92271262338061255
42 41 0.30331870059955296 1.0498576790186152
43 42 0.28755474459148550 0.73867698789744696
44 43 0.0000000000000000 0.79005016762214764
45 44 0.57067306732654188 0.0000000000000000
46 45 0.87455684095044839 0.93948776058346750
47 46 0.37500572347501365 0.98109660706253266
48 47 -0.14769283033564165 0.78401265530230047
49 48 0.0000000000000000 1.2686234963229299
50 49 0.60136008311655142 0.0000000000000000
51 50 0.79851667937097781 0.84075382752435801
52 51 0.75036023117937511 0.77428621161408018
53 52 0.27310878912639885 0.63305806072430315
54 53 0.0000000000000000 0.75036023117937511
55 54 0.56120402227094435 0.0000000000000000
56 55 0.84075382752435801 0.87455684095044839
57 56 0.79005016762214764 0.74592494251227448
58 57 -0.19917672243110918 0.71589621368616485
59 58 0.0000000000000000 1.2767816346447423
60 59 0.59976366750186694 0.0000000000000000
61 60 0.87455684095044839 0.79851667937097781
62 61 0.37500572347501365 0.27310878912639885
63 62 -0.14769283033564165 0.56120402227094435
64 63 0.0000000000000000 0.79005016762214764
65 64 0.60136008311655142 0.0000000000000000
66 65 0.93948776058346750 0.84075382752435801
67 66 0.55294328167143081 0.28755474459148550
68 67 -0.33844913000759452 0.66214866089962809
69 68 0.0000000000000000 1.2686234963229299
70 69 0.67688316349483535 0.0000000000000000
71 70 0.84075382752435801 0.84075382752435801
72 71 0.79005016762214764 -0.19917672243110918
73 72 -0.19917672243110918 0.59976366750186694
74 73 0.0000000000000000 0.86706624329676873
75 74 0.59976366750186694 0.0000000000000000
76 75 0.92271262338061255 0.87455684095044839
77 76 0.86706624329676873 -0.14769283033564165
78 77 -0.41868507162453339 0.68533268581138396
79 78 0.0000000000000000 1.2127161502064945
80 79 0.68844986769196281 0.0000000000000000
81 80 0.92271262338061255 0.0000000000000000
82 81 0.86706624329676873 0.0000000000000000
83 82 1.0498576790186152 0.0000000000000000
84 83 0.0000000000000000 0.0000000000000000
85 84 0.73867698789744696 0.0000000000000000
86 85 0.84075382752435801 0.0000000000000000
87 86 0.79005016762214764 0.0000000000000000
88 87 0.77428621161408018 0.0000000000000000
89 88 0.0000000000000000 0.0000000000000000
90 89 0.63305806072430315 0.0000000000000000
91 90 0.93948776058346750 0.0000000000000000
92 91 1.2127161502064945 0.0000000000000000
93 92 0.98109660706253266 0.0000000000000000
94 93 0.0000000000000000 0.0000000000000000
95 94 0.78401265530230047 0.0000000000000000
96 95 0.87455684095044839 0.0000000000000000
97 96 1.2686234963229299 0.0000000000000000
98 97 0.74592494251227448 0.0000000000000000
99 98 0.0000000000000000 0.0000000000000000
100 99 0.71589621368616485 0.0000000000000000
101 100 0.84075382752435801 0.0000000000000000
102 101 0.79005016762214764 0.0000000000000000
103 102 0.77428621161408018 0.0000000000000000
104 103 0.0000000000000000 0.0000000000000000
105 104 0.63305806072430315 0.0000000000000000
106 105 0.79851667937097781 0.0000000000000000
107 106 0.75036023117937511 0.0000000000000000
108 107 0.27310878912639885 0.0000000000000000
109 108 0.0000000000000000 0.0000000000000000
110 109 0.56120402227094435 0.0000000000000000
111 110 0.87455684095044839 0.0000000000000000
112 111 1.2686234963229299 0.0000000000000000
113 112 0.74592494251227448 0.0000000000000000
114 113 0.0000000000000000 0.0000000000000000
115 114 0.71589621368616485 0.0000000000000000
116 115 0.84075382752435801 0.0000000000000000
117 116 1.2767816346447423 0.0000000000000000
118 117 0.28755474459148550 0.0000000000000000
119 118 0.0000000000000000 0.0000000000000000
120 119 0.66214866089962809 0.0000000000000000
121 120 0.79851667937097781 0.0000000000000000
122 121 0.75036023117937511 0.0000000000000000
123 122 0.27310878912639885 0.0000000000000000
124 123 0.0000000000000000 0.0000000000000000
125 124 0.56120402227094435 0.0000000000000000
126 125 0.84075382752435801 0.0000000000000000
127 126 0.79005016762214764 0.0000000000000000
128 127 -0.19917672243110918 0.0000000000000000
129 128 0.0000000000000000 0.0000000000000000
130 129 0.59976366750186694 0.0000000000000000
131 130 0.84075382752435801 0.0000000000000000
132 131 1.2767816346447423 0.0000000000000000
133 132 0.28755474459148550 0.0000000000000000
134 133 0.0000000000000000 0.0000000000000000
135 134 0.66214866089962809 0.0000000000000000
136 135 0.87455684095044839 0.0000000000000000
137 136 1.2686234963229299 0.0000000000000000
138 137 -0.14769283033564165 0.0000000000000000
139 138 0.0000000000000000 0.0000000000000000
140 139 0.68533268581138396 0.0000000000000000
141 140 0.84075382752435801 0.0000000000000000
142 141 0.79005016762214764 0.0000000000000000
143 142 -0.19917672243110918 0.0000000000000000
144 143 0.0000000000000000 0.0000000000000000
145 144 0.59976366750186694 0.0000000000000000
146 145 0.92271262338061255 0.0000000000000000
147 146 0.86706624329676873 0.0000000000000000
148 147 -0.41868507162453339 0.0000000000000000
149 148 0.0000000000000000 0.0000000000000000
150 149 0.68844986769196281 0.0000000000000000
151 150 0.87455684095044839 0.0000000000000000
152 151 1.2686234963229299 0.0000000000000000
153 152 -0.14769283033564165 0.0000000000000000
154 153 0.0000000000000000 0.0000000000000000
155 154 0.68533268581138396 0.0000000000000000
156 155 0.93948776058346750 0.0000000000000000
157 156 1.2127161502064945 0.0000000000000000
158 157 -0.33844913000759452 0.0000000000000000
159 158 0.0000000000000000 0.0000000000000000
160 159 0.73888153309055049 0.0000000000000000
More information about the petsc-users
mailing list