0.1.4 AtomicWeight

This commit is contained in:
Rob Tillaart 2023-04-12 16:52:57 +02:00
parent d05534f704
commit f93645a5b4
18 changed files with 933 additions and 76 deletions

View File

@ -2,7 +2,7 @@
// FILE: AtomicWeight.cpp
// AUTHOR: Rob Tillaart
// DATE: 2022-03-09
// VERSION: 0.1.3
// VERSION: 0.1.4
// PURPOSE: Arduino library for atomic weights
// URL: https://github.com/RobTillaart/AtomicWeight
@ -146,7 +146,8 @@ elements[119] =
PTOE::PTOE(const uint8_t size)
{
_size = size;
_size = size;
_found = 0;
}
@ -164,7 +165,7 @@ uint8_t PTOE::electrons(const uint8_t el)
uint8_t PTOE::neutrons(const uint8_t el)
{
return round(weight(el) - el);
return round(weight(el)) - el;
}
@ -223,6 +224,107 @@ float PTOE::weightFactor()
}
////////////////////////////////////////////////////////////////
//
// EXPERIMENTAL
//
uint8_t PTOE::splitElements(const char * formula)
{
uint8_t count = 0;
char elem[3] = { 0, 0, 0 };
char * p = (char *) formula;
while (*p != '\0')
{
// SKIP non element info
if (*p == '(')
{
p++; // skip '('
continue;
}
if (*p == ')')
{
p++; // skip ')'
continue;
}
if (isdigit(*p))
{
p++; // skip digit
continue;
}
// GET ELEMENT := [ Upper | Upper,lower ]
elem[0] = 0;
elem[1] = 0;
if (! isupper(*p)) return 0; // fail
elem[0] = *p;
p++;
if (islower(*p))
{
elem[1] = *p;
p++;
}
// FIND INDEX OF ELEMENT
int z = find(elem);
if (z == 255)
{
return 0; // fail
}
// DO WE HAVE IDENTIFIED IT ALREADY?
bool found = false;
for (int i = 0; i < count; i++)
{
if (_elems[i] == z)
{
found = true;
}
}
if (found == false)
{
_elems[count] = z;
count++;
}
}
// // DEBUG
// for (int i = 0; i < count; i++)
// {
// Serial.print(i);
// Serial.print('\t');
// Serial.print(_elems[i]);
// Serial.print('\t');
// Serial.println(name(_elems[i]));
// }
_found = count;
return count;
}
uint8_t PTOE::element(uint8_t el)
{
if (el >= _found) return 255;
return _elems[el];
}
uint32_t PTOE::count(const char * formula, const char * el)
{
p = (char *)formula;
return _count('\0', el);
}
float PTOE::atomPercentage(const char * formula, const char * el)
{
float total = count(formula);
if (total == 0) return 0;
p = (char *)formula;
return 100.0 * _count('\0', el) / total;
}
////////////////////////////////////////////////////////////////
//
// PRIVATE
@ -256,7 +358,7 @@ float PTOE::_weight(const char sep, const char * el)
elem[1] = *p;
p++;
}
// can be optimized
// can be optimized?
if ((el == NULL) || (strcmp(elem, el) == 0))
{
int z = find(elem);
@ -285,5 +387,63 @@ float PTOE::_weight(const char sep, const char * el)
}
// -- END OF FILE --
uint32_t PTOE::_count(const char sep, const char * el)
{
uint32_t sum = 0;
char elem[3] = { 0, 0, 0 };
int count = 0;
int w = 0;
while (*p != sep)
{
// HANDLE GROUP (...)
if (*p == '(')
{
p++; // skip '('
w = _count(')', el);
p++; // skip ')'
}
else
{
w = 0;
// GET ELEMENT := [ Upper | Upper,lower ]
elem[1] = 0;
if (! isupper(*p)) return 0; // fail
elem[0] = *p;
p++;
if (islower(*p))
{
elem[1] = *p;
p++;
}
// can be optimized
if ((el == NULL) || (strcmp(elem, el) == 0))
{
int z = find(elem);
if (z == 255) return 0; // fail
w = 1;
}
}
count = 0;
// get optional digits
while (isdigit(*p))
{
count = count * 10 + (*p - '0');
p++;
}
// correct for no digits
if (count == 0) count = 1;
// DEBUG
// Serial.println(w);
// Serial.println(count);
sum += w * count;
}
return sum;
}
// -- END OF FILE --

View File

@ -3,14 +3,14 @@
// FILE: AtomicWeight.h
// AUTHOR: Rob Tillaart
// DATE: 2022-03-09
// VERSION: 0.1.3
// VERSION: 0.1.4
// PURPOSE: Arduino library for atomic weights
// URL: https://github.com/RobTillaart/AtomicWeight
#include "Arduino.h"
#define ATOMIC_WEIGHT_LIB_VERSION (F("0.1.3"))
#define ATOMIC_WEIGHT_LIB_VERSION (F("0.1.4"))
/////////////////////////////////////////////////////////////////////////
@ -28,16 +28,16 @@ public:
uint8_t neutrons(const uint8_t el);
uint8_t protons(const uint8_t el);
// weight of one atom
float weight(const uint8_t el);
// if (el != NULL) weights one element in a formula, e.g el == "H"
// if (el == NULL) weights the whole formula
float weight(const char * formula, const char * el = NULL);
// mass percentage of one element in a formula.
float massPercentage(const char * formula, const char * el);
char * name(const uint8_t el);
uint8_t find(const char * abbrev);
@ -46,15 +46,36 @@ public:
float weightFactor();
////////////////////////////////////////////////////////////////
//
// EXPERIMENTAL 0.1.4
//
// SPLIT FORMULA IN ELEMENTS
uint8_t splitElements(const char * formula);
uint8_t element(uint8_t el);
// if (el != NULL) count atoms of one element in a formula, e.g el == "H"
// if (el == NULL) count all atoms in the whole formula
uint32_t count(const char * formula, const char * el = NULL);
// atom percentage of one element in a formula.
float atomPercentage(const char * formula, const char * el);
private:
uint8_t _size;
const float _weightFactor = 1.0 / 222.909;
// if (el == NULL) ==> whole weight otherwise only of element.
float _weight(char sep, const char * el);
char *p; // for _weight().
uint32_t _count(const char sep, const char * el);
char *p; // for _weight() and _count()
// for splitElements
uint8_t _elems[20]; // max 20 elements in formula.
uint8_t _found;
};
// -- END OF FILE --
// -- END OF FILE --

View File

@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.4] - 2023-04-12
- add **splitElements()** split a formula in an internal list of elements.
- add **element()** access to split elements. See example.
- add **count()** to count atoms in a formula.
- add **atomPercentage()** to calculate percentage of atoms of an element.
- add examples.
- update readme.md
- update keywords.txt
- minor edits.
## [0.1.3] - 2023-01-01
- refactor interface
- add **weight(formula, element)**
@ -14,7 +25,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- update readme.md.
- update keywords.txt
## [0.1.2] - 2023-01-01
- add weight(formula) group () support
- rewrote example

View File

@ -13,25 +13,40 @@ Arduino library for atomic weights.
## Description
This library is mainly as a base for educational purposes.
This library is mainly to be used as a base for educational purposes.
Learning the **periodic table of elements**, the abbreviations and weight.
It also provides the number of electrons, neutrons and protons per element.
Furthermore the library has a **weight()** function, that returns the weight
of either an element or by formula. See examples.
Furthermore the library has a **weight()** function, which returns the weight
of either an element or of a formula (e.g. a molecule).
The weight function can also be used to get the weight of a particular element
from a formula, e.g. the weight of the Oxygen atoms in the **H2SO4** molecule.
Note: library is still experimental. More testing is needed.
This latter function allows the library to calculate the **massPercentage()** too.
The library also has a **count()** function, to count the atoms in a formula.
Derived is the **atomPercentage()** function to give the percentage of atoms
that is a certain element.
Note: library is experimental. More testing is needed.
#### internal
#### Internal
The PTOE class uses a table that has compressed weight to save RAM
The PTOE class uses a table that has compressed weight to save RAM.
- it stores weights as **uint16_t**, 0..65535 instead of floats.
- weight factor = 222.909 = (65535.0 / heaviest element = 118)
- weight factor = 222.909 = 65535.0 / weight heaviest element(118)
- error < 0.3%
- the table (and thus the class) does not handle isotopes.
#### Related
Useful list of formulae.
- https://en.wikipedia.org/wiki/Glossary_of_chemical_formulae
## Interface
```cpp
@ -48,87 +63,132 @@ The parameter **element** in the following functions is 0..118.
- **float weight(uint8_t element)** returns the weight of the element.
The error < 0.3%, table uses "weight compression".
- **float weight(char \* formula, char \* abbreviation == NULL)** see below.
- If (el != NULL) returns the total weight of one element in a formula
- if (el == NULL) returns the weight of the whole formula
- If (el != NULL) returns the total weight of one element in a formula.
- if (el == NULL) returns the weight of the whole formula.
- Returns 0 if it cannot parse the formula given.
- **float massPercentage(char \* formula, char \* abbreviation)**
Returns mass percentage of a selected element in a formula
Returns mass percentage of a selected element in a formula.
- **uint8_t find(char \* abbreviation)** returns the element number.
This function is relative expensive as it searches linear through the internal array of elements.
- **char \* name(uint8_t element)** returns the abbreviation of element.
#### weight
#### SplitElements
The **weight(int n)** call returns the weight of a single atom (by index).
(0.1.4 experimental)
- **uint8_t splitElements(const char \* formula)** split a formula in an internal list of elements.
Returns the number of different elements found.
Max nr of elements is hardcoded to 20.
- **uint8_t element(uint8_t el)** access the internal list of elements by index el.
Note: el should be between 0 and the max nr returned by **splitElements()**.
See example.
#### AtomPercentage
(0.1.4 experimental)
- **uint32_t count(const char \* formula, const char \* el = NULL)**
- If (el != NULL) returns the total atoms of one element in a formula.
- if (el == NULL) returns the total atoms of the whole formula.
- Returns 0 if it cannot parse the formula given.
- **float atomPercentage(const char \* formula, const char \* el)**
Returns atom percentage of the selected element in a formula.
#### Weight
The **weight(uint8_t element)** call returns the weight of a single atom (by index).
The **weight(formula)** call is meant to calculate the weight of a molecule.
A molecule defined as one or more atoms.
A molecule is defined as one or more atoms.
The latter function does not care about the order of the atoms.
So "C6H6" is equal to "H6C6" or even "CCCCCCHHHHHH" or "C3H3C3H3" etc.
Elements are defined as one or two characters long.
The first must be upper case, the (optional) second must be lower case.
The first char must be upper case, the (optional) second must be lower case.
If no number is provided the count of 1 is assumed.
The functions returns a float, so to get the integer weight, one should use **round()**.
If the formula is not parseable it will return a weight of 0.
If the formula can not be parsed it will return a weight of 0.
(Since 0.1.2)
The weight formula parsing experimentally supports brackets () to indicate groups in the formula.
Valid formula's might look as:
- "B" = single element
- "Na" = single element
- "C6" = single element, multiple times
- "H2SO4" compound molecule, no groups
- "C6(COOH)2" compound molecule, with repeating groups
- "YBa2Cu3O7" some superconductor-ish material
(Since 0.1.3)
The **weight(formula, element)** function is meant to calculate the total weight of one element
in a molecule. E.g one can weigh the H atoms in H2O (2 of 18).
#### Formulas
#### debug
The weight formula parsing supports round brackets () to indicate groups in the formula.
- **float weightFactor()** returns weightFactor.
Valid formula's might look like:
- "B" = single element
- "Na" = single element
- "C6" = single element, multiple times
- "H2SO4" compound molecule, no groups
- "C6(COOH)2" compound molecule, with a repeating group
- "YBa2Cu3O7" some superconductor-ish material
- "Ba((OH)4(COOH)2)c" recursive repeating groups (artificial example).
## Operation
#### MassPercentage
See examples
The **massPercentage(formula, element)** function can determine the percentage of the weight
a selected element has in a formula, e.g. the weight of the Oxygen in **H2SO4**.
This is calculated by dividing the weight of the element by the total weight.
If you want to do that for all elements it might be more efficient to calculate the weight
of the whole formula once.
#### Debug
- **float weightFactor()** returns the weightFactor, that was used to
minimize the memory used for the elements mass lookup table.
## Future
#### must
#### Must
- improve documentation
- reorganize.
#### should
#### Should
- add examples
- extend formula parser with error codes.
- which ones?
- support \[] square brackets too.
- (NH4)2\[Pt(SCN)6]
- look for optimizations
- 3x almost same parser
#### could
#### Could
- extend unit tests
- state table
- liquid, gas, solid, unknown (2 bits per element) = ~30 bytes
- room temperature + sea level pressure
- (short) table of English names
- which ones ?
- function **float atomicPercentage("H2O", "H")** ~33%
- performance functions
- especially **find()** ?
- case (in)sensitive **find()**
- always or configurable
- more expensive search
- alphabetical array?
#### Wont (unless)
- support hydrates ?
- **Ba(BrO3)2·2H2O** new separator + starts with number.
- other liquids than water?
- https://en.wikipedia.org/wiki/Glossary_of_chemical_formulae
- is there a faster data structure?
- search by nr is O(1)
- search by name is O(n)
#### wont (unless)
- only if more RAM is used.
- parameters element should be less than \_size
- user responsibility
- more information?
@ -140,5 +200,3 @@ See examples
- 2 bytes per temp 4 x 118 = 476 bytes
- compression 3 bytes for 2 temps 2x 12 bits = 0..4095 K = 354 bytes

View File

@ -140,4 +140,5 @@ elements[119] =
};
// -- END OF FILE --
// -- END OF FILE --

View File

@ -0,0 +1,96 @@
// FILE: atomic_count_atomPercentage.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AtomicWeight
#include "Arduino.h"
#include "AtomicWeight.h"
PTOE ptoe;
char formula0[24] = "C6H6O6";
char formula1[24] = "((COH)3)2";
char formula2[24] = "H2SO4";
char formula3[24] = "CuO2";
// char formula4[24] = "(COH)3(COH)2COH";
// char formula4[24] = "(CH)6O6";
char formula4[24] = "xH2"; // fails => 0;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ATOMIC_WEIGHT_LIB_VERSION: ");
Serial.println(ATOMIC_WEIGHT_LIB_VERSION);
// COUNT
Serial.println(ptoe.count("C"));
Serial.println(ptoe.count("C", "C"));
Serial.println(ptoe.count("H2SO4"));
Serial.println(ptoe.count("H2SO4", "C"));
Serial.println(ptoe.count("H2SO4", "H"));
Serial.println(ptoe.count("H2SO4", "S"));
Serial.println(ptoe.count("H2SO4", "O"));
// PERCENTAGE
Serial.println("\nform.\telem.\tperc.\n");
Serial.print("C");
Serial.print(" \t C \t");
Serial.println(ptoe.atomPercentage("C", "C"));
Serial.print("C6");
Serial.print(" \t C \t");
Serial.println(ptoe.atomPercentage("C6", "C"));
Serial.print("He6");
Serial.print(" \t C \t");
Serial.println(ptoe.atomPercentage("He6", "C"));
Serial.print("NaCl");
Serial.print(" \t Na \t");
Serial.println(ptoe.atomPercentage("NaCl", "Na"));
Serial.print("NaCl");
Serial.print(" \t Cl \t");
Serial.println(ptoe.atomPercentage("NaCl", "Cl"));
Serial.print(formula0);
Serial.print(" \t H \t");
Serial.println(ptoe.atomPercentage(formula0, "H"));
Serial.print(formula1);
Serial.print(" \t H \t");
Serial.println(ptoe.atomPercentage(formula1, "H"));
Serial.print(formula2);
Serial.print(" \t H \t");;
Serial.println(ptoe.atomPercentage(formula2, "H"));
Serial.print(formula3);
Serial.print(" \t H \t");
Serial.println(ptoe.atomPercentage(formula3, "Cu"));
Serial.print(formula3);
Serial.print(" \t H \t");
Serial.println(ptoe.atomPercentage(formula3, "O"));
Serial.print(formula4);
Serial.print(" \t H \t");
Serial.println(ptoe.atomPercentage(formula4, "H"));
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,76 @@
atomic_count_split.ino
ATOMIC_WEIGHT_LIB_VERSION: 0.1.4
C 1 atoms.
C 100.000
C6 6 atoms.
C 100.000
CCCCCCC 7 atoms.
C 100.000
He6 6 atoms.
He 100.000
NaCl 2 atoms.
Na 50.000
Cl 50.000
C6H6O6 18 atoms.
C 33.333
H 33.333
O 33.333
Al2Si2O5(OH)4 17 atoms.
Al 11.765
Si 11.765
O 52.941
H 23.529
H2SO4 7 atoms.
H 28.571
S 14.286
O 57.143
CuO2 3 atoms.
Cu 33.333
O 66.667
(COH)3(COH)2COH 18 atoms.
C 33.333
O 33.333
H 33.333
YBa2Cu3O7 13 atoms.
Y 7.692
Ba 15.385
Cu 23.077
O 53.846
Al(NO2)3 10 atoms.
Al 10.000
N 30.000
O 60.000
Ba(C2H3O2)2 15 atoms.
Ba 6.667
C 26.667
H 40.000
O 26.667
done

View File

@ -0,0 +1,81 @@
// FILE: atomic_count_split.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AtomicWeight
#include "Arduino.h"
#include "AtomicWeight.h"
PTOE ptoe;
// https://en.wikipedia.org/wiki/Glossary_of_chemical_formulae
char formula0[24] = "C6H6O6"; // Benzenehexol
char formula1[24] = "Al2Si2O5(OH)4"; // kaolin
char formula2[24] = "H2SO4"; // sulpheric acid
char formula3[24] = "CuO2"; // CopperOxide
char formula4[24] = "(COH)3(COH)2COH"; // fake
// char formula4[24] = "(CH)6O6"; // fake
// char formula4[24] = "xH2"; // fails => 0;
char formula5[24] = "YBa2Cu3O7"; // superconductor
char formula6[24] = "Al(NO2)3"; // aluminium nitrite
char formula7[24] = "Ba(C2H3O2)2"; // barium acetate
char formula8[24] = "Ca2SbMg4FeBe2Si4O20"; // Welshite
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ATOMIC_WEIGHT_LIB_VERSION: ");
Serial.println(ATOMIC_WEIGHT_LIB_VERSION);
test("C");
test("C6");
test("CCCCCCC");
test("He6");
test("NaCl");
test(formula0);
test(formula1);
test(formula2);
test(formula3);
test(formula4);
test(formula5);
test(formula6);
test(formula7);
test(formula8);
Serial.println("done");
}
void loop()
{
}
void test(char * formula)
{
Serial.println();
Serial.print(formula);
Serial.print("\t");
Serial.print(ptoe.count(formula));
Serial.println(" atoms.");
uint8_t cnt = ptoe.splitElements(formula);
for (int i = 0; i < cnt; i++)
{
Serial.print(ptoe.name(ptoe.element(i)));
Serial.print("\t");
Serial.println(ptoe.atomPercentage(formula, ptoe.name(ptoe.element(i))), 3);
}
Serial.println();
}
// -- END OF FILE --

View File

@ -0,0 +1,87 @@
BOARD: Arduino UNO
IDE: 1.8.19
atomic_count_split.ino
ATOMIC_WEIGHT_LIB_VERSION: 0.1.4
C 1 atoms.
C 100.000
C6 6 atoms.
C 100.000
CCCCCCC 7 atoms.
C 100.000
He6 6 atoms.
He 100.000
NaCl 2 atoms.
Na 50.000
Cl 50.000
C6H6O6 18 atoms.
C 33.333
H 33.333
O 33.333
Al2Si2O5(OH)4 17 atoms.
Al 11.765
Si 11.765
O 52.941
H 23.529
H2SO4 7 atoms.
H 28.571
S 14.286
O 57.143
CuO2 3 atoms.
Cu 33.333
O 66.667
(COH)3(COH)2COH 18 atoms.
C 33.333
O 33.333
H 33.333
YBa2Cu3O7 13 atoms.
Y 7.692
Ba 15.385
Cu 23.077
O 53.846
Al(NO2)3 10 atoms.
Al 10.000
N 30.000
O 60.000
Ba(C2H3O2)2 15 atoms.
Ba 6.667
C 26.667
H 40.000
O 26.667
Ca2SbMg4FeBe2Si4O20 34 atoms.
Ca 5.882
Sb 2.941
Mg 11.765
Fe 2.941
Be 5.882
Si 11.765
O 58.824
done

View File

@ -0,0 +1,51 @@
// FILE: atomic_weight_find.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AtomicWeight
#include "Arduino.h"
#include "AtomicWeight.h"
PTOE ptoe;
uint32_t start, stop;
volatile char *p = 0;
volatile int n;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
delay(10);
start = micros();
for (int i = 0; i < 119; i++)
{
p = ptoe.name(i);
}
stop = micros();
Serial.print("NAME:\t");
Serial.println((stop - start) * 1.0 / 119);
delay(10);
start = micros();
for (int i = 0; i < 119; i++)
{
n = ptoe.find(ptoe.name(i));
}
stop = micros();
Serial.print("FIND:\t");
Serial.println((stop - start) * 1.0 / 119);
Serial.println("\ndone...");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -1,8 +1,9 @@
// FILE: atomic_weight_formula.ino
// FILE: atomic_weight_massPercentage.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AtomicWeight
#include "Arduino.h"
#include "AtomicWeight.h"
@ -27,44 +28,45 @@ void setup()
Serial.print("ATOMIC_WEIGHT_LIB_VERSION: ");
Serial.println(ATOMIC_WEIGHT_LIB_VERSION);
Serial.println("\nform.\telem.\tperc.\n");
Serial.print("C");
Serial.print(" \t");
Serial.print(" \t C \t");
Serial.println(ptoe.massPercentage("C", "C"));
Serial.print("C6");
Serial.print(" \t");
Serial.print(" \t C \t");
Serial.println(ptoe.massPercentage("C6", "C"));
Serial.print("He6");
Serial.print(" \t");
Serial.print(" \t C \t");
Serial.println(ptoe.massPercentage("He6", "C"));
Serial.print("NaCl");
Serial.print(" \t");
Serial.print(" \t Na \t");
Serial.println(ptoe.massPercentage("NaCl", "Na"));
Serial.print("NaCl");
Serial.print(" \t");
Serial.print(" \t Cl \t");
Serial.println(ptoe.massPercentage("NaCl", "Cl"));
Serial.print(formula0);
Serial.print(" \t");
Serial.print(" \t H \t");
Serial.println(ptoe.massPercentage(formula0, "H"));
Serial.print(formula1);
Serial.print(" \t");
Serial.print(" \t H \t");
Serial.println(ptoe.massPercentage(formula1, "H"));
Serial.print(formula2);
Serial.print(" \t");
Serial.print(" \t H \t");;
Serial.println(ptoe.massPercentage(formula2, "H"));
Serial.print(formula3);
Serial.print(" \t");
Serial.print(" \t H \t");
Serial.println(ptoe.massPercentage(formula3, "H"));
Serial.print(formula4);
Serial.print(" \t");
Serial.print(" \t H \t");
Serial.println(ptoe.massPercentage(formula4, "H"));
}
@ -76,4 +78,4 @@ void loop()
// -- END OF FILE --
// -- END OF FILE --

View File

@ -0,0 +1,14 @@
ATOMIC_WEIGHT_LIB_VERSION: 0.1.3 (output adjusted after release)
form. elem. perc.
C C 100.00
C6 C 100.00
He6 C 0.00
NaCl Na 39.34
NaCl Cl 60.66
C6H6O6 H 3.48
((COH)3)2 H 3.48
H2SO4 H 2.06
CuO2 H 0.00
xH2 H 0.00

View File

@ -0,0 +1,80 @@
// FILE: atomic_weight_split.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AtomicWeight
#include "Arduino.h"
#include "AtomicWeight.h"
PTOE ptoe;
char formula0[24] = "C6H6O6";
char formula1[24] = "((COH)3)2";
char formula2[24] = "H2SO4";
char formula3[24] = "CuO2";
char formula4[24] = "(COH)3(COH)2COH";
// char formula4[24] = "(CH)6O6";
// char formula4[24] = "xH2"; // fails => 0;
char formula5[24] = "YBa2Cu3O7";
uint8_t cnt;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ATOMIC_WEIGHT_LIB_VERSION: ");
Serial.println(ATOMIC_WEIGHT_LIB_VERSION);
test("C");
test("C6");
test("CCCCCCC");
test("He6");
test("NaCl");
test(formula0);
test(formula1);
test(formula2);
test(formula3);
test(formula4);
test(formula5);
Serial.println("done");
}
void loop()
{
}
void test(char * formula)
{
Serial.println();
Serial.println(formula);
uint8_t cnt = ptoe.splitElements(formula);
for (int i = 0; i < cnt; i++)
{
Serial.print(ptoe.name(ptoe.element(i)));
Serial.print("\t");
Serial.println(ptoe.massPercentage(formula, ptoe.name(ptoe.element(i))), 3);
}
delay(100);
uint32_t start = micros();
cnt = ptoe.splitElements(formula);
uint32_t stop = micros();
Serial.print("TIME:\t");
Serial.print(stop - start);
Serial.println(" us.");
Serial.println();
}
// -- END OF FILE --

View File

@ -0,0 +1,74 @@
BOARD: Arduino UNO
IDE: 1.8.19
D:\Rob\WORK\Arduino\libraries\AtomicWeight\examples\atomic_weight_split\atomic_weight_split.ino
ATOMIC_WEIGHT_LIB_VERSION: 0.1.3
C
C 100.000
TIME: 28 us.
C6
C 100.000
TIME: 28 us.
CCCCCCC
C 100.000
TIME: 160 us.
He6
He 100.000
TIME: 24 us.
NaCl
Na 39.338
Cl 60.662
TIME: 88 us.
C6H6O6
C 41.388
H 3.479
O 55.133
TIME: 76 us.
((COH)3)2
C 41.388
O 55.133
H 3.479
TIME: 76 us.
H2SO4
H 2.058
S 32.696
O 65.246
TIME: 92 us.
CuO2
Cu 66.512
O 33.488
TIME: 108 us.
(COH)3(COH)2COH
C 41.388
O 55.133
H 3.479
TIME: 204 us.
YBa2Cu3O7
Y 13.346
Ba 41.228
Cu 28.617
O 16.810
TIME: 328 us.
done

View File

@ -11,12 +11,19 @@ size KEYWORD2
electrons KEYWORD2
protons KEYWORD2
neutrons KEYWORD2
weight KEYWORD2
massPercentage KEYWORD2
name KEYWORD2
find KEYWORD2
splitElements KEYWORD2
element KEYWORD2
count KEYWORD2
atomPercentage KEYWORD2
# Constants ( LITERAL1)
ATOMIC_WEIGHT_LIB_VERSION LITERAL1

View File

@ -1,7 +1,7 @@
{
"name": "AtomicWeight",
"keywords": "Mass,atoms,weight,PTOE,periodic,elements",
"description": "Arduino library for atomic weights.",
"keywords": "Mass,atoms,weight,PTOE,periodic,elements,massPercentage",
"description": "Arduino library for atomic weights, calculate massPercentage of elements in a formula.",
"authors":
[
{
@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/AtomicWeight.git"
},
"version": "0.1.3",
"version": "0.1.4",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,9 +1,9 @@
name=AtomicWeight
version=0.1.3
version=0.1.4
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for AtomicWeight
paragraph=Mass,atoms,weight,PTOE,periodic,elements
sentence=Arduino library for atomic weights, calculate massPercentage of elements in a formula.
paragraph=Mass,atoms,weight,PTOE,periodic,elements,massPercentage,atomPercentage
category=Data Processing
url=https://github.com/RobTillaart/AtomicWeight
architectures=*

View File

@ -157,7 +157,46 @@ unittest(test_massPercentage)
}
unittest(test_count)
{
PTOE ptoe;
assertEqual( 2, ptoe.count("Cl2"));
assertEqual( 2, ptoe.count("NaCl"));
assertEqual( 1, ptoe.count("NaCl", "Na"));
assertEqual( 1, ptoe.count("NaCl", "Cl"));
assertEqual( 18, ptoe.count("C6H6O6"));
assertEqual( 17, ptoe.count("Al2Si2O5(OH)4"));
assertEqual( 9, ptoe.count("Al2Si2O5(OH)4", "O"));
assertEqual( 4, ptoe.count("Al2Si2O5(OH)4", "H"));
assertEqual( 13, ptoe.count("YBa2Cu3O7"));
assertEqual( 3, ptoe.count("C(O(H2)2)3", "O"));
assertEqual( 12, ptoe.count("C(O(H2)2)3", "H"));
}
unittest(test_atomPercentage)
{
PTOE ptoe;
assertEqualFloat( 7.692, ptoe.atomPercentage("YBa2Cu3O7", "Y"), 0.1);
assertEqualFloat( 15.385, ptoe.atomPercentage("YBa2Cu3O7", "Ba"), 0.1);
assertEqualFloat( 23.077, ptoe.atomPercentage("YBa2Cu3O7", "Cu"), 0.1);
assertEqualFloat( 53.846, ptoe.atomPercentage("YBa2Cu3O7", "O"), 0.1);
assertEqualFloat( 10.000, ptoe.atomPercentage("Al(NO2)3", "Al"), 0.1);
assertEqualFloat( 30.000, ptoe.atomPercentage("Al(NO2)3", "N"), 0.1);
assertEqualFloat( 60.000, ptoe.atomPercentage("Al(NO2)3", "O"), 0.1);
assertEqualFloat( 6.667, ptoe.atomPercentage("Ba(C2H3O2)2", "Ba"), 0.1);
assertEqualFloat( 26.667, ptoe.atomPercentage("Ba(C2H3O2)2", "C"), 0.1);
assertEqualFloat( 40.000, ptoe.atomPercentage("Ba(C2H3O2)2", "H"), 0.1);
assertEqualFloat( 26.667, ptoe.atomPercentage("Ba(C2H3O2)2", "O"), 0.1);
}
unittest_main()
// --------
// -- END OF FILE --