Skip to content

Commit 0804e2f

Browse files
authored
Merge pull request #18 from KVSlab/update-tests
Update tests [WIP]
2 parents 72c735e + e682487 commit 0804e2f

13 files changed

+903
-34
lines changed

.github/workflows/check_and_test_package.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
if: steps.cache.outputs.cache-hit != 'true'
8888

8989
- name: Install OasisMove
90-
run: python3 -m pip install .[test]
90+
run: python3 -m pip install --editable .[test]
9191

9292
- name: Run tests
9393
run: python3 -m pytest tests

src/oasismove/problems/NSfracStep/DrivenCavity.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
# Override some problem specific parameters
9-
def problem_parameters(NS_parameters, **NS_namespace):
9+
def problem_parameters(NS_parameters, scalar_components, **NS_namespace):
1010
NS_parameters.update(
1111
# Mesh parameters
1212
Nx=50,
@@ -28,6 +28,10 @@ def problem_parameters(NS_parameters, **NS_namespace):
2828
pressure_degree=1,
2929
use_krylov_solvers=True)
3030

31+
scalar_components += ["alfa", "beta"]
32+
Schmidt["alfa"] = 1.
33+
Schmidt["beta"] = 10.
34+
3135

3236
# Create a mesh
3337
def mesh(Nx=50, Ny=50, **params):
@@ -39,12 +43,16 @@ def mesh(Nx=50, Ny=50, **params):
3943
def create_bcs(V, **NS_namespace):
4044
noslip = "std::abs(x[0]*x[1]*(1-x[0]))<1e-8"
4145
top = "std::abs(x[1]-1) < 1e-8"
46+
bottom = "std::abs(x[1]) < 1e-8"
4247
bc0 = DirichletBC(V, 0, noslip)
4348
bc00 = DirichletBC(V, 1, top)
4449
bc01 = DirichletBC(V, 0, top)
50+
bcbeta = DirichletBC(V, 1, bottom)
4551
return dict(u0=[bc00, bc0],
4652
u1=[bc01, bc0],
47-
p=[])
53+
p=[],
54+
alfa=[bc00],
55+
beta=[bcbeta])
4856

4957

5058
def initialize(x_1, x_2, bcs, **NS_namespace):

src/oasismove/problems/NSfracStep/MovingCylinder.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ def update_boundary_conditions(t, NS_expressions, **NS_namespace):
166166
NS_expressions["circle_y"].t = t
167167

168168

169-
def temporal_hook(t, St, F, A_ratio, tstep, save_solution_frequency, forces, q_, u_inf, D, viz_u, viz_p, u_vec,
170-
newfolder, p_, u_, **NS_namespace):
169+
def temporal_hook(mesh, t, dt, nu, St, F, A_ratio, tstep, save_solution_frequency, forces, q_, u_inf, D, viz_u, viz_p,
170+
u_vec, newfolder, p_, u_, **NS_namespace):
171171
# Save fluid velocity and pressure solution
172172
if tstep % save_solution_frequency == 0:
173173
assign(u_vec.sub(0), u_[0])
@@ -208,3 +208,11 @@ def temporal_hook(t, St, F, A_ratio, tstep, save_solution_frequency, forces, q_,
208208
lift_coeff = sum(drag_and_lift[1::2]) / factor
209209
data = [t, tstep, drag_coeff, lift_coeff, pressure_coeff]
210210
write_data_to_file(newfolder, data, "forces.txt")
211+
212+
# Compute mean velocity and Reynolds number at inlet
213+
if tstep % 10 == 0:
214+
h = mesh.hmin()
215+
L = 62 * D
216+
compute_flow_quantities(u_, L, nu, mesh, t, tstep, dt, h, outlet_area=1, boundary=None, outlet_ids=[],
217+
inlet_ids=[], id_wall=0, period=1.0, newfolder=None, dynamic_mesh=False,
218+
write_to_file=False)

src/oasismove/problems/NSfracStep/MovingTaylorGreen3D.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from oasismove.problems.NSfracStep import *
55
from oasismove.problems.NSfracStep.MovingCommon import get_visualization_writers
66

7+
comm = MPI.comm_world
8+
79

810
def problem_parameters(commandline_kwargs, NS_parameters, NS_expressions, **NS_namespace):
911
"""
@@ -175,10 +177,17 @@ def update_boundary_conditions(t, NS_expressions, **NS_namespace):
175177
value.t = t
176178

177179

178-
def temporal_hook(u_, save_solution_frequency, u_vec, viz_u, tstep, t, **NS_namespace):
180+
def temporal_hook(nu, L, dt, mesh, u_, save_solution_frequency, u_vec, viz_u, tstep, t, **NS_namespace):
179181
# Save velocity and pressure
180182
if tstep % save_solution_frequency == 0:
181183
for i in range(3):
182184
assign(u_vec.sub(i), u_[i])
183185

184186
viz_u.write(u_vec, t)
187+
188+
# Compute mean velocity and Reynolds number at inlet
189+
if tstep % 10 == 0:
190+
h = mesh.hmin()
191+
compute_flow_quantities(u_, L, nu, mesh, t, tstep, dt, h, outlet_area=1, boundary=None, outlet_ids=[],
192+
inlet_ids=[], id_wall=0, period=1.0, newfolder=None, dynamic_mesh=False,
193+
write_to_file=False)

src/oasismove/problems/NSfracStep/MovingWall.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from oasismove.problems.NSfracStep import *
22
from oasismove.problems.NSfracStep.MovingCommon import get_coordinate_map, get_visualization_writers
33

4+
comm = MPI.comm_world
5+
46

57
def problem_parameters(NS_parameters, **NS_namespace):
68
"""
@@ -196,11 +198,20 @@ def update_boundary_conditions(t, NS_expressions, **NS_namespace):
196198
value.update(t)
197199

198200

199-
def temporal_hook(t, tstep, save_solution_frequency, p_, u_, viz_u, viz_p, u_vec, **NS_namespace):
201+
def temporal_hook(mesh, dt, h0, eps, nu, t, tstep, save_solution_frequency, p_, u_, viz_u, viz_p, u_vec,
202+
**NS_namespace):
200203
# Write solution at time t
201204
if tstep % save_solution_frequency == 0:
202205
assign(u_vec.sub(0), u_[0])
203206
assign(u_vec.sub(1), u_[1])
204207

205208
viz_u.write(u_vec, t)
206209
viz_p.write(p_, t)
210+
211+
# Compute mean velocity and Reynolds number at inlet
212+
if tstep % 10 == 0:
213+
h = mesh.hmin()
214+
L = h0 * (1 + eps)
215+
compute_flow_quantities(u_, L, nu, mesh, t, tstep, dt, h, outlet_area=1, boundary=None, outlet_ids=[],
216+
inlet_ids=[], id_wall=0, period=1.0, newfolder=None, dynamic_mesh=False,
217+
write_to_file=False)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#include "Probe.h"
2+
3+
using namespace dolfin;
4+
5+
Probe::~Probe()
6+
{
7+
clear();
8+
}
9+
10+
Probe::Probe(const Array<double>& x, const FunctionSpace& V) :
11+
_element(V.element()), _num_evals(0)
12+
{
13+
auto mesh = V.mesh();
14+
std::size_t gdim = mesh->geometry().dim();
15+
_x.resize(3);
16+
17+
// Find the cell that contains probe
18+
const Point point(gdim, x.data());
19+
unsigned int cell_id = mesh->bounding_box_tree()->compute_first_entity_collision(point);
20+
21+
// If the cell is on this process, then create an instance
22+
// of the Probe class. Otherwise raise a dolfin_error.
23+
if (cell_id != std::numeric_limits<unsigned int>::max())
24+
{
25+
// Store position of probe
26+
for (std::size_t i = 0; i < 3; i++)
27+
_x[i] = (i < gdim ? x[i] : 0.0);
28+
29+
// Compute in tensor (one for scalar function, . . .)
30+
_value_size_loc = 1;
31+
for (std::size_t i = 0; i < _element->value_rank(); i++)
32+
_value_size_loc *= _element->value_dimension(i);
33+
34+
_probes.resize(_value_size_loc);
35+
36+
// Create cell that contains point
37+
dolfin_cell.reset(new Cell(*mesh, cell_id));
38+
dolfin_cell->get_cell_data(ufc_cell);
39+
40+
coefficients.resize(_element->space_dimension());
41+
42+
// Cell vertices
43+
dolfin_cell->get_vertex_coordinates(vertex_coordinates);
44+
45+
// Create work vector for basis
46+
basis_matrix.resize(_value_size_loc);
47+
for (std::size_t i = 0; i < _value_size_loc; ++i)
48+
basis_matrix[i].resize(_element->space_dimension());
49+
50+
std::vector<double> basis(_value_size_loc);
51+
const int cell_orientation = 0;
52+
for (std::size_t i = 0; i < _element->space_dimension(); ++i)
53+
{
54+
_element->evaluate_basis(i, &basis[0], &x[0],
55+
vertex_coordinates.data(),
56+
cell_orientation);
57+
for (std::size_t j = 0; j < _value_size_loc; ++j)
58+
basis_matrix[j][i] = basis[j];
59+
}
60+
}
61+
else
62+
{
63+
dolfin_error("Probe.cpp","set probe","Probe is not found on processor");
64+
}
65+
}
66+
//
67+
Probe::Probe(const Probe& p)
68+
{
69+
basis_matrix = p.basis_matrix;
70+
coefficients = p.coefficients;
71+
_x = p._x;
72+
vertex_coordinates = p.vertex_coordinates;
73+
_element = p._element;
74+
dolfin_cell.reset(new Cell(p.dolfin_cell->mesh(), p.dolfin_cell->index()));
75+
ufc_cell = p.ufc_cell;
76+
_value_size_loc = p._value_size_loc;
77+
_num_evals = p._num_evals;
78+
_probes = p._probes;
79+
}
80+
//
81+
void Probe::eval(const Function& u)
82+
{
83+
// Restrict function to cell
84+
u.restrict(&coefficients[0], *_element, *dolfin_cell,
85+
vertex_coordinates.data(), ufc_cell);
86+
87+
// Make room for one more evaluation
88+
for (std::size_t j = 0; j < _value_size_loc; j++)
89+
_probes[j].push_back(0.);
90+
91+
// Compute linear combination
92+
for (std::size_t i = 0; i < _element->space_dimension(); i++)
93+
{
94+
for (std::size_t j = 0; j < _value_size_loc; j++)
95+
_probes[j][_num_evals] += coefficients[i]*basis_matrix[j][i];
96+
}
97+
98+
_num_evals++;
99+
}
100+
// Remove one instance of the probe
101+
void Probe::erase_snapshot(std::size_t i)
102+
{
103+
for (std::size_t j = 0; j < _value_size_loc; j++)
104+
_probes[j].erase(_probes[j].begin()+i);
105+
106+
_num_evals--;
107+
}
108+
// Reset probe by removing all values
109+
void Probe::clear()
110+
{
111+
for (std::size_t j = 0; j < _value_size_loc; j++)
112+
_probes[j].clear();
113+
114+
_num_evals = 0;
115+
}
116+
// Return probe values for chosen component of tensor
117+
std::vector<double> Probe::get_probe_sub(std::size_t i)
118+
{
119+
return _probes[i];
120+
}
121+
// Return probe values for entire tensor at snapshot
122+
std::vector<double> Probe::get_probe_at_snapshot(std::size_t i)
123+
{
124+
std::vector<double> x(_value_size_loc);
125+
for (std::size_t j = 0; j < _value_size_loc; j++)
126+
x[j] = _probes[j][i];
127+
return x;
128+
}
129+
// Return robe value for given component at given snapshot
130+
double Probe::get_probe_component_and_snapshot(std::size_t comp, std::size_t i)
131+
{
132+
return _probes[comp][i];
133+
}
134+
// Return coordinates of probe
135+
std::vector<double> Probe::coordinates()
136+
{
137+
std::vector<double> x(_x);
138+
return x;
139+
}
140+
//
141+
void Probe::dump(std::size_t i, std::string filename, std::size_t id)
142+
{
143+
//std::ofstream fp(filename.c_str(), std::ios_base::app);
144+
std::ofstream fp(filename.c_str(), std::ios_base::out);
145+
std::ostringstream ss;
146+
ss << std::scientific;
147+
ss << "Probe id = " << id << std::endl;
148+
ss << "Number of evaluations = " << number_of_evaluations() << std::endl;
149+
ss << "Coordinates:" << std::endl;
150+
ss << _x[0] << " " << _x[1] << " " << _x[2] << std::endl;
151+
ss << "Values for component " << i << std::endl;
152+
for (std::size_t j = 0; j < _probes[i].size(); j++)
153+
ss << _probes[i][j] << std::endl;
154+
ss << std::endl;
155+
cout << ss.str() << endl;
156+
fp << ss.str();
157+
}
158+
void Probe::dump(std::string filename, std::size_t id)
159+
{
160+
//std::ofstream fp(filename.c_str(), std::ios_base::app);
161+
std::ofstream fp(filename.c_str(), std::ios_base::out);
162+
std::ostringstream ss;
163+
ss << std::scientific;
164+
ss << "Probe id = " << id << std::endl;
165+
ss << "Number of evaluations = " << number_of_evaluations() << std::endl;
166+
ss << "Coordinates:" << std::endl;
167+
ss << _x[0] << " " << _x[1] << " " << _x[2] << std::endl;
168+
ss << "Values for all components:" << std::endl;
169+
std::size_t N = _probes[0].size();
170+
for (std::size_t j = 0; j < N; j++)
171+
{
172+
for (std::size_t i = 0; i < _value_size_loc; i++)
173+
ss << _probes[i][j] << " " ;
174+
ss << std::endl;
175+
}
176+
cout << ss.str() << endl;
177+
fp << ss.str();
178+
}
179+
//
180+
void Probe::restart_probe(const Array<double>& u)
181+
{
182+
for (std::size_t j = 0; j < _value_size_loc; j++)
183+
_probes[j].push_back(u[j]);
184+
}
185+
186+

0 commit comments

Comments
 (0)