cable.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. """
  2. This module can be used to solve problems related
  3. to 2D Cables.
  4. """
  5. from sympy.core.sympify import sympify
  6. from sympy.core.symbol import Symbol,symbols
  7. from sympy import sin, cos, pi, atan, diff, Piecewise, solve, rad
  8. from sympy.functions.elementary.miscellaneous import sqrt
  9. from sympy.solvers.solveset import linsolve
  10. from sympy.matrices import Matrix
  11. from sympy.plotting import plot
  12. class Cable:
  13. """
  14. Cables are structures in engineering that support
  15. the applied transverse loads through the tensile
  16. resistance developed in its members.
  17. Cables are widely used in suspension bridges, tension
  18. leg offshore platforms, transmission lines, and find
  19. use in several other engineering applications.
  20. Examples
  21. ========
  22. A cable is supported at (0, 10) and (10, 10). Two point loads
  23. acting vertically downwards act on the cable, one with magnitude 3 kN
  24. and acting 2 meters from the left support and 3 meters below it, while
  25. the other with magnitude 2 kN is 6 meters from the left support and
  26. 6 meters below it.
  27. >>> from sympy.physics.continuum_mechanics.cable import Cable
  28. >>> c = Cable(('A', 0, 10), ('B', 10, 10))
  29. >>> c.apply_load(-1, ('P', 2, 7, 3, 270))
  30. >>> c.apply_load(-1, ('Q', 6, 4, 2, 270))
  31. >>> c.loads
  32. {'distributed': {}, 'point_load': {'P': [3, 270], 'Q': [2, 270]}}
  33. >>> c.loads_position
  34. {'P': [2, 7], 'Q': [6, 4]}
  35. """
  36. def __init__(self, support_1, support_2):
  37. """
  38. Initializes the class.
  39. Parameters
  40. ==========
  41. support_1 and support_2 are tuples of the form
  42. (label, x, y), where
  43. label : String or symbol
  44. The label of the support
  45. x : Sympifyable
  46. The x coordinate of the position of the support
  47. y : Sympifyable
  48. The y coordinate of the position of the support
  49. """
  50. self._left_support = []
  51. self._right_support = []
  52. self._supports = {}
  53. self._support_labels = []
  54. self._loads = {"distributed": {}, "point_load": {}}
  55. self._loads_position = {}
  56. self._length = 0
  57. self._reaction_loads = {}
  58. self._tension = {}
  59. self._lowest_x_global = sympify(0)
  60. self._lowest_y_global = sympify(0)
  61. self._cable_eqn = None
  62. self._tension_func = None
  63. if support_1[0] == support_2[0]:
  64. raise ValueError("Supports can not have the same label")
  65. elif support_1[1] == support_2[1]:
  66. raise ValueError("Supports can not be at the same location")
  67. x1 = sympify(support_1[1])
  68. y1 = sympify(support_1[2])
  69. self._supports[support_1[0]] = [x1, y1]
  70. x2 = sympify(support_2[1])
  71. y2 = sympify(support_2[2])
  72. self._supports[support_2[0]] = [x2, y2]
  73. if support_1[1] < support_2[1]:
  74. self._left_support.append(x1)
  75. self._left_support.append(y1)
  76. self._right_support.append(x2)
  77. self._right_support.append(y2)
  78. self._support_labels.append(support_1[0])
  79. self._support_labels.append(support_2[0])
  80. else:
  81. self._left_support.append(x2)
  82. self._left_support.append(y2)
  83. self._right_support.append(x1)
  84. self._right_support.append(y1)
  85. self._support_labels.append(support_2[0])
  86. self._support_labels.append(support_1[0])
  87. for i in self._support_labels:
  88. self._reaction_loads[Symbol("R_"+ i +"_x")] = 0
  89. self._reaction_loads[Symbol("R_"+ i +"_y")] = 0
  90. @property
  91. def supports(self):
  92. """
  93. Returns the supports of the cable along with their
  94. positions.
  95. """
  96. return self._supports
  97. @property
  98. def left_support(self):
  99. """
  100. Returns the position of the left support.
  101. """
  102. return self._left_support
  103. @property
  104. def right_support(self):
  105. """
  106. Returns the position of the right support.
  107. """
  108. return self._right_support
  109. @property
  110. def loads(self):
  111. """
  112. Returns the magnitude and direction of the loads
  113. acting on the cable.
  114. """
  115. return self._loads
  116. @property
  117. def loads_position(self):
  118. """
  119. Returns the position of the point loads acting on the
  120. cable.
  121. """
  122. return self._loads_position
  123. @property
  124. def length(self):
  125. """
  126. Returns the length of the cable.
  127. """
  128. return self._length
  129. @property
  130. def reaction_loads(self):
  131. """
  132. Returns the reaction forces at the supports, which are
  133. initialized to 0.
  134. """
  135. return self._reaction_loads
  136. @property
  137. def tension(self):
  138. """
  139. Returns the tension developed in the cable due to the loads
  140. applied.
  141. """
  142. return self._tension
  143. def tension_at(self, x):
  144. """
  145. Returns the tension at a given value of x developed due to
  146. distributed load.
  147. """
  148. if 'distributed' not in self._tension.keys():
  149. raise ValueError("No distributed load added or solve method not called")
  150. if x > self._right_support[0] or x < self._left_support[0]:
  151. raise ValueError("The value of x should be between the two supports")
  152. A = self._tension['distributed']
  153. X = Symbol('X')
  154. return A.subs({X:(x-self._lowest_x_global)})
  155. def apply_length(self, length):
  156. """
  157. This method specifies the length of the cable
  158. Parameters
  159. ==========
  160. length : Sympifyable
  161. The length of the cable
  162. Examples
  163. ========
  164. >>> from sympy.physics.continuum_mechanics.cable import Cable
  165. >>> c = Cable(('A', 0, 10), ('B', 10, 10))
  166. >>> c.apply_length(20)
  167. >>> c.length
  168. 20
  169. """
  170. dist = ((self._left_support[0] - self._right_support[0])**2
  171. - (self._left_support[1] - self._right_support[1])**2)**(1/2)
  172. if length < dist:
  173. raise ValueError("length should not be less than the distance between the supports")
  174. self._length = length
  175. def change_support(self, label, new_support):
  176. """
  177. This method changes the mentioned support with a new support.
  178. Parameters
  179. ==========
  180. label: String or symbol
  181. The label of the support to be changed
  182. new_support: Tuple of the form (new_label, x, y)
  183. new_label: String or symbol
  184. The label of the new support
  185. x: Sympifyable
  186. The x-coordinate of the position of the new support.
  187. y: Sympifyable
  188. The y-coordinate of the position of the new support.
  189. Examples
  190. ========
  191. >>> from sympy.physics.continuum_mechanics.cable import Cable
  192. >>> c = Cable(('A', 0, 10), ('B', 10, 10))
  193. >>> c.supports
  194. {'A': [0, 10], 'B': [10, 10]}
  195. >>> c.change_support('B', ('C', 5, 6))
  196. >>> c.supports
  197. {'A': [0, 10], 'C': [5, 6]}
  198. """
  199. if label not in self._supports:
  200. raise ValueError("No support exists with the given label")
  201. i = self._support_labels.index(label)
  202. rem_label = self._support_labels[(i+1)%2]
  203. x1 = self._supports[rem_label][0]
  204. y1 = self._supports[rem_label][1]
  205. x = sympify(new_support[1])
  206. y = sympify(new_support[2])
  207. for l in self._loads_position:
  208. if l[0] >= max(x, x1) or l[0] <= min(x, x1):
  209. raise ValueError("The change in support will throw an existing load out of range")
  210. self._supports.pop(label)
  211. self._left_support.clear()
  212. self._right_support.clear()
  213. self._reaction_loads.clear()
  214. self._support_labels.remove(label)
  215. self._supports[new_support[0]] = [x, y]
  216. if x1 < x:
  217. self._left_support.append(x1)
  218. self._left_support.append(y1)
  219. self._right_support.append(x)
  220. self._right_support.append(y)
  221. self._support_labels.append(new_support[0])
  222. else:
  223. self._left_support.append(x)
  224. self._left_support.append(y)
  225. self._right_support.append(x1)
  226. self._right_support.append(y1)
  227. self._support_labels.insert(0, new_support[0])
  228. for i in self._support_labels:
  229. self._reaction_loads[Symbol("R_"+ i +"_x")] = 0
  230. self._reaction_loads[Symbol("R_"+ i +"_y")] = 0
  231. def apply_load(self, order, load):
  232. """
  233. This method adds load to the cable.
  234. Parameters
  235. ==========
  236. order : Integer
  237. The order of the applied load.
  238. - For point loads, order = -1
  239. - For distributed load, order = 0
  240. load : tuple
  241. * For point loads, load is of the form (label, x, y, magnitude, direction), where:
  242. label : String or symbol
  243. The label of the load
  244. x : Sympifyable
  245. The x coordinate of the position of the load
  246. y : Sympifyable
  247. The y coordinate of the position of the load
  248. magnitude : Sympifyable
  249. The magnitude of the load. It must always be positive
  250. direction : Sympifyable
  251. The angle, in degrees, that the load vector makes with the horizontal
  252. in the counter-clockwise direction. It takes the values 0 to 360,
  253. inclusive.
  254. * For uniformly distributed load, load is of the form (label, magnitude)
  255. label : String or symbol
  256. The label of the load
  257. magnitude : Sympifyable
  258. The magnitude of the load. It must always be positive
  259. Examples
  260. ========
  261. For a point load of magnitude 12 units inclined at 30 degrees with the horizontal:
  262. >>> from sympy.physics.continuum_mechanics.cable import Cable
  263. >>> c = Cable(('A', 0, 10), ('B', 10, 10))
  264. >>> c.apply_load(-1, ('Z', 5, 5, 12, 30))
  265. >>> c.loads
  266. {'distributed': {}, 'point_load': {'Z': [12, 30]}}
  267. >>> c.loads_position
  268. {'Z': [5, 5]}
  269. For a uniformly distributed load of magnitude 9 units:
  270. >>> from sympy.physics.continuum_mechanics.cable import Cable
  271. >>> c = Cable(('A', 0, 10), ('B', 10, 10))
  272. >>> c.apply_load(0, ('X', 9))
  273. >>> c.loads
  274. {'distributed': {'X': 9}, 'point_load': {}}
  275. """
  276. if order == -1:
  277. if len(self._loads["distributed"]) != 0:
  278. raise ValueError("Distributed load already exists")
  279. label = load[0]
  280. if label in self._loads["point_load"]:
  281. raise ValueError("Label already exists")
  282. x = sympify(load[1])
  283. y = sympify(load[2])
  284. if x > self._right_support[0] or x < self._left_support[0]:
  285. raise ValueError("The load should be positioned between the supports")
  286. magnitude = sympify(load[3])
  287. direction = sympify(load[4])
  288. self._loads["point_load"][label] = [magnitude, direction]
  289. self._loads_position[label] = [x, y]
  290. elif order == 0:
  291. if len(self._loads_position) != 0:
  292. raise ValueError("Point load(s) already exist")
  293. label = load[0]
  294. if label in self._loads["distributed"]:
  295. raise ValueError("Label already exists")
  296. magnitude = sympify(load[1])
  297. self._loads["distributed"][label] = magnitude
  298. else:
  299. raise ValueError("Order should be either -1 or 0")
  300. def remove_loads(self, *args):
  301. """
  302. This methods removes the specified loads.
  303. Parameters
  304. ==========
  305. This input takes multiple label(s) as input
  306. label(s): String or symbol
  307. The label(s) of the loads to be removed.
  308. Examples
  309. ========
  310. >>> from sympy.physics.continuum_mechanics.cable import Cable
  311. >>> c = Cable(('A', 0, 10), ('B', 10, 10))
  312. >>> c.apply_load(-1, ('Z', 5, 5, 12, 30))
  313. >>> c.loads
  314. {'distributed': {}, 'point_load': {'Z': [12, 30]}}
  315. >>> c.remove_loads('Z')
  316. >>> c.loads
  317. {'distributed': {}, 'point_load': {}}
  318. """
  319. for i in args:
  320. if len(self._loads_position) == 0:
  321. if i not in self._loads['distributed']:
  322. raise ValueError("Error removing load " + i + ": no such load exists")
  323. else:
  324. self._loads['disrtibuted'].pop(i)
  325. else:
  326. if i not in self._loads['point_load']:
  327. raise ValueError("Error removing load " + i + ": no such load exists")
  328. else:
  329. self._loads['point_load'].pop(i)
  330. self._loads_position.pop(i)
  331. def solve(self, *args):
  332. """
  333. This method solves for the reaction forces at the supports, the tension developed in
  334. the cable, and updates the length of the cable.
  335. Parameters
  336. ==========
  337. This method requires no input when solving for point loads
  338. For distributed load, the x and y coordinates of the lowest point of the cable are
  339. required as
  340. x: Sympifyable
  341. The x coordinate of the lowest point
  342. y: Sympifyable
  343. The y coordinate of the lowest point
  344. Examples
  345. ========
  346. For point loads,
  347. >>> from sympy.physics.continuum_mechanics.cable import Cable
  348. >>> c = Cable(("A", 0, 10), ("B", 10, 10))
  349. >>> c.apply_load(-1, ('Z', 2, 7.26, 3, 270))
  350. >>> c.apply_load(-1, ('X', 4, 6, 8, 270))
  351. >>> c.solve()
  352. >>> c.tension
  353. {A_Z: 8.91403453669861, X_B: 19*sqrt(13)/10, Z_X: 4.79150773600774}
  354. >>> c.reaction_loads
  355. {R_A_x: -5.25547445255474, R_A_y: 7.2, R_B_x: 5.25547445255474, R_B_y: 3.8}
  356. >>> c.length
  357. 5.7560958484519 + 2*sqrt(13)
  358. For distributed load,
  359. >>> from sympy.physics.continuum_mechanics.cable import Cable
  360. >>> c=Cable(("A", 0, 40),("B", 100, 20))
  361. >>> c.apply_load(0, ("X", 850))
  362. >>> c.solve(58.58)
  363. >>> c.tension
  364. {'distributed': 36465.0*sqrt(0.00054335718671383*X**2 + 1)}
  365. >>> c.tension_at(0)
  366. 61717.4130533677
  367. >>> c.reaction_loads
  368. {R_A_x: 36465.0, R_A_y: -49793.0, R_B_x: 44399.9537590861, R_B_y: 42868.2071025955}
  369. """
  370. if len(self._loads_position) != 0:
  371. sorted_position = sorted(self._loads_position.items(), key = lambda item : item[1][0])
  372. sorted_position.append(self._support_labels[1])
  373. sorted_position.insert(0, self._support_labels[0])
  374. self._tension.clear()
  375. moment_sum_from_left_support = 0
  376. moment_sum_from_right_support = 0
  377. F_x = 0
  378. F_y = 0
  379. self._length = 0
  380. tension_func = []
  381. x = symbols('x')
  382. for i in range(1, len(sorted_position)-1):
  383. if i == 1:
  384. self._length+=sqrt((self._left_support[0] - self._loads_position[sorted_position[i][0]][0])**2 + (self._left_support[1] - self._loads_position[sorted_position[i][0]][1])**2)
  385. else:
  386. self._length+=sqrt((self._loads_position[sorted_position[i-1][0]][0] - self._loads_position[sorted_position[i][0]][0])**2 + (self._loads_position[sorted_position[i-1][0]][1] - self._loads_position[sorted_position[i][0]][1])**2)
  387. if i == len(sorted_position)-2:
  388. self._length+=sqrt((self._right_support[0] - self._loads_position[sorted_position[i][0]][0])**2 + (self._right_support[1] - self._loads_position[sorted_position[i][0]][1])**2)
  389. moment_sum_from_left_support += self._loads['point_load'][sorted_position[i][0]][0] * cos(pi * self._loads['point_load'][sorted_position[i][0]][1] / 180) * abs(self._left_support[1] - self._loads_position[sorted_position[i][0]][1])
  390. moment_sum_from_left_support += self._loads['point_load'][sorted_position[i][0]][0] * sin(pi * self._loads['point_load'][sorted_position[i][0]][1] / 180) * abs(self._left_support[0] - self._loads_position[sorted_position[i][0]][0])
  391. F_x += self._loads['point_load'][sorted_position[i][0]][0] * cos(pi * self._loads['point_load'][sorted_position[i][0]][1] / 180)
  392. F_y += self._loads['point_load'][sorted_position[i][0]][0] * sin(pi * self._loads['point_load'][sorted_position[i][0]][1] / 180)
  393. label = Symbol(sorted_position[i][0]+"_"+sorted_position[i+1][0])
  394. y2 = self._loads_position[sorted_position[i][0]][1]
  395. x2 = self._loads_position[sorted_position[i][0]][0]
  396. y1 = 0
  397. x1 = 0
  398. if i == len(sorted_position)-2:
  399. x1 = self._right_support[0]
  400. y1 = self._right_support[1]
  401. else:
  402. x1 = self._loads_position[sorted_position[i+1][0]][0]
  403. y1 = self._loads_position[sorted_position[i+1][0]][1]
  404. angle_with_horizontal = atan((y1 - y2)/(x1 - x2))
  405. tension = -(moment_sum_from_left_support)/(abs(self._left_support[1] - self._loads_position[sorted_position[i][0]][1])*cos(angle_with_horizontal) + abs(self._left_support[0] - self._loads_position[sorted_position[i][0]][0])*sin(angle_with_horizontal))
  406. self._tension[label] = tension
  407. tension_func.append((tension, x<=x1))
  408. moment_sum_from_right_support += self._loads['point_load'][sorted_position[i][0]][0] * cos(pi * self._loads['point_load'][sorted_position[i][0]][1] / 180) * abs(self._right_support[1] - self._loads_position[sorted_position[i][0]][1])
  409. moment_sum_from_right_support += self._loads['point_load'][sorted_position[i][0]][0] * sin(pi * self._loads['point_load'][sorted_position[i][0]][1] / 180) * abs(self._right_support[0] - self._loads_position[sorted_position[i][0]][0])
  410. label = Symbol(sorted_position[0][0]+"_"+sorted_position[1][0])
  411. y2 = self._loads_position[sorted_position[1][0]][1]
  412. x2 = self._loads_position[sorted_position[1][0]][0]
  413. x1 = self._left_support[0]
  414. y1 = self._left_support[1]
  415. angle_with_horizontal = -atan((y2 - y1)/(x2 - x1))
  416. tension = -(moment_sum_from_right_support)/(abs(self._right_support[1] - self._loads_position[sorted_position[1][0]][1])*cos(angle_with_horizontal) + abs(self._right_support[0] - self._loads_position[sorted_position[1][0]][0])*sin(angle_with_horizontal))
  417. self._tension[label] = tension
  418. tension_func.insert(0,(tension, x<=x2))
  419. self._tension_func = Piecewise(*tension_func)
  420. angle_with_horizontal = pi/2 - angle_with_horizontal
  421. label = self._support_labels[0]
  422. self._reaction_loads[Symbol("R_"+label+"_x")] = -sin(angle_with_horizontal) * tension
  423. F_x += -sin(angle_with_horizontal) * tension
  424. self._reaction_loads[Symbol("R_"+label+"_y")] = cos(angle_with_horizontal) * tension
  425. F_y += cos(angle_with_horizontal) * tension
  426. label = self._support_labels[1]
  427. self._reaction_loads[Symbol("R_"+label+"_x")] = -F_x
  428. self._reaction_loads[Symbol("R_"+label+"_y")] = -F_y
  429. elif len(self._loads['distributed']) != 0 :
  430. if len(args) == 0:
  431. raise ValueError("Provide the lowest point of the cable")
  432. lowest_x = sympify(args[0])
  433. self._lowest_x_global = lowest_x
  434. a = Symbol('a', positive=True)
  435. c = Symbol('c')
  436. # augmented matrix form of linsolve
  437. M = Matrix(
  438. [[(self._left_support[0]-lowest_x)**2, 1, self._left_support[1]],
  439. [(self._right_support[0]-lowest_x)**2, 1, self._right_support[1]],
  440. ])
  441. coefficient_solution = list(linsolve(M, (a, c)))
  442. if len(coefficient_solution) ==0 or coefficient_solution[0][0]== 0:
  443. raise ValueError("The lowest point is inconsistent with the supports")
  444. A = coefficient_solution[0][0]
  445. C = coefficient_solution[0][1] + coefficient_solution[0][0]*lowest_x**2
  446. B = -2*coefficient_solution[0][0]*lowest_x
  447. self._lowest_y_global = coefficient_solution[0][1]
  448. lowest_y = self._lowest_y_global
  449. # y = A*x**2 + B*x + C
  450. # shifting origin to lowest point
  451. X = Symbol('X')
  452. Y = Symbol('Y')
  453. Y = A*(X + lowest_x)**2 + B*(X + lowest_x) + C - lowest_y
  454. temp_list = list(self._loads['distributed'].values())
  455. applied_force = temp_list[0]
  456. horizontal_force_constant = (applied_force * (self._right_support[0] - lowest_x)**2) / (2 * (self._right_support[1] - lowest_y))
  457. self._tension.clear()
  458. tangent_slope_to_curve = diff(Y, X)
  459. self._tension['distributed'] = horizontal_force_constant / (cos(atan(tangent_slope_to_curve)))
  460. label = self._support_labels[0]
  461. self._reaction_loads[Symbol("R_"+label+"_x")] = self.tension_at(self._left_support[0]) * cos(atan(tangent_slope_to_curve.subs(X, self._left_support[0] - lowest_x)))
  462. self._reaction_loads[Symbol("R_"+label+"_y")] = self.tension_at(self._left_support[0]) * sin(atan(tangent_slope_to_curve.subs(X, self._left_support[0] - lowest_x)))
  463. label = self._support_labels[1]
  464. self._reaction_loads[Symbol("R_"+label+"_x")] = self.tension_at(self._left_support[0]) * cos(atan(tangent_slope_to_curve.subs(X, self._right_support[0] - lowest_x)))
  465. self._reaction_loads[Symbol("R_"+label+"_y")] = self.tension_at(self._left_support[0]) * sin(atan(tangent_slope_to_curve.subs(X, self._right_support[0] - lowest_x)))
  466. def draw(self):
  467. """
  468. This method is used to obtain a plot for the specified cable with its supports,
  469. shape and loads.
  470. Examples
  471. ========
  472. For point loads,
  473. >>> from sympy.physics.continuum_mechanics.cable import Cable
  474. >>> c = Cable(("A", 0, 10), ("B", 10, 10))
  475. >>> c.apply_load(-1, ('Z', 2, 7.26, 3, 270))
  476. >>> c.apply_load(-1, ('X', 4, 6, 8, 270))
  477. >>> c.solve()
  478. >>> p = c.draw()
  479. >>> p # doctest: +ELLIPSIS
  480. Plot object containing:
  481. [0]: cartesian line: Piecewise((10 - 1.37*x, x <= 2), (8.52 - 0.63*x, x <= 4), (2*x/3 + 10/3, x <= 10)) for x over (0.0, 10.0)
  482. ...
  483. >>> p.show()
  484. For uniformly distributed loads,
  485. >>> from sympy.physics.continuum_mechanics.cable import Cable
  486. >>> c=Cable(("A", 0, 40),("B", 100, 20))
  487. >>> c.apply_load(0, ("X", 850))
  488. >>> c.solve(58.58)
  489. >>> p = c.draw()
  490. >>> p # doctest: +ELLIPSIS
  491. Plot object containing:
  492. [0]: cartesian line: 0.0116550116550117*(x - 58.58)**2 + 0.00447086247086247 for x over (0.0, 100.0)
  493. [1]: cartesian line: -7.49552913752915 for x over (0.0, 100.0)
  494. ...
  495. >>> p.show()
  496. """
  497. x = Symbol("x")
  498. annotations = []
  499. support_rectangles = self._draw_supports()
  500. xy_min = min(self._left_support[0],self._lowest_y_global)
  501. xy_max = max(self._right_support[0], max(self._right_support[1],self._left_support[1]))
  502. max_diff = xy_max - xy_min
  503. if len(self._loads_position) != 0:
  504. self._cable_eqn = self._draw_cable(-1)
  505. annotations += self._draw_loads(-1)
  506. elif len(self._loads['distributed']) != 0 :
  507. self._cable_eqn = self._draw_cable(0)
  508. annotations += self._draw_loads(0)
  509. if not self._cable_eqn:
  510. raise ValueError("solve method not called and/or values provided for loads and supports not adequate")
  511. cab_plot = plot(*self._cable_eqn,(x,self._left_support[0],self._right_support[0]),
  512. xlim=(xy_min-0.5*max_diff,xy_max+0.5*max_diff),
  513. ylim=(xy_min-0.5*max_diff,xy_max+0.5*max_diff),
  514. rectangles=support_rectangles,show= False,annotations=annotations, axis=False)
  515. return cab_plot
  516. def _draw_supports(self):
  517. member_rectangles = []
  518. xy_min = min(self._left_support[0],self._lowest_y_global)
  519. xy_max = max(self._right_support[0], max(self._right_support[1],self._left_support[1]))
  520. max_diff = xy_max - xy_min
  521. supp_width = 0.075*max_diff
  522. member_rectangles.append(
  523. {
  524. 'xy': (self._left_support[0]-supp_width,self._left_support[1]),
  525. 'width': supp_width,
  526. 'height':supp_width,
  527. 'color':'brown',
  528. 'fill': False
  529. }
  530. )
  531. member_rectangles.append(
  532. {
  533. 'xy': (self._right_support[0],self._right_support[1]),
  534. 'width': supp_width,
  535. 'height':supp_width,
  536. 'color':'brown',
  537. 'fill': False
  538. }
  539. )
  540. return member_rectangles
  541. def _draw_cable(self,order):
  542. xy_min = min(self._left_support[0],self._lowest_y_global)
  543. xy_max = max(self._right_support[0], max(self._right_support[1],self._left_support[1]))
  544. max_diff = xy_max - xy_min
  545. if order == -1 :
  546. x,y = symbols('x y')
  547. line_func = []
  548. sorted_position = sorted(self._loads_position.items(), key = lambda item : item[1][0])
  549. for i in range(len(sorted_position)):
  550. if(i==0):
  551. y = ((sorted_position[i][1][1] - self._left_support[1])*(x-self._left_support[0]))/(sorted_position[i][1][0]- self._left_support[0]) + self._left_support[1]
  552. else:
  553. y = ((sorted_position[i][1][1] - sorted_position[i-1][1][1] )*(x-sorted_position[i-1][1][0]))/(sorted_position[i][1][0]- sorted_position[i-1][1][0]) + sorted_position[i-1][1][1]
  554. line_func.append((y,x<=sorted_position[i][1][0]))
  555. y = ((sorted_position[len(sorted_position)-1][1][1] - self._right_support[1])*(x-self._right_support[0]))/(sorted_position[i][1][0]- self._right_support[0]) + self._right_support[1]
  556. line_func.append((y,x<=self._right_support[0]))
  557. return [Piecewise(*line_func)]
  558. elif order == 0:
  559. x0 = self._lowest_x_global
  560. diff_force_height = max_diff*0.075
  561. a,c,x,y = symbols('a c x y')
  562. parabola_eqn = a*(x-x0)**2 + c - y
  563. points = [(self._left_support[0],self._left_support[1]),(self._right_support[0],self._right_support[1])]
  564. equations = []
  565. for px, py in points:
  566. equations.append(parabola_eqn.subs({x: px, y: py}))
  567. solution = solve(equations, (a, c))
  568. parabola_eqn = solution[a]*(x-x0)**2 + solution[c]
  569. return [parabola_eqn, self._lowest_y_global - diff_force_height]
  570. def _draw_loads(self,order):
  571. xy_min = min(self._left_support[0],self._lowest_y_global)
  572. xy_max = max(self._right_support[0], max(self._right_support[1],self._left_support[1]))
  573. max_diff = xy_max - xy_min
  574. if(order==-1):
  575. arrow_length = max_diff*0.1
  576. force_arrows = []
  577. for key in self._loads['point_load']:
  578. force_arrows.append(
  579. {
  580. 'text': '',
  581. 'xy':(self._loads_position[key][0]+arrow_length*cos(rad(self._loads['point_load'][key][1])),\
  582. self._loads_position[key][1] + arrow_length*sin(rad(self._loads['point_load'][key][1]))),
  583. 'xytext': (self._loads_position[key][0],self._loads_position[key][1]),
  584. 'arrowprops': {'width': 1, 'headlength':3, 'headwidth':3 , 'facecolor': 'black', }
  585. }
  586. )
  587. mag = self._loads['point_load'][key][0]
  588. force_arrows.append(
  589. {
  590. 'text':f'{mag}N',
  591. 'xy': (self._loads_position[key][0]+arrow_length*1.6*cos(rad(self._loads['point_load'][key][1])),\
  592. self._loads_position[key][1] + arrow_length*1.6*sin(rad(self._loads['point_load'][key][1]))),
  593. }
  594. )
  595. return force_arrows
  596. elif (order == 0):
  597. x = symbols('x')
  598. force_arrows = []
  599. x_val = [self._left_support[0] + ((self._right_support[0]-self._left_support[0])/10)*i for i in range(1,10)]
  600. for i in x_val:
  601. force_arrows.append(
  602. {
  603. 'text':'',
  604. 'xytext':(
  605. i,
  606. self._cable_eqn[0].subs(x,i)
  607. ),
  608. 'xy':(
  609. i,
  610. self._cable_eqn[1].subs(x,i)
  611. ),
  612. 'arrowprops':{'width':1, 'headlength':3.5, 'headwidth':3.5, 'facecolor':'black'}
  613. }
  614. )
  615. mag = 0
  616. for key in self._loads['distributed']:
  617. mag += self._loads['distributed'][key]
  618. force_arrows.append(
  619. {
  620. 'text':f'{mag} N/m',
  621. 'xy':((self._left_support[0]+self._right_support[0])/2,self._lowest_y_global - max_diff*0.15)
  622. }
  623. )
  624. return force_arrows
  625. def plot_tension(self):
  626. """
  627. Returns the diagram/plot of the tension generated in the cable at various points.
  628. Examples
  629. ========
  630. For point loads,
  631. >>> from sympy.physics.continuum_mechanics.cable import Cable
  632. >>> c = Cable(("A", 0, 10), ("B", 10, 10))
  633. >>> c.apply_load(-1, ('Z', 2, 7.26, 3, 270))
  634. >>> c.apply_load(-1, ('X', 4, 6, 8, 270))
  635. >>> c.solve()
  636. >>> p = c.plot_tension()
  637. >>> p
  638. Plot object containing:
  639. [0]: cartesian line: Piecewise((8.91403453669861, x <= 2), (4.79150773600774, x <= 4), (19*sqrt(13)/10, x <= 10)) for x over (0.0, 10.0)
  640. >>> p.show()
  641. For uniformly distributed loads,
  642. >>> from sympy.physics.continuum_mechanics.cable import Cable
  643. >>> c=Cable(("A", 0, 40),("B", 100, 20))
  644. >>> c.apply_load(0, ("X", 850))
  645. >>> c.solve(58.58)
  646. >>> p = c.plot_tension()
  647. >>> p
  648. Plot object containing:
  649. [0]: cartesian line: 36465.0*sqrt(0.00054335718671383*X**2 + 1) for X over (0.0, 100.0)
  650. >>> p.show()
  651. """
  652. if len(self._loads_position) != 0:
  653. x = symbols('x')
  654. tension_plot = plot(self._tension_func, (x,self._left_support[0],self._right_support[0]), show=False)
  655. else:
  656. X = symbols('X')
  657. tension_plot = plot(self._tension['distributed'], (X,self._left_support[0],self._right_support[0]), show=False)
  658. return tension_plot