chemistry_paradise 1.3.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of chemistry_paradise might be problematic. Click here for more details.

Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +522 -0
  3. data/bin/chemistry_paradise +7 -0
  4. data/bin/wetter +7 -0
  5. data/chemistry_paradise.gemspec +46 -0
  6. data/doc/README.gen +518 -0
  7. data/doc/bugs/BUGS.md +16 -0
  8. data/doc/formulas/biochemical_calculations.md +5 -0
  9. data/doc/todo/todo_for_the_chemistry_paradise_project.md +33 -0
  10. data/doc/todo/todo_for_the_wetter_project.md +7 -0
  11. data/lib/chemistry_paradise/base/base.rb +215 -0
  12. data/lib/chemistry_paradise/base/colours.rb +93 -0
  13. data/lib/chemistry_paradise/commandline/help.rb +40 -0
  14. data/lib/chemistry_paradise/commandline/menu.rb +88 -0
  15. data/lib/chemistry_paradise/commandline/parse_commandline.rb +94 -0
  16. data/lib/chemistry_paradise/constants/constants.rb +77 -0
  17. data/lib/chemistry_paradise/constants/file_constants.rb +33 -0
  18. data/lib/chemistry_paradise/constants/german_names_of_elements_to_element_symbol.rb +157 -0
  19. data/lib/chemistry_paradise/converters/celsius_to_fahrenheit.rb +143 -0
  20. data/lib/chemistry_paradise/converters/celsius_to_kelvin.rb +125 -0
  21. data/lib/chemistry_paradise/converters/fahrenheit_to_celsius.rb +132 -0
  22. data/lib/chemistry_paradise/converters/shared.rb +21 -0
  23. data/lib/chemistry_paradise/gui/gtk3/calculate_molecular_weight/calculate_molecular_weight.rb +34 -0
  24. data/lib/chemistry_paradise/gui/gtk3/show_periodic_table/show_periodic_table.rb +34 -0
  25. data/lib/chemistry_paradise/gui/gtk3/temperature_converter/temperature_converter.rb +112 -0
  26. data/lib/chemistry_paradise/gui/gtk3/wetter/wetter.rb +119 -0
  27. data/lib/chemistry_paradise/gui/libui/temperature_converter/temperature_converter.rb +100 -0
  28. data/lib/chemistry_paradise/gui/libui/wetter/wetter.rb +47 -0
  29. data/lib/chemistry_paradise/gui/shared_code/calculate_molecular_weight/calculate_molecular_weight_module.rb +171 -0
  30. data/lib/chemistry_paradise/gui/shared_code/show_periodic_table/show_periodic_table_module.rb +318 -0
  31. data/lib/chemistry_paradise/gui/shared_code/temperature_converter/temperature_converter_module.rb +192 -0
  32. data/lib/chemistry_paradise/gui/shared_code/wetter/wetter_module.rb +349 -0
  33. data/lib/chemistry_paradise/gui/swing/TemperatureConverter$1.class +0 -0
  34. data/lib/chemistry_paradise/gui/swing/TemperatureConverter.class +0 -0
  35. data/lib/chemistry_paradise/gui/swing/TemperatureConverter.java +133 -0
  36. data/lib/chemistry_paradise/gui/unified_widgets/wetter/wetter.rb +62 -0
  37. data/lib/chemistry_paradise/images/show_periodic_table.png +0 -0
  38. data/lib/chemistry_paradise/images/vienna_map.png +0 -0
  39. data/lib/chemistry_paradise/interactive_chemistry_shell.rb +246 -0
  40. data/lib/chemistry_paradise/project/project.rb +24 -0
  41. data/lib/chemistry_paradise/requires/common_external_requires.rb +17 -0
  42. data/lib/chemistry_paradise/requires/require_the_project.rb +21 -0
  43. data/lib/chemistry_paradise/requires/require_the_project_including_the_graphical_user_interface.rb +7 -0
  44. data/lib/chemistry_paradise/requires/require_the_project_including_the_web_interface.rb +4 -0
  45. data/lib/chemistry_paradise/shared.rb +130 -0
  46. data/lib/chemistry_paradise/show_electron_configuration.rb +250 -0
  47. data/lib/chemistry_paradise/show_element.rb +145 -0
  48. data/lib/chemistry_paradise/sinatra/sinatra.rb +131 -0
  49. data/lib/chemistry_paradise/sinatra/wetter/app.rb +68 -0
  50. data/lib/chemistry_paradise/sinatra/wetter/start_sinatra_interface.rb +31 -0
  51. data/lib/chemistry_paradise/split_molecule_names.rb +228 -0
  52. data/lib/chemistry_paradise/toplevel_methods/atomgewichte.rb +31 -0
  53. data/lib/chemistry_paradise/toplevel_methods/convert_parens.rb +64 -0
  54. data/lib/chemistry_paradise/toplevel_methods/display_where_the_molmasses_are_kept.rb +24 -0
  55. data/lib/chemistry_paradise/toplevel_methods/e.rb +16 -0
  56. data/lib/chemistry_paradise/toplevel_methods/kelvin.rb +34 -0
  57. data/lib/chemistry_paradise/toplevel_methods/language.rb +50 -0
  58. data/lib/chemistry_paradise/toplevel_methods/misc.rb +147 -0
  59. data/lib/chemistry_paradise/toplevel_methods/periodic_table.rb +16 -0
  60. data/lib/chemistry_paradise/toplevel_methods/remove_this_molecule_from.rb +63 -0
  61. data/lib/chemistry_paradise/toplevel_methods/roebe.rb +16 -0
  62. data/lib/chemistry_paradise/toplevel_methods/show_electron_negativity_chart.rb +26 -0
  63. data/lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb +559 -0
  64. data/lib/chemistry_paradise/utility_scripts/combustion_analysis.rb +207 -0
  65. data/lib/chemistry_paradise/utility_scripts/electron_negativity_chart.rb +78 -0
  66. data/lib/chemistry_paradise/utility_scripts/equalize_chemical_formula.rb +84 -0
  67. data/lib/chemistry_paradise/utility_scripts/equation_solver.rb +130 -0
  68. data/lib/chemistry_paradise/utility_scripts/orbitals.rb +70 -0
  69. data/lib/chemistry_paradise/utility_scripts/show_electron_negativity_of_this_element.rb +103 -0
  70. data/lib/chemistry_paradise/verbose_chemical_calculation.rb +21 -0
  71. data/lib/chemistry_paradise/version/version.rb +19 -0
  72. data/lib/chemistry_paradise/wetter/wetter.rb +539 -0
  73. data/lib/chemistry_paradise/www/wetter/embeddable_interface.rb +78 -0
  74. data/lib/chemistry_paradise/www/wetter/wetter.cgi +28 -0
  75. data/lib/chemistry_paradise/yaml/atomgewichte.yml +113 -0
  76. data/lib/chemistry_paradise/yaml/colours_for_the_elements.yml +13 -0
  77. data/lib/chemistry_paradise/yaml/dichte.yml +21 -0
  78. data/lib/chemistry_paradise/yaml/electron_negativity_chart.yml +111 -0
  79. data/lib/chemistry_paradise/yaml/molecular_formula_of_different_molecules.yml +13 -0
  80. data/lib/chemistry_paradise/yaml/periodic_table_of_the_elements.yml +125 -0
  81. data/lib/chemistry_paradise.rb +1 -0
  82. data/test/testing_chemistry_paradise.rb +49 -0
  83. metadata +173 -0
@@ -0,0 +1,228 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === ChemistryParadise::SplitMoleculeNames
6
+ #
7
+ # Input to this class here can be something like 'C12H11O11'. See the
8
+ # bottom of this file to find out what can be done.
9
+ #
10
+ # Do not input the charge of the molecule in question.
11
+ # =========================================================================== #
12
+ # require 'chemistry_paradise/split_molecule_names.rb'
13
+ # =========================================================================== #
14
+ require 'chemistry_paradise/base/base.rb'
15
+
16
+ module ChemistryParadise
17
+
18
+ class SplitMoleculeNames < ::ChemistryParadise::Base
19
+
20
+ # ========================================================================= #
21
+ # === initialize
22
+ # ========================================================================= #
23
+ def initialize(
24
+ i = 'C12H11O11'
25
+ )
26
+ reset
27
+ set_input(i)
28
+ split # This is the actual run method.
29
+ end
30
+
31
+ # ========================================================================= #
32
+ # === reset (reset tag)
33
+ # ========================================================================= #
34
+ def reset
35
+ super()
36
+ # ======================================================================= #
37
+ # === @result
38
+ # ======================================================================= #
39
+ @result = []
40
+ # ======================================================================= #
41
+ # === @total
42
+ # ======================================================================= #
43
+ @total = Hash.new(0) # Hash that keeps track of n elements each. Default value will be 0.
44
+ end
45
+
46
+ # ========================================================================= #
47
+ # === result?
48
+ # ========================================================================= #
49
+ def result?
50
+ @result
51
+ end; alias result result? # === result
52
+
53
+ # ========================================================================= #
54
+ # === is_number?
55
+ #
56
+ # This returns true if the input is a number, else false. For this it
57
+ # uses the .to_i method trick, which returns 0 for non-numbers.
58
+ # ========================================================================= #
59
+ def is_number?(i)
60
+ result = (i.to_i.to_s == i)
61
+ return result
62
+ end
63
+
64
+ # ========================================================================= #
65
+ # === split
66
+ #
67
+ # This method will return an array with all the elements.
68
+ # ========================================================================= #
69
+ def split(
70
+ i = @input
71
+ )
72
+ array = []
73
+ _ = ''.dup
74
+ i = i.first if i.is_a? Array
75
+ if i
76
+ chars = i.chars
77
+ chars.each_with_index {|token, index|
78
+ if is_number? token # We found a number here. We simply append it then.
79
+ _ << token # return the old data
80
+ elsif token.downcase == token
81
+ _ << token
82
+ else # Not a number, must be a character.
83
+ unless _.empty?
84
+ array << _
85
+ _ = ''.dup
86
+ end
87
+ _ = token
88
+ end
89
+ array << _ if (index + 1) == i.size
90
+ }
91
+ @result = array
92
+ determine_total
93
+ return array
94
+ end
95
+ end
96
+
97
+ # ========================================================================= #
98
+ # === ignore_numbers
99
+ #
100
+ # This will ignore numbers in a guaranteed manner.
101
+ # ========================================================================= #
102
+ def ignore_numbers(i)
103
+ return i.gsub(/\d+/,'')
104
+ end
105
+
106
+ # ========================================================================= #
107
+ # === return_n_elements
108
+ #
109
+ # Must return an Integer.
110
+ # ========================================================================= #
111
+ def return_n_elements(i)
112
+ result = 0
113
+ if i =~ /\d+/ # Ok has at least one number.
114
+ i = i[/(\d+)/][0]
115
+ else
116
+ i = 1
117
+ end
118
+ result = i.to_i
119
+ return result
120
+ end
121
+
122
+ # ========================================================================= #
123
+ # === determine_total
124
+ #
125
+ # If the input is @result then it was already properly pre-sorted for us.
126
+ # ========================================================================= #
127
+ def determine_total(i = @result)
128
+ i.each {|entry|
129
+ this_real_key = ignore_numbers(entry)
130
+ if @total.has_key? this_real_key
131
+ @total[this_real_key] = @total[this_real_key]+return_n_elements(entry)
132
+ else # Else we must make a new key; the Laufindex kann aber 1-n sein.
133
+ @total[this_real_key] = return_n_elements(entry)
134
+ end
135
+ }
136
+ end
137
+
138
+ # ========================================================================= #
139
+ # === total?
140
+ # ========================================================================= #
141
+ def total?
142
+ @total # This must always be a Hash.
143
+ end
144
+
145
+ # ========================================================================= #
146
+ # === set_input
147
+ # ========================================================================= #
148
+ def set_input(i)
149
+ if i
150
+ i = i.join(' ').strip if i.is_a? Array
151
+ i = i.dup if i.frozen?
152
+ if i.include?(' ') and (i =~ /^\d+/) # starts with a number.
153
+ # Input is e. g. "5 H2O"
154
+ splitted = i.split(' ')
155
+ n_times = splitted.first.to_i
156
+ new_result = ''.dup
157
+ splitted.last.chars.each {|this_char|
158
+ if this_char =~ /\d+/
159
+ this_char = this_char.to_i * n_times
160
+ end
161
+ new_result << this_char.to_s
162
+ }
163
+ unless new_result[-1, 1] =~ /\d+/
164
+ new_result << n_times.to_s
165
+ end
166
+ i = new_result
167
+ end
168
+ i.tr!('₁', '1') if i.include? '₁'
169
+ i.tr!('₂', '2') if i.include? '₂'
170
+ i.tr!('₃', '3') if i.include? '₃'
171
+ i.tr!('₄', '4') if i.include? '₄'
172
+ i.tr!('₅', '5') if i.include? '₅'
173
+ i.tr!('₆', '6') if i.include? '₆'
174
+ i.tr!('₇', '7') if i.include? '₇'
175
+ i.tr!('₈', '8') if i.include? '₈'
176
+ i.tr!('₉', '9') if i.include? '₉'
177
+ i = convert_parens(i) if i.include? ')'
178
+ if i.include? ' '
179
+ i.strip!
180
+ if i.include? '+' # Ok, input here can be like: '2 Fe + 3 Cl2'
181
+ i.gsub!(/(\d)+ /,'\1')
182
+ i.gsub!(/ \+ /,'')
183
+ end
184
+ # ===================================================================== #
185
+ # But it can also include internal ' ', which we will remove next.
186
+ # ===================================================================== #
187
+ i.delete!(' ')
188
+ end
189
+ end
190
+ @input = i
191
+ end
192
+
193
+ end
194
+
195
+ # =========================================================================== #
196
+ # === ChemistryParadise.split_this_molecular_formula_into_a_hash
197
+ # =========================================================================== #
198
+ def self.split_this_molecular_formula_into_a_hash(i)
199
+ i = i.first if i.is_a? Array
200
+ SplitMoleculeNames.new(i).result
201
+ end
202
+
203
+ end
204
+
205
+ if __FILE__ == $PROGRAM_NAME
206
+ include ChemistryParadise
207
+ alias e puts
208
+ puts 'Testing whether this class can properly Split Compound Names:'
209
+ if ARGV.empty?
210
+ pp SplitMoleculeNames.new.result
211
+ e
212
+ _ = 'P2O5'; e 'Input is: '+_; print ' '; pp SplitMoleculeNames.new(_).result
213
+ _ = 'C6H14N4O2'; e 'Input is: '+_; print ' '; pp SplitMoleculeNames.new(_).result
214
+ _ = 'Al2(SO4)3'; e 'Input is: '+_; print ' '; pp SplitMoleculeNames.new(_).result
215
+ _ = 'Fe(OH)3'; e 'Input is: '+_; print ' '; pp SplitMoleculeNames.new(_).result
216
+ _ = 'H2SO4'; e 'Input is: '+_; print ' '; pp SplitMoleculeNames.new(_).result
217
+ _ = '(NH₄)₂SO₄'; e 'Input is: '+_; print ' '; pp SplitMoleculeNames.new(_).result
218
+ _ = 'Fe2(SO4)3'; e 'Input is: '+_; print ' '; pp SplitMoleculeNames.new(_).result
219
+ _ = 'P2O5'; e 'Input is: '+_; print ' '; pp SplitMoleculeNames.new(_).result
220
+ _ = '5 H2O'; e 'Input is: '+_; print ' '; pp SplitMoleculeNames.new(_).result
221
+ e
222
+ pp SplitMoleculeNames.new('Fe2(SO4)3').total?
223
+ p 'Will test this input next: 2 Fe + 3 Cl2'
224
+ pp SplitMoleculeNames.new('2 Fe + 3 Cl2').result
225
+ else
226
+ SplitMoleculeNames.new(ARGV).result
227
+ end
228
+ end # $CHEMISTRY/split_molecule_names.rb
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'chemistry_paradise/toplevel_methods/atomgewichte.rb'
6
+ # =========================================================================== #
7
+ require 'chemistry_paradise/constants/file_constants.rb'
8
+
9
+ module ChemistryParadise
10
+
11
+ # ========================================================================= #
12
+ # === ChemistryParadise.file_atomgewichte
13
+ # ========================================================================= #
14
+ def self.file_atomgewichte
15
+ Constants::FILE_ATOMGEWICHTE
16
+ end
17
+
18
+ # ========================================================================= #
19
+ # === ChemistryParadise.atomgewichte?
20
+ # ========================================================================= #
21
+ def self.atomgewichte?
22
+ require 'yaml'
23
+ YAML.load_file(Constants::FILE_ATOMGEWICHTE)
24
+ end
25
+
26
+ end
27
+
28
+ if __FILE__ == $PROGRAM_NAME
29
+ puts ChemistryParadise.file_atomgewichte
30
+ pp ChemistryParadise.atomgewichte?
31
+ end
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'chemistry_paradise/toplevel_methods/convert_parens.rb'
6
+ # =========================================================================== #
7
+ module ChemistryParadise
8
+
9
+ # ========================================================================= #
10
+ # === ChemistryParadise.convert_parens
11
+ #
12
+ # This method will properly convert the () parens that can be found
13
+ # in a chemical formula.
14
+ #
15
+ # For instance:
16
+ #
17
+ # Al2(SO4)3
18
+ #
19
+ # Is effectively the same such as:
20
+ #
21
+ # Al2SO4SO4SO4
22
+ #
23
+ # ========================================================================= #
24
+ def self.convert_parens(i)
25
+ if i.is_a? Array
26
+ i.each {|entry| convert_parens(entry) }
27
+ else
28
+ i = i.dup if i.frozen?
29
+ i.tr!('₁', '1') if i.include? '₁'
30
+ i.tr!('₂', '2') if i.include? '₂'
31
+ i.tr!('₃', '3') if i.include? '₃'
32
+ i.tr!('₄', '4') if i.include? '₄'
33
+ i.tr!('₅', '5') if i.include? '₅'
34
+ i.tr!('₆', '6') if i.include? '₆'
35
+ i.tr!('₇', '7') if i.include? '₇'
36
+ i.tr!('₈', '8') if i.include? '₈'
37
+ i.tr!('₉', '9') if i.include? '₉'
38
+ if i.include? '('
39
+ n_parens = i.count('(')
40
+ if n_parens > 1 # Ok, this may be a really complex formula, like:
41
+ # Fe(OH)3 + H2SO4 -> Fe2(SO4)3 + H2O
42
+ splitted = i.split(' ')
43
+ splitted = splitted.map {|entry| convert_parens(entry) } # For now we remove some things.
44
+ return splitted.join(' ')
45
+ else
46
+ regex = /\((.+)\)(\d)([A-Za-z]{0,3}\d{0,3})/ # See: https://rubular.com/r/clShxRBIpzEtlF
47
+ i =~ regex
48
+ i = i[0, i.index('(')]
49
+ i << $1 * $2.to_i # Which group * n repetition
50
+ i << $3.dup if $3
51
+ end
52
+ end
53
+ return i
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+ if __FILE__ == $PROGRAM_NAME
60
+ alias e puts
61
+ e ChemistryParadise.convert_parens('Al2(SO4)3')
62
+ e ChemistryParadise.convert_parens('(NH4)2SO4')
63
+ e ChemistryParadise.convert_parens('(NH₄)₂SO₄')
64
+ end
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'chemistry_paradise/toplevel_methods/display_where_the_molmasses_are_kept.rb'
6
+ # =========================================================================== #
7
+ require 'chemistry_paradise/toplevel_methods/e.rb'
8
+ require 'chemistry_paradise/requires/common_external_requires.rb'
9
+ require 'chemistry_paradise/constants/file_constants.rb'
10
+
11
+ module ChemistryParadise
12
+
13
+ # ========================================================================= #
14
+ # === ChemistryParadise.display_where_the_molmasses_are_kept
15
+ # ========================================================================= #
16
+ def self.display_where_the_molmasses_are_kept
17
+ Colours.e(Constants::FILE_ATOMGEWICHTE)
18
+ end
19
+
20
+ end
21
+
22
+ if __FILE__ == $PROGRAM_NAME
23
+ ChemistryParadise.display_where_the_molmasses_are_kept
24
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'chemistry_paradise/toplevel_methods/e.rb'
6
+ # =========================================================================== #
7
+ module ChemistryParadise
8
+
9
+ # ========================================================================= #
10
+ # === ChemistryParadise.e
11
+ # ========================================================================= #
12
+ def self.e(i = '')
13
+ puts i
14
+ end
15
+
16
+ end
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'chemistry_paradise/toplevel_methods/kelvin.rb'
6
+ # =========================================================================== #
7
+ module ChemistryParadise
8
+
9
+ # ========================================================================= #
10
+ # === ChemistryParadise.kelvin
11
+ #
12
+ # This method will output how many Kelvin n Celsius are.
13
+ #
14
+ # The temperature T in Kelvin (K) is equal to the temperature T in
15
+ # degrees Celsius (°C) plus 273.1.
16
+ #
17
+ # The formula thus is:
18
+ #
19
+ # T(K) = T(°C) + 273.15
20
+ #
21
+ # ========================================================================= #
22
+ def self.kelvin(n_celsius)
23
+ if n_celsius.is_a? Array
24
+ n_celsius = n_celsius.first
25
+ end
26
+ n_kelvin = n_celsius.to_f + 273.15
27
+ return n_kelvin
28
+ end; self.instance_eval { alias celsius_to_kelvin kelvin } # === ChemistryParadise.celsius_to_kelvin
29
+
30
+ end
31
+
32
+ if __FILE__ == $PROGRAM_NAME
33
+ puts ChemistryParadise.kelvin(ARGV)
34
+ end # kelvin 37
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # This class can help set a different language for the project. By default,
6
+ # the german language is used, but you can call the module-method below,
7
+ # called ChemistryParadise.use_english, to enable english instead.
8
+ # =========================================================================== #
9
+ # require 'chemistry_paradise/toplevel_methods/language.rb'
10
+ # =========================================================================== #
11
+ module ChemistryParadise
12
+
13
+ # ========================================================================= #
14
+ # === @use_this_language
15
+ # ========================================================================= #
16
+ @use_this_language = :german
17
+
18
+ # ========================================================================= #
19
+ # === ChemistryParadise.set_use_this_language
20
+ # ========================================================================= #
21
+ def self.set_use_this_language(i = :german)
22
+ @use_this_language = i
23
+ end
24
+
25
+ # ========================================================================= #
26
+ # === ChemistryParadise.use_which_language?
27
+ # ========================================================================= #
28
+ def self.use_which_language?
29
+ @use_this_language
30
+ end
31
+
32
+ # ========================================================================= #
33
+ # === ChemistryParadise.do_use_english
34
+ #
35
+ # Use this method if you want to use the english language.
36
+ # ========================================================================= #
37
+ def self.do_use_english
38
+ set_use_this_language(:english)
39
+ end; self.instance_eval { alias use_english do_use_english } # === ChemistryParadise.use_english
40
+
41
+ # ========================================================================= #
42
+ # === ChemistryParadise.do_use_german
43
+ #
44
+ # Use this method if you want to use the german language.
45
+ # ========================================================================= #
46
+ def self.do_use_german
47
+ set_use_this_language(:german)
48
+ end; self.instance_eval { alias use_german do_use_german } # === ChemistryParadise.use_german
49
+
50
+ end
@@ -0,0 +1,147 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'chemistry_paradise/toplevel_methods/misc.rb'
6
+ # =========================================================================== #
7
+ module ChemistryParadise
8
+
9
+ # ========================================================================= #
10
+ # === ChemistryParadise.sanitize
11
+ #
12
+ # Usage example:
13
+ #
14
+ # ChemistryParadise.sanitize('C6H13N3O3') # => "C₆H₁₃N₃O₃"
15
+ #
16
+ # ========================================================================= #
17
+ def self.sanitize(i)
18
+ if i.is_a? Array
19
+ i = i.first
20
+ end
21
+ i = i.dup
22
+ i.gsub!(/1/, '₁')
23
+ i.gsub!(/2/, '₂')
24
+ i.gsub!(/3/, '₃')
25
+ i.gsub!(/4/, '₄')
26
+ i.gsub!(/5/, '₅')
27
+ i.gsub!(/6/, '₆')
28
+ i.gsub!(/7/, '₇')
29
+ i.gsub!(/8/, '₈')
30
+ i.gsub!(/9/, '₉')
31
+ i.gsub!(/0/, '₀')
32
+ return i
33
+ end
34
+
35
+ # ========================================================================= #
36
+ # === ChemistryParadise.calculate_atomic_mass_based_on_isotope_percentage
37
+ #
38
+ # This method accepts as many arguments as the user wants to.
39
+ #
40
+ # It has to follow a specific format. The first entry must be
41
+ # the "Häufigkeit" and the second entry must be the corresponding
42
+ # atomic mass ("relative_atommasse").
43
+ #
44
+ # Usage example:
45
+ #
46
+ # ChemistryParadise.calculate_atomic_mass_based_on_isotope_percentage(78.99, 23.985, 10.00, 24.985, 11.01, 25.983)
47
+ #
48
+ # ========================================================================= #
49
+ def self.calculate_atomic_mass_based_on_isotope_percentage(*array)
50
+ round_to_n_positions = 3
51
+ result = 0
52
+ array.map! {|entry| entry.to_s.tr(',','.').to_f }
53
+ entries = array.each_slice(2)
54
+ entries.each {|häufigkeit, relative_atommasse|
55
+ if häufigkeit > 0
56
+ häufigkeit = häufigkeit / 100.0
57
+ end
58
+ zwischenergebnis = relative_atommasse * häufigkeit
59
+ result += zwischenergebnis
60
+ }
61
+ return result.round(round_to_n_positions)
62
+ end; self.instance_eval { alias berechne_atommasse calculate_atomic_mass_based_on_isotope_percentage } # === ChemistryParadise.berechne_atommasse
63
+
64
+ # ========================================================================= #
65
+ # === ChemistryParadise.torr_to_bar
66
+ #
67
+ # This method can be used to convert n Torr into n bar.
68
+ #
69
+ # The first argument is "n Torr".
70
+ #
71
+ # The result will be in "n bar".
72
+ # ========================================================================= #
73
+ def self.torr_to_bar(i = 720)
74
+ if i.is_a? Array
75
+ i = i.first
76
+ end
77
+ result = i.to_f * (1.013 / 760)
78
+ rounded_result = result.round(3)
79
+ beautified_result = '%.3f' % rounded_result
80
+ return beautified_result
81
+ end
82
+
83
+ # ========================================================================= #
84
+ # === ChemistryParadise.log
85
+ #
86
+ # Calculate the log of something.
87
+ # ========================================================================= #
88
+ def self.log(of)
89
+ return Math.log10(of)
90
+ end
91
+
92
+ require 'chemistry_paradise/constants/file_constants.rb'
93
+ require 'chemistry_paradise/split_molecule_names.rb'
94
+ # ========================================================================= #
95
+ # === ChemistryParadise.logempirische_formel
96
+ #
97
+ # This variant is german.
98
+ #
99
+ # We will assume 100 g as input.
100
+ # ========================================================================= #
101
+ def self.empirische_formel(
102
+ i = 'KClO4', # 'H2O'
103
+ round_to_n_decimal_positions = 2
104
+ )
105
+ i = i.to_s.strip.dup
106
+ require 'yaml'
107
+ dataset = YAML.load_file(Constants::FILE_ATOMGEWICHTE)
108
+ # ======================================================================= #
109
+ # So, 100 g H2O here.
110
+ # ======================================================================= #
111
+ array = SplitMoleculeNames.new(i).result
112
+ hash = {} # Store the mol here.
113
+ result = {}
114
+ # ======================================================================= #
115
+ # array may look like this: ["H2", "O"]
116
+ # ======================================================================= #
117
+ array.each {|entry|
118
+ n_times = 1
119
+ if entry =~ /\d+/ # At the least one number.
120
+ n_times = entry.scan(/\d+/).flatten.first.to_i
121
+ # =================================================================== #
122
+ # Now remove the number.
123
+ # =================================================================== #
124
+ entry.gsub!(/\d+/,'')
125
+ end
126
+ key = dataset[entry]
127
+ molmass = key * n_times
128
+ hash[entry] = molmass
129
+ }
130
+ total_mass = hash.values.sum
131
+ hash.each_pair {|key, value|
132
+ new_value = (100.0 / total_mass) * value
133
+ if round_to_n_decimal_positions
134
+ new_value = new_value.round(round_to_n_decimal_positions)
135
+ end
136
+ result[key] = new_value
137
+ }
138
+ return result
139
+ end
140
+
141
+ end
142
+
143
+ if __FILE__ == $PROGRAM_NAME
144
+ alias e puts
145
+ pp ChemistryParadise.empirische_formel
146
+ e ChemistryParadise.torr_to_bar(720).to_s+' bar'
147
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'chemistry_paradise/toplevel_methods/periodic_table.rb'
6
+ # =========================================================================== #
7
+ module ChemistryParadise
8
+
9
+ # ========================================================================= #
10
+ # === ChemistryParadise.periodic_table?
11
+ # ========================================================================= #
12
+ def self.periodic_table?
13
+ '/Users/x/DATA/SCIENCE/YAML/periodic_table.yml'
14
+ end
15
+
16
+ end
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'chemistry_paradise/toplevel_methods/remove_this_molecule_from.rb'
6
+ # =========================================================================== #
7
+ require 'chemistry_paradise/split_molecule_names.rb'
8
+
9
+ module ChemistryParadise
10
+
11
+ # ========================================================================= #
12
+ # === ChemistryParadise.remove_this_molecule_from
13
+ #
14
+ # The first argument is the molecule that should be deducted from
15
+ # the formula.
16
+ # ========================================================================= #
17
+ def self.remove_this_molecule_from(
18
+ this_molecule = 'OH',
19
+ from = 'C2H5NO2'
20
+ )
21
+ array_big_molecule = ::ChemistryParadise.split_this_molecular_formula_into_a_hash(from)
22
+ array_this_molecule = ::ChemistryParadise.split_this_molecular_formula_into_a_hash(this_molecule)
23
+ # ======================================================================= #
24
+ # Next, iterate over the small molecule.
25
+ # ======================================================================= #
26
+ array_this_molecule.each {|this_compound|
27
+ unless this_compound =~ /\d+/
28
+ this_compound << '1' # Must be at the least one.
29
+ end
30
+ # ===================================================================== #
31
+ # Select the entry that is the correct one.
32
+ # ===================================================================== #
33
+ this_entry = array_big_molecule.find {|entry|
34
+ entry[0,1] == this_compound[0,1]
35
+ }
36
+ begin
37
+ n_times_in_the_big_molecule = /(\d{1,3})/.match(this_entry)[1].to_i
38
+ n_times_in_the_small_molecule = /(\d{1,3})/.match(this_compound)[1].to_i
39
+ new_n_times = n_times_in_the_big_molecule - n_times_in_the_small_molecule
40
+ new_entry = this_compound[0,1]+new_n_times.to_s
41
+ # ===================================================================== #
42
+ # Now we have the new entry; we need to re-insert it into the big
43
+ # original Array.
44
+ # ===================================================================== #
45
+ proper_index = array_big_molecule.index {|entry|
46
+ entry[0,1] == this_compound[0,1]
47
+ }
48
+ array_big_molecule[proper_index] = new_entry
49
+ rescue NoMethodError => error
50
+ pp error
51
+ end
52
+ }
53
+ return array_big_molecule.join # Join it up again.
54
+ end
55
+
56
+ end
57
+
58
+ if __FILE__ == $PROGRAM_NAME
59
+ puts 'Deducting OH from C2H5NO2 next:'
60
+ puts ::ChemistryParadise.remove_this_molecule_from('OH', 'C2H5NO2')
61
+ puts 'Deducting H2O from C2H5NO2 next:'
62
+ puts ::ChemistryParadise.remove_this_molecule_from('H2O', 'C2H5NO2')
63
+ end