no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


Previous revision
Next revision
tutorials:t23 [2019/06/25 15:20] – [Complementary files] pwarczok
Line 1: Line 1:
 +===== T23: Introduction to cell simulations: long-range diffusion =====
  
 +//This tutorial was tested on\\
 +MatCalc version 6.01 rel 1.003\\
 +license: free\\
 +database: mc_fe.tdb; mc_fe.ddb//
 +
 +==== Complementary files ====
 +
 +Click {{:tutorials:t23:script:t23_6021003.mcs|here}} to view the script for this tutorial.
 +
 +==== Contents ====
 +
 +  * setting up a simulation grid
 +  * assigning properties to cells
 +  * setting up display of cell simulation results
 +  * setting up simulation parameters
 +  * boundary conditions 
 +
 +MatCalc can be used to carry out simulations of long-range diffusion problems, and problems in which diffusion is coupled with precipitation, using a finite-element method with the system divided into a number of cells. Many examples of the application of this functionality, in different systems and with different boundary conditions, can be found in the [[examples:diffusion| relevant part of the 'Examples' section]]. The present tutorial demonstrates a simple case for which an analytical solution of Fick’s 2nd Law is available. The numerical and analytical solutions are compared.
 +
 +The functionality for setting up and running diffusion simulations is only available through the scripting language and not through the GUI. This tutorial therefore also serves as a refresher for some of the basic scripting commands that we have already encountered, as well as introducing the new commands needed to set up and run diffusion simulations.
 +
 +===== Description of the problem =====
 +
 +Fick's Second Law of Diffusion is a partial differential equation of the form:\\
 +
 +\[ \left({\partial C \over \partial t}\right) = D . \left({\partial^2 C \over \partial x^2}\right) \]
 +
 +where $D$ is the diffusion coefficient, which is here assumed to be independent of concentration. This equation can be solved to give the concentration $C$ of the diffusing species as a function of distance $x$ and time $t$. The form of the solution depends on the boundary conditions. In most cases, a numerical solution using finite-difference or finite-element methods is required, but there are some special cases in which an analytical solution is also available.  
 +
 +One of these is the thin-film solution, in which an very thin layer containing a finite amount of the diffusing species is sandwiched between two semi-infinite bars of material. The boundary conditions are that:
 +
 +  * initially, the concentration of the solute in the bar is zero; this is expressed mathematically as $ C\{x=0,t=0\}=0$ , where $x$ is the distance from the midpoint marked by the central black vertical line in the diagram below, and
 +
 +  * the total amount of solute is finite and constant throughout the time period considered; this is expressed mathematically as
 +
 +\[\int_{-\infty}^{\infty} C \{x,t\} \mathrm{d}x = B,\] where $B$ represents the amount of solute in the system.
 +
 +{{:tutorials:t23:img:t23_problem_schematic.png?500|}}
 +
 +The standard solution when the boundary conditions are of this type is of Gaussian form:
 +
 +\[ C\{x,t\} = {B  \over 2 \sqrt{\pi D t}} . \mathrm{exp}\left({-x^2  \over 4 D t}\right) \]
 +
 +In this tutorial, we will set up a cell-based numerical simulation of a situation that fulfils these conditions to a reasonably good approximation, i.e. a thin layer of FCC iron with a relatively high carbon content sandwiched between two bars of FCC iron with a very low carbon content. The results of this simulation will be compared with those obtained using the analytical solution. 
 +===== Setting up the system =====
 +
 +
 +Start MatCalc and create a new script file as explained in [[tutorials:t13|Tutorial 13]].
 +==== a. Thermodynamic and kinetic setup ====
 +
 +The first task that the script must perform is to create a new MatCalc workspace. Type the following command into your script window:
 +
 +<code>
 +use-module core
 +</code>
 +
 +This tells MatCalc to use the //core module//, which it selects at the begining by default anyway; later in this tutorial we will see the use of the //simulation module//.
 +
 +<code>
 +new-workspace 
 +</code>
 +
 +This creates a new workspace. If an existing unsaved workspace is open when the script is run, you will be prompted to save it. To disable this prompt, append ''f'' to the ''new-workspace'' command. 
 +
 +Some workspace information can be added as shown:
 +
 +<code>
 +@$************************************
 +$ enter workspace info
 +@$************************************
 +@ set-workspace-info Script T23
 +@ set-workspace-info +Simulation of long-range diffusion of C
 +@ set-workspace-info +out of thin film
 +@ set-workspace-info +with matrix phase FCC_A1.
 +</code>
 +
 +The next step is to set up the thermodynamic and kinetic data. Enter the following commands into your script window and save the script.
 +
 +<code>
 +open-thermodyn-database mc_fe.tdb
 +select-elements fe c
 +select-phases FCC_A1
 +read-thermodyn-database
 +read-mobility-database mc_fe.ddb
 +</code>
 +
 +Following this, define the variables that will specify the initial carbon content in the bar (''c_comp_bar_wp'') and the thin layer (''c_comp_layer_wp'') so that these can be used later. Note that the carbon content of the bar is set to be very low, but for numerical reasons it is not advisable to set this to exactly zero.
 +
 +<code>
 +set-variable-value c_comp_bar_wp 1e-10
 +set-variable-value c_comp_layer_wp 0.8
 +</code>
 +
 +In the cell simulation, compositions must be entered in molar notation ''un$c''. The code below enables you to enter a composition in weight percent (''wp'') and then save the composition in mole fraction as a variable for use in the cell simulation. 
 +
 +<code>
 +enter-composition wp c=c_comp_bar_wp
 +set-variable-value c_comp_bar un$c
 +enter-composition wp c=c_comp_layer_wp
 +set-variable-value c_comp_layer un$c
 +</code>
 +
 +Then set a temperature and calculate an equilibrium to initialise the system and check that everything is working OK.
 +
 +<code>
 +set-temperature-celsius 1000
 +calculate-equilibrium
 +</code>
 +
 +At this stage, also specify the simulation time (in seconds) for subsequent use:
 +
 +<code>
 +set-variable-value sim_time 1e7
 +</code>
 +
 +Save the script. It is recommended to test it out at this stage to catch any typing errors before going further.
 +
 +==== b. Precipitation domain setup ====
 +
 +Before setting up the cell simulation, you must define a //precipitation domain//, i.e. a matrix of phase FCC_A1; this is necessary for subsequent use in the cell simulation.
 +
 +<code>
 +create-precipitation-domain aus_domain
 +set-precipitation-parameter aus_domain X FCC_A1 
 +</code>
 +
 +
 +
 +==== c. Creating the simulation grid ====
 +
 +The next step is to set up the cell simulation. First change to the //simulation// module:
 +
 +<code>
 +use-module simulation
 +</code>
 +
 +Define a variable representing the number of cells:
 +
 +<code>
 +set-variable-value num_cells_x 99
 +</code>
 +
 +Then create a simulation grid using the command:
 +
 +<code>
 +create-simulation-grid num_cells_x 1 1 
 +</code>
 +
 +The three numbers here represent the number of cells in the x-, y- and z-directions, respectively. Define a variable representing the length of the bar:
 +
 +<code>
 +set-variable-value barlength 0.099
 +</code>
 +
 +Specify the dimensions in these directions (in m) using the command:
 +
 +<code>
 +set-grid-coordinates barlength 1 1 
 +</code>
 +
 +and the geometry of the grid, using the command: 
 +
 +<code>
 +set-grid-geometry p 
 +</code>
 +
 +The three geometry options are:
 +  * p - planar 
 +  * c - cylindrical
 +  * s - spherical 
 +
 +To visualise this grid, create a new window of the type g5 (Paint grid: 2D cells):
 +
 +<code>
 +new-gui-window g5
 +</code>
 +
 +Give this window a name so that it can be referred to easily later. The variable ''active_frame_id'' used here is one of MatCalc's internal variables; see  [[reference:internal|the section of the Reference book on internal variables]] for details.
 +
 +<code>
 +set-variable-value paint_window_id active_frame_id 
 +</code>
 +
 +Modify the position, size and zoom level using the following two commands:
 +
 +<code>
 +move-gui-window paint_window_id 0 100 750 160 $ change position and size
 +set-gui-window-property paint_window_id i Z 200 $ zoom 200%
 +</code>
 +
 +The grid should now look as shown:
 +
 +{{:tutorials:t23:img:t23_grid.png?500}}
 +
 +In the next section, properties will be assigned to the cells in the grid. 
 +
 +==== d. Assigning properties to the cells ====
 +
 +For the diffusion simulation, it is necessary to create a //material// and assign to this the //precipitation domain// called ''aus_domain'' that was created above: 
 +
 +<code>
 +create-material aus_material
 +set-material-property aus_material G D aus_domain 
 +$ [g]eneral properties: [d]omain
 +</code>
 +
 +Then set the diffusion coefficient. Here, a user-specified value is used, instead of one calculated using information from the database, to better facilitate comparison with the analytical solution. The diffusion coefficient is stored in a variable for later use. The syntax below means [d]iffusion [c]oefficient of [c]arbon, [f]unction or expression.
 +
 +<code>
 +set-variable-value diffCoeff 1e-12
 +set-material-property aus_material D C C F diffCoeff
 +$[d]iffusion [c]oefficient of [c]arbon, [f]unction or expression
 +</code>
 +
 +Following this, assign properties to the cells. Using the syntax below, the material ''aus_material'' is attached to all the cells.
 +
 +<code>
 +set-cell-property a m aus_material $[a]ll cells, [m]aterial
 +</code>
 +
 +Set the temperature of all cells to 1000˚C, by first specifying that the temperatures should be in ˚C:
 +
 +<code>
 +set-simulation-parameter N Y
 +</code>
 +
 +specifying the temperature in the form of a variable:
 +
 +<code>
 +set-variable-value sim_temp 1000
 +</code>
 +
 +and then attaching this temperature property to all cells:
 +
 +<code>
 +set-cell-property a V T sim_temp $ [a]ll cells, [v]ariable, [t]emperature
 +</code>
 +
 +Assign the lower carbon content, ''c_comp_bar'', to all cells. 
 +
 +<code>
 +set-cell-property a V C C c_comp_bar 
 +$ [a]ll cells, [v]ariable, [c]omposition [c]arbon
 +</code>
 +
 +The next stage is to select the cell in the middle of the bar using its cell index. First, we note that the first cell has the index 0, so the highest cell index in the array obtained using:
 +
 +<code>
 +set-variable-value max_cell_index num_cells_x-1
 +</code>
 +
 +and the index of the cell in the middle is given by:
 +
 +<code>
 +set-variable-value middle_cell_index max_cell_index/2
 +</code>
 +
 +Then select the cell in the centre by its c[e]ll index:
 +
 +<code>
 +add-cell-selection E middle_cell_index $ select the cell in the centre.
 +</code>
 +
 +Assign the higher carbon content, ''c_comp_layer'', to the selected cell (denoted by an asterisk).
 +
 +<code>
 +set-cell-property * V C C c_comp_layer 
 +</code>
 +
 +The g5 window shows, by default, the selected cell(s) in blue:
 +
 +{{:tutorials:t23:img:t23_grid_selection.png?500}}
 +
 + 
 +
 +
 +==== e. Display ====
 +
 +The next stage is to set up the graphical display to look at the results. Firstly, assign a property to the existing paint window created above, ''paint_window_id'' so that the cells are coloured according to their carbon content.
 +
 +<code>
 +set-gui-window-property paint_window_id I W V $ display cell variable
 +set-gui-window-property paint_window_id I V _cwp$c $ set which variable
 +set-gui-window-property paint_window_id I R 0..1
 +</code>
 +
 +In the first line in the code above, the syntax represents s[i]mulation results, display [w]hat, [v]ariable. In the second, the [v]ariable is specified as ''_cwp$c'', and in the third, the [r]ange is specified. The variable syntax ''_cwp$c'' represents the set of values of ''wp$c'' (weight percent of carbon) in the cells. Variables of this type can be found in the **variables** window under **cafe: cells**. 
 +
 +The cell array is shown schematically below, where blue represents a lower carbon content, and yellow a higher.
 +
 +{{:tutorials:t23:img:t23_grid_c_initial.png?500}}
 +
 +In addition, make a profile plot of the carbon content. Create a window of type **g1: plot grid: 1D profile**. 
 +
 +<code>
 +new-gui-window g1
 +</code>
 +
 +Assign a name to both the plot window and the plot itself:
 +
 +<code>
 +set-variable-value profile_window_id active_frame_id 
 +set-variable-value profile_plot_id last_plot_id 
 +</code>
 +
 +Add a [s]eries, [n]ew of the [s]imulation variable ''_cwp$c''
 +
 +<code>
 +set-plot-option profile_plot_id S N S _cwp$c
 +</code>
 +
 +Specify that the default x-axis should be used, and give it a title:
 +
 +<code>
 +set-gui-window-property profile_window_id S U Y 
 +set-gui-window-property profile_window_id S T Position [m]
 +</code>
 +
 +Label the y-axis and specify its range: 
 +
 +<code>
 +set-plot-option profile_plot_id A Y 1 T Carbon content [wt.%]
 +set-plot-option profile_plot_id A Y 1 S 0..c_comp_layer_wp 
 +</code>
 +
 +Give the plotted series the name 'Numerical':
 +
 +<code>
 +set-plot-option profile_plot_id S M 0 Numerical 
 +</code>
 +
 +Specify the range of cells over which the profile is to be taken. In the syntax here, the first line gives the index of the s[t]arting cell (here 0) and the second line gives the index of the st[o]p cell (here max_cell_index).
 +
 +<code>
 +set-gui-window-property profile_window_id I t 0
 +set-gui-window-property profile_window_id I o max_cell_index
 +</code>
 +
 +Give the window an appropriate size:
 +
 +<code>
 +move-gui-window profile_window_id 20 20 600 400
 +</code>
 +
 +The plot should look as shown:
 +
 +{{:tutorials:t23:img:t23_plot_c_initial.png}}
 +
 +We will now create a third window to display the time-dependent evolution of the carbon concentration in selected cells. The required window type is **g2: Plot grid: cell history**. Create the window and assign a name to the window and plot. 
 +
 +<code>
 +new-gui-window g2
 +set-variable-value history_window_id active_frame_id 
 +set-variable-value history_plot_id last_plot_id 
 +</code>
 +
 +Use the following commands to specify that the default x-axis should be used and to assign it a title and a range.
 +
 +<code>
 +set-gui-window-property history_window_id S U Y  $use default x-axis
 +set-gui-window-property history_window_id S T Time [s]
 +set-gui-window-property history_window_id S S 0..sim_time 
 +</code>
 +
 +Label the y-axis and specify its range:
 +
 +<code>
 +set-plot-option history_plot_id A Y 1 T Carbon content [wt.%]
 +set-plot-option history_plot_id A Y 1 S 0..c_comp_layer_wp 
 +</code>
 +
 +Now add two series, firstly the composition of the middle cell, and then that of the cell adjacent to it. :!:
 +
 +<code>
 +set-plot-option history_plot_id S N S _cwp$c{middle_cell_index}
 +set-plot-option history_plot_id S N S _cwp$c{middle_cell_index-1}
 +</code>
 +
 +Give these series titles. Note that the quotation marks are needed for a multi-word title, as in the case of the series 'next to centre' 
 +
 +<code>
 +set-plot-option history_plot_id S M 0 centre 
 +set-plot-option history_plot_id S M 1 'next to centre'  
 +</code>
 +
 +Finally, resize and move the history plot.
 +
 +<code>
 +move-gui-window history_window_id 800 20 600 400
 +</code>
 +
 +
 +==== f. Analytical solution ====
 +
 +In the final stage before setting up and running the simulation, the analytical solution will be added to the existing profile plot. Since the expression is rather complicated, it will be divided up into simpler terms:
 +
 +''Bval*term_1*term_2''
 +
 +where ''Bval'' represents the integral giving the amount of solute in the system:
 +
 +\[B = \int_{-\infty}^{\infty} C \{x,t\} \mathrm{d}x,\] 
 +
 +''term_1'' is the term 
 +
 +\[ {1  \over 2 \sqrt{\pi D t}} \]
 +
 +and ''term_2'' is the term
 +
 +\[ \mathrm{exp}\left({-x^2  \over 4 D t}\right) \]
 +
 +The value of the term $B$ can be estimated by assuming that the initial concentration of carbon in the bar is small enough to be neglected, such that initially all the carbon is in the layer in the middle of the bar. The value of $B$ is then simply the carbon concentration in the layer (in the units in which the curve is to be plotted, here weight percent,'' c_comp_layer_wp''), multiplied by the width of the layer. The layer is one cell wide, i.e. the bar length divided by the number of cells.
 +
 +<code>
 +set-variable-value Bval c_comp_layer_wp*(barlength/num_cells_x)
 +</code>
 +
 +The cell variables (prefixed with **''_c''**) that we have seen earlier in this tutorial can be included in variables and functions. For example, ''term_1'' uses the cell variable ''_ctime'' to represent the time in the cell simulation. The variable ''diffCoeff'' was defined above, and ''pi'' is a built-in variable. (For details of the built-in variables of MatCalc, consult [[reference:internal|the section of the Reference Book on internal variables]].)
 +
 +<code>
 +set-function-expression term_1 1/(2*sqrt(pi*diffCoeff*_ctime))
 +</code>
 +
 +It should be noted that the term //x// in the mathematical formulation of the analytical expression is measured from the centre of the range, whereas the cell variable corresponding to the x-position of the centre of the cells, ''_ccenter_x'', is measured from the left-hand side of the system. We must therefore create an expression for the shifted x-coordinate:
 +
 +<code>
 +set-variable-value midpt barlength/2
 +set-function-expression x_shifted _ccenter_x-midpt
 +set-function-expression term_2 exp((-1*x_shifted**2)/(4*diffCoeff*_ctime))
 +</code>
 +
 +The analytical expression is then given by:
 +
 +<code>
 +set-function-expression analytical_expr Bval*term_1*term_2
 +</code>
 +
 +and this series is plotted onto the existing profile plot using the command:
 +
 +<code>
 +set-plot-option profile_plot_id S N F analytical_expr 0..barlength
 +</code>
 +
 +The ''0..barlength'' expression in the command above represents the range over which the function is to be evaluated; this corresponds to the length of the bar.
 +
 +Note that when the analytical expression is first plotted, it looks slightly strange. This is because the time is initially zero, leading to a value of zero in the denominator of the first term, ''term_1''.
 +
 +==== g. Setting up and running the simulation ====
 +
 +The final set of commands deals with the simulation time and temperature, boundary conditions, type of simulation and update interval.
 +
 +<code>
 +set-simulation-parameter e sim_time $ end
 +</code>
 +
 +Specify the update interval:
 +
 +<code>
 +set-simulation-parameter u 10 $ update interval
 +</code>
 +
 +It is now necessary to tell the simulation module what type of problem should be simulated, i.e. how temperature and diffusion should be treated. In this case, we wish to simulate a **diffusion-field problem**, in which the elements are allowed to diffuse through the system:
 +
 +<code>
 +set-simulation-parameter d f $ diffusion-field simulation
 +</code>
 +
 +The temperature control, by contrast, is set to be isothermal and the hold temperature specified in terms of the variable ''sim_temp'' set earlier.
 +
 +<code>
 +set-simulation-parameter t i sim_temp $ temperature: isothermal
 +</code>
 +
 +The final step is to set up the default boundary conditions, i.e. the boundary conditions that apply in the absence of any other boundary conditions being set for specific cells or groups of cells. The type of boundary condition required is [g]eometric, and is set to [o]pen to environment. This means that there is no periodicity or symmetry.
 +
 +<code>
 +set-default-boundary-cond G O $ open to environment.
 +</code>
 +
 +Save the script.
 +
 +To run the script, click on **Script > Run Script**, press **Shift + F2** or click on the {{:tutorials:t13:img:icon_run_script.png|Run Script}} button. Then select the name of the script you wish to run from the drop-down menu and click on **Go**.
 +
 +When the script has been correctly executed, select **Simulation > Start simulation** or press **Ctrl + J** to start the simulation.  Output will appear in the Console, starting with: 
 +
 +<code>
 +#####################################################################
 +
 + starting microstructure simulation ...
 +
 +#####################################################################
 +</code>
 +
 +and finishing with:
 +
 +<code>
 +####################################################################################################
 +
 +
 +microstructure simulation finished in 59.07 secs (tot 59.07 secs) on Fri, 2016-06-10, 17:57:34 
 +
 +
 +####################################################################################################
 +</code>
 +
 +
 +===== Interpreting the results =====
 +
 +You will notice that, at first, the peak value of the analytical expression is much higher than the maximum value of 0.8 wt. % in the numerical simulation. The reason for this is that the analytical solution is based on the assumption that all the carbon is initially in an infinitesimally thin layer, whereas in the numerical solution, we have put the carbon into a cell of finite thickness. As the simulation proceeds, the two curves start to approach each other until they eventually almost exactly coincide. By default, the profile plot is set to show a 'stepped' profile in which the concentration is shown as discrete values corresponding to the single concentration value assigned to each cell. In the **options** window for this plot, select **no** under **use steps** to instead see a smooth curve that may enable easier comparison with the analytical curve.
 +
 +When the simulation is finished, you can examine the profiles at different times by looking at **Global > Buffers > Edit Buffer States**. You can obtain a set of profiles as shown below by loading up selected buffer states and using ‘Duplicate and Lock all Series’ on the plot right-click menu.  
 +
 +{{:tutorials:t23:img:t23_plot_c_series.png}}
 +
 +On the cell history plot, you should be able to see how the carbon concentration of the central cell decreases, while that of the adjacent cell decreases, until at longer exposure times, they converge.
 +
 +
 +{{:tutorials:t23:img:t23_symmetric_history.png}}
 +
 +
 +===== Subsequent articles =====
 +
 +Go to the [[:tutorials|MatCalc tutorial index]].
tutorials/t23.txt · Last modified: 2023/08/18 13:33 by pwarczok
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki