Generating High-Quality Portable PDF Files
Total Page:16
File Type:pdf, Size:1020Kb
From: http:// ... TKP4106 (Automatic HTML etc. to PDF Conversion) Creator: Tore Haug-Warberg Department of Chemical Engineering NTNU (Norway) Created: Thu Jan 22 15:05:03 +0100 2015 PDF name: 2015 01 22 15 05 03.pdf Contents 1 Homepage 4 2 Tore Haug-Warberg (Programming)6 2.1 Real Programmers use FORTRAN . 10 3 Heinz A. Preisig (Modelling) 18 4 Frequently Asked Questions (FAQ) 20 5 Syllabus 23 5.1 Introduction to Python . 24 5.1.1 Seven Topics in Python . 28 5.1.2 Verbatim: \script" . 56 5.1.3 Emacs quick reference . 57 5.1.4 Vim quick reference . 59 5.1.5 TextPad quick reference . 61 5.1.6 LaTeX (Cambridge University) . 63 5.1.7 High-quality portable PDF (Schatz) . 69 5.1.8 Regex (Stephen Ramsay) . 71 5.1.9 Regex quick reference . 74 5.1.10 BNF and EBNF (L. M. Garshol) . 75 1 5.1.11 Windows shortcuts (OIT) . 86 5.1.12 Linux programming (digilife) . 87 5.1.13 Mac shortcuts (macmost) . 89 5.1.14 Commenting Python code (MIT) . 90 5.1.15 Programming paradigms (Kurt Normark) . 92 5.1.16 Real Programmers (Ed Post), see also Sec. 2.1................. 98 5.1.17 Seven Topics in Python, see also Sec. 5.1.1................... 99 5.1.18 Seven Topics in Python (Haug-Warberg), see also Sec. 5.1.1.......... 100 5.1.19 Epydoc (sourceforge) . 101 5.1.20 Epytext markup (sourceforge) . 102 5.1.21 Python Docstrings (Sourceforge) . 112 5.1.22 Scientific Python (numpy.org) . 114 5.2 Exercise 1 . 116 5.3 Regular expressions . 123 5.3.1 A Smalltalk about Modelling . 127 5.3.2 Regular Expressions, see also Sec. 5.1.8..................... 132 5.4 Exercise 2 . 133 5.5 Documenting your code . 139 5.5.1 The real programmer, see also Sec. 2.1...................... 143 5.5.2 epydoc, see also Sec. 5.1.19............................ 144 5.5.3 Verbatim: \atoms.py" . 145 5.5.4 epytext, see also Sec. 5.1.20............................ 147 5.5.5 docstring, see also Sec. 5.1.21........................... 148 5.5.6 Epydoc output file . 149 5.6 Exercise 3 . 150 5.7 Molecular formula parser . 163 5.7.1 Verbatim: \atoms.py" . 166 5.7.2 Backus-Naur Formalism, see also Sec. 5.1.10................... 168 5.8 Exercise 4 . 169 5.9 The atom matrix . 181 5.9.1 Verbatim: \atom matrix.py" . 185 5.9.2 Verbatim: \molecular weight.py" . 186 5.10 Exercise 5 . 188 5.11 Independent reactions . 201 5.11.1 Verbatim: \rref.py" . 205 5.11.2 Verbatim: \null.py" . 207 5.11.3 The mass balance . 208 5.12 Exercise 6 . 214 5.13 Root solvers . 240 5.13.1 Verbatim: \sqrt.py" . 242 5.13.2 Verbatim: \pv.py" . 243 5.13.3 The energy balance . 245 5.14 Exercise 7 . 254 5.15 A thermodynamic equation solver . 260 5.15.1 Verbatim: \solve.py" . 261 5.15.2 Verbatim: \hpn.py" . 262 5.15.3 Verbatim: \mprod.py" . 265 5.15.4 The energy balance . 266 5.16 Exercise 8 . 275 5.17 The reactor model . 289 5.17.1 Verbatim: \srk ammonia.py" . 291 5.17.2 Verbatim: “flowsheet.py" . 294 2 5.17.3 Verbatim: \ammonia reactor.py" . 300 5.17.4 Verbatim: \tkp4106.py" . 303 5.17.5 ammonia reactor.py, see also Sec. 5.19.2..................... 304 5.17.6 srk ammonia.py, see also Sec. 5.17.1....................... 305 5.17.7 Modelling issues . 306 5.18 Exercise 9 . 319 5.19 Integration . 327 5.19.1 Verbatim: “flowsheet.py" . 329 5.19.2 Verbatim: \ammonia reactor.py" . 335 5.19.3 flowsheet.py, see also Sec. 5.19.1......................... 338 5.19.4 ammonia reactor.py, see also Sec. 5.19.2..................... 339 5.19.5 Modelling issues . 340 5.20 Exercise 10 . 353 5.21 Unit testing . 379 5.22 Exercise 11 . 380 5.23 Putting the model to work . 397 5.23.1 Verbatim: \graph.gp" . 399 5.23.2 Verbatim: \graph.dat" . 400 5.23.3 graph.pdf . 401 5.23.4 ammonia reactor.py, see also Sec. 5.19.2..................... 402 5.23.5 graph.gp, see also Sec. 5.23.1........................... 403 5.23.6 Modelling perspectives (Norwegian) . 404 5.24 Exercise 12 . 405 3 TKP4106 Process Modelling This is the joint homepage for TKP4106 Process Modelling. The course is composed of two parallel sessions - Modelling theory (HAP) and Programming (THW) - each of them having a dedicated webpage. The syllabus and the FAQ list are in common. ALL THE COURSE MATERIAL NEEDED TO COMPLETE THE COURSE IS ON THE WEB. THAT INCLUDES WHITE PAPERS, SOURCE CODE AND EXTERNAL REFERENCES. WHICH MEANS YOU'LL ACTUALLY HAVE TO BE READ THE PAGES TOP DOWN - NOT SIMPLY BROWSE THEM. We also expect you to visit all the external links in order to get an overview of the entire course. Lecturer's home page: 1. Tore Haug-Warberg (Programming) 2. Heinz A. Preisig (Modelling) Common parts: 1. Python manual (very good) 2. Frequently Asked Questions (FAQ) 3. Syllabus Process modelling builds on the basic conservation principles, transport phenomena, thermodynamics and mathematical physics. We teach on how these models are being built systematically so that we have precisely the knowledge required - neither more nor less. Models we establish formulate many different mathematical problems that need to be solved simultaneously in order to get an over-all solution. We learn on how to approach, and to solve, these problems effectively using mathematical and computer-based numerical tools. Programming is seen as a core activity for achieving this latter goal. Examples taken from the different corners of our discipline are the subjects of our discussions. Learning outcome: 1. Get a birdsview of the modelling process. 2. Establish an integration of the different involved subjects. 3. Programming as part of solving technical problems. 4. Abstraction of the plant. 5. Formulation of complete process models. 6. Solving simple mathematical and numerical problems using computers. 7. Programming methods and a programming language. 8. Have a systematic approach to problem solving. 9. Know how to generate models. TKP4106 Programming Activities "The easiest way to tell a Real Programmer from the crowd is by the programming language he (or she) uses. Real Programmers use Fortran. Quiche Eaters use Pascal. Nicklaus Wirth, the designer of Pascal, gave a talk once at which he was asked, "How do you pronounce your name?". He replied, "You can either call me by name, pronouncing it 'Veert', or call me by value, 'Worth'." One can tell immediately by this comment that Nicklaus Wirth is a Quiche Eater. The only parameter passing mechanism endorsed by Real Programmers is call-by-value-return, as implemented in the IBM/370 Fortran G and H compilers. Real Programmers don't need all these abstract concepts to get their jobs done-- they are perfectly happy with a keypunch, a Fortran IV compiler, and a beer." Real Programmers use FORTRAN This page gives a brief introduction to the programming activities in Process Modelling TKP4106. For easy off-line browsing you can download the contents of the entire course as a 6.2 MB PDF-file here. There is also a FAQ list and a syllabus available. See also the other links at the top of the page. The Goals section below gives an overview of where we are heading. We will be using Python for the programming, but we shall try to stay away from external libraries and rather work out the software as needed. This brings in topics like: formula parsing, atom matrix and matrix product calculation, row-reduced-echelon-form, nullspace, linear and non-linear equation solving, Euler and Runge-Kutta integration, a thermodynamic equation of state and an object-oriented flowsheet module with stream and reactor objects. To increase the learning effect you are not given the programs out of the box. Instead you are asked to change these stub programs into workable code as a compulsory part of the course. The work flow of a typical modelling job has 5 dedicated tasks: - Algebra | Theory part of TKP4106 - Analysis | - " - ................................................ - Algorithm | Computer lab activity in TKP4106 - Programming | - " - - Simulation | - " - but most students find it difficult to work in a top-bottom style and have a tendency of doing things backwards. In particular there is a lack of understanding (and appreciation) for the necessity of doing the algebra and the analysis before one starts doing programming. The short-cut seldomly works and a lot of time and frustration is spent on a task that would otherwise be affordable. The Golden Rule is therefore: Always do your paper work before you attempt any serious programming! There are many ways to Rome, and.