Class: ChemistryParadise::CalculateAtomicMass

Inherits:
Base
  • Object
show all
Defined in:
lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb

Overview

ChemistryParadise::CalculateAtomicMass

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
ARRAY_HELP_OPTIONS =
#

ARRAY_HELP_OPTIONS

#
%w(
  HELP help --help --h -h
)
ATOMGEWICHTE =
YAML.load_file(File.expand_path('~')+File.basename(_))
DEFAULT_INPUT =
#

ChemistryParadise::CalculateAtomicMass::DEFAULT_INPUT

#
'P2O5'

Constants inherited from Base

Base::FILE_MOLECULAR_FORMULA_OF_DIFFERENT_MOLECULES

Constants included from Shared

Shared::ARRAY_TEST_THESE_MOLECULES

Constants included from Constants

ChemistryParadise::Constants::ELECTRON_NEGATIVITY_CHART, ChemistryParadise::Constants::FILE_ATOMGEWICHTE, ChemistryParadise::Constants::FILE_ELECTRON_NEGATIVITY_CHART, ChemistryParadise::Constants::FILE_PERIODIC_TABLE_OF_THE_ELEMENTS, ChemistryParadise::Constants::N, ChemistryParadise::Constants::PLANK_CONSTANT, ChemistryParadise::Constants::PROPER_FILLORDER, ChemistryParadise::Constants::SPEED_OF_LIGHT

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#be_quiet, #be_verbose?, #cd, #cliner, #commandline_arguments?, #do_use_the_english_language, #do_use_the_german_language, #do_we_use_english?, #esystem, #first_argument?, #gold, #grey, #initialize_the_internal_hash, #internal_hash?, #is_on_roebe?, #mediumpurple, #namespace?, #olivedrab, #rev, #royalblue, #set_be_verbose, #set_commandline_arguments, #sfancy, #steelblue, #teal, #tomato, #use_which_language?, #yellow

Methods included from Shared

#convert_parens, #is_number?, #periodic_table?, periodic_table?, #return_range_for_this_period, #square

Methods included from Constants

#electron_negativity_chart?

Constructor Details

#initialize(optional_input = ARGV, run_already = true) ⇒ CalculateAtomicMass

#

initialize

#


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 50

def initialize(
    optional_input = ARGV,
    run_already    = true
  )
  reset
  set_input(optional_input)
  case run_already.to_s
  when 'do_not_report'
    @report_result = false
    run_already = true
  end
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :do_not_exit
    # ===================================================================== #
    when :do_not_exit
      @may_we_exit = false
    else
      if yielded.is_a? Hash
        _ = yielded
        if _.has_key? :verbosity
          @report_result = _[:verbosity]
        end
        if _.has_key? :may_we_exit
          @may_we_exit =_[:may_we_exit]
        end
      end
    end
  end
  run if run_already
end

Class Method Details

.[](i) ⇒ Object

#

ChemistryParadise::CalculateAtomicMass[]

#


498
499
500
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 498

def self.[](i)
  parse(i)
end

.parse(i) ⇒ Object

#

ChemistryParadise::CalculateAtomicMass.parse

#


505
506
507
508
509
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 505

def self.parse(i)
  _ = new(i, false)
  _.calculate
  return _.nice_result
end

Instance Method Details

#atomgewichte?Boolean

#

atomgewichte?

#

Returns:

  • (Boolean)


222
223
224
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 222

def atomgewichte?
  ATOMGEWICHTE
end

#bold_red(i) ⇒ Object

#

bold_red

#


251
252
253
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 251

def bold_red(i)
  swarn(i)
end

#calculate_compound(i) ⇒ Object Also known as: ba, summenformel, atomgewicht, molmasse, molare_masse, calculcate_molecular_weight

#

calculate_compound

This method will attempt to calculate the given input.

The input to this method will usually be something like “Pb3”.

#


200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 200

def calculate_compound(i)
  result = 0
  if i =~ /\d+/ # if input has a number
    splitted = i.split(/(\d+)/)
  else # else assume that the number is one.
    splitted = [ i, 1 ]
  end
  if atomgewichte?.has_key? splitted[0]
    result = splitted[1].to_i * ATOMGEWICHTE[splitted[0]]
  end
  gather_individual_components(i, result)
  return result
end

#calculate_result(i = input? ) ⇒ Object Also known as: calculate

#

calculate_result

Use this method to calculate the result (the molecular mass).

Keep in mind that the input may be somewhat complex, such as “(NH₄)₂SO₄”. This has to be expanded first.

#


473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 473

def calculate_result(
    i = input?
  )
  mass_number = 0 # This will become the result eventually.
  i.squeeze!(' ')
  # ======================================================================= #
  # Let class SplitMoleculeNames split the components next.
  # ======================================================================= #
  splitted = SplitMoleculeNames.new(i).result
  splitted = sanitize_input_for_element_names(splitted)
  splitted.each {|entry|
    add_this_amount = calculate_compound(entry)
    if @show_the_steps
      e "#{rev}Now adding #{royalblue(add_this_amount)} for the "\
        "compound #{steelblue(entry)}."
    end
    mass_number += add_this_amount
  }
  set_result(mass_number)
  determine_output_string
end

#consider_modifying_the_output_stringObject

#

consider_modifying_the_output_string

#


377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 377

def consider_modifying_the_output_string
  if @show_more # This can modify the output string.
    _ = ''.dup
    Hash[@individual_components].each_pair {|key, value|
      key = key.scan(/./).map {|entry|
        # if entry =~ /^\d+/ # Starts with a number.
        #   entry << 'x'
        # end # Disabled as of July 2021. May be re-enabled at a later time.
        entry
      }
      key = key.join
      _ << key+': '+value.round(7).to_s+' | '
    }
    @output_string = @output_string.dup if @output_string.frozen?
    @output_string << "#{N}#{bold_red('  (The individual components were: ').dup}#{rev}"
    _.strip!
    splitted = _.split('|').map {|entry|
      Colours.kde_colour_palette_plasma_blue(entry)
    }
    joined = splitted.join(teal('|'))
    @output_string << joined+
                      bold_red(')')
  end
end

#determine_output_stringObject

#

determine_output_string

This method will determine the String that will be shown to the user on the commandline.

#


353
354
355
356
357
358
359
360
361
362
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 353

def determine_output_string
  _ = nice_result
  if do_we_use_english?
    @output_string = "#{rev}The molecular mass of #{sfancy(@input)} is "\
                     "#{simp(_+' u (g / mol)')}."
  else # then we probably use german
    @output_string = "#{rev}Die molekulare Masse von #{sfancy(@input)} "\
                     "beträgt #{simp(_+' u (g / mol)')}."
  end
end

#do_show_detailsObject

#

do_show_details

#


336
337
338
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 336

def do_show_details
  @show_more = true
end

#do_show_the_stepsObject

#

do_show_the_steps

#


405
406
407
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 405

def do_show_the_steps
  @show_the_steps = true
end

#exit_the_program(may_we_exit = @may_we_exit) ⇒ Object

#

exit_the_program

#


455
456
457
458
459
460
461
462
463
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 455

def exit_the_program(
    may_we_exit = @may_we_exit
  )
  case may_we_exit
  when :then_exit
    may_we_exit = true
  end
  exit if may_we_exit
end

#gather_individual_components(name, weight) ⇒ Object

#

gather_individual_components

#


243
244
245
246
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 243

def gather_individual_components(name, weight)
  array = [name, weight]
  @individual_components << array
end

#input?Boolean

#

input?

#

Returns:

  • (Boolean)


166
167
168
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 166

def input?
  @input
end

#is_included?(i = input?) ) ⇒ Boolean Also known as: include?

#

is_included?

#

Returns:

  • (Boolean)


265
266
267
268
269
270
271
272
273
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 265

def is_included?(i = input?)
  i = i.dup
  # ======================================================================= #
  # First remove all numbers from the input:
  # ======================================================================= #
  i.delete!('0-9')   
  i = i[0,2] if i.size > 2 # Take only the first two characters, in the event that we have more than two characters in the supplied input.
  atomgewichte?.has_key?(i)
end
#

menu (menu tag)

The @commandline_arguments will be checked against the internal menu.

#


295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 295

def menu(i = @commandline_arguments)
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i # case tag
    # ===================================================================== #
    # === molmasse C5H6N2O2 --english
    # ===================================================================== #
    when /^-?-?english$/
      do_use_the_english_language
    # ===================================================================== #
    # === molmasse C5H6N2O2 --show-steps
    # ===================================================================== #
    when /^-?-?show(-|_)?steps$/
      do_show_the_steps
    # ===================================================================== #
    # === molmasse C5H6N2O2 --show-details
    # ===================================================================== #
    when /^-?-?show(-|_)?details?$/,
         '--detail'
      do_show_details
    # ===================================================================== #
    # === molmasse --help
    # ===================================================================== #
    when *ARRAY_HELP_OPTIONS
      show_help
      exit_the_program
    # ===================================================================== #
    # === molmasse SUM
    # ===================================================================== #
    when 'SUM',
         'SHOW',
         'MORE'
      do_show_details
    end
  end
end

#nice_resultObject

#

nice_result

#


258
259
260
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 258

def nice_result
  @result.to_f.round(3).to_s
end

#opnnObject

#

opnn

#


343
344
345
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 343

def opnn
  super(NAMESPACE)
end

#report_resultObject

#

report_result

#


236
237
238
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 236

def report_result
  e @output_string
end

#report_result?Boolean

#

report_result?

#

Returns:

  • (Boolean)


278
279
280
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 278

def report_result?
  @report_result
end

#report_that_this_element_does_not_exist(i, may_we_exit = @may_we_exit) ⇒ Object

#

report_that_this_element_does_not_exist

#


445
446
447
448
449
450
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 445

def report_that_this_element_does_not_exist(
    i, may_we_exit = @may_we_exit
  )
  opnn; e 'The given element at `'+sfancy(i.to_s)+'` does not exist.'
  exit_the_program(may_we_exit)
end

#resetObject

#

reset

#


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 90

def reset
  super()
  # ======================================================================= #
  # === @commandline_arguments
  # ======================================================================= #
  @commandline_arguments = []
  # ======================================================================= #
  # === @show_more
  # ======================================================================= #
  @show_more = true
  # ======================================================================= #
  # === @show_the_steps
  #
  # If the following variable is set to true then we will display,
  # on the commandline, what is done when. This gives the user more
  # information about what is going on, and it may also help during
  # debugging this class/project.
  #
  # Note that @show_the_steps is a bit different to @show_more, so
  # these two variables are presently kept separate.
  # ======================================================================= #
  @show_the_steps = false
  # ======================================================================= #
  # === @individual_components
  # ======================================================================= #
  @individual_components = []
  # ======================================================================= #
  # === @output_string
  # ======================================================================= #
  @output_string = ''.dup
  # ======================================================================= #
  # === @report_result
  # ======================================================================= #
  @report_result = true
  # ======================================================================= #
  # === @may_we_exit
  # ======================================================================= #
  @may_we_exit = true
  # ======================================================================= #
  # === @molecular_formula_of_different_molecules
  # ======================================================================= #
  @molecular_formula_of_different_molecules = YAML.load_file(
    FILE_MOLECULAR_FORMULA_OF_DIFFERENT_MOLECULES
  )
  set_input
end

#result?Boolean Also known as: result, masse?

#

result?

#

Returns:

  • (Boolean)


285
286
287
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 285

def result?
  @result
end

#runObject

#

run (run tag)

#


429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 429

def run
  check_against_menu
  use_this_input = input?
  if use_this_input.to_s.size < 3
    unless is_included?
      report_that_this_element_does_not_exist(use_this_input)
    end
  end
  calculate_result
  consider_modifying_the_output_string
  report_result if report_result?
end

#sanitize_inputObject

#

sanitize_input (sanitize tag)

This method will presently sanitize the input, by working on ₂ and on ₃, and replace these with the corresponding numbers. This may be useful depending on the chemical formula at hand, such as carbon dioxide (CO₂).

#


417
418
419
420
421
422
423
424
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 417

def sanitize_input
  @input.tr!('','2') if @input.include? ''
  @input.tr!('','3') if @input.include? ''
  @input.tr!('','4') if @input.include? ''
  if @input.include? ''
    @input.tr!('','|')
  end
end

#sanitize_input_for_element_names(array) ⇒ Object

#

sanitize_input_for_element_names

#


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 173

def sanitize_input_for_element_names(array)
  result = []
  if array.any? {|entry| entry == entry.downcase }
    array.each {|entry|
      case entry
      when *ARRAY_HELP_OPTIONS
        show_help :then_exit
      end
      if entry == entry.downcase
        result[-1] = result[-1]+entry
      else
        result << entry
      end
    }
  else
    result = array
  end
  return result
end

#set_input(i = DEFAULT_INPUT) ⇒ Object

#

set_input

#


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 140

def set_input(i = DEFAULT_INPUT)
  if i.is_a? Array
    @commandline_arguments = i[1..-1] # Set the remaining as commandline arguments.
    i = i.first # And re-assign the first input.
  end
  i = DEFAULT_INPUT if i.nil?
  case i.to_s
  when 'do_not_report'
    @report_result = false
    i = nil
  when 'harnstoff'
    i = @molecular_formula_of_different_molecules['harnstoff']
  end
  i = ARRAY_TEST_THESE_MOLECULES if i == :test_default_molecules
  i = i.to_s.dup
  # if i.include? ', '
  #   i = i.split(',').map(&:strip)
  # end
  # i = [i] unless i.is_a? Array
  @input = i
  sanitize_input
end

#set_result(i) ⇒ Object

#

set_result

#


229
230
231
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 229

def set_result(i)
  @result = i
end

#show_help(i = :then_exit) ⇒ Object

#

show_help

#


367
368
369
370
371
372
# File 'lib/chemistry_paradise/utility_scripts/calculate_atomic_mass.rb', line 367

def show_help(
    i = :then_exit
  )
  opn; e 'Pass -SUM to show the individual sub-steps as well.'
  exit_the_program(i)
end