> 科技
代写Diffusion Simulation 编程
2024-05-08  来源:互联网  


Background
Assignment: Temperature Diffusion Simulation Background Information:
This assignment explores temperature diffusion, a fundamental concept in thermodynamics where heat moves from higher to lower temperature regions. The simulation models how heat diffuses over time across a medium, either in one dimension (1D) or two dimensions (2D), using a nearest-neighbor averaging algorithm. Understanding this can help in environmental studies, materials science, and climatology.
A nearest-neighbor (NN) averaging algorithm takes the mean of a data point and its immediate neighbours (i.e. cells sharing a side with it, so not including diagonal neighbours). At the boundaries of a 2D table, or 1D vector, there are fewer values that are averaged due to missing neighbours. For example in the table
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
a NN average of the middle value 5.0 would be (5.0+2.0+6.0+8.0+4.0)/5 = 5.0, and the NN average of the top left corner 1.0 would be (1.0+2.0+4.0)/3=2.333.
Objective:
Develop Python programs that simulate and visualise temperature diffusion. You start with simple 1D diffusion and progress to more complex 2D and graph scenarios. You will need to handle boundary conditions properly to ensure the simulation does not attempt to access out-of-range data.
Questions for analysis (code)
A template file for the assignment code is provided here:In this file, there are several functions that you must implement. These functions only have a pass statement that you should replace with your own implementation. The provided functions exercise_1D_diffusion, exercise_2D_diffusion, exercise_2D_diffusion_numpy, and exercise_graph_diffusion can be used to call the associated implementation for testing and debugging purposes. You do not have to solve all the tasks, but you can only gain marks for the ones that you have attempted (see Marking criteria below for details on how we will mark your submission). Note that only task 6 should use numpy. Other tasks should use base python.
Note that the figures shown here are examples only to demonstrate the type of plot required. They should not be assumed to be correct solutions of questions in this assignment (and indeed may have been created with different parameters from the questions in this assignment).
Task 1: 1D temperature initial state
Implement create_array() function to create a 1D array of 10 elements with initial temperatures at 0°C in the interior and 1.0°C at boundary cells.
Task 2: 1D NN averaging algorithm
Implement simulate_1d_diffusion() function to apply a nearest-neighbor averaging algorithm to simulate temperature diffusion across the 1D array. Remember to handle edge cases correctly.
Task 3: 1D diffusion visualisation
Implement plot_temperatures() function to visualize and compare the temperature changes across the original and two iterations of NN averaging, using line plots (e.g. Figure 1).

Task 4: 2D temperature initial state
Implement create_grid() function to generate a 2D grid of 5x5 elements with initial temperatures at 0°C in the interior and 1.0°C at boundary cells.
Task 5: 2D temperature diffusion
Implement simulate_2d_diffusion() function to simulate one iteration of temperature diffusion in the 2D grid using nearest-neighbor averaging. Ensure correct handling of boundaries.
Task 6: vectorised 2D diffusion.
Implement simulate_large_scale() function, using NumPy, to optimize the diffusion simulation for a larger grid (i.e. 10x10). This function should compute the simulation for multiple iterations and visualise each step using a heatmap from matplotlib e.g. see Figure 2. You should show 5 heatmaps, including the initial state and 4 further iterations of the simulation. For COMP1730, any working solution is acceptable. For COMP6730, for full marks the solution needs to involve a vectorised expression with no nested loops (hint: look at the numpy pad function.)

Task 7: 2D diffusion on arbitrary graph.
Temperature diffusion in structured grids as seen in the previous questions is a standard model in environmental science, physics, and engineering. However, many real-world systems are better represented as arbitrary networks or graphs where nodes represent locations (like cities or habitats) and edges represent connections (like roads, rivers, or migration paths). This question explores temperature diffusion across such arbitrary graphs, simulating how heat (or other information) might spread in a network.
Graph Representation: The graph is represented using an adjacency list (edges), where each index corresponds to a node and its list contains connected nodes. Temperatures are stored in a separate list (temperatures), indexed correspondingly. You are provided with function that create_graph() that will create such a graph, where nodes represent different regions, each with an initial random temperature, and edges between nodes indicate that temperature can diffuse between these nodes. Study this function to understand how the two list store the graph and how the graph is created.
You are also provided with function visualize_graph that will display such a graph. An example plot is shown in Figure 3. You do not need to understand the implementation of this function.
You should implement the function simulate_diffusion() which has as parameters the two lists defining the graph and should simulate one iteration of temperature diffusion, calculated by NN average, on the graph. It returns the updated node temperatures.


Questions for the individual report
The template is a plain text file; write your answers where indicated in this file. Do not convert it to doc, docx, pdf or any other format.
The questions for you to answer in the report are:
Report question 1: Write your name and ANU ID.
Report question 2: Select a piece of code in your assignment solution that you have written, and explain:
(a) What does this piece of code do?
(b) How does it work?
(c) What other possible ways did you consider to implement this functionality, and why did you choose the one you did?
For this question, you should choose a piece of code of reasonable size and complexity. If your code has an appropriate level of functional decomposition, then a single function is likely to be a suitable choice, but it can also be a part of a function. It should not be something trivial (like a single line, or a simple loop).
For all parts of this question, it is important that your answers are at an appropriate level of detail. For part (a), describe the purpose of the code, including assumptions and restrictions. For parts (b) and (c), provide a high-level description of the algorithmic idea (and alternative ideas), not a line-by-line description of each statement.
There is no hard limit on how short or how long your answer can be, but an answer that is short and clear is always better than one that is long and confusing, if both of them convey the same essential information. As a rough guideline, an appropriate answer may be about 100 - 300 words for each of parts (a) - (c).

Restrictions on code
There are certain restrictions on your code:
You should NOT use any global variables or code outside of functions.
You can import modules that you find useful

请加QQ:99515681  邮箱:99515681@qq.com   WX:codinghelp