Contents MTHS2008 Assessed Python Coursework 2/2 (2025-26) 1 Submission instructions . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Deadline extensions and feedback . . . . . . . . . . . . . . . . . . . . . 1 AI policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 MTHS2008 Assessed Python Coursework 2/2 (2025-26) This coursework counts for 20% of the module grade. Coursework released: 3pm Thursday 19 March 2026 Submission deadline: 5pm Thursday 7 May 2026 Submission instructions You will be allowed to submit two documents: • One Python file which should include your code and be an edited version of the template Python file provided on Moodle. – python_cw2.py Please submit this file with this name exactly. Do not add your name or student number. • One python_cw2.pdf report file where you can include any text or figures. – Your report document must be neat and clearly legible. – Questions (b), (c), (e), (f), (h) and (i) should be included in this file. – If you are using Microsoft Word, you can print your document as a PDF. On the printer options, choose “Microsoft Print to PDF”. Your two documents should be submitted electronically via the Python course- work 2 submission box on the MTHS2008 Moodle page by the deadline indicated at the top of this document. Submissions up to five working days late will be subject to a penalty of 5% of the maximum mark per working day. Deadline extensions and feedback Deadline extensions due to Support Plans and Extenuating Circumstances can be requested according to School and University policies, as applicable to this module. 1 You are also eligible to submit a request for a no-evidence extension, up to a maximum of 5 working days. This is known as a coursework self cert and details of this policy are given here. Due to these policies, solutions and feedback cannot typically be released earlier than 10 working days after the main cohort submission deadline. We aim to release your marked coursework, the solutions and the associated feedback to you between 10 and 15 working days from the main cohort submission deadline. Note that your code, and any resulting plots, and your written analysis will be assessed. For questions worth more than 3 marks, a mark scheme is provided. The maximum number of marks available on this coursework is 50. AI policy You are allowed to use AI tools (such as ChatGPT) to help you answer questions and write code, but not to answer questions and write code for you. If you decide to use an AI tool to help you, you should include a log of all prompts to and responses from the AI tool. This should be included in the PDF report file that you submit. The undeclared use of AI tools is considered academic misconduct (see the University’s policy on Academic Misconduct). The use of AI tools in the way described in the previous paragraph will not be regarded as academic misconduct. Examples of acceptable uses of AI tools include: • Seeking clarification on what a particular line of code does. • Seeking an understanding of why a particular line or block of code that you have written does not work correctly. • Signposting to useful resources that discuss specific aspects of coding. If you are unsure whether what you intend to do counts as academic misconduct, please contact Cato Hastings before implementing any AI tools. In line with the Department for Chemical and Environmental Engineering’s AI policy, for the submission box to open for you on Moodle, you must: • Declare whether or not you have used any generative AI tools, as indicated by the assessment criteria outlined above, in preparing your work. • Confirm that the submitted work reflects your own learning and judgement, and that any use of AI has been acknowledged transparently. 2 Background The advection-diffusion equation can be used to model how the concentration of a chemical varies in time and space under the combined effects of advection and diffusion. This can be very useful in modelling pollutant transport. The advection-diffusion equation in one spatial dimension is given by ∂u ∂t − a∂ 2u ∂x2 + b∂u ∂x = 0 . (1) We solve this equation for x ∈ [0, 1] and t ∈ [0, Tf ] with Dirichlet boundary conditions u(0, t) = α, u(1, t) = β , where α and β are constants, and a zero initial condition everywhere except at the boundary u(x, 0) = 0, x ∈ (0, 1) . Here, • u(x, t) represents the concentration of the chemical at position x and time t. • a is the diffusion coefficient, which quantifies how quickly the chemical spreads out due to diffusion. • b is the advection velocity, which quantifies how quickly the chemical is transported by fluid flow. Questions We first consider the steady-state solution of the advection-diffusion equation in 1 spatial dimension. That is, the solution, u(x), satisfying −ad 2u dx2 + b du dx = 0 , (2) for x ∈ [0, 1]. We can adapt our boundary conditions as u(0) = α, u(1) = β. (3) We wish to approximate solutions to this equation numerically. (a) Complete the function steady_advection_1d_solver(a, b, alpha, beta, Nx) in the file python_cw2.py. The function should solve the steady-state advection-diffusion equation (2) using finite differences, subject to the boundary conditions (3). 3 [10 marks] The input parameters are: • a – a constant, the diffusion coefficient. • b – a constant, the advection velocity. • alpha and beta – constants, the values of u at the boundaries x = 0 and x = 1 respectively. • Nx – a constant, the number of spatial intervals in the domain. Your function should return an array of size Nx+1 representing your approximate solution u evaluated at each point of the spatial domain. You can use the functions developed in Python Workshop 3 for solving Poisson’s equation as a starting point. You should follow the following steps: • Use central difference approximations (of O(h2)) for both u′′(x) and u′(x). • Set up an appropriate tridiagonal matrix using scipy.sparse.diags(), with the csr format. • Implement the correct boundary conditions, by moving the contributions from the boundary conditions to the right-hand side of the matrix equation. • Solve your matrix equation using scipy.sparse.linalg.spsolve(). Mark scheme for part (a): Marks Description 10 The function works correctly and returns the outputs in the correct format. 6 The function either does not return the correct values due to a minor error, or returns the outputs in an incorrect format. Much correct progress has been made. 3 The function either does not return the correct values due to a major error or several minor errors. However, some correct progress has been made. 0 The code is, for want of a more polite term, gibberish. 4 (b) Run your function with parameter values a = 0.01, b = 1, alpha = 1, beta = 0 and Nx = 300. Plot your approximation of u(x) against x ∈ [0, 1]. Making sure to label each curve clearly, on the same figure, include the analytic solution (for these parameter values) u(x) = e b/a − ebx/a eb/a − 1 . [4 marks] Mark scheme for part (b): Marks are available for: • the correctness of the curves plotted, (2 marks) • the plot labelling. (2 marks) (c) For the parameter values given in part (b), what might you expect to see if we reduce the number of intervals in the x-direction to Nx = 40? Briefly explain your answer. [2 marks] We now wish to consider the time-dependent advection-diffusion equation given in (1). (d) Complete the function full_advection_1D_solver(a, b, alpha, beta, Nx, Nt, Tf) in python_cw2.py, to solve the time-dependent advection-diffusion equation using the Backward Euler method. [12 marks] The input parameters are: • a – a constant, the diffusion coefficient. • b – a constant, the advection velocity. 5 • alpha and beta – constants, the values of u at the boundaries x = 0 and x = 1 respectively. • Nx – the number of intervals for the x-domain. • Nt – the number of intervals for the t-domain. • Tf – the total time of the simulation. Your function should return an array of size (Nt+1, Nx+1) representing your approximate solution u evaluated at each point of the space-time domain. You can use heat_equation_backward_euler.py, available on Moodle as a starting point. You should follow the following steps: • use central difference approximations (of O(h2)) for both ∂ 2u ∂x2 and ∂u ∂x , • set up a tridiagonal matrix using scipy.sparse.diags(), with the csr format, • solve your matrix equation using scipy.sparse.linalg.spsolve() where your time-stepping gives the equation (I + µA)un+1 = un + f where un represents the vector of solutions at timepoint t = tn. • implement the boundary conditions. Contributions from the boundary conditions should be moved to the right-hand side of the equation, and can be accommodated by altering f in the time-stepping equation. Mark scheme for part (d): Marks Description 12 The function works correctly and returns the outputs in the correct format. 8 The function either does not return the correct values due to a minor error, or returns the outputs in an incorrect format. Much correct progress has been made. 4 The function either does not return the correct values due to a major error or several minor errors. However, some correct progress has been made. 0 The code is, for want of a more polite term, gibberish. 6 (e) Run the function you have written in part (d) with the same parameters as in part (b), and with Nt = 200 and Tf = 2. Complete the function plot_steady_and_time_dependent_solutions_at_given_timepoints(u_steady, u_time, plot_time_indices, Tf) in python_cw2.py. [8 marks] Your function should aim to create a single figure comparing the steady-state solution with the time-dependent solution at 6 different timepoints. Your function should take inputs: • u_steady – a 1D array of size Nx + 1, representing your approximate steady-state solution. • u_time – a 2D array of size (Nt+1, Nx+1), representing your approximate time-dependent solution. • plot_time_indices – a python list of length 6, giving the indices of the timepoints at which you wish to plot your solution. • Tf – the total time of the time-dependent run. Your function should return None using a blank return command. However, your function should also save a single figure to file using the matplotlib function savefig, as shown in the template Python file. The figure should include a plot of the solution u(x, t) against x at the 6 given time steps, along with the steady-state solution, u(x) from part (b). Your figure should be labelled clearly. Mark scheme for part (e): Marks are available for: • the correctness of the curves, (3 marks) • a sensible choice of time steps, (2 marks) • the plot labelling. (3 marks) (f) Briefly describe what your plot from part (e) shows. [3 marks] 7 At a given timepoint t, we can calculate the total mass in the system by integrating the concentration u(x, t) over the spatial domain M(t) = ∫ 1 0 u(x, t) dx . (4) We wish to study how the total mass changes over time. (g) Complete the function calculate_total_mass_at_fixed_time(c, h) in python_cw2.py, which calculates the total mass of the chemical in the system at a fixed timepoint. Integrate your approximation from part (e) for x ∈ [0, 1] using the composite Simpson’s rule. [5 marks] Your function should take as inputs • c – a 1D array of size Nx+1 representing an approximate concentration (varying over x) at a fixed timepoint, • h – the step size in the x-direction. Your function should output a single floating point number representing the integral of the input vector c over the x domain. Mark scheme for part (g): Marks Description 5 The function works correctly and returns the outputs in the correct format. 3 The function either does not return the correct values due to a minor error, or returns the outputs in an incorrect format. Much correct progress has been made. 1 The function either does not return the correct values due to a major error or several minor errors. However, some correct progress has been made. 0 The code is, for want of a more polite term, gibberish. 8 (h) Plot the total mass of the chemical in the system against time, using your approximation of u(x, t) from part (e). Label your plot appropriately. [3 marks] (i) Using your plot from part (h) as a reference, briefly describe how the total mass changes over time. Are there any changes in behaviour? If so, give an estimate for when the changes occur. [3 marks] Report queries about and suspected errors in this document to Cato Hastings (spring lecturer). 9
学霸联盟