def test_updating_particle_arrays(self): # Given xd = [0.5] hd = self.src.h[:1] dest = get_particle_array(name='dest', x=xd, h=hd) sph_eval = SPHEvaluator([dest, self.src], equations=self.equations, dim=1) sph_eval.evaluate() rho0 = dest.rho[0] # When. dest.x[0] = 0.0 sph_eval.update_particle_arrays([dest, self.src]) sph_eval.evaluate() # Then. self.assertNotEqual(rho0, dest.rho[0]) self.assertAlmostEqual(dest.rho[0], 7.0, places=1)
def _plot_cd_vs_t(self): from pysph.solver.utils import iter_output, load from pysph.tools.sph_evaluator import SPHEvaluator from pysph.sph.equation import Group from pysph.sph.wc.transport_velocity import (SetWallVelocity, MomentumEquationPressureGradient, SolidWallNoSlipBC, SolidWallPressureBC, VolumeSummation) data = load(self.output_files[0]) solid = data['arrays']['solid'] fluid = data['arrays']['fluid'] x, y = solid.x.copy(), solid.y.copy() cx = 0.5 * L; cy = 0.5 * H inside = np.sqrt((x-cx)**2 + (y-cy)**2) <= a dest = solid.extract_particles(inside.nonzero()[0]) # We use the same equations for this as the simulation, except that we # do not include the acceleration terms as these are externally # imposed. The goal of these is to find the force of the fluid on the # cylinder, thus, gx=0.0 is used in the following. equations = [ Group( equations=[ VolumeSummation( dest='fluid', sources=['fluid', 'solid'] ), VolumeSummation( dest='solid', sources=['fluid', 'solid'] ), ], real=False), Group( equations=[ SetWallVelocity(dest='solid', sources=['fluid']), ], real=False), Group( equations=[ SolidWallPressureBC(dest='solid', sources=['fluid'], gx=0.0, b=1.0, rho0=rho0, p0=p0), ], real=False), Group( equations=[ # Pressure gradient terms MomentumEquationPressureGradient( dest='fluid', sources=['solid'], gx=0.0, pb=pb), SolidWallNoSlipBC( dest='fluid', sources=['solid'], nu=nu), ], real=True), ] sph_eval = SPHEvaluator( arrays=[dest, fluid], equations=equations, dim=2, kernel=QuinticSpline(dim=2) ) t, cd = [], [] for sd, fluid in iter_output(self.output_files, 'fluid'): fluid.remove_property('vmag2') t.append(sd['t']) sph_eval.update_particle_arrays([dest, fluid]) sph_eval.evaluate() Fx = np.sum(-fluid.au*fluid.m) cd.append(Fx/(nu*rho0*Umax)) t, cd = list(map(np.asarray, (t, cd))) # Now plot the results. import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt f = plt.figure() plt.plot(t, cd) plt.xlabel('$t$'); plt.ylabel(r'$C_D$') fig = os.path.join(self.output_dir, "cd_vs_t.png") plt.savefig(fig, dpi=300) plt.close() return t, cd
def _plot_force_vs_t(self): from pysph.solver.utils import iter_output, load from pysph.tools.sph_evaluator import SPHEvaluator from pysph.sph.equation import Group from pysph.base.kernels import QuinticSpline from pysph.sph.wc.transport_velocity import ( MomentumEquationPressureGradient, SummationDensity, SetWallVelocity) data = load(self.output_files[0]) solid = data['arrays']['solid'] fluid = data['arrays']['fluid'] prop = [ 'awhat', 'auhat', 'avhat', 'wg', 'vg', 'ug', 'V', 'uf', 'vf', 'wf', 'wij', 'vmag', 'pavg', 'nnbr', 'auf', 'avf', 'awf' ] for p in prop: solid.add_property(p) fluid.add_property(p) # We find the force of the solid on the fluid and the opposite of that # is the force on the solid. Note that the assumption is that the solid # is far from the inlet and outlet so those are ignored. print(self.nu, p0, self.dc, rho) equations = [ Group(equations=[ SummationDensity(dest='fluid', sources=['fluid', 'solid']), SummationDensity(dest='solid', sources=['fluid', 'solid']), SetWallVelocity(dest='solid', sources=['fluid']), ], real=False), Group( equations=[ # Pressure gradient terms MomentumEquationPressureGradient(dest='solid', sources=['fluid'], pb=p0), SolidWallNoSlipBCReverse(dest='solid', sources=['fluid'], nu=self.nu), ], real=True), ] sph_eval = SPHEvaluator(arrays=[solid, fluid], equations=equations, dim=2, kernel=QuinticSpline(dim=2)) t, cd, cl = [], [], [] import gc print(self.dc, self.dx, self.nu) print('fxf', 'fxp', 'fyf', 'fyp', 'cd', 'cl', 't') for sd, arrays in iter_output(self.output_files[:]): fluid = arrays['fluid'] solid = arrays['solid'] for p in prop: solid.add_property(p) fluid.add_property(p) t.append(sd['t']) sph_eval.update_particle_arrays([solid, fluid]) sph_eval.evaluate() fxp = sum(solid.m * solid.au) fyp = sum(solid.m * solid.av) fxf = sum(solid.m * solid.auf) fyf = sum(solid.m * solid.avf) fx = fxf + fxp fy = fyf + fyp cd.append(fx / (0.5 * rho * umax**2 * self.dc)) cl.append(fy / (0.5 * rho * umax**2 * self.dc)) print(fxf, fxp, fyf, fyp, cd[-1], cl[-1], t[-1]) gc.collect() t, cd, cl = list(map(np.asarray, (t, cd, cl))) # Now plot the results. import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt plt.figure() plt.plot(t, cd, label=r'$C_d$') plt.plot(t, cl, label=r'$C_l$') plt.xlabel(r'$t$') plt.ylabel('cd/cl') plt.legend() plt.grid() fig = os.path.join(self.output_dir, "force_vs_t.png") plt.savefig(fig, dpi=300) plt.close() return t, cd, cl