/usr/lib/python2.7/dist-packages/PySPH-1.0a4.dev0-py2.7-linux-x86_64.egg/pysph/solver/tests/test_solver.py is in python-pysph 0~20160514.git91867dc-4build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | try:
# This is for Python-2.6.x
from unittest2 import TestCase, main
except ImportError:
from unittest import TestCase, main
try:
from unittest import mock
except ImportError:
import mock
import numpy as np
import numpy.testing as npt
from pysph.solver.solver import Solver
class TestSolver(TestCase):
def setUp(self):
patcher = mock.patch(
'pysph.sph.acceleration_eval.AccelerationEval', spec=True
)
AccelerationEval = patcher.start()
self.a_eval = AccelerationEval()
self.addCleanup(patcher.stop)
patcher = mock.patch(
'pysph.sph.integrator.PECIntegrator', spec=True
)
PECIntegrator = patcher.start()
self.integrator = PECIntegrator()
self.addCleanup(patcher.stop)
def test_solver_dumps_output_given_output_at_times(self):
# Given
dt = 0.1
self.integrator.compute_time_step.return_value = dt
tf = 10.05
pfreq = 5
output_at_times = [0.3, 0.35]
solver = Solver(
integrator=self.integrator, tf=tf, dt=dt,
output_at_times=output_at_times
)
solver.set_print_freq(pfreq)
solver.acceleration_eval = self.a_eval
solver.particles = []
# When
record = []
record_dt = []
def _mock_dump_output():
# Record the time at which the solver dumped anything
record.append(solver.t)
# This smells but ...
sd = solver._get_solver_data()
record_dt.append(sd['dt'])
solver.dump_output = mock.Mock(side_effect=_mock_dump_output)
solver.solve(show_progress=False)
# Then
expected = np.asarray(
[0.0, 0.3, 0.35] + np.arange(0.45, 10.1, 0.5).tolist() + [10.05]
)
error_message = "Expected %s, got %s"%(expected, record)
self.assertEqual(len(expected), len(record), error_message)
self.assertTrue(
np.max(np.abs(expected - record)) < 1e-12, error_message
)
self.assertEqual(101, solver.count)
# The final timestep should not be a tiny one due to roundoff.
self.assertTrue(solver.dt > 0.1*0.25)
npt.assert_array_almost_equal(
[0.1]*len(record_dt), record_dt, decimal=12
)
def test_solver_honors_set_time_step(self):
# Given
dt = 0.1
tf = 1.0
pfreq = 1
solver = Solver(
integrator=self.integrator, tf=tf, dt=dt, adaptive_timestep=False
)
solver.set_print_freq(pfreq)
solver.acceleration_eval = self.a_eval
solver.particles = []
record = []
def _mock_dump_output():
# Record the time at which the solver dumped anything
record.append(solver.t)
solver.dump_output = mock.Mock(side_effect=_mock_dump_output)
# When
solver.set_time_step(0.2)
solver.solve(show_progress=False)
# Then
expected = np.asarray([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
error_message = "Expected %s, got %s"%(expected, record)
self.assertEqual(len(expected), len(record), error_message)
self.assertTrue(
np.max(np.abs(expected - record)) < 1e-12, error_message
)
if __name__ == '__main__':
main()
|