NAME

Chemistry::Harmonia - Decision of simple and difficult chemical puzzles.

SYNOPSIS

use Chemistry::Harmonia qw(:redox);
use Data::Dumper;

for my $formula ('Fe3O4', '[Cr(CO(NH2)2)6]4[Cr(CN)6]3'){
  my $ose = oxidation_state( $formula );
  print Dumper $ose;
}

my $chemical_equation = 'KMnO4 + NH3 --> N2 + MnO2 + KOH + H2O';
print Dumper parse_chem_mix( $chemical_equation );

Will print something like:

  $VAR1 = {
          'O' => {
                  'num' => [ 4 ],
                  'OS' => [ [ -2 ] ]
                 },
          'Fe' => {
                  'num' => [ 3 ],
                  'OS' => [ [ 2, 3, 3 ] ]
                 }
         };
  $VAR1 = {
          'H' => { 'num' => [ 96 ],
                   'OS' => [ [ 1 ] ]
                 },
          'O' => { 'num' => [ 24 ],
                   'OS' => [ [ -2 ] ]
                 },
          'N' => { 'num' => [ 48, 18 ],
                   'OS' => [ [ -3 ], [ -3 ] ]
                 },
          'C' => { 'num' => [ 24, 18 ],
                   'OS' => [ [ 4 ], [ 2 ] ]
                 },
          'Cr' => { 'num' => [ 4, 3 ],
                    'OS' => [ [ 3 ], [ 2 ] ]
                  }
        };
  $VAR1 = [
	    ['KMnO4', 'NH3'],
	    ['N2', 'MnO2', 'KOH','H2O']
	];

DESCRIPTION

The module provides the necessary subroutines to solve some puzzles of the general inorganic chemistry. The methods implemented in this module, are all oriented to known rules and laws of general chemistry.

SUBROUTINES

Chemistry::Harmonia provides these subroutines:

oxidation_state( $formula_of_substance )
parse_chem_mix( $mix_of_substances [, \%coefficients ] )

All of them are context-sensitive.

oxidation_state( $formula_of_substance )

This subroutine returns the hash reference of hash integer oxidation state (key 'OS') and hash with the number of atoms for each element (key 'num') for the inorganic $formula_of_substance.

Always use the upper case for the first character in the element name and the lower case for the second character from Periodic Table. Examples: Na, Ag, Co, Ba, C, O, N, F, etc. Compare: Co - cobalt and CO - carbon monoxide.

For very difficult mysterious formula (usually organic) returns undef.

parse_chem_mix( $mix_of_substances [, \%coefficients ] )

This subroutine parses $mix_of_substances (usually chemical equation) into arrays of the initial substances and products. A chemical equation consists of the chemical formulas of the reactants (the starting substances) and the chemical formula of the products (substances formed in the chemical reaction).

Separator of the reactants from products can be sequence '=', '-' and single '>'. For example: =, ==, =>, ==>, -, --, ->, -->, etc. Spaces round a separator are not essential. If the separator is not set, last substance of a mix will be a product only.

Each individual substance's chemical formula is separated from others by a plus sign ('+'), comma and/or space. Valid examples:

print Dumper parse_chem_mix( 'KNO3 + S K2SO4 , NO SO2' );

Will print something like:

$VAR1 = [
          [ 'KNO3','S','K2SO4','NO' ],
          [ 'SO2' ]
      ];

If in $mix_of_substances is stoichiometric coefficients they collect in hash reference \%coefficients. Attention: the reactants and products should be separated here. Next example:

my %coef;
my $chem_eq = 'BaS + 2 H2O = Ba(OH)2 + 1 Ba(SH)2';

my $out_ce = parse_chem_mix( $chem_eq, \%coef );
print Dumper( $out_ce, \%coef );

Will print something like:

$VAR1 = [ [ 'BaS','H2O'],
          [ 'Ba(OH)2', 'Ba(SH)2']
      ];
$VAR2 = {
        'Ba(SH)2' => '1',
        'H2O' => '2'
      };

EXPORT

Chemistry::Harmonia exports nothing by default. Each of the subroutines can be exported on demand, as in

use Chemistry::Harmonia qw( oxidation_state );

the tag redox exports the subroutines oxidation_state and parse_chem_mix:

use Chemistry::Harmonia qw(:redox);

and the tag all exports them all:

use Chemistry::Harmonia qw(:all);

DEPENDENCIES

Chemistry::Harmonia is known to run under perl 5.8.8 on Linux. The distribution uses Chemistry::File::Formula, Algorithm::Combinatorics and Inline::Files for the subroutine oxidation_state() and Carp.

SEE ALSO

Greenwood, Norman N.; Earnshaw, Alan. (1997), Chemistry of the Elements (2nd ed.), Oxford: Butterworth-Heinemann

Irving Langmuir. The arrangement of electrons in atoms and molecules. J. Am. Chem. Soc. 1919, 41, 868-934.

Chemistry-Elements, Chemistry::Mol, Chemistry::File and Chemistry::MolecularMass.

AUTHOR

Alessandro Gorohovski, <angel@feht.dgtu.donetsk.ua>

COPYRIGHT AND LICENSE

Copyright (C) 2011 by A. N. Gorohovski

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.