def __init__(self, size, array_size=None): super().__init__(size, array_size=array_size) self.fluid = Fluid(self.array_size, 0.00001, 0.000000001) self.c = int(0.5 * self.array_size) amount = np.random.randint(0, 255) for j in [-1, 0, 1]: for i in [-1, 0, 1]: self.fluid.add_density(self.c + i, self.c + j, amount)
def __init__(self, curve=None, nominal_speed=1800, eta=[0], fluid=Fluid()): self.c0 = -6.6e3 self.c1 = 27.0 self.c2 = 19.0 if curve: self.c0 = curve[0] self.c1 = curve[1] self.c2 = curve[2] self.n0 = nominal_speed self.fluid = fluid
def get_mass_flow_inlet(st: Geometry, rot: Geometry, param: Regime, fluid: Fluid, p0, ksi): from hyd.flow import G sum_area = get_area_inlet(st.angle_slot, rot.angle_slot, st.r_inner, st.high_slot, param.rpm) t1 = param.T_inlet p1 = param.p_inlet * 1E+5 ro = fluid.get_ro(p1, t1) p0 = p0 * 1E+5 return G(ksi, sum_area, fluid.k, ro, p1, p0)
def __init__(self, geo, dim, dt, scheme, bc_centre, bc_fringe, materialSet, fluid_velocity): self.__dt = dt grid = Grid(dim) grid.setUp(geo) self.__numberOfIterations = 10 if bc_fringe.flag_iteration_required else 1 bc_centre.set_parameter(materialSet, geo.radius_pipe) bc_fringe.set_parameter(materialSet, geo.radius_BHE) self.__stratification = Stratification(grid, materialSet) self.__equationSystem = EquationSystem(grid, scheme, bc_centre, bc_fringe, self.__stratification.layers) self.__fluid_velocity = fluid_velocity self.__plot = Plot() self.__fluid = Fluid(materialSet.fluid, np.pi * geo.radius_pipe * geo.radius_pipe, self.__stratification.numberOfLayers + 1) self.__numberOfSubTimeSteps = dt
def main(): fluid = Fluid(dt=0.001, diffusion=0.00001, viscosity=0.00001, size=64) fig, ax = plt.subplots() plot = plt.imshow(fluid.density, animated=True) def update(*args): from random import randint x, y = randint(0, 53), randint(0, 53) fluid.add_density(x, y, 100) fluid.add_velocity(x, y, 1000, 10000) fluid.step() plot.set_array(fluid.density) plot.autoscale() return (plot,) _ = animation.FuncAnimation(fig, update, interval=3, blit=True) plt.show()
directions = tuple(-circle(p * np.pi * 2 / DOTS + np.pi / 2) for p in range(DOTS)) points = tuple(r[count] * circle(p + 1 * np.pi * 2 / DOTS) + center for p in range(DOTS)) count += 1 while count < LAYERS: r[count] = np.min(center) - INFLOW_PADDING * (count + 1) directions += tuple(-circle(p * np.pi * 2 / DOTS + np.pi / 2) for p in range(DOTS)) points += tuple(r[count] * circle(p * np.pi * 2 / DOTS) + center for p in range(DOTS)) count += 1 channels = 'r', 'g', 'b' fluid = Fluid(RESOLUTION, VISCOSITY, channels) inflow_dye_field = np.zeros((fluid.size, len(channels))) inflow_velocity_field = np.zeros_like(fluid.velocity_field) for i, p in enumerate(points): distance = np.linalg.norm(fluid.indices - p, axis=1) mask = distance <= INFLOW_RADIUS for d in range(2): inflow_velocity_field[..., d][mask] = directions[i][d] * INFLOW_VELOCITY inflow_dye_field[..., 1][mask] = 1 for frame in range(DURATION): print(f'Computing frame {frame}.')
INFLOW_PADDING = 50 INFLOW_DURATION = 60 INFLOW_RADIUS = 8 INFLOW_VELOCITY = 12 def circle(theta): return np.asarray((np.cos(theta), np.sin(theta))) center = np.asarray(RESOLUTION) // 2 r = np.min(center) - INFLOW_PADDING directions = tuple(-circle(p * np.pi * 2 / 3) for p in range(3)) points = tuple(r * circle(p * np.pi * 2 / 3) + center for p in range(3)) fluid = Fluid(RESOLUTION, VISCOSITY, ('r', 'g', 'b')) inflow_dye_field = np.zeros((np.product(RESOLUTION), 3)) inflow_velocity_field = np.zeros_like(fluid.velocity_field) for i, p in enumerate(points): _ = np.dstack(tuple(fluid.indices[..., d] - p[d] for d in range(2))) _ = np.linalg.norm(_.squeeze(), axis=1) for d in range(2): inflow_velocity_field[..., d][ _ <= INFLOW_RADIUS] = directions[i][d] * INFLOW_VELOCITY inflow_dye_field[..., i][_ <= INFLOW_RADIUS] = 1 for frame in range(DURATION): print(f'Computing frame {frame}.')
# ---------------------------------------------------------------------------- # Title: Scientific Visualisation - Python & Matplotlib # Author: Nicolas P. Rougier # License: BSD # ---------------------------------------------------------------------------- import numpy as np from fluid import Fluid, inflow from scipy.special import erf import matplotlib.pyplot as plt import matplotlib.animation as animation shape = 256, 256 duration = 500 fluid = Fluid(shape, "dye") inflows = [ inflow(fluid, x) for x in np.linspace(-np.pi, np.pi, 8, endpoint=False) ] # Animation setup fig = plt.figure(figsize=(5, 5), dpi=100) ax = fig.add_axes([0, 0, 1, 1], frameon=False) ax.set_xlim(0, 1) ax.set_xticks([]) ax.set_ylim(0, 1) ax.set_yticks([]) im = ax.imshow( np.zeros(shape), extent=[0, 1, 0, 1], vmin=0, vmax=1, origin="lower",
class FluidSim(engine.ArrayViewerGL): def __init__(self, size, array_size=None): super().__init__(size, array_size=array_size) self.fluid = Fluid(self.array_size, 0.00001, 0.000000001) self.c = int(0.5 * self.array_size) amount = np.random.randint(0, 255) for j in [-1, 0, 1]: for i in [-1, 0, 1]: self.fluid.add_density(self.c + i, self.c + j, amount) def process_events(self, events): super().process_events(events) for event in events: if event.type == engine.MOUSE_DRAGGED: x = int(event.pos[0] // self.scale) y = int(event.pos[1] // self.scale) if event.button == 1: dx = event.rel[0] // self.scale dy = event.rel[1] // self.scale for i in [-1, 0, 1]: for j in [-1, 0, 1]: self.fluid.add_velocity(x + i, y + j, dx * 2, dy * 2) elif event.button == 3: for i in [-1, 0, 1]: for j in [-1, 0, 1]: self.fluid.add_density(x + i, y + j, 200) elif event.type == engine.KEY_DOWN: if event.key == pygame.K_SPACE: self.fluid.density *= 0 elif event.type == engine.KEY_HOLD: if event.key == pygame.K_UP: self.fluid.diff += 0.000001 elif event.key == pygame.K_DOWN: self.fluid.diff -= 0.000001 def update(self, input, t, dt): super().update(input, t, dt) # amount = np.random.randint(0, 255) amount = 150 for j in [-1, 0, 1]: for i in [-1, 0, 1]: self.fluid.add_density(self.c + i, self.c + j, amount) # angle = t / 1e9 * np.pi / 180 * 10 angle = np.random.randint(0, 360) * np.pi / 180 x = 5 * math.cos(angle) y = 5 * math.sin(angle) for j in [-1, 0, 1]: for i in [-1, 0, 1]: self.fluid.add_velocity(self.c + i, self.c + j, x, y) self.fluid.step(dt) self.array[:, :, 0] = self.fluid.density self.array[:, :, 1] = self.fluid.v_x * 255 self.array[:, :, 2] = self.fluid.v_y * 255
class BHE: def __init__(self, geo, dim, dt, scheme, bc_centre, bc_fringe, materialSet, fluid_velocity): self.__dt = dt grid = Grid(dim) grid.setUp(geo) self.__numberOfIterations = 10 if bc_fringe.flag_iteration_required else 1 bc_centre.set_parameter(materialSet, geo.radius_pipe) bc_fringe.set_parameter(materialSet, geo.radius_BHE) self.__stratification = Stratification(grid, materialSet) self.__equationSystem = EquationSystem(grid, scheme, bc_centre, bc_fringe, self.__stratification.layers) self.__fluid_velocity = fluid_velocity self.__plot = Plot() self.__fluid = Fluid(materialSet.fluid, np.pi * geo.radius_pipe * geo.radius_pipe, self.__stratification.numberOfLayers + 1) self.__numberOfSubTimeSteps = dt @property def dt(self): return self.__dt @property def dim(self): return self.__dim @property def m(self): return self.__m @property def r(self): return self.__r @property def memory(self): return self.__memory def step_forward(self, numberOfTimeSteps): for timeStep in range(numberOfTimeSteps): print("Time step {} of {}".format(timeStep, numberOfTimeSteps)) self.__equationSystem.assemble_rightHandSide(timeStep, self.__dt) self.__equationSystem.set_boundary_centre() for j in range(self.__numberOfIterations): # print(" Iteration {} of {}".format(j, self.__numberOfIterations)) self.__fluid.solve(self.__fluid_velocity, self.__equationSystem.memory) self.__equationSystem.memory.current_fluid_temperatureList = self.__fluid.result self.__equationSystem.set_boundary_fringe() self.__equationSystem.solve() self.__equationSystem.calculate_fluxes() self.show_result(Print()) self.show_result(self.__plot) def assemble_matrix(self): self.__equationSystem.assemble_matrix(self.__dt) def show_result(self, output): output.execute(self.__equationSystem)
def __init__(self): self.fluid = Fluid(0, 0)
from fluid import Fluid import numpy as np import pygame, sys, math from pygame import surfarray from pygame.locals import * surfarray.use_arraytype('numpy') w = h = 800 fluid = Fluid(w, h) data = np.zeros((w * h)) data[fluid.ix(5, 2):fluid.ix(20, 20)] = 2 #for i in range(10): # print(i) # fluid.diffuse(fluid.dens, data, 0.8) pygame.init() disp = pygame.display.set_mode((w, h), 0, 32) clock = pygame.time.Clock() while True: dt = clock.tick(60) print(dt) #fluid.diffuse(fluid.dens, data, 0.8, dt) #fluid.add_source(fluid.dens, data, dt) striped = np.zeros((w, h, 3), np.int32) striped[:] = (0, 0, 0) striped[1:30, 2:50] = (255, 255, 255)
from PIL import Image from scipy.special import erf from fluid import Fluid RESOLUTION = 500, 500 DURATION = 200 INFLOW_PADDING = 50 INFLOW_DURATION = 60 INFLOW_RADIUS = 8 INFLOW_VELOCITY = 1 INFLOW_COUNT = 5 print('Generating fluid solver, this may take some time.') fluid = Fluid(RESOLUTION, 'dye') center = np.floor_divide(RESOLUTION, 2) r = np.min(center) - INFLOW_PADDING points = np.linspace(-np.pi, np.pi, INFLOW_COUNT, endpoint=False) points = tuple(np.array((np.cos(p), np.sin(p))) for p in points) normals = tuple(-p for p in points) points = tuple(r * p + center for p in points) inflow_velocity = np.zeros_like(fluid.velocity) inflow_dye = np.zeros(fluid.shape) for p, n in zip(points, normals): mask = np.linalg.norm(fluid.indices - p[:, None, None], axis=0) <= INFLOW_RADIUS inflow_velocity[:, mask] += n[:, None] * INFLOW_VELOCITY inflow_dye[mask] = 1
def __init__(self, curve=None, nominal_speed=1800, fluid=Fluid()): self.c = np.array([9.6490e3, 0, 0]) self.fluid = fluid if curve: self.c = curve