~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to src/solver/chemset.ml

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(***************************************************************************
 
2
 *   Copyright (C) 2004 by Thomas Nagy                                     *
 
3
 *   tnagy2^8@yahoo.fr                                                     *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 
19
 ***************************************************************************)
 
20
 
 
21
open Printf;;
 
22
open Hashtbl;;
 
23
open List;;
 
24
open String;;
 
25
 
 
26
type chemtbl = (string, int) Hashtbl.t;;
 
27
type chemrecord = {mutable hashtbl:chemtbl; mutable formula:string};;
 
28
type item = {ikey:string; itbl:chemrecord; mutable sign:int};;
 
29
type listitems = (item) list;;
 
30
 
 
31
 
 
32
(* add a symbol to a molecule *)
 
33
let chem_addsym (tbl:chemtbl) (sym:string) (qte:int) = 
 
34
    let prev_qte = ref 0 in
 
35
    if Hashtbl.mem tbl sym then prev_qte := Hashtbl.find tbl sym;
 
36
    Hashtbl.replace tbl sym (!prev_qte+qte)
 
37
;;
 
38
 
 
39
(* add merge two sub_molecules *)
 
40
let chem_add (tbl1:chemrecord) (tbl2:chemrecord) = 
 
41
    Hashtbl.iter (fun sym qte -> chem_addsym tbl1.hashtbl sym qte) tbl2.hashtbl;
 
42
    tbl1.formula <- tbl1.formula^tbl2.formula;
 
43
    tbl1
 
44
;;
 
45
 
 
46
(* multiply a sub-molecule (amount of atoms) by an integer value *)
 
47
let chem_mult (tbl:chemrecord) (qte:int) = 
 
48
    Hashtbl.iter (fun sym old_qte-> Hashtbl.replace tbl.hashtbl sym (old_qte*qte) ) tbl.hashtbl;
 
49
    tbl.formula <- "("^tbl.formula^")"^string_of_int(qte);
 
50
    tbl
 
51
;;
 
52
 
 
53
(* creates a small molecule *)
 
54
let createchem (sym:string) (qte:int) =
 
55
 
 
56
    let prettyformula () =
 
57
        if String.contains sym '+' || String.contains sym '-' then begin
 
58
            if qte == 1 then "<b><sup>"^sym^"</sup></b>"
 
59
                else "<b><sup>"^string_of_int(qte)^sym^"</sup></b>" end 
 
60
        else begin
 
61
            if qte == 1 then sym
 
62
            else sym^"<b><sub>"^string_of_int(qte)^"</sub></b>"
 
63
        end
 
64
    in
 
65
    
 
66
    let table = Hashtbl.create 10 in
 
67
    Hashtbl.add table sym qte;
 
68
    { hashtbl=table ; formula=prettyformula() }
 
69
    (*if (qte!=1) then { hashtbl=table ; formula=prettyformula() }
 
70
    else { hashtbl=table ; formula=sym }*)
 
71
;;
 
72
 
 
73
let chem_negate (l:listitems) =
 
74
    List.iter (fun i -> i.sign <- -1) l
 
75
;;
 
76
 
 
77
(* outputs a molecule *)
 
78
let chem_printitem (i:item) =
 
79
    Printf.printf "item : %s %s %d \n" i.ikey (i.itbl).formula i.sign;
 
80
    Hashtbl.iter (fun sym qte -> Printf.printf " * %s %d\n" sym qte) i.itbl.hashtbl
 
81
;;
 
82