# Monte Carlo Simulation(教程,scratchapixel,编程语言)

• TAG :

https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/monte-carlo-methods-in-practice/monte-carlo-simulation

the content of this chapter is exclusive and copyrighted. please do not copy and/or distribute without the permisson of Scathapixel. this free content requies hours of work and is totally unique on the web. if u found it useful, please consider making a donation.

simulating neutrons transport
as mentioned in the introduction of this lesson, Monte Carlo methods were initially developed by scientists such as von Neumann, Metropolis and Ulam who worked on atomic energy in the lates 40s. one of their motivations (or one application they found of MC methods, one might never know if necessity here, was the mother of the invention), was to simulate the transport of neutrons 中子 through various materials. the maths and physics involved in this research is out of scope and will not be dealt with in this lesson. however, because this is probably the first and most interesting application of a Method Carlo method, we will present a version (an ersatz 替代品) of their experiment but quite simplifed (not particularly accurate from a scientific point of view but still producing plausible results) and also without really explaining the origin of some of the equations involved. the same method can be used to simulate the propagation of light (photons) in tissue (skin) or smoke (for a complete introduction to light transport in participating medium, check the lessons on volume rendering and subsurface scattering in the advanced section).

background
the program we will be using to explain the concept of MC simulation is based on equations and a mathematical framework that we will not explain in this lesson. the equations relate to the way light (photons) or neutrons propagates through tissues and other materials. readers interested in this subject can find them explained in the lessons on the subsurface scattering and volume rendering (which are yet to be completed). 这个课程还没有更新 do not stress out if u do not understand the maths used in this chapter (however most of the equations will be fully explained as usual). this is not the point of this chapter (and they are also explained in the lessons aforementioned). the point of this chapter is to show u the principle of a Monte Carlo simulation or the use of stochastic 随机的 sampling to approximate the result of equations which are very hard or simply impossible to solve analytically. to understand the simulation though, u need to know a few basic things about the propagation of photons in tissues (the principles are the same for the interactions of neutrons with materials). this will eventually prepare u to read the lesson on volume rendering and subsurface scattering. 为啥啥做准备。

when a photon interacts with an object made of certain material (we assume this object has a certain thickness), three things can happen to this photon as it travels though. if the photon interacts with an atom making up that material, it can either be absorbed (the energy of the photon is passed on to the atom which is returned back by the atom to its environment in the form of heat), or it can be scattered (the direction along which it travels is being changed). there is always the eventuality that the photon does not interact with an atom at all, in which case it just keeps traveling in the same direction until it eventually either interacts with an atom or leave the object on the other side. the amount of light passing from one side of the object (or layer) to the other is called transmission. an illustration of these terms can be found in figure 1. Figure 1: the principle of simulating neutrons (or photons) transport is simple. Photons travelling through the medium, can “collide” with particles within the medium and be either absorbed or scattered by these particles. Eventually, photons which haven’t been absorbed might exit the medium from either side of the slab.

note though that when a photon is being scattered, its direction changes. if the photon is cattered in any random direction around the atom, we speak of isotropic scattering (the scattering is equal-iso means equal in greek- in all direction). however, sometimes, photons are being deflected within a given cone of directions centred around the photon incoming direction. in this case we speak of anisotropic scattering (the photon is not being scattered in all directions but within a prefered subset of directions centred about the photon incoming traveling direction). In the case of anisotropic scattering, the new direction of a scattered photon can be computed using what we call a phase or angular function (check the lesson on subsurface or volume rendering to find more information on this function). In this chapter, we will only deal with isotropic scattering mostly to avoid complications in the code (we will thus assume that the photon can be scattered in any direction around the atom it interacts with). Check figure 2 for a visual representation of these two scattering modes. Figure 2: istropic (in all directions) and anisotropic scattering.

while bouncing from atoms to atoms, photons do follow some sort of random walk, which eventually leads some of them to leave the object on the side of the slab where they entered the object in the first place. photons leaving the object on that side, make up what we call diffuse reflection or reflectance (the part of light that is not being transmitted to the other side, but which is being reflected). in literature, it is often designated as Rd. however not that these photons enter and leave the object on the same side but not at the same point on the surface (they leave some distance away, noted d in figure 3, from the point of illumination). in other words, light propagates传播. which is the reason why “translucent” materials such as skin illuminated by a laser (or a collimated平行 beam of light), do seem to scatter light away from the point of contact between the skin and the laser (figure 3).

0 Photons escaping the slab平板 on the other side though (the opposite side) contribute to what we call transmission (or transmittance透视), which is often denoted with the letter T (figure 3).

in physics, two quantities can be used to define the property of a material to absorb and scatter photons. they are simply called, the absorption and scattering coefficients. They are usually denoted by the greek letter σ (sigma): σa for the absorption coefficient and σs for the scattering coefficients. they measure, the likelihood of a photon to be absorbed or scattered per unit length of material (the unit length can be millimetre, centimetre 厘米, etc.). in other words, the absorption coefficient for instance, indicates how likely a photon is to be absorbed as it travels through a slab of material. note that these values can be greater than 1. they are note to be confused with probabilities even though the concept is very similar (rememeber that a probability can never be greater than 1). in physics, the rate at which the photon is being absorbed or scattered as it travels through a slab of material, is defined by a very famous law named the Beer-Lambert law. the equation for this law can take on different forms. in this lesson we will use the following formula: where, σt=σa+σs. this term σt (read “sigma t”) is called at extinction coefficient. u can see a plot of this equation in figure 4. if u are not familar with the exponential function (the e term), we recommond u to plot the curve for different values of the σt (u can use Grapher on Mac or GnuPlot of linux). the parameter x, is the distance that the photon travels in the slab. this equation can be seen as the amount of photons (or light) transmitted on the other side of the slab (which is why we use the term T in the equation above). as u can see by just looking at the graph, the greater the distance, the smaller the transmission, and the rate at which transimission decreases wtih distance, follows an exponential decay. the reason why transmission decreases with distance, is because photons get absorbed along their way. so of course, the greater the distance they have to travel to escape the surface on the other side, the more likely they are to be absorbed.

if we call xi the distance from the point of incidence and the point in the slab where the ith photon is either scattered or absorbed, u can see by looking at figure 5 that because each photon has its own “fate”, this distance varies from photon to photon: some photons are scattered a short distance away from the point where they entered the slab, while some others travel a much larger distance before interacting with an atom. in fact this distance which is known as the free path length, is…, random. it should not come to a surprise to see this concept coming up again, since u should have realised by now, that the concept of random variable is central to this lesson and the concept of Monte Carlo methods. the probability that a photon will be absorbed or scattered (as a function of distance) 这是计算概率的, can be computed with the following equation: x是距离 figure 5: the distance at which a photon is absorbed or scattered is random.

again, it is an exponential function and we know what this function looks like (figure 4). it does not matter if u do not understand where these equations come from. u will find them explained in the lesson on subsurface scattering.

However ,you should already be familiar though with the concept of probability and probability density function which we already talked about in lesson 16 (in probability theory, a probability density function or pdf, is a function that describes the relative likelihood for this random variable to take on a given value). The equation above is an example of such pdf.

there is something interesting to note about this function: the probability decreases (it actually converges to 0) as the distance x increases. It might seem counter-intuitive at first, because u could expect this probability to actually increase with x instead. indeed, if the photon is not scattered or absorbed after some distance x, then we are almost sure that it will eventually be scattered or absorbed at some point further down along its path, if it keeps travelling through the slab. thus naturally, u might be inclined to think that the probability increases with distance. but reality, the opposite is true. why? if the probability of a photon to be scattered or absorbed converges to 0 for values of x greater than x0, the equation just tells us that the photon is more likely to be scattered or absorbed for distance smaller than x0. for instance, if u look at figure 6, the probability of a photon to experience a scattering event is greater at distance x1 than it is at distance x2. in other words, u are almost certain that the photon is more likely to be scattered or absorbed for disances smaller than x0 thus the probability itself for a photon to be scattered for distance greater than x0 becomes very small. it is an important point to understand and remember.

Finally, let’s mention that the probability of a photon to be absorbed is σa/σt and its probability to be scattered is σs/σt.

principle of the experiment: let us simulate!

what if we could use a computer to simulate the path of a photon traveling through a slab for material? the idea is pretty simple. all we need to do is create a virutal photon, and initially place it at the surface of the slab and simulate its random walk which can be done by executing the following decision tree: 决策树

1/ step1: compute the new position of the photon. move along some distance away from the current position x in the direction v.
2/ step2: is the photon still travelling through the slab? this can be easily tested by computing the position of the photon and compare it to the boundaries of the slab.
2.1/ if the photon is outside the slab boundaries, stop the simulation.
2.2/ if the photon is still travelling through the slab, continue.
3/ step3: at this stage, the photon can either be absorbed or scattered. compute the photon probability to either be absorbed or scattered (using the absorption and scattering coefficient).
3.1/ if u the photon is absorbed, stop the simulation (the photon disappeared).
3.2/ if u the photon is scattered, update the direction along which the photon travels(v) and go back to step 1.
Here is a visual representation of that tree: if we repeat this experiment thousands of times, eventually, at the end, we get an estimation of how much light is reflected or transmitted by this slab of material. the main difficulty with the process described above though, is to determine the distance by which we should be moving the photon over, at each iteration of the loop. technically, this distance which we know is called the free path length, should be chosen randomly however what should it be? short, long, one time short, the next time long? surely, the choice of distance, however random, must be related somehow to the properties of the material themselves, notably its scattering and absorption coefficient (which we talked about already). indeed, we can guess that if the absorption and the scattering coefficients for example are very low, then the likelihood of a photon to be absorbed or reflected is also low. thus distances between scattering events should be long in that case. thus distances between scattering events should be long in that case. of course the opposite is true. when these coefficients have high values, the probability of a photon to interact with an atom is high, so u should expect distances between scattering events to be short. this likelihood is actually defined by the equation for the PDF we introduced earlier on; which says that the probability that the photon interacts with an atom decreases exponentially with the distance x (where “how quickly this probability decreases is controlled by the extinction coefficient 消亡系数 which is the sum of the absorption and scattering coefficient”). 而消亡系数是吸收和散射系数的和。