Click here to Skip to main content
15,883,835 members
Articles / Programming Languages / C#
Tip/Trick

Validation Dimensions of Physical Quantities .NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
1 Jul 2015GPL32 min read 10.3K   86   1  
This is the simple and fast validator for physical quantities and units. The validator can compare them using loaded physical expressions

Screenshot of the demonstration program

Introduction

This is the simple and fast validator for physical quantities and units. It was created as one class which includes all required fields and methods. The main task is the comparison of the physical quantities and a mathematical expression with units.

To parse line with math expression to the number, this validator uses MathParserTK namespace with class MathParser. You can find it here.

For example, if you input quantity 'F' (force) and expression ('kg*m/s^2') the comparator returns bool value — true.

Background

The idea was to create a simple validator for physical quantities and units in which you can in an easy way add new derived unts and expressions with quantities.

How It Works

To use physical expression, this program contains Dictionary BaseUnits. Value of an each base SI unit is the prime number. At first, the program loads derived units, calculates their values and saves them in an array. So there is a unique value for every unit (base and derived). Then program load expressions with physical quantities (e. g. F=N - force is measured in Newtons). Derived units and quantities with their unts are stored in Dictionaries too. To define some unit by expression program should only replace every unit with its value and calculate the resulting math expression.

How it works chart

Using the Code

At the beginning, you should create an object of PhisicalUnitsValidator. Then you should load derived units and expression with quantities and units from files. So everybody can expand the list of derived units and quantities by adding them to the corresponding files. Next you can use one of the available methods to compare physical quantity with units` / quantities` expression or to define unit by expression.

Example:

C#
public static void Main()
{  
    PhisicalUnitsValidator mainUnitsValidator = new PhisicalUnitsValidator();
    mainUnitsValidator.LoadDerivedUnitsFromFile("../../Resources/DerivedUnits.txt");
    mainUnitsValidator.LoadQuantitiesFromFile("../../Resources/QuantitiesUnits.txt");
    Console.WriteLine("Please, enter the units` expression:");
    string expression = Console.ReadLine(); // kg*m/s^2
    Console.WriteLine("Please, enter the quntity to compare:");
    string  quantity = Console.ReadLine(); // F
    Console.WriteLine(
        mainUnitsValidator.CompareQuantityWithUnitsExpression(quantity,expression)); // true
}   

Points of Interest

I use simple information about prime numbers and learned some new facts about physical quantities and their interaction.

History

  • 01/07/2015: Created the first version of the validator
  • 17/07/2015: Arrays of units and physical quantities are replaced with dictionaries; basic units rendered in a constant array; class ceased to be static

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --