{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "Text provided under a Creative Commons Attribution license, CC-BY. All code is made available under the FSF-approved BSD-3 license. (c) Lorena A. Barba, Gilbert F. Forsyth 2017. Thanks to NSF for support via CAREER award #1149784." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[@LorenaABarba](https://twitter.com/LorenaABarba)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "12 steps to Navier–Stokes\n", "=====\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This lesson complements the first interactive module of the online [CFD Python](https://github.com/barbagroup/CFDPython) class, by Prof. Lorena A. Barba, called **12 Steps to Navier–Stokes.** The interactive module starts with simple exercises in 1D that at first use little of the power of Python. We now present some new ways of doing the same things that are more efficient and produce prettier code.\n", "\n", "This lesson was written with BU graduate student Gilbert Forsyth.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Defining Functions in Python \n", "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In steps 1 through 8, we wrote Python code that is meant to run from top to bottom. We were able to reuse code (to great effect!) by copying and pasting, to incrementally build a solver for the Burgers' equation. But moving forward there are more efficient ways to write our Python codes. In this lesson, we are going to introduce *function definitions*, which will allow us more flexibility in reusing and also in organizing our code. \n", "\n", "We'll begin with a trivial example: a function which adds two numbers. \n", "\n", "To create a function in Python, we start with the following:\n", "\n", " def simpleadd(a,b):\n", "\n", "This statement creates a function called `simpleadd` which takes two inputs, `a` and `b`. Let's execute this definition code." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def simpleadd(a, b):\n", " return a+b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `return` statement tells Python what data to return in response to being called. Now we can try calling our `simpleadd` function:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simpleadd(3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, there can be much more happening between the `def` line and the `return` line. In this way, one can build code in a *modular* way. Let's try a function which returns the `n`-th number in the Fibonacci sequence. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def fibonacci(n):\n", " a, b = 0, 1\n", " for i in range(n):\n", " a, b = b, a + b\n", " return a\n", " " ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fibonacci(7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once defined, the function `fibonacci` can be called like any of the built-in Python functions that we've already used. For exmaple, we might want to print out the Fibonacci sequence up through the `n`-th value:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "1\n", "2\n", "3\n", "5\n", "8\n", "13\n", "21\n", "34\n" ] } ], "source": [ "for n in range(10):\n", " print(fibonacci(n))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use the capacity of defining our own functions in Python to help us build code that is easier to reuse, easier to maintain, easier to share!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Pending.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Learn more\n", "-----\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember our short detour on using [array operations with NumPy](./07_Step_5.ipynb)?\n", "\n", "Well, there are a few more ways to make your scientific codes in Python run faster. We recommend the article on the Technical Discovery blog about [Speeding Up Python](http://technicaldiscovery.blogspot.com/2011/06/speeding-up-python-numpy-cython-and.html) (June 20, 2011), which talks about NumPy, Cython and Weave. It uses as example the Laplace equation (which we will solve in [Step 9](./12_Step_9.ipynb)) and makes neat use of defined functions.\n", "\n", "But a recent new way to get fast Python codes is [Numba](http://numba.pydata.org). We'll learn a bit about that after we finish the **12 steps to Navier–Stokes**.\n", "\n", "There are many exciting things happening in the world of high-performance Python right now!\n", "\n", "***" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"../styles/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> (The cell above executes the style for this notebook.)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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": 0 }