Skip to content

Commit fa7d2dc

Browse files
committed
Adding build files
1 parent 38d8148 commit fa7d2dc

6 files changed

Lines changed: 360 additions & 0 deletions

File tree

jb/_config.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Book settings
2+
title: Modeling and Simulation in Python
3+
author: Allen B. Downey
4+
5+
latex:
6+
latex_documents:
7+
targetname: book.tex
8+
9+
execute:
10+
execute_notebooks: 'off'
11+
12+
repository:
13+
url: https://github.com/AllenDowney/ModSimPy
14+
html:
15+
use_repository_button: true
16+
17+
parse:
18+
myst_extended_syntax: true

jb/_toc.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
file: index
2+
sections:
3+
- file: preface
4+
- file: chap01
5+
- file: chap02
6+
- file: chap03
7+
- file: chap04
8+
- file: chap05
9+
- file: chap06
10+
- file: chap07
11+
- file: chap08
12+
- file: chap09
13+
- file: chap10
14+
- file: chap11
15+
- file: chap12
16+
- file: chap13
17+
- file: chap14
18+
- file: chap15
19+
- file: chap16
20+
- file: chap17
21+
- file: chap18
22+
- file: chap19
23+
- file: chap20
24+
- file: chap21
25+
- file: chap22
26+
- file: chap23
27+
- file: chap24
28+
- file: chap25
29+
- file: chap26

jb/build.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# pip install jupyter-book
2+
# pip install ghp-import
3+
4+
# Build the Jupyter book version
5+
6+
# copy the chapter notebooks
7+
cp ../ModSimPySolutions/chapters/chap*.ipynb .
8+
9+
# add tags to hide the solutions
10+
python prep_notebooks.py
11+
12+
# build the HTML version
13+
jb build .
14+
15+
# push it to GitHub
16+
ghp-import -n -p -f _build/html

jb/index.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Modeling and Simulation in Python
2+
3+
by Allen B. Downey
4+
5+
*Modeling and Simulation in Python* is a Free Book. It is available under the [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](https://creativecommons.org/licenses/by-nc-sa/4.0/), which means that you are free to copy and modify it, as long as you attribute the work and don’t use it for commercial purposes.
6+
7+
Other Free Books by Allen Downey are available from [Green Tea Press](https://greenteapress.com/wp/).
8+
9+
**Run the notebooks**
10+
11+
[Download the notebooks as a Zip file](https://github.com/AllenDowney/ModSimPy/raw/master/ModSimPyNotebooks.zip)
12+
13+
Or use these links to run the notebooks on Colab:
14+
15+
* [Chapter 1](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap01.ipynb)
16+
17+
* [Chapter 2](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap02.ipynb)
18+
19+
* [Chapter 3](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap03.ipynb)
20+
21+
* [Chapter 4](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap04.ipynb)
22+
23+
* [Chapter 5](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap05.ipynb)
24+
25+
* [Chapter 6](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap06.ipynb)
26+
27+
* [Chapter 7](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap07.ipynb)
28+
29+
* [Chapter 8](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap08.ipynb)
30+
31+
* [Chapter 9](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap09.ipynb)
32+
33+
* [Chapter 10](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap10.ipynb)
34+
35+
* [Chapter 11](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap11.ipynb)
36+
37+
* [Chapter 12](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap12.ipynb)
38+
39+
* [Chapter 13](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap13.ipynb)
40+
41+
* [Chapter 14](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap14.ipynb)
42+
43+
* [Chapter 15](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap15.ipynb)
44+
45+
* [Chapter 16](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap16.ipynb)
46+
47+
* [Chapter 17](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap17.ipynb)
48+
49+
* [Chapter 18](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap18.ipynb)
50+
51+
* [Chapter 19](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap19.ipynb)
52+
53+
* [Chapter 20](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap20.ipynb)
54+
55+
* [Chapter 21](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap21.ipynb)
56+
57+
* [Chapter 22](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap22.ipynb)
58+
59+
* [Chapter 23](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap23.ipynb)
60+
61+
* [Chapter 24](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap24.ipynb)
62+
63+
* [Chapter 25](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap25.ipynb)
64+
65+
* [Chapter 26](https://colab.research.google.com/github/AllenDowney/ModSimPy/blob/master/chapters/chap26.ipynb)

jb/preface.md

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# Preface
2+
3+
The essential skills of modeling — abstraction, analysis, simulation,
4+
and validation — are central in engineering, natural sciences, social
5+
sciences, medicine, and many other fields. Some students learn these
6+
skills implicitly, but in most schools they are not taught explicitly,
7+
and students get little practice. That's the problem this book is meant to address.
8+
9+
At Olin College, we teach these skills in a class called "Modeling and
10+
Simulation", which all students take in their first semester. My
11+
colleagues, John Geddes and Mark Somerville, and I developed this class and taught it for the first time in 2009.
12+
13+
It is based on our belief that modeling should be taught explicitly,
14+
early, and throughout the curriculum. It is also based on our conviction that computation is an essential part of this process.
15+
16+
If students are limited to the mathematical analysis they can do by
17+
hand, they are restricted to a small number of simple physical systems, like a projectile moving in a vacuum or a block on a frictionless plane.
18+
19+
And they only see bad models; that is, models that are too
20+
simple for their intended purpose. In nearly every mechanical system,
21+
air resistance and friction are essential features; if we ignore them,
22+
our predictions will be wrong and our designs won’t work.
23+
24+
In most introductory physics classes, students don't make modeling
25+
decisions; sometimes they are not even aware of the decisions that have been made for them. Our goal is to teach the entire modeling process and give students a chance to practice it.
26+
27+
**How much programming do I need?**
28+
29+
If you have never programmed before, you should be able to read this
30+
book, understand it, and do the exercises. I will do my best to explain everything you need to know; in particular, I have chosen carefully the vocabulary I introduce, and I try to define each term the first time it is used. If you find that I have used a term without defining it, let me know.
31+
32+
If you have programmed before, you will have an easier time getting
33+
started, but you might be uncomfortable in some places. I take an
34+
approach to programming you have probably not seen before.
35+
36+
Most programming classes have two big problems:
37+
38+
1. They go "bottom up", starting with basic language features and
39+
gradually adding more powerful tools. As a result, it takes a long
40+
time before students can do anything more interesting than convert
41+
Fahrenheit to Celsius.
42+
43+
2. They have no context. Students learn to program with no particular
44+
goal in mind, so the exercises span an incoherent collection of
45+
topics, and the exercises tend to be unmotivated.
46+
47+
In this book, you learn to program with an immediate goal in mind:
48+
writing simulations of physical systems. And we proceed "top down", by
49+
which I mean we use professional-strength data structures and language
50+
features right away. In particular, we use the following Python libraries:
51+
52+
- NumPy for basic numerical computation (see
53+
<https://www.numpy.org/>).
54+
55+
- SciPy for scientific computation (see <https://www.scipy.org/>).
56+
57+
- Matplotlib for visualization (see <https://matplotlib.org/>).
58+
59+
- Pandas for working with data (see <https://pandas.pydata.org/>).
60+
61+
- SymPy for symbolic computation, (see <https://www.sympy.org>).
62+
63+
- Pint for units like kilograms and meters (see
64+
<https://pint.readthedocs.io>).
65+
66+
- Jupyter for reading, running, and developing code (see
67+
<https://jupyter.org>).
68+
69+
These tools let you work on more interesting programs sooner, but there are some drawbacks: they can be hard to use, and it can be challenging to keep track of which library does what and how they interact.
70+
71+
I have tried to mitigate these problems by providing a library that makes it easier to get started with these tools, and provides some
72+
additional capabilities.
73+
74+
Some features in the ModSim library are like training wheels; at some
75+
point you will probably stop using them and start working with the
76+
underlying libraries directly. Other features you might find useful the whole time you are working through the book, and later.
77+
78+
I encourage you to read the the ModSim library code. Most of it is not
79+
complicated, and I tried to make it readable. Particularly if you have
80+
some programming experience, you might learn something by reverse
81+
engineering my design decisions.
82+
83+
**How much math and science do I need?**
84+
85+
I assume that you know what derivatives and integrals are, but that's
86+
about all. In particular, you don’t need to know (or remember) much
87+
about finding derivatives or integrals of functions analytically. If you know the derivative of $x^2$ and you can integrate $2x~dx$, that will do it. More importantly, you should understand what those concepts *mean*; but if you don’t, this book might help you figure it out.
88+
89+
You don't have to know anything about differential equations.
90+
91+
As for science, we will cover topics from a variety of fields, including demography, epidemiology, medicine, thermodynamics, and mechanics. For the most part, I don’t assume you know anything about these topics. In fact, one of the skills you need to do modeling is the ability to learn enough about new fields to develop models and simulations.
92+
93+
When we get to mechanics, I assume you understand the relationship
94+
between position, velocity, and acceleration, and that you are familiar with Newton’s laws of motion, especially the second law, which is often expressed as $F = ma$ (force equals mass times acceleration).
95+
96+
I think that's everything you need, but if you find that I left
97+
something out, please let me know.
98+
99+
**Getting started**
100+
101+
To run the examples and work on the exercises in this book, you will need an environment where you can run Jupyter notebooks.
102+
103+
Jupyter is a software development environment where you can run Python code, including the examples in this book, and write your own code.
104+
105+
A Jupyter notebook is a document that contains text, code, and results from running the code.
106+
Each chapter of this book is a Jupyter notebook where you can run the examples and work on exercises.
107+
108+
To run the notebooks, you have two options:
109+
110+
1. You can install Python and Jupyter on your computer and download my notebooks.
111+
112+
2. You can run the notebooks on Colab.
113+
114+
To run the notebooks on Colab, go to [the landing page for this book](https://allendowney.github.io/ModSimPy/index.html) and follow the links to the chapters.
115+
116+
To run the notebooks on your computer, there are three steps:
117+
118+
1. Download the notebooks and copy them to your computer.
119+
120+
2. Install Python, Jupyter, and some additional libraries.
121+
122+
3. Run Jupyter and open the notebooks.
123+
124+
To get the notebooks, download [this Zip archive](http://modsimpy.com/zip). You will need a program like
125+
WinZip or gzip to unpack the Zip file. Make a note of the location of
126+
the files you download.
127+
128+
The next two sections provide details for the other steps.
129+
Installing and running software can be challenging, especially if you are not familiar with the command line.
130+
If you run into problems, you might want to work on Colab, at least to get started.
131+
132+
133+
**Installing Python**
134+
135+
You might already have Python installed on your computer, but you might not have the latest version. To use the code in this book, you need Python 3.6 or later. Even if you have the latest version, you probably don't have all of the libraries we need.
136+
137+
You could update Python and install these libraries, but I strongly
138+
recommend that you don’t go down that road. I think you will find it
139+
easier to use **Anaconda**, which is a free Python distribution that includes all the libraries you need for this book (and more).
140+
141+
Anaconda is available for Linux, macOS, and Windows. By default, it puts all files in your home directory, so you don’t need administrator (root) permission to install it, and if you have a version of Python already, Anaconda will not remove or modify it.
142+
143+
Start at <https://www.anaconda.com/download>. Download the installer for your system and run it. I recommend you run the installer as a normal user, not as administrator or root.
144+
145+
I suggest you accept the recommended options. On Windows you have the
146+
option to install Visual Studio Code, which is an interactive
147+
environment for writing programs. You won't need it for this book, but
148+
you might want it for other projects.
149+
150+
By default, Anaconda installs most of the packages you need, but there
151+
are a few more you have to add. Once the installation is complete, open a command window. On macOS or Linux, you can use Terminal. On Windows, open the Anaconda Prompt that should be in your Start menu.
152+
153+
Run the following command (copy and paste it if you can, to avoid
154+
typos):
155+
156+
```
157+
conda install jupyter pandas sympy
158+
conda install beautifulsoup4 lxml html5lib
159+
conda install -c unidata pint
160+
```
161+
162+
That should be everything you need.
163+
164+
165+
**Running Jupyter**
166+
167+
If you have not used Jupyter before, you can read about it at <https://jupyter.org>.
168+
169+
To start Jupyter on macOS or Linux, open a Terminal; on Windows, open
170+
Git Bash. Use `cd` to “change directory" into the directory that contains the notebooks.
171+
172+
```
173+
cd ModSimPy
174+
```
175+
176+
Then launch the Jupyter notebook server:
177+
178+
```
179+
jupyter notebook
180+
```
181+
182+
Jupyter should open a window in a browser, and you should see the list
183+
of notebooks in my repository. Click on the first notebook, and follow
184+
the instructions to run the first few "cells". The first time you run a notebook, it might take several seconds to start, while some Python
185+
files get initialized. After that, it should run faster.
186+
187+
You can also launch Jupyter from the Start menu on Windows, the Dock on macOS, or the Anaconda Navigator on any system. If you do that, Jupyter might start in your home directory or somewhere else in your file system, so you might have to navigate to find the directory with the notebooks.
188+
189+
190+
**Contributor List**
191+
192+
If you have a suggestion or correction, send it to
193+
<downey@allendowney.com>. Or if you are a Git user, open an issue, or send me a pull request on [this repository](https://github.com/AllenDowney/ModSimPy).
194+
195+
If I make a change based on your feedback, I will add you to the
196+
contributor list, unless you ask to be omitted.
197+
198+
If you include at least part of the sentence the error appears in, that makes it easy for me to search. Page and section numbers are fine, too, but not as easy to work with. Thanks!
199+
200+
- My early work on this book benefited from conversations with my
201+
colleagues at Olin College, including John Geddes, Mark Somerville, Alison Wood, Chris Lee, and Jason Woodard.
202+
203+
- I am grateful to Lisa Downey and Jason Woodard for their thoughtful and careful copy editing.
204+
205+
- Thanks to Alessandra Ferzoco, Erhardt Graeff, Emily Tow, Kelsey
206+
Houston-Edwards, Linda Vanasupa, Matt Neal, Joanne Pratt, and Steve Matsumoto for their helpful suggestions.

jb/prep_notebooks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import nbformat as nbf
2+
from glob import glob
3+
4+
# Collect a list of all notebooks in the content folder
5+
notebooks = glob("*.ipynb")
6+
7+
# Text to look for in adding tags
8+
text_search_dict = {
9+
"# Solution": "hide-cell", # Hide the cell with a button to show
10+
}
11+
12+
# Search through each notebook and look for the text, add a tag if necessary
13+
for ipath in notebooks:
14+
ntbk = nbf.read(ipath, nbf.NO_CONVERT)
15+
16+
for cell in ntbk.cells:
17+
cell_tags = cell.get('metadata', {}).get('tags', [])
18+
cell_tags = []
19+
for key, val in text_search_dict.items():
20+
if key in cell['source']:
21+
if val not in cell_tags:
22+
cell_tags.append(val)
23+
if len(cell_tags) > 0:
24+
cell['metadata']['tags'] = cell_tags
25+
26+
nbf.write(ntbk, ipath)

0 commit comments

Comments
 (0)