Skip to content

Commit cca3ed1

Browse files
committed
Fixing weighted gradient because of new local/global stuff
1 parent fb991d3 commit cca3ed1

File tree

5 files changed

+41
-91
lines changed

5 files changed

+41
-91
lines changed

demo/Probe.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
p = Probes(x.flatten(), W)
1818
x = x*0.9
1919
p.add_positions(x.flatten(), W)
20-
print 'hei'
2120
for i in range(6):
2221
p(w0)
23-
print 'hei'
2422

2523
print p.array(2, "testarray") # dump snapshot 2
2624
print p.array(filename="testarray") # dump all snapshots
2725
print p.dump("testarray")
26+

fenicstools/WeightedGradient.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
__copyright__ = "Copyright (C) 2013 " + __author__
44
__license__ = "GNU Lesser GPL version 3 or any later version"
55
import os, inspect
6-
from dolfin import compile_extension_module, Function, FunctionSpace, assemble, TrialFunction, TestFunction, dx, Matrix
6+
from dolfin import info, compile_extension_module, Function, FunctionSpace, assemble, TrialFunction, TestFunction, dx, Matrix
77

88
fem_folder = os.path.abspath(os.path.join(inspect.getfile(inspect.currentframe()), "../fem"))
99
gradient_code = open(os.path.join(fem_folder, 'gradient_weight.cpp'), 'r').read()
@@ -60,6 +60,8 @@ def weighted_gradient_matrix(mesh, i, family='CG', degree=1, constrained_domain=
6060
CC.append(Cp)
6161
return CC
6262
else:
63-
dP = assemble(TrialFunction(S).dx(i)*TestFunction(DG)*dx)
63+
dP = assemble(TrialFunction(S).dx(i)*TestFunction(DG)*dx)
6464
Cp = compiled_gradient_module.compute_weighted_gradient_matrix(G, dP, dg)
65+
#info(G, True)
66+
#info(dP, True)
6567
return Cp

fenicstools/fem/gradient_weight.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ namespace dolfin
2626
// Compute weights
2727
GenericVector& dg_vector = *DG.vector();
2828
dg_vector.zero();
29-
for (CellIterator cell(*mesh); !cell.end(); ++cell)
29+
for (CellIterator cell(*mesh, "all"); !cell.end(); ++cell)
3030
{
3131
const std::vector<dolfin::la_index>& dofs
3232
= dofmap_u->cell_dofs(cell->index());
3333

3434
std::fill(ws.begin(), ws.end(), 1./cell->volume());
35-
dg_vector.add(ws.data(), dofs.size(), dofs.data());
35+
dg_vector.add_local(ws.data(), dofs.size(), dofs.data());
3636
}
37-
dg_vector.apply("insert");
37+
dg_vector.apply("insert");
3838
}
3939

4040
std::size_t dof_owner(std::vector<std::vector<std::size_t> > all_ranges,
@@ -55,7 +55,7 @@ namespace dolfin
5555
std::vector<double> values;
5656
std::vector<std::vector<std::size_t> > allcolumns;
5757
std::vector<std::vector<double> > allvalues;
58-
58+
5959
const std::pair<std::size_t, std::size_t> row_range = A.local_range(0);
6060
const std::size_t m = row_range.second - row_range.first;
6161
GenericVector& weight = *DG.vector();
@@ -66,7 +66,7 @@ namespace dolfin
6666
int dm = weight_range.second-weight_range.first;
6767

6868
const MPI_Comm mpi_comm = DG.function_space()->mesh()->mpi_comm();
69-
69+
7070
// Communicate local_ranges of weights
7171
std::vector<std::vector<std::size_t> > all_ranges;
7272
MPI::all_gather(mpi_comm, weight_range_vec, all_ranges);
@@ -94,7 +94,7 @@ namespace dolfin
9494
}
9595
}
9696
}
97-
97+
9898
// Communicate to all which weights are needed by the process
9999
std::vector<std::vector<std::size_t> > dofs_needed_recv;
100100
MPI::all_to_all(mpi_comm, dofs_needed, dofs_needed_recv);
@@ -110,7 +110,7 @@ namespace dolfin
110110
std::map<std::size_t, double> send_weights;
111111
for (std::size_t k = 0; k < dofs.size(); k++)
112112
{
113-
weights_to_send[p].push_back(weight[dofs[k]]);
113+
weights_to_send[p].push_back(weight[dofs[k]-weight_range.first]);
114114
}
115115
}
116116
std::vector<std::vector<double> > weights_to_send_recv;
@@ -124,7 +124,9 @@ namespace dolfin
124124
continue;
125125

126126
for (std::size_t k = 0; k < dofs_needed[p].size(); k++)
127-
received_weights[dofs_needed[p][k]] = weights_to_send_recv[p][k];
127+
{
128+
received_weights[dofs_needed[p][k]] = weights_to_send_recv[p][k];
129+
}
128130
}
129131

130132
for (std::size_t row = 0; row < m; row++)
@@ -142,7 +144,7 @@ namespace dolfin
142144
}
143145
else
144146
{
145-
values[i] = weight[columns[i]];
147+
values[i] = weight[columns[i]-weight_range.first];
146148
}
147149
// values[i] = 1./values[i];
148150
}
@@ -161,7 +163,7 @@ namespace dolfin
161163
}
162164
else
163165
{
164-
w = weight[dof];
166+
w = weight[dof-weight_range.first];
165167
}
166168
values[i] = values[i]*w;
167169
// values[i] = values[i]*values[i];
@@ -171,14 +173,15 @@ namespace dolfin
171173
allvalues.push_back(values);
172174
allcolumns.push_back(columns);
173175
}
176+
174177
for (std::size_t row = 0; row < m; row++)
175178
{
176179
// Get global row number
177180
const std::size_t global_row = row + row_range.first;
178181

179182
A.setrow(global_row, allcolumns[row], allvalues[row]);
180183
}
181-
A.apply("insert");
184+
A.apply("insert");
182185
}
183186

184187
std::shared_ptr<GenericMatrix> MatTransposeMatMult(GenericMatrix& A, GenericMatrix& B)
@@ -208,6 +211,7 @@ namespace dolfin
208211
Mat CC;
209212
PetscErrorCode ierr = MatMatMult(Ap->mat(), Bp->mat(), MAT_INITIAL_MATRIX, PETSC_DEFAULT, &CC);
210213
dolfin::PETScMatrix CCC = PETScMatrix(CC);
214+
CCC.apply("insert");
211215
return CCC.copy();
212216
}
213217

tests/test_StatisticsProbes.py

Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import nose
22

33
from dolfin import FunctionSpace, UnitCubeMesh, UnitSquareMesh, interpolate, \
4-
Expression, VectorFunctionSpace
4+
Expression, VectorFunctionSpace, MPI, mpi_comm_world
55
from fenicstools import *
66
from numpy import array
77

@@ -17,22 +17,10 @@ def test_StatisticsProbes_segregated_2D():
1717
for i in range(5):
1818
probes(u0, v0)
1919

20-
id0, p = probes[0]
21-
if len(p) > 0:
22-
assert p.number_of_evaluations() == 5
23-
assert p.value_size() == 5
24-
25-
mean = p.mean()
26-
var = p.variance()
27-
nose.tools.assert_almost_equal(p[0][0], 2.5)
28-
nose.tools.assert_almost_equal(p[0][4], 0.625)
29-
nose.tools.assert_almost_equal(p[1][0], 0.5)
30-
nose.tools.assert_almost_equal(p[1][1], 0.25)
31-
nose.tools.assert_almost_equal(mean[0], 0.5)
32-
nose.tools.assert_almost_equal(mean[1], 0.25)
33-
nose.tools.assert_almost_equal(var[0], 0.25)
34-
nose.tools.assert_almost_equal(var[1], 0.0625)
35-
nose.tools.assert_almost_equal(var[2], 0.125)
20+
p = probes.array()
21+
if MPI.rank(mpi_comm_world()) == 0:
22+
nose.tools.assert_almost_equal(p[0,0], 2.5)
23+
nose.tools.assert_almost_equal(p[0,4], 0.625)
3624

3725
def test_StatisticsProbes_segregated_3D():
3826
mesh = UnitCubeMesh(4, 4, 4)
@@ -47,26 +35,10 @@ def test_StatisticsProbes_segregated_3D():
4735
for i in range(5):
4836
probes(u0, v0, w0)
4937

50-
id0, p = probes[0]
51-
if len(p) > 0:
52-
assert p.number_of_evaluations() == 5
53-
assert p.value_size() == 9
54-
55-
mean = p.mean()
56-
var = p.variance()
57-
nose.tools.assert_almost_equal(p[0][0], 2.5)
58-
nose.tools.assert_almost_equal(p[0][4], 0.3125)
59-
nose.tools.assert_almost_equal(p[1][0], 0.5)
60-
nose.tools.assert_almost_equal(p[1][1], 0.25)
61-
nose.tools.assert_almost_equal(p[1][2], 0.25)
62-
nose.tools.assert_almost_equal(mean[0], 0.5)
63-
nose.tools.assert_almost_equal(mean[1], 0.25)
64-
nose.tools.assert_almost_equal(mean[2], 0.25)
65-
nose.tools.assert_almost_equal(var[0], 0.25)
66-
nose.tools.assert_almost_equal(var[1], 0.0625)
67-
nose.tools.assert_almost_equal(var[2], 0.0625)
68-
nose.tools.assert_almost_equal(var[3], 0.125)
69-
nose.tools.assert_almost_equal(var[4], 0.125)
38+
p = probes.array()
39+
if MPI.rank(mpi_comm_world()) == 0:
40+
nose.tools.assert_almost_equal(p[0,0], 2.5)
41+
nose.tools.assert_almost_equal(p[0,4], 0.3125)
7042

7143
def test_StatisticsProbes_vector_2D():
7244
mesh = UnitSquareMesh(4, 4)
@@ -79,22 +51,10 @@ def test_StatisticsProbes_vector_2D():
7951
for i in range(5):
8052
probes(u0)
8153

82-
id0, p = probes[0]
83-
if len(p) > 0:
84-
assert p.number_of_evaluations() == 5
85-
assert p.value_size() == 5
86-
87-
mean = p.mean()
88-
var = p.variance()
89-
nose.tools.assert_almost_equal(p[0][0], 2.5)
90-
nose.tools.assert_almost_equal(p[0][4], 0.625)
91-
nose.tools.assert_almost_equal(p[1][0], 0.5)
92-
nose.tools.assert_almost_equal(p[1][1], 0.25)
93-
nose.tools.assert_almost_equal(mean[0], 0.5)
94-
nose.tools.assert_almost_equal(mean[1], 0.25)
95-
nose.tools.assert_almost_equal(var[0], 0.25)
96-
nose.tools.assert_almost_equal(var[1], 0.0625)
97-
nose.tools.assert_almost_equal(var[2], 0.125)
54+
p = probes.array()
55+
if MPI.rank(mpi_comm_world()) == 0:
56+
nose.tools.assert_almost_equal(p[0,0], 2.5)
57+
nose.tools.assert_almost_equal(p[0,4], 0.625)
9858

9959
def test_StatisticsProbes_vector_3D():
10060
mesh = UnitCubeMesh(4, 4, 4)
@@ -107,26 +67,11 @@ def test_StatisticsProbes_vector_3D():
10767
for i in range(5):
10868
probes(u0)
10969

110-
id0, p = probes[0]
111-
if len(p) > 0:
112-
assert p.number_of_evaluations() == 5
113-
assert p.value_size() == 9
70+
p = probes.array()
71+
if MPI.rank(mpi_comm_world()) == 0:
11472

115-
mean = p.mean()
116-
var = p.variance()
117-
nose.tools.assert_almost_equal(p[0][0], 2.5)
118-
nose.tools.assert_almost_equal(p[0][4], 0.3125)
119-
nose.tools.assert_almost_equal(p[1][0], 0.5)
120-
nose.tools.assert_almost_equal(p[1][1], 0.25)
121-
nose.tools.assert_almost_equal(p[1][2], 0.25)
122-
nose.tools.assert_almost_equal(mean[0], 0.5)
123-
nose.tools.assert_almost_equal(mean[1], 0.25)
124-
nose.tools.assert_almost_equal(mean[2], 0.25)
125-
nose.tools.assert_almost_equal(var[0], 0.25)
126-
nose.tools.assert_almost_equal(var[1], 0.0625)
127-
nose.tools.assert_almost_equal(var[2], 0.0625)
128-
nose.tools.assert_almost_equal(var[3], 0.125)
129-
nose.tools.assert_almost_equal(var[4], 0.125)
73+
nose.tools.assert_almost_equal(p[0,0], 2.5)
74+
nose.tools.assert_almost_equal(p[0,4], 0.3125)
13075

131-
if __name__ == '__main__':
132-
nose.run(defaultTest=__name__)
76+
#if __name__ == '__main__':
77+
#nose.run(defaultTest=__name__)

tests/test_WeightedGradient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ def test_WeightedGradient():
3030
du.vector()[:] = wx[2] * u.vector()
3131
nose.tools.assert_almost_equal(du.vector().min(), 3.)
3232

33-
#if __name__ == '__main__':
34-
#nose.run(defaultTest=__name__)
33+
if __name__ == '__main__':
34+
nose.run(defaultTest=__name__)

0 commit comments

Comments
 (0)