Coverage for mpcforces_extractor\test_force_extractor.py: 99%

67 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-01 11:08 +0100

1import unittest 

2from unittest.mock import patch, mock_open 

3from mpcforces_extractor.force_extractor import MPCForceExtractor 

4from mpcforces_extractor.reader.modelreaders import FemFileReader 

5from mpcforces_extractor.datastructure.entities import Element 

6from mpcforces_extractor.visualization.tcl_visualize import VisualizerConnectedParts 

7from mpcforces_extractor.writer.summary_writer import SummaryWriter 

8from mpcforces_extractor.datastructure.subcases import Subcase 

9from mpcforces_extractor.datastructure.rigids import MPC 

10from mpcforces_extractor.test_ressources.simple_model import ( 

11 get_simple_model_fem, 

12 get_simple_model_mpc, 

13) 

14 

15 

16class TestFMPCForceExtractor(unittest.TestCase): 

17 def test_init(self): 

18 """ 

19 Test the init method. Make sure all variables are set correctly (correct type) 

20 """ 

21 

22 # Test the init method 

23 force_extractor = MPCForceExtractor( 

24 fem_file_path="test.fem", 

25 mpc_file_path="test.mpc", 

26 output_folder=None, 

27 ) 

28 self.assertEqual(force_extractor.fem_file_path, "test.fem") 

29 self.assertEqual(force_extractor.mpc_file_path, "test.mpc") 

30 self.assertEqual(force_extractor.output_folder, None) 

31 

32 @patch( 

33 "mpcforces_extractor.reader.modelreaders.FemFileReader._FemFileReader__read_lines" 

34 ) 

35 @patch( 

36 "mpcforces_extractor.reader.mpcforces_reader.MPCForcesReader._MPCForcesReader__read_lines" 

37 ) 

38 def test_extract_forces_and_summary(self, mock_read_lines_mpc, mock_read_lines_fem): 

39 """ 

40 Test the extract_forces method. Make sure the forces are extracted correctly 

41 """ 

42 

43 mock_read_lines_fem.return_value = get_simple_model_fem() 

44 mock_read_lines_mpc.return_value = get_simple_model_mpc() 

45 

46 # Test the extract_forces method 

47 force_extractor = MPCForceExtractor( 

48 fem_file_path="test.fem", 

49 mpc_file_path="test.mpc", 

50 output_folder=None, 

51 ) 

52 force_extractor.build_fem_and_subcase_data(8) 

53 

54 force_1 = [0.00, 0.00, -1.00, -0.91, 0.00, 0.00] 

55 force_2 = [0.00, 0.00, 1.00, 1.32, 5.84, 1.94] 

56 

57 for subcase in Subcase.subcases: 

58 part_id2forces = list(MPC.id_2_instance.values())[0].get_part_id2force( 

59 subcase 

60 ) 

61 force_calc_1 = part_id2forces[1] 

62 force_calc_2 = part_id2forces[2] 

63 

64 diff_1 = sum([abs(a_i - b_i) for a_i, b_i in zip(force_1, force_calc_1)]) 

65 diff_2 = sum([abs(a_i - b_i) for a_i, b_i in zip(force_2, force_calc_2)]) 

66 print(diff_1, diff_2) 

67 self.assertTrue(diff_1 < 0.01 or diff_2 < 0.01) 

68 

69 summary_writer = SummaryWriter(force_extractor, force_extractor.output_folder) 

70 summary_writer.add_header() 

71 summary_writer.add_mpc_lines() 

72 

73 lines = [line.strip() for line in summary_writer.lines] 

74 for line in lines: 

75 print(line) 

76 

77 self.assertTrue("FZ: -1.000" in lines) 

78 self.assertTrue("MX: -0.907" in lines) 

79 self.assertTrue("FZ: 1.000" in lines) 

80 self.assertTrue("MX: 1.319" in lines) 

81 

82 @patch( 

83 "mpcforces_extractor.visualization.tcl_visualize.open", new_callable=mock_open 

84 ) 

85 @patch( 

86 "mpcforces_extractor.reader.modelreaders.FemFileReader._FemFileReader__read_lines" 

87 ) 

88 def test_visualize_tcl_commands(self, mock_read_lines_fem, mock_write): 

89 

90 mock_read_lines_fem.return_value = get_simple_model_fem() 

91 

92 reader = FemFileReader(None, 8) 

93 reader.create_entities() 

94 

95 # Visualize 

96 part_id2connected_node_ids = Element.get_part_id2node_ids_graph() 

97 self.assertTrue(part_id2connected_node_ids is not None) 

98 self.assertTrue(len(part_id2connected_node_ids) == 2) 

99 

100 visualizer = VisualizerConnectedParts(None) 

101 visualizer.output_tcl_lines_for_part_vis() 

102 

103 commands = visualizer.commands 

104 self.assertTrue(len(commands) == 6) 

105 

106 commands_expected = [] 

107 commands_expected.append("*createentity comps name=part1") 

108 commands_expected.append("*createmark elements 1 1 2 3 4") 

109 commands_expected.append('*movemark elements 1 "part1"') 

110 commands_expected.append("*createentity comps name=part2") 

111 commands_expected.append("*createmark elements 1 5 6 7 8") 

112 commands_expected.append('*movemark elements 1 "part2"') 

113 

114 for _, command in enumerate(commands): 

115 self.assertTrue(command in commands_expected) 

116 

117 

118if __name__ == "__main__": 

119 unittest.main()