def test_cg(): w1 = Mass.from_position_m(10, PositionM(2, 5, 20)) w2 = Mass.from_position_m(10, PositionM(10, 5, 30)) masses = MassesCollection() masses.add_mass(w1) masses.add_mass(w2) assert masses.mass_kg == 20 assert masses.cg_m.x == 6 assert masses.cg_m.y == 5 assert masses.cg_m.z == 25
def test_find_corrector(): w1 = Mass.from_position_m(10, PositionM(10, 0, -10)) masses = MassesCollection() masses.add_mass(w1) corrector = find_corrector(masses, target_mass_kg=20, target_x_m=50, target_y_m=0, target_z_m=0) expected_value = 98.06649999999999 tol = 1e-9 assert expected_value - tol <= corrector.weight_N <= expected_value + tol
def test_corrector_force(): w1 = Mass.from_position_m(10, PositionM(10, 0, -10)) masses = MassesCollection() masses.add_mass(w1) corrector = find_corrector(masses, target_mass_kg=20, target_x_m=50, target_y_m=0, target_z_m=0) expected_value = -98.06649999999999 tol = 1e-9 assert corrector.force_N_m.fx == 0 assert corrector.force_N_m.fy == 0 assert expected_value - tol <= corrector.force_N_m.fz <= expected_value + tol assert corrector.force_N_m.px == 90 assert corrector.force_N_m.py == 0 assert corrector.force_N_m.pz == 10
def test_total_mass(): w1 = Mass.from_position_m(10, PositionM(2, 3, 4)) w2 = Mass.from_position_m(10, PositionM(2, 3, 4)) w3 = Mass.from_position_m(15, PositionM(2, 3, 4)) masses = MassesCollection() masses.add_mass(w1) masses.add_mass(w2) masses.add_mass(w3) assert masses.mass_kg == 35
def test_total_weight(): w1 = Mass.from_position_m(10, PositionM(2, 3, 4)) w2 = Mass.from_position_m(10, PositionM(2, 3, 4)) w3 = Mass.from_position_m(15, PositionM(2, 3, 4)) masses = MassesCollection() masses.add_mass(w1) masses.add_mass(w2) masses.add_mass(w3) expected_value = 343.23274999999995 tol = 1e-9 assert expected_value - tol <= masses.weight_N <= expected_value + tol
def test_total_force(): w1 = Mass.from_position_m(10, PositionM(2, 3, 4)) w2 = Mass.from_position_m(10, PositionM(2, 3, 4)) w3 = Mass.from_position_m(15, PositionM(2, 3, 4)) masses = MassesCollection() masses.add_mass(w1) masses.add_mass(w2) masses.add_mass(w3) expected_value = -343.23274999999995 tol = 1e-9 assert masses.force_N_m.fx == 0 assert masses.force_N_m.fy == 0 assert expected_value - tol <= masses.force_N_m.fz <= expected_value + tol
def test_find_corrector(): w1 = Mass.from_position_m(10, PositionM(10, 0, -10)) masses = MassesCollection() masses.add_mass(w1) corrector = find_corrector(masses, target_mass_kg=20, target_x_m=50, target_y_m=0, target_z_m=0) assert corrector.mass_kg == 10 assert corrector.cg_m.x == 90 assert corrector.cg_m.y == 0 assert corrector.cg_m.z == 10 # should work with the weight instead of the collection corrector = find_corrector(w1, target_mass_kg=20, target_x_m=50, target_y_m=0, target_z_m=0) assert corrector.mass_kg == 10 assert corrector.cg_m.x == 90 assert corrector.cg_m.y == 0 assert corrector.cg_m.z == 10 w1 = Mass.from_position_m(10, PositionM(10, 0, 0)) masses = MassesCollection() masses.add_mass(w1) corrector = find_corrector(masses, target_mass_kg=20, target_x_m=0, target_y_m=0, target_z_m=0) assert corrector.mass_kg == 10 assert corrector.cg_m.x == -10 assert corrector.cg_m.y == 0 assert corrector.cg_m.z == 0 w1 = Mass.from_position_m(10, PositionM(0, 0, 0)) masses = MassesCollection() masses.add_mass(w1) corrector = find_corrector(masses, target_mass_kg=20, target_x_m=0, target_y_m=0, target_z_m=0) assert corrector.mass_kg == 10 assert corrector.cg_m.x == 0 assert corrector.cg_m.y == 0 assert corrector.cg_m.z == 0
def load_masses_from_file( filename: str) -> Tuple[MassesCollection, str, str, int]: r"""Load the mass collection from a weights file. Parameters ---------- filename : Path to the file The file has the following format: # comment mass_unit, position_unit mass_1, x_coordinate_1, y_coordinate_1, z_coordinate_1, name mass_2, x_coordinate_2, y_coordinate_2, z_coordinate_2, name ... mass_n, x_coordinate_n, y_coordinate_n, z_coordinate_n, name Returns ------- masses collection, initial_mass_unit, initial_position_unit """ masses = MassesCollection() with open(filename) as f: # Do not consider comment lines and empty lines lines = list( filter(lambda l: not l.startswith("#") and len(l) > 0, f.readlines())) # units units_data = (lines[0].strip()).split(",") initial_mass_unit = units_data[0].strip() initial_position_unit = units_data[1].strip() # Counter for templated lines nb_templated_lines = 0 # data for line in lines[1:]: if "{{" not in line: # The line is a plain line (i.e. not a templated line) parts = (line.strip()).split(",") w = float(parts[0]) x = float(parts[1]) y = float(parts[2]) z = float(parts[3]) try: name = parts[4].strip() except IndexError: name = "" kwargs_weight = {initial_mass_unit: w} mass_kg = kg(**kwargs_weight) position = Position(x, y, z, initial_position_unit) position_m = position2positionm(position) mass = Mass.from_position_m(mass_kg, position_m, name) masses.add_mass(mass) else: # This is a templated line, do not use it to compute the weights collection nb_templated_lines += 1 return masses, initial_mass_unit, initial_position_unit, nb_templated_lines