Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bondgraph.mp 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. %% bondgraph.mp
  2. %% Copyright 2007 Henrik Tidefelt
  3. %
  4. % This work may be distributed and/or modified under the
  5. % conditions of the LaTeX Project Public License, either version 1.3
  6. % of this license or (at your option) any later version.
  7. % The latest version of this license is in
  8. % http://www.latex-project.org/lppl.txt
  9. % and version 1.3 or later is part of all distributions of LaTeX
  10. % version 2005/12/01 or later.
  11. %
  12. % This work has the LPPL maintenance status `maintained´.
  13. %
  14. % The Current Maintainer of this work is Henrik Tidefelt,
  15. % tidefelt@isy.liu.se.
  16. %
  17. % This work, referred to as blockdraw.mp, consists of the files
  18. % shiftoff.mp
  19. % blockdraw.mp
  20. % bondgraph.mp
  21. % blockdraw_mp.pdf
  22. %
  23. %
  24. % === Notes regarding the backward incompatible changes of 2007-01-21. ===
  25. %
  26. % As it was discovered that MetaPost uses the verbatimtex construct only in
  27. % the file it appears in, it was concluded that package files cannot contain
  28. % any content that depends on the LaTeX preamble. In other words, any
  29. % btex ... etex constructs must be moved from package files to the application
  30. % sources from where the package files are included.
  31. %
  32. % With the definition of, say, p-junction blocks in the application source,
  33. % it is easy to change notation from the letters "p" and "s" to the numbers
  34. % "0" and "1".
  35. %
  36. % The obvious drawback, beside backward incompatibility, is that this will
  37. % cause a lot of code replication, making bond graph source files
  38. % less concise, and require multiple file search-and-replace actions when
  39. % changing junction notation for a collection of graphs. However, since
  40. % MetaPost only allows literal character strings in the btex ... etex
  41. % construct, these inconveniences seem impossible to work around. Ideas,
  42. % anyone?
  43. input blockdraw;
  44. implicitdraw := false;
  45. boolean junctionimplicitdraw;
  46. junctionimplicitdraw := true;
  47. boolean useopenbonds;
  48. useopenbonds := false;
  49. connectionlw := 0.5pt;
  50. ahlength := 3mm;
  51. bboxmargin := 1.5mm;
  52. def withbbmargin(expr pic ) =
  53. begingroup
  54. save tmppic;
  55. picture tmppic;
  56. tmppic = pic;
  57. setbounds tmppic to bbox pic;
  58. tmppic
  59. endgroup
  60. enddef;
  61. def junction(expr txt, z ) =
  62. begingroup
  63. save tmppic;
  64. picture tmppic;
  65. tmppic := nullpicture;
  66. addto tmppic also shiftoff( txt scaled textscale, to_center );
  67. setbounds tmppic to (-smallblockr,-smallblockr)--(-smallblockr,smallblockr)--(smallblockr,smallblockr)--(smallblockr,-smallblockr)--cycle;
  68. tmppic := tmppic shifted z;
  69. if junctionimplicitdraw:
  70. draw tmppic;
  71. fi
  72. tmppic
  73. endgroup
  74. enddef;
  75. def junctionlbl(expr txt, arrowdir, lbl, z, arrow ) =
  76. begingroup
  77. save tmppic, ahlength, p, d;
  78. ahlength := 1mm;
  79. path p;
  80. numeric d;
  81. picture tmppic;
  82. tmppic := nullpicture;
  83. addto tmppic also shiftoff( txt scaled textscale, to_center );
  84. if arrowdir = to_top:
  85. p := ((lrcorner tmppic)--(urcorner tmppic)) shifted ( z + (connectionlw + 2*ahlength*sind(0.5*ahangle),0) );
  86. d = to_rt;
  87. elseif arrowdir = to_lft:
  88. p := ((urcorner tmppic)--(ulcorner tmppic)) shifted ( z + (0,connectionlw + 2*ahlength*sind(0.5*ahangle)) );
  89. d = to_top;
  90. elseif arrowdir = to_bot:
  91. p := ((urcorner tmppic)--(lrcorner tmppic)) shifted ( z + (connectionlw + 2*ahlength*sind(0.5*ahangle),0) );
  92. d = to_rt;
  93. elseif arrowdir = to_rt:
  94. p := ((ulcorner tmppic)--(urcorner tmppic)) shifted ( z + (0,connectionlw + 2*ahlength*sind(0.5*ahangle)) );
  95. d = to_top;
  96. fi
  97. setbounds tmppic to (-smallblockr,-smallblockr)--(-smallblockr,smallblockr)--(smallblockr,smallblockr)--(smallblockr,-smallblockr)--cycle;
  98. tmppic := tmppic shifted z;
  99. if junctionimplicitdraw:
  100. if arrow:
  101. drawarrow p withpen pencircle scaled connectionlw;
  102. fi
  103. draw conlabel( d, lbl, mspoint( p, 0.5, 0 ) );
  104. draw tmppic;
  105. fi
  106. tmppic
  107. endgroup
  108. enddef;
  109. def terminal(expr d, txt, z ) =
  110. begingroup
  111. save tmppic;
  112. picture tmppic;
  113. tmppic := shiftoff( withbbmargin( txt scaled textscale ), d ) shifted ( z - smallblockr * dir_to( d ) );
  114. if junctionimplicitdraw:
  115. draw tmppic;
  116. fi
  117. tmppic
  118. endgroup
  119. enddef;
  120. def ignorepicture(expr pic ) =
  121. show 0
  122. enddef;
  123. def terminalto(expr j, txt, z ) =
  124. begingroup
  125. ignorepicture( terminal( to_dir( z - (center j) ), txt, z ) );
  126. bgconnect( pointpicture( z ) , j )
  127. endgroup
  128. enddef;
  129. def terminalfr(expr j, txt, z ) =
  130. begingroup
  131. ignorepicture( terminal( to_dir( z - (center j) ), txt, z ) );
  132. bgconnect( j, pointpicture( z ) )
  133. endgroup
  134. enddef;
  135. def bgconnect(expr pica, picb ) =
  136. begingroup
  137. save d;
  138. pair d;
  139. if (center pica) = (center picb):
  140. d := (1,0);
  141. else:
  142. d := unitvector( (center picb) - (center pica) );
  143. fi
  144. ((center pica)+d*smallblockr)--((center picb)-d*smallblockr)
  145. endgroup
  146. enddef;
  147. def bond(expr p ) =
  148. begingroup
  149. save t;
  150. numeric t;
  151. t := arctime arclength(p) - ahlength of p;
  152. save z;
  153. pair z;
  154. z = (point t of p) + dir(angle(direction t of p)+90) * ahlength*sind(0.5*ahangle);
  155. save res;
  156. path res;
  157. if useopenbonds:
  158. draw p--z withpen pencircle scaled connectionlw
  159. else:
  160. draw p withpen pencircle scaled connectionlw;
  161. res = (subpath (t,infinity) of p)--z--cycle;
  162. filldraw res withpen pencircle scaled connectionlw
  163. fi
  164. endgroup
  165. enddef;
  166. def causalmark(expr p, where ) =
  167. begingroup
  168. save d;
  169. numeric d;
  170. d = angle( direction where of p );
  171. if where = 0:
  172. d := d + 180;
  173. fi
  174. save z;
  175. pair z;
  176. z = ( point where of p ) + dir(d) * connectionlw;
  177. save res;
  178. path res;
  179. res = (z+dir(d+90)*ahlength*sind(0.5*ahangle))--(z+dir(d-90)*ahlength*sind(0.5*ahangle));
  180. draw res withpen pencircle scaled (2*connectionlw)
  181. endgroup
  182. enddef;
  183. def hbond(expr p ) =
  184. begingroup
  185. bond( p );
  186. causalmark( p, infinity )
  187. endgroup
  188. enddef;
  189. def tbond(expr p ) =
  190. begingroup
  191. bond( p );
  192. causalmark( p, 0 )
  193. endgroup
  194. enddef;
  195. def flowlabel(expr p, txt ) =
  196. % the midpoint has typically singular direction, so we can't do the obvious:
  197. % draw( conlabel( to_dir( dir( angle( direction (0.5*length(p)) of p ) - 90 ) ), txt, mspoint( p, 0.5, 0 ) ) )
  198. draw conlabel( to_dir( dir( angle( (point infinity of p) - (point 0 of p) ) - 90 ) ), txt, mspoint( p, 0.5, 0 ) ) withpen pencircle scaled connectionlw
  199. enddef;
  200. def effortlabel(expr p, txt ) =
  201. % the midpoint has typically singular direction, so we can't do the obvious:
  202. % draw( conlabel( to_dir( dir( angle( direction (0.5*length(p)) of p ) + 90 ) ), txt, mspoint( p, 0.5, 0 ) ) )
  203. draw conlabel( to_dir( dir( angle( (point infinity of p) - (point 0 of p) ) + 90 ) ), txt, mspoint( p, 0.5, 0 ) ) withpen pencircle scaled connectionlw
  204. enddef;