Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Choose an Element (Material Science)

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 May 2013CPOL3 min read 10.5K   361   3  
A DLL with a few classes and functions to provide data within Matlab on the elements from the periodic table.

Introduction

This DLL file will provide a few classes and functions to provide data from the periodic table. Included is a User Control aimed at providing a graphical way to choose a single element from the periodic table. In addition, the DLL exposes a collection of element objects for each of the elements in the periodic table.

Image 1 

Background

I am failing Material Science. I wrote this DLL to help me as I have gone into this subject without any prior knowledge of chemistry at all.

All the code snippets are written for Matlab, as this is the programming language we use at school but I have included in the source code the VB.NET project that I used to test the DLL file for those using Visual Basic.

I feel that I must repeat I am actually failing this subject please take this into consideration if you are to use this somewhere. The information within the DLL file should be considered actuate enough for school work only. God help me if I have to study Material Science 2. 

Using the code 

Before using the code the DLL file must be imported into Matlab. To this the file should be in the same directory as the script file you are writing. After this it is just a matter of adding the following code snippet to the your code. 

[Script_Folder, Script_Name, Script_Ext] = fileparts(which(mfilename('fullpath')));
Search_Path=[Script_Folder,'\','*.dll'];
File_List = dir(Search_Path);
for i=1:length(File_List)
    Next_Path=[Script_Folder,'\',File_List(i).name];
    NET.addAssembly(Next_Path);
end  

This code will search through all the files within the script’s folder and any files having the ‘dll’ extension will be imported and available to the script. If you have other DLL files in your scripts directory that you would not like to import then you would have to specify the directory implicitly.  

NET.addAssembly(Path_to_File); 

Replacing Path_to_File in the above statement with the path to where the DLL file is. This approach is a little messy if you are using your script on lots of computers though.

After this, it is easy to access the element object with the DLL file. If you use the following code, it will pop up a form on which you can choose an element from the periodic table.  

ElementChoice=Materials.Materials.ChooseElement; 

This will return an Element object into ElementChoice which you can then access its properties such as ElementChoice.Atomic_Number. These properties are as follows:

Atomic Number, Name, Symbol, Atomic Mass, Allotrope Names, Block, Group, Period, Series, Atomic Weight, Brinell Hardness, Bulk Modulus, Density, Mohs Hardness, Molar Volume, Poission Ratio, Shear Modulus, Sound Speed, Thermal Expansion, Vickers Hardness, Young’s Modulus, Boiling Point, Melting Point, Adiabatic Index, Critical Pressure, Critical Temperature, Curie Point, Fusion Heat, Neel Point, Phase, Specific Heat, Superconducting Point, Vaporization Heat, Colour, Electrical Conductivity, Electrical Type, Magnetic Type, Refractive Index, Electron Affinity, Electronegativity, Valence, Atomic Radius, Covalent Radius, Electron Configuration, Van der Waals Radius, Decay Mode, Halflife, Radioactive. 

There is also a collection of element available as well.  

MM = Materials.Materials;
PT = MM.Periodic_Table;
EL = PT.Elements;
for i=0:117
    ElementChoice = EL.Item(i);
end 

This code will loop through all the elements in the collection assigning each in turn to ElementChoice again. This is useful if you are testing for something in an equation and would like to see which element best fits.

Lastly there is another function to access the elements by search using either the name or symbol of the element. 

MM = Materials.Materials;
PT = MM.Periodic_Table;
EL = PT.Elements;
ElementChoice = EL.Find('He');
    
fprintf('Element Data for %s\n\n',char(ElementChoice.Name))
 
fprintf('\tAtomic_Number:  %1.0f\n',ElementChoice.Atomic_Number)
fprintf('\tName:  %s\n',char(ElementChoice.Name))
fprintf('\tSymbol:  %s\n',char(ElementChoice.Symbol))
fprintf('\tAtomic_Weight:  %s\n',char(ElementChoice.Atomic_Weight))

This code should find the element data for Helium and start to display some of the properties. 

Points of Interest

Currently all the numerical values are passed to Matlab as strings except for the atomic Number.

If you search for an element and don't find one it will cause an error in Matlab. It its recommend to use a try statement with this function. 

History 

  • Revision 1.0.0 - Initial release. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
I am a construction worker that has been put out to pasture as I am currently suffering carpenters disease. (Carpal Tunnel, the loss of feeling in the fingers) I have taken this as an opportunity and I am currently trying to bumble my way through a degree in civil engineering. Programming is a hobby I picked up when travelling away to work on remote sites.

Comments and Discussions

 
-- There are no messages in this forum --