def temp_dump(): # Initialize the Ellipsoids ell1 = Ellipsoid(1, 5, 5, 5, 2.0, 2.0, 2.0, np.array( [0.52532199, 0., -0., 0.85090352])) ell2 = Ellipsoid(2, 5.5, 5.5, 5.5, 2.0, 2.0, 2.0, np.array([0.52532199, 0., -0., 0.85090352])) ells = [ell1, ell2] # Inititalize the simulation box sbox = Simulation_Box(10, 10, 10) write_dump(ells, sbox, len(ells)) return sbox
def test_assign_voxels_to_ellipsoid(dec_info): # Initialize the Ellipsoids ell1 = Ellipsoid(1, 1, 0.5, 0.75, 2.0, 1.5, 1.5, np.array([0.52532199, 0., -0., 0.85090352])) ell2 = Ellipsoid(2, 1.9, 1.68, 2.6, 2.0, 1.5, 1.5, np.array([0.52532199, 0., -0., 0.85090352])) ells = [ell1, ell2] assign_voxels_to_ellipsoid(dec_info[2], ells, dec_info[1]) ref1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16] ref2 = [14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27] assert set(ell1.inside_voxels) == set(ref1) assert set(ell2.inside_voxels) == set(ref2)
def test_reassign_shared_voxels(dec_info): # Initialize the Ellipsoids ell1 = Ellipsoid(1, 1, 0.5, 0.75, 2.0, 1.5, 1.5, np.array([0.52532199, 0., -0., 0.85090352])) ell2 = Ellipsoid(2, 1.9, 1.68, 2.6, 2.0, 1.5, 1.5, np.array([0.52532199, 0., -0., 0.85090352])) # Update the ellipsoid dictionary of ficture function ell1.inside_voxels.extend([1, 2, 3, 4, 5, 10, 11, 13, 14, 7, 16, 8, 19, 6]) ell2.inside_voxels.extend( [12, 14, 15, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 16, 19, 9]) ells = [ell1, ell2] reassign_shared_voxels(dec_info[2], ells) ref1 = [1, 2, 3, 4, 5, 10, 11, 13, 7, 8, 6, 19] ref2 = [12, 15, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 9, 16, 14] assert set(ref1) == set(ell1.inside_voxels) assert set(ref2) == set(ell2.inside_voxels)
def particle_generator(particle_data, sim_box): """ Initializes ellipsoids by assigning them random positions and speeds within the simulation box. :param particle_data: Ellipsoid information such as such as Major, Minor, Equivalent diameters and its tilt angle. :type particle_data: Python dictionary :param sim_box: Simulation box representing RVE. :type sim_box: :class:`entities.Simulation_Box` :returns: Ellipsoids for the packing routine :rtype: list """ num_particles = particle_data['Number'] # Total number of ellipsoids Ellipsoids = [] for n in range(num_particles): iden = n+1 # ellipsoid 'id' a = particle_data['Major_diameter'][n] / 2. # Semi-major length b = particle_data['Minor_diameter1'][n] / 2. # Semi-minor length 1 c = particle_data['Minor_diameter2'][n] / 2. # Semi-minor length 2 # Random placement for ellipsoids x = random.uniform(c, sim_box.w - c) y = random.uniform(c, sim_box.h - c) z = random.uniform(c, sim_box.d - c) # Angle represents inclination of Major axis w.r.t positive x-axis angle = np.radians(particle_data['Orientation'][n]) # Extract the angle vec_a = np.array([a*np.cos(angle), a*np.sin(angle), 0.0]) # Tilt vector wrt (+ve) x axis cross_a = np.cross(np.array([1, 0, 0]), vec_a) # Do the cross product to find the quaternion axis norm_cross_a = np.linalg.norm(cross_a, 2) # norm of the vector (Magnitude) quat_axis = cross_a/norm_cross_a # normalize the quaternion axis # Find the quaternion components qx, qy, qz = quat_axis * np.sin(angle/2) qw = np.cos(angle/2) quat = np.array([qw, qx, qy, qz]) # instance of Ellipsoid class ellipsoid = Ellipsoid(iden, x, y, z, a, b, c, quat) ellipsoid.color = (random.randint(0, 255), random.randint( 0, 255), random.randint(0, 255)) # Define random speed values along the 3 axes x, y & z ellipsoid.speedx = np.random.uniform(low=-c/20., high=c/20.) ellipsoid.speedy = np.random.uniform(low=-c/20., high=c/20.) ellipsoid.speedz = np.random.uniform(low=-c/20., high=c/20.) Ellipsoids.append(ellipsoid) # adds ellipsoid to list return Ellipsoids
def test_collision_routine_sphere(): # Initialize the Ellipsoids el1 = Ellipsoid(1, 5, 5, 5, 2.0, 2.0, 2.0, np.array([0.52532199, 0., -0., 0.85090352])) el2 = Ellipsoid(2, 5.5, 5.5, 5.5, 2.0, 2.0, 2.0, np.array([0.52532199, 0., -0., 0.85090352])) el1.speedx, el1.speedy, el1.speedz = 0.1, 0.075, -0.05 el2.speedx, el2.speedy, el2.speedz = -0.1, -0.025, -0.36 # Test if the 'collision_react' function is called twice with mock.patch( 'kanapy.collision_detect_react.collision_react') as mocked_method: collision_routine(el1, el2) assert mocked_method.call_count == 2
def test_particle_grow(rot_surf): ell1 = Ellipsoid(1, 1, 0.5, 0.75, 2.0, 1.5, 1.5, rot_surf[0]) ell2 = Ellipsoid(2, 1.9, 1.68, 2.6, 2.0, 1.5, 1.5, rot_surf[0]) ell1.speedx, ell1.speedy, ell1.speedz = -0.02, 0.075, -0.05 ell2.speedx, ell2.speedy, ell2.speedz = 0.5, -0.025, -0.36 ells = [ell1, ell2] sb = Simulation_Box(10, 10, 10) particle_grow(sb, ells, True, 10) assert round(ell1.x, 6) == 0.78 assert round(ell1.y, 6) == 1.325 assert round(ell1.z, 6) == 0.2 assert round(ell2.x, 6) == 7.4 assert round(ell2.y, 6) == 1.405 assert round(ell2.z, 6) == 8.64
def read_dump(dump_file): """ Reads the (.dump) file to extract information for voxelization (meshing) routine :param dump_file: Contains information of ellipsoids generated in the packing routine. :type dump_file: document :returns: * Cuboid object that represents the RVE. * List of ellipsoid objects that represent the grains. :rtype: Tuple of python objects (:obj:`Cuboid`, :obj:`Ellipsoid`) """ print(' Reading the .dump file for particle information') try: # Read the Simulation box dimensions with open(dump_file, 'r+') as fd: lookup = "ITEM: NUMBER OF ATOMS" lookup2 = "ITEM: BOX BOUNDS ff ff ff" for num, lines in enumerate(fd, 1): if lookup in lines: number_particles = int(next(fd)) par_line_num = num + 7 if lookup2 in lines: valuesX = re.findall(r'\S+', next(fd)) RVE_minX, RVE_maxX = list(map(float, valuesX)) valuesY = re.findall(r'\S+', next(fd)) RVE_minY, RVE_maxY = list(map(float, valuesY)) valuesZ = re.findall(r'\S+', next(fd)) RVE_minZ, RVE_maxZ = list(map(float, valuesZ)) except FileNotFoundError: print(' .dump file not found, make sure "packingRoutine()" function is executed first!') raise FileNotFoundError # Create an instance of simulation box sim_box = Cuboid(RVE_minX, RVE_maxY, RVE_maxX, RVE_minY, RVE_maxZ, RVE_minZ) # Read the particle shape & position information # Create instances for ellipsoids & assign values from dump files Ellipsoids = [] with open(dump_file, "r") as f: count = 0 for num, lines in enumerate(f, 1): if num >= par_line_num: count += 1 values = re.findall(r'\S+', lines) int_values = list(map(float, values[1:])) values = [values[0]] + int_values iden = count # ellipsoid 'id' a, b, c = values[4], values[5], values[6] # Semi-major length, Semi-minor length 1 & 2 x, y, z = values[1], values[2], values[3] qx, qy, qz, qw = values[7], values[8], values[9], values[10] quat = np.array([qw, qx, qy, qz]) ellipsoid = Ellipsoid(iden, x, y, z, a, b, c, quat) # instance of Ellipsoid class # Find the original particle if the current is duplicate for c in values[0]: if c == '_': split_str = values[0].split("_") original_id = int(split_str[0]) ellipsoid.duplicate = original_id break else: continue Ellipsoids.append(ellipsoid) return sim_box, Ellipsoids
def test_collision_react(): # Initialize the Ellipsoids el1 = Ellipsoid(1, 5, 5, 5, 2.0, 2.0, 2.0, np.array([0.52532199, 0., -0., 0.85090352])) el2 = Ellipsoid(2, 5.5, 5.5, 5.5, 2.0, 2.0, 2.0, np.array([0.52532199, 0., -0., 0.85090352])) el1.speedx, el1.speedy, el1.speedz = 0.1, 0.075, -0.05 el2.speedx, el2.speedy, el2.speedz = -0.1, -0.025, -0.36 # Test different conditions # Condition: xdiff > 0 && zdiff > 0 collision_react(el1, el2) collision_react(el2, el1) assert round(el1.speedx, 6) == -0.077728 assert round(el1.speedy, 6) == -0.077728 assert round(el1.speedz, 6) == -0.077728 assert round(el2.speedx, 6) == 0.216198 assert round(el2.speedy, 6) == 0.216198 assert round(el2.speedz, 6) == 0.216198 # Condition: xdiff > 0 && zdiff < 0 el2.z = 4.5 collision_react(el1, el2) collision_react(el2, el1) assert round(el1.speedx, 6) == -0.077728 assert round(el1.speedy, 6) == -0.077728 assert round(el1.speedz, 6) == 0.077728 assert round(el2.speedx, 6) == 0.216198 assert round(el2.speedy, 6) == 0.216198 assert round(el2.speedz, 6) == -0.216198 # Condition: xdiff < 0 && zdiff > 0 el2.x = 4.5 el2.z = 5.5 collision_react(el1, el2) collision_react(el2, el1) assert round(el1.speedx, 6) == 0.077728 assert round(el1.speedy, 6) == -0.077728 assert round(el1.speedz, 6) == -0.077728 assert round(el2.speedx, 6) == -0.216198 assert round(el2.speedy, 6) == 0.216198 assert round(el2.speedz, 6) == 0.216198 # Condition: xdiff < 0 && zdiff < 0 el2.x = 4.5 el2.z = 4.5 collision_react(el1, el2) collision_react(el2, el1) assert round(el1.speedx, 6) == 0.077728 assert round(el1.speedy, 6) == -0.077728 assert round(el1.speedz, 6) == 0.077728 assert round(el2.speedx, 6) == -0.216198 assert round(el2.speedy, 6) == 0.216198 assert round(el2.speedz, 6) == -0.216198 # Condition: xdiff = 0 && zdiff = 0 el2.x = 5.0 el2.z = 5.0 collision_react(el1, el2) collision_react(el2, el1) assert round(el1.speedx, 6) == 0.000038 assert round(el1.speedy, 6) == 0.134629 assert round(el1.speedz, 6) == 0.0 assert round(el2.speedx, 6) == 0.000106 assert round(el2.speedy, 6) == -0.374466 assert round(el2.speedz, 6) == 0.0 # Condition: xdiff = 0 && zdiff > 0 el2.x = 5.0 el2.z = 5.5 collision_react(el1, el2) collision_react(el2, el1) assert round(el1.speedx, 6) == 0.0 assert round(el1.speedy, 6) == 0.095197 assert round(el1.speedz, 6) == -0.095197 assert round(el2.speedx, 6) == 0.0 assert round(el2.speedy, 6) == -0.264788 assert round(el2.speedz, 6) == 0.264788 # Condition: xdiff = 0 && zdiff < 0 el2.x = 5.0 el2.z = 4.5 collision_react(el1, el2) collision_react(el2, el1) assert round(el1.speedx, 6) == 0.0 assert round(el1.speedy, 6) == 0.095197 assert round(el1.speedz, 6) == 0.095197 assert round(el2.speedx, 6) == 0.0 assert round(el2.speedy, 6) == -0.264788 assert round(el2.speedz, 6) == -0.264788 # Condition: xdiff < 0 && zdiff = 0 el2.x = 4.5 el2.z = 5.0 collision_react(el1, el2) collision_react(el2, el1) assert round(el1.speedx, 6) == 0.095197 assert round(el1.speedy, 6) == 0.095197 assert round(el1.speedz, 6) == 0.0 assert round(el2.speedx, 6) == -0.264788 assert round(el2.speedy, 6) == -0.264788 assert round(el2.speedz, 6) == 0.0 # Condition: xdiff > 0 && zdiff = 0 el2.x = 5.5 el2.z = 5.0 collision_react(el1, el2) collision_react(el2, el1) assert round(el1.speedx, 6) == -0.095197 assert round(el1.speedy, 6) == 0.095197 assert round(el1.speedz, 6) == 0.0 assert round(el2.speedx, 6) == 0.264788 assert round(el2.speedy, 6) == -0.264788 assert round(el2.speedz, 6) == 0.0