owned this note
owned this note
Published
Linked with GitHub
[TOC]
Random lecture notes
====================
Python / NumPy related
----------------------
- Division:
- in Python 2: `4/5 == 1` (*!!!*)
- in Python 2: `4.0/5 == 0.8`
- in Python 3: `4/5 == 0.8`
- To create arrays:
- `np.linspace(from, to, npts)`
- `np.arange(from, to)`
- `np.arange(from, to, increment)`
- `np.zeros(npts)`; `np.ones(npts)`; `np.empty(npts)`
- `np.array([val1, val2, ..., valN])`
- `np.copy(anotherarray)`
- Array slicing (`x` is a NumPy array)
- `x[2]` gives element 2, i.e. the 3rd element
- `x[2:5]` gives elements 2, 3, and 4
- Mathematical operations on arrays (`x` and `y` are arrays *of same length*)
- `x * 5`: multiply all elements by a constant 5
- `x * y`: multiply two arrays *element-by-element*, resulting array is `[x[0]*y[0], x[1]*y[1], ..., x[N-1]*y[N-1]]`
- For loops
- General syntax
```python
for loopvar in range(fromvalue, tovalue, increment):
# Do something with loopvar, e.g.
print(loopvar)
```
- Following code prints `12, 13, 14`
```python
mylist = [2, 3, 4]
for number in mylist:
print(number + 10)
```
- Following code does the same
```python
for number in range(2,5):
print(number + 10)
```
- Print contents of an array:
```python
for ix in range(0, len(x)):
print("Element", ix, "in array x is", x[ix])
```
- Useful IPython magic commands
- Listing variables in memory
```python
%whos
```
- Execute a Python script from the IPython prompt
```python
%run <script file>
```
- Calculating time needed for code execution
```python
%timeit <command>
```
Debugging tips
-------------
If the code seems to have problems...
- Re-run the script. Does the problem repeat? (i.e. is it an systematic error, and not an artifact due to e.g. hardware problems)
- Check the finite difference formulation
- If OK, go systematically backwards checking the values
- e.g. `dx`, `dy`
- Print values using `print()` within the code to check their value
- Pay attention to *small* differences: Is `nt` accidentally replaced by `nx`, for example?
- Do *one change at a time*, and re-run to see whether it made a difference. Changing multiple things at once might introduce a change in results but you cannot tell which correction caused it.
- Parentheses!
- Parentheses!
- Check the loops: Are you calculating each point you need to calculate?
- If you have some kind of solution, but which is wrong:
- Change physical parameters: Does the system behave like expected? If not, then that physical parameter is not likely to have not taken into consideration in the calculations
- In the results, are the fixed boundary conditions what you expect?
A few useful web pages
----------------------
- [Numerical accuracy in Python](https://docs.python.org/2/tutorial/floatingpoint.html)
- [Representation of floating point numbers in computers](https://en.wikipedia.org/wiki/Computer_number_format)
- Stokes/heat diffusion solver: https://github.com/larskaislaniemi/PyLamp/archive/simplified.zip
Student questions
=================
-