{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# F - callbacks\n", "\n", "![lec-0F-ga](lec-0F-ga.png)\n", "\n", "* To try it on *python tutor live*, [click here][preloaded ptl F]\n", "\n", "[preloaded ptl F]: http://www.pythontutor.com/live.html#code=%23%20%3D%3D%3D%20Lecture%20zero,%20part%20F%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%23%20---%20Remember%20from%20part%20E%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Amain_R%20%3D%208.314%0Adef%20function_P_sat(dummy_T%29%3A%20%23%20Let's%20%20use%20this%20approximate%20correlation%20for%20water%20saturation%20pressure%0A%09return%201e5%20*%20(%2010**(%205.40221%20-%20(%201838.675%20/%20(%20dummy_T%20-%2031.737%20%29%20%29%20%29%20%29%0Amain_Rho_sat%20%3D%201000%20%23%20kg%20/%20m%5E3%20%0Amain_mol_weight%20%3D%2018%20%23%20g%20/%20mol%0Adef%20function_V(dummy_T,dummy_P%29%3A%20%23%20now%20if%20a%20volume%20at%20pressure%20lower%20than%20saturation%20pressure%20is%20inquired,%20a%20ideal%20gas%20phase%20volume%20is%20provided%0A%09if%20(dummy_P%29%20%3C%20function_P_sat(dummy_T%29%3A%0A%09%09return%20main_R%20*%20dummy_T%20/%20dummy_P%0A%09else%3A%20%20%23%20and%20if%20a%20volume%20at%20pressure%20higher%20than%20saturation%20pressure%20is%20inquired,%20a%20liquid%20phase%20volume%20is%20provided%0A%09%09return%20main_mol_weight%20/%20(%20main_Rho_sat%20*%201000%20%29%0Amain_P%3D1e5%0Alist_T%20%3D%20%5B273,%20298,%20373,%20500,%20600%5D%0A%23%20---%20the%20functional%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Adef%20function_trapez(f,inf,sup%29%3A%20%23%20this%20function%20takes%20as%20arguments%20a%20pointer-to-function%20%22f%22%20and%20two%20values%20%22inf%22%20and%20%22sup%22%20and%20calculates%20a%201-trapeze%20approximate%20integral%0A%20%20%20%20return%20(f(sup%29-f(inf%29%29%20/%202%20%23%20this%20is%20called%20a%20call%20back%20because%20the%20function_trapeze%20will%20the%20%22call%20back%22%20the%20function%20f,%20then%20providing%20the%20values%20inf%20and%20sup%20in%20pace%20of%20its%20dummy_arguments%0A%23%20---%20the%20closure%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Adef%20function_V_298(dummy_P%29%3A%20%23sets%20dummy_T%20to%20298,%20passes%20on%20dummy_P%0A%09return%20function_V(dummy_T%3D298,dummy_P%3Ddummy_P%29%0APinf%3D0.9*function_P_sat(298%29%20%23%20define%20the%20lower%20and%20upper%20limits%20of%20integration%0APsup%3D0.8*function_P_sat(298%29%20%23small%20perturbation%0A%23%20pass%20the%20function%20and%20limits%20to%20the%20approximate%20%22integrator%22%0A%23%20---%20the%20callback%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Aans%3Dfunction_trapez(function_V_298,Pinf,Psup%29%20%23note%20no%20empty%20parentheses%20after%20function_V_298,%20that%20means%20pass%20the%20pointer,%20do%20not%20evaluate%20now%0Aprint(ans%29%20&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-live.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.054819897503086856\n" ] } ], "source": [ "# The following code should be studied on the platform provided by http://www.pythontutor.com/live.html#mode=edit\n", "# This platform graphically depicts the behavior of variables, assignments, functions and pointers,\n", "# which is the major source of confusion among beginners in programming\n", "\n", "# === Lecture zero, part F ========================================================================================================================================================================\n", "\n", "# --- Remember from part E -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "main_R = 8.314\n", "\n", "def function_P_sat(dummy_T): # Let's use this approximate correlation for water saturation pressure\n", "\treturn 1e5 * ( 10**( 5.40221 - ( 1838.675 / ( dummy_T - 31.737 ) ) ) )\n", "\t\n", "main_Rho_sat = 1000 # kg / m^3 \n", "main_mol_weight = 18 # g / mol\n", "\n", "def function_V(dummy_T,dummy_P): # now if a volume at pressure lower than saturation pressure is inquired, a ideal gas phase volume is provided\n", "\tif (dummy_P) < function_P_sat(dummy_T):\n", "\t\treturn main_R * dummy_T / dummy_P\n", "\telse: # and if a volume at pressure higher than saturation pressure is inquired, a liquid phase volume is provided\n", "\t\treturn main_mol_weight / ( main_Rho_sat * 1000 )\n", "\n", "main_P=1e5\n", "\n", "list_T = [273, 298, 373, 500, 600]\n", "\n", "# --- the functional -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "def function_trapez(f,inf,sup): # this function takes as arguments a pointer-to-function \"f\" and two values \"inf\" and \"sup\" and calculates a 1-trapeze approximate integral\n", " return (f(sup)-f(inf)) / 2 # this is called a call back because the function_trapeze will the \"call back\" the function f, then providing the values inf and sup in pace of its dummy_arguments\n", "\n", "# we can, on some applications, use integrals like this to calculate work in an open flow system (integrate V dP)\n", "\n", "# e.g. calculate work in depressurization of ideal gas between .9 and .8 of the substances saturation pressure at 298 K\n", "\n", "# --- the closure -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "# define a function_V to run at constant T of 298\n", "\n", "def function_V_298(dummy_P): #sets dummy_T to 298, passes on dummy_P\n", "\treturn function_V(dummy_T=298,dummy_P=dummy_P)\n", "\n", "# define the lower and upper limits of integration\n", "\n", "Pinf=0.9*function_P_sat(298)\n", "Psup=0.8*function_P_sat(298) #small perturbation\n", "\n", "# pass the function and limits to the approximate \"integrator\"\n", "\n", "# --- the callback -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "ans = function_trapez(function_V_298,Pinf,Psup) #note no empty parentheses after function_V_298, that means pass the pointer, do not evaluate now\n", "\n", "print(ans) " ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }