Build a Python Library for Material Science

Arunkumar N
Variablz Academy
Published in
5 min readAug 2, 2022

In this AI-driven world, technology has been changing on a day-to-day basis. From Aluminium frame shuttle racquets to highly flexible carbon fiber racquets. From heavy metals to lightweight composites in automobile and space industries. Everything is upgrading at a fast pace. So we must adapt to learn technical skills and apply them in our core field.

In this article, we focus on how we can develop a simple python library for Hall-Petch relation in the material science domain.

Python Library for Material Science: (Credits to: Aatomz Research)

Grain and Grain boundary

If we break a metal, we can see grains where atoms are arranged periodically. The collection of grains forms a solid. Each grain is separated by a grain boundary. The properties such as strength and hardness of a material can be influenced by the amount of grain and grain boundaries.

This image showcases the grain and grain boundaries
Credits to: https://material-properties.org

Hall-Petch Relation:

The relationship between yield strength and grain size is given by

σy = σ∞ + k/√D where,

σy is the yield strength

σ∞ is a constant (i.e.) the yield stress for infinitely large grain size

k is a constant (i.e.) the locking parameter is the resistance of grain boundaries in stopping the dislocation

From this relationship, we can conclude that for smaller grain sizes, the yield strength will be maximum.

Python Module (Library):

Compilation of different functions or operations for a highly reusable purpose is a module. For example, a calculator does addition, division, and many processes. Here calculator is a module that contains all functions.

Like a toolkit, medikit whenever there is a need, we take and use it. In the same way, we can import the module whenever it is needed and utilize it.

Now let’s look at some essential formulas and how we can apply those in creating a module using python.

1) Calculating grains per square mm = N

We can calculate the grain count by using the formula,

N = (2(n-1)) × (100/M)2

where n is ASTM grain size number

M is Magnification

It is nothing but a module for doing mathematical operations. Here we are importing that module since we are doing calculations.

Here in this code, we define (def) what we will do(calculate_graincount). This is known as defining a Function. Inside the parenthesis(n, M) are Parameters.

The sentence inside the triple quotation is Docstring, where helpful information can be given.

The corresponding grain count formula is coded and returned the grain count so that we can use it further anywhere; otherwise, we can use it only once.

After running the code, we can call the function above and enter the desired values. Here the values(6,1) is Argument. we can get the output after running the called function.

2) Calculating average grain diameter = D

We can calculate average grain diameter by D = 1/√N, where N is the of grains per square mm.

Here math.sqrt won’t work if the math library is not imported.

3) Calculating locking parameter = k

It can be calculated from the graph by finding the slope when yield stress(σy) is plotted against D-1/2. It is the contribution of grain boundary toughness.

slope_k = (y2-y1)/(x2-x1)

4) Calculating yield strength = σy

The stress at which plastic deformation occurs when a material loses its elasticity is the Yield strength. It is vital in any engineering and design application in the manufacturing and construction sectors. It can be calculated by using the Hall-Petch relation,

σy = σ∞ + k/√D

After creating a module, we have to download it as a python file(.py) and save it.

Permanently adding the module.

We must add the created module permanently in the anaconda environment so we can import the library as we need.

Importing Module

Now we can import the created module and use it.

I hope this article helped you to gain some knowledge about creating a module in python for the benefit of the core engineering purpose where mathematical calculations are essential.

Awake the genius within you. See you in the upcoming article.

Cheers!

Follow me on LinkedIn.

--

--