TITLE: Solving for streamlines from numerical velocity field
QUESTION [8 upvotes]: Say I have a given numerical velocity field in two dimensions, (u,v). I am trying to find the streamlines from this data set at a particular contour level and I thus have to solve the differential equation 
$$
dy/dx = v/u=g(x,y)
$$
I can rewrite the equation to
$$
dy = g(x_i,y_i)dx
$$
The subscript $i$ denotes that $g$ is given at discrete points. Do I solve this equation simply by choosing a $y_i$ and then summing for all $x_i$?

REPLY [3 votes]: It's easy to spoil decent content, therefore: never change a (bounty) winning answer. Instead, it will be referred to as the
first answer
to the question. There are two unsatisfactory issues with that first answer, though:

The software accomplishing all this is [ available for free, but it is ] far from optimal.
In two respects: (1) too many zeroes are stored and (2) there is a lot of computation with those zeroes.
The example chosen in the first answer does very much bear the flavor of Finite Differences.
I would like to present another example, more Finite Element like, for the sheer purpose of demonstrating the power of the Least Squares method for unstructured grids.

So there are two reasons for devising a second answer.
Ideal flow past a circular cylinder (called Potential flow around a circular cylinder in
Wikipedia)
has been selected as the demonstration problem here and now:

Instead of taking mean values, known velocities are evaluated at the midpoints of the line segments,
as is shown in the picture on the left:
$$
u_{12} = u\left(\frac{x_1+x_2}{2},\frac{y_1+y_2}{2}\right) \quad ; \quad
v_{12} = v\left(\frac{x_1+x_2}{2},\frac{y_1+y_2}{2}\right)
$$ $$
d\psi = -v\,dx + u\,dy \quad \Longrightarrow \quad
\int_{(1)}^{(2)} d\psi = -\int_{(1)}^{(2)}v\, dx + \int_{(1)}^{(2)}u\, dy
\quad \Longrightarrow \\ \psi_2-\psi_1 = -v_{12}(x_2-x_1) + u_{12}(y_2-y_1)
\quad \mbox{: numerically}
$$
Velocity and stream function values are given by
Wikipedia 
in polar coordinates. Which must be converted into Cartesian coordinates for our purpose.
The velocities are input for the program, the stream function is for comparison of the numerical
outcome with the exact solution eventually:
$$
\begin{cases}
V_{r}=\left(1-{R^{2}/r^{2}}\right)\cos(\theta)\\
V_{\theta}=-\left(1+R^{2}/r^{2}\right)\sin(\theta)\end{cases}
\quad \Longrightarrow \\ \begin{cases}
u(x,y) = \left[1-R^2(x^2-y^2)/(x^2+y^2)^2\right]\\
v(x,y) = -R^2.2xy/(x^2+y^2)^2\end{cases}\\
\psi =\left(r-\frac{R^2}{r}\right)\sin(\theta) = \left(1-\frac{R^2}{x^2+y^2}\right)y
$$
Another unsatisfactory issue in the first answer is inefficiency of the code in space & time:
too many zeroes are stored and there is a lot of computation with those zeroes. This can be greatly improved.
There are two main stages involved with the final equations: assembly and solving.
Assembly. In the first answer, $M \times N$ overdetermined equations $A$ are defined first,
where in our case $M = 180$ and $N = 100$. While forming the system matrix $S=A^TA$ we thus have $180\times 100^2$
operations, which is ridiculous because $A$ is almost empty: only $2$ of the $100$ entries in a row are non-zero!
It's easy to improve this by defining $A$ as being an $1 \times 2$ array and forming $A^TA$ as an elementary matrix $E$,
corresponding with just one equation; $r$ is the right hand side of it.
$$
E = A^TA = \begin{bmatrix} -1 \\ +1 \end{bmatrix} \begin{bmatrix} -1 & +1 \end{bmatrix} =
\begin{bmatrix} +1 & -1 \\ -1 & +1 \end{bmatrix} \\
r = \begin{bmatrix} -1 \\ +1 \end{bmatrix}\left[-v_{12}(x_2-x_1) + u_{12}(y_2-y_1)\right]
$$
The elementary matrices $E$ and loads $r$ must subsequently be assembled into the system matrix $S$ and global load vector $b$.
Which happens to be exactly the same procedure as is common with the conventional finite element method.
The name Least Squares Finite Element Method is justified herewith.
Solving. Instead of defining a full $100\times 100$ system matrix $S$, it's far more efficient to define
it as a band matrix. That will save us a factor $10$ of space in the first place.
But there are a few other properties that are interesting:
$$ \begin{cases}
S_{ij} = \sum_k A_{ki}A_{kj} \quad \Longrightarrow \quad S_{ij}=S_{ji} \\
S_{ii} = \sum_k A_{ki}A_{ki} \quad \Longrightarrow \quad S_{ii} > 0 \\
\left(\sum_k A_{ki}A_{kj}\right)^2 \le \sum_k A_{ki}^2\cdot\sum_k A_{kj}^2 \quad \Longrightarrow \quad
\left| S_{ij}\right| \le \sqrt{S_{ii} S_{jj}} \end{cases}
$$
The last property by Schwarz inequality. Symmetry will save us another factor $2$ in space, but more importantly, the
computation time for solving the equations will be reduced with a factor $100$ or so. The last two properties show that
the system matrix $S$ is positive definite and diagonally dominant, meaning that all pivots are on the main diagonal.
All of the improvements have been implemented in the updated version of the software, labeled as "Second answer" of the
question in MSE publications / references 2016.
There is still another issue with the streamline (= streamfunction isoline) plot, as shown in the above picture on the right.
Solving for the parameters of a
Quadrilateral Interpolation,
as would be needed with the algorithm for defining an isoline plot, is virtually impossible with quadrilaterals that
are not paralellograms. Therefore we have subdivided each of our quads into four
triangles. Thanks to an algorithm by Paul Bourke,
defining isolines with triangles is a matter of routine.
The end-result is shown in the above picture on the right. Needless to say that there is a match with the exact solution for the streamline function.