Assignment 2 100 | 2022/10/28
Assignment 2 will focus on writing a basic scientific computing application in Julia. We will focus on the simulation of “particle transport”, where particles move based on a prescribed background velocity field.
Background
In 2D, the position/coordinates of the ith particle are modeled as a system of ordinary differential equations (ODEs)
Copyright By PowCoder代写 加微信 powcoder
for , where is the total number of particles.
The solution to these differential equations can be approximated using a time-stepping method, where the unknowns can be approximated at a set of discrete times . For example, we can use the forward Euler (https://en.wikipedia.org/wiki/Euler_method) method to approximate
the solution. This method approximates , where is a small time-step size. Then, evaluating the velocities at the same time yields the iteration
where (similarly for the other coordinates).
More advanced time-stepping schemes are significantly more accurate than forward Euler while retaining low computational cost. For example, the second order explicit midpoint method (https://en.wikipedia.org/wiki/Midpoint_method) updates each position in two stages:
Here, “x_tmp, y_tmp” are intermediate approximations of the solution which are used to increase
the accuracy.
(https://canvas.rice.edu/courses/50146/modules/items/559768)
½Úϸ¡¢Ï¢ÐÅϸÏê
Part 0: Github
Create a new directory in ” caam-419-519-submissions ” titled ” homework-2 “. You will commit all files
used in this homework to this directory.
Part 1: writing two general ODE solvers
Write a function “solve” which solves a general system of ODEs using a time-stepping method. This code should be able to switch between two different time-stepping schemes: forward Euler and the explicit midpoint method. For reference, the classroom demo we wrote previously is available here: particle_demo.jl (https://canvas.rice.edu/courses/50146/files/4038680?wrap=1) (https://canvas.rice.edu/courses/50146/files/4038680/download?download_frd=1)
First, create custom types “ForwardEuler” and “ExplicitMidpoint”. The explicit midpoint struct should have the form
struct ExplicitMidpoint{T}
# This is a custom constructor which initializes “u_tmp” based on an initial condition
ExplicitMidpoint(u) = new{typeof(u)}(similar(u))
ForwardEuler
ExplicitMidpoint(u)
solve(method, u0, rhs!, tspan, dt, parameters;
num_saved_steps=1)
solve(method::ForwardEuler,
solve(method::ExplicitMidpoint, …)
ForwardEuler()
ExplicitMidpoint(u)
ExplicitMidpoint
parameters
num_saved_steps
where ” ” is a temporary storage array used in the time-stepping loop. This should be initialized by calling ” “, where “u” is the initial condition for your solver. The
” function.
The function ”
…) ” and ”
loops using the forward Euler method or the explicit midpoint method. Users will specify which function is to be called by passing in either ” ” or ” ” into “solve”. Note that the specialized method for the explicit midpoint method should use the
” ” field in the ” ” struct to store intermediate solutions.
The remaining inputs of ” ” are as follows:
” u0 ” is the matrix containing the initial positions of the particles.
” rhs! ” is the function which defines the system of ODEs.
” ” is the timespan over which the ODE should be solved (for example, tspan = (0, 1)) ” dt ” is the timestep size
” are additional inputs used by .
” defines the number of solution snapshots to be saved over the course of
” type does not need any fields, and is used only to dispatch the specialized “solve”
” should have signature ”
“. I expect two specializations of this function: ”
“, each of which will implement time-stepping
(https://canvas.rice.edu/courses/50146/modules/items/559768)
the simulation. This should default to 1.
” ” should output a list of saved solutions (the first and last entries should correspond to the initial and final conditions, respectively), as well as the number of total ” rhs! ” evaluations.
Save these functions in “part_1.jl”, and commit this to the repository.
Part 2: modeling the physics
The ODE should be defined by a function which has the form ” rhs!(du, u, parameters, t) “, where ” parameters ” holds any additional information needed to evaluate ” rhs! “. The arguments are
” du “: a 2-by-(number of particles) matrix which stores the evaluation of the right hand side. ” u “: the 2-by-(number of particles) which defines the current positions of the particles
” parameters “: additional parameters used to evaluate ” rhs! “.
” t “: the time at which the right hand side is evaluated.
Implement a rhs! function for a prescribed rotating velocity
where is a parameter we will set to 5 for this problem. Pass in the value of “C” using the “parameters” argument.
Please make sure that your ” rhs! ” function is
1. type stable (use to check this)
2. allocation-free (use or to verify that no allocations
and-pay-attention-to-memory-allocation) occur)
Save the ” rhs! ” function in “part_2.jl”, and commit this to the repository.
Part 3: verification
Test your code by solving a 2D particle system with 100 particles and over the time span (0, 5). For the velocity in Part 2, the particles should return to the initial condition. This allows us to compute the difference between the initial particle positions and the final particle positions at FinalTime = 5 for both Euler and the explicit midpoint method. We will refer to these as the “error”.
Verify that your “solve” function is type stable. Then, write a script “part_3.jl” which
computes the error for dt = [1/16, 1/32, 1/64, 1/128, 1/256, 1/512, 1/1024
saves a PNG plot of the error vs timestep size dt (use a log-log scale)
@code_warntype
saves a PNG plot of the error vs number of “rhs!” evaluations (use a log-log scale).
(https://canvas.rice.edu/courses/50146/modules/items/559768)
\usepackage{jldcode}
@code_warntype
@code_warntype
caam-419-519-
submissions/homework-2
you delete the tag, and re-tag the latest commit!
creates a gif of the solution evolution over time span (0, 10) using at least 100 frames. Commit the script “part_3.jl” to your repository.
Part 4: write-up
Create a document named homework-2.tex using LaTeX. The document should have a title that reads: ¡°CAAM 419/519, Homework #2¡±. The author should be your NetID, formatted in a monospaced font. The date should be the date I am preparing this homework: September 30, 2022.
To include Julia code, please use the “jlcode” package (download “jlcode.sty” from https://github.com/wg030/jlcode (https://github.com/wg030/jlcode) into your LaTeX folder and include ” ” in your LaTeX file).
The contents of the document should be as follows:
1. Section 1: document verify the correctness of your Forward Euler and explicit midpoint ODE solver (Part 3)
1. Include a plot of the initial particle positions and final particle positions at FinalTime = 1 using the velocities from Part 2.
2. Plot the error vs dt for both solvers on a log-log scale. Add a legend to identify which method each plot corresponds to. Label the x and y axes as “Error” and “Timestep size”, respectively. The errors for each method should converge to zero as the timestep size decreases. Include a short discussion of what you observe qualitatively.
2. Section 2: analyze the efficiency of your “rhs!” function (Part 2)
1. Include the output of for a representative set of inputs “du, u, parameters,
t” (just the first part with the variables is fine)
2. Include the output of
3. Section 3: analyze the efficiency of your solver (Part 3)
1. Include the output of for a representative set of inputs to “solve” (just the
first part with the variables is fine)
2. Produce a plot with the error on the x-axis and the number of function evaluations on the
y-axis for both the forward Euler method and the explicit midpoint method on the same plot, and add a legend to label each line. Use a log-log plot.
Comment and save the code used to generate all plots in a separate file “part_5.jl”. Commit “part_5.jl”, your LaTeX report, and the compiled pdf to your repository ”
“. Once your assignment is completed, tag the commit you want us to grade with the “homework-2” tag. If you end up making changes before the deadline, make sure
(https://canvas.rice.edu/courses/50146/modules/items/559768)
(https://canvas.rice.edu/courses/50146/modules/items/559768)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com