0.2.2 AtomicWeight

This commit is contained in:
Rob Tillaart 2024-04-07 17:42:13 +02:00
parent 8ed84672f8
commit 8085a821bf
18 changed files with 143 additions and 87 deletions

View File

@ -1,4 +1,5 @@
# These are supported funding model platforms # These are supported funding model platforms
github: RobTillaart github: RobTillaart
custom: "https://www.paypal.me/robtillaart"

View File

@ -1,12 +1,12 @@
name: Arduino-lint name: Arduino-lint
on: [push, pull_request] on: [push, pull_request]
jobs: jobs:
lint: lint:
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 5
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- uses: arduino/arduino-lint-action@v1 - uses: arduino/arduino-lint-action@v1
with: with:
library-manager: update library-manager: update

View File

@ -1,4 +1,3 @@
---
name: Arduino CI name: Arduino CI
on: [push, pull_request] on: [push, pull_request]
@ -6,9 +5,10 @@ on: [push, pull_request]
jobs: jobs:
runTest: runTest:
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 20
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1 - uses: ruby/setup-ruby@v1
with: with:
ruby-version: 2.6 ruby-version: 2.6

View File

@ -9,10 +9,11 @@ on:
jobs: jobs:
test: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 5
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- name: json-syntax-check - name: json-syntax-check
uses: limitusus/json-syntax-check@v1 uses: limitusus/json-syntax-check@v2
with: with:
pattern: "\\.json$" pattern: "\\.json$"

View File

@ -2,7 +2,7 @@
// FILE: AtomicWeight.cpp // FILE: AtomicWeight.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// DATE: 2022-03-09 // DATE: 2022-03-09
// VERSION: 0.2.1 // VERSION: 0.2.2
// PURPOSE: Arduino library for atomic weights // PURPOSE: Arduino library for atomic weights
// URL: https://github.com/RobTillaart/AtomicWeight // URL: https://github.com/RobTillaart/AtomicWeight
@ -28,11 +28,11 @@ uint8_t PTOE::size()
} }
char * PTOE::name(const uint8_t el) char * PTOE::name(const uint8_t element)
{ {
// catch out of range. // catch out of range.
if (el > _size) return NULL; if (element > _size) return NULL;
return elements[el].name; return elements[element].name;
} }
@ -40,34 +40,34 @@ uint8_t PTOE::find(const char * abbrev)
{ {
// case insensitive? // case insensitive?
// caching? // caching?
// param check? // parameter check?
// uint8_t len = strlen(abbrev); // uint8_t length = strlen(abbrev);
// if ((len == 1) || (len == 2)) // if ((length == 1) || (length == 2) || (length == 3))
// { // {
for (uint8_t i = 0; i < _size; i++) for (uint8_t i = 0; i < _size; i++)
{ {
if (strcmp(elements[i].name, abbrev) == 0) return i; if (strcmp(elements[i].name, abbrev) == 0) return i;
} }
// } // }
return 255; return 255; // out of range.
} }
uint8_t PTOE::electrons(const uint8_t el) uint8_t PTOE::electrons(const uint8_t element)
{ {
return el; return element;
} }
uint8_t PTOE::neutrons(const uint8_t el) uint8_t PTOE::neutrons(const uint8_t element)
{ {
return round(weight(el)) - el; return round(weight(element)) - element;
} }
uint8_t PTOE::protons(const uint8_t el) uint8_t PTOE::protons(const uint8_t element)
{ {
return el; return element;
} }
@ -75,10 +75,10 @@ uint8_t PTOE::protons(const uint8_t el)
// //
// WEIGHT // WEIGHT
// //
float PTOE::weight(const uint8_t el) float PTOE::weight(const uint8_t element)
{ {
if (el > _size) return 0; // catch out of range. if (element > _size) return 0; // catch out of range.
return elements[el].weight * ATOMIC_WEIGHT_FACTOR; return elements[element].weight * ATOMIC_WEIGHT_FACTOR;
} }
@ -147,7 +147,10 @@ uint8_t PTOE::splitElements(const char * formula)
// GET ELEMENT := [ Upper | Upper,lower ] // GET ELEMENT := [ Upper | Upper,lower ]
elem[0] = 0; elem[0] = 0;
elem[1] = 0; elem[1] = 0;
if (! isupper(*p)) return 0; // fail if (! isupper(*p))
{
return 0; // fail
}
elem[0] = *p; elem[0] = *p;
p++; p++;
if (islower(*p)) if (islower(*p))
@ -156,8 +159,8 @@ uint8_t PTOE::splitElements(const char * formula)
p++; p++;
} }
// FIND INDEX OF ELEMENT // FIND INDEX OF ELEMENT
int z = find(elem); int index = find(elem);
if (z == 255) if (index == 255)
{ {
return 0; // fail return 0; // fail
} }
@ -166,14 +169,14 @@ uint8_t PTOE::splitElements(const char * formula)
bool found = false; bool found = false;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
if (_splitList[i] == z) if (_splitList[i] == index)
{ {
found = true; found = true;
} }
} }
if ((found == false) && (count < ATOMIC_WEIGHT_MAX_SPLIT_LIST)) if ((found == false) && (count < ATOMIC_WEIGHT_MAX_SPLIT_LIST))
{ {
_splitList[count] = z; _splitList[count] = index;
count++; count++;
} }
} }
@ -193,10 +196,13 @@ uint8_t PTOE::splitElements(const char * formula)
} }
uint8_t PTOE::element(uint8_t el) uint8_t PTOE::element(uint8_t element)
{ {
if (el >= _found) return 255; if (element >= _found)
return _splitList[el]; {
return 255; // out of range
}
return _splitList[element];
} }
@ -204,19 +210,19 @@ uint8_t PTOE::element(uint8_t el)
// //
// COUNT // COUNT
// //
uint32_t PTOE::count(const char * formula, const char * el) uint32_t PTOE::count(const char * formula, const char * element)
{ {
p = (char *)formula; p = (char *)formula;
return _count('\0', el); return _count('\0', element);
} }
float PTOE::atomPercentage(const char * formula, const char * el) float PTOE::atomPercentage(const char * formula, const char * element)
{ {
float total = count(formula); float total = count(formula);
if (total == 0) return 0; if (total == 0) return 0;
p = (char *)formula; p = (char *)formula;
return 100.0 * _count('\0', el) / total; return 100.0 * _count('\0', element) / total;
} }
@ -234,14 +240,14 @@ float PTOE::weightFactor()
// //
// PRIVATE // PRIVATE
// //
float PTOE::_weight(const char sep, const char * abbrev) float PTOE::_weight(const char separator, const char * abbrev)
{ {
float sum = 0; float sum = 0;
float w = 0; float w = 0;
char elem[3] = { 0, 0, 0 }; char elem[3] = { 0, 0, 0 };
uint32_t count = 0; uint32_t count = 0;
while (*p != sep) while (*p != separator)
{ {
w = 0; w = 0;
// HANDLE GROUP (...) // HANDLE GROUP (...)
@ -266,9 +272,9 @@ float PTOE::_weight(const char sep, const char * abbrev)
// can be optimized? // can be optimized?
if ((abbrev == NULL) || (strcmp(elem, abbrev) == 0)) if ((abbrev == NULL) || (strcmp(elem, abbrev) == 0))
{ {
int z = find(elem); int index = find(elem);
if (z == 255) return 0; // fail if (index == 255) return 0; // fail
w = weight(z); w = weight(index);
} }
} }
@ -291,14 +297,14 @@ float PTOE::_weight(const char sep, const char * abbrev)
} }
uint32_t PTOE::_count(const char sep, const char * abbrev) uint32_t PTOE::_count(const char separator, const char * abbrev)
{ {
uint32_t sum = 0; uint32_t sum = 0;
char elem[3] = { 0, 0, 0 }; char elem[3] = { 0, 0, 0 };
uint32_t count = 0; uint32_t count = 0;
int w = 0; int w = 0;
while (*p != sep) while (*p != separator)
{ {
// HANDLE GROUP (...) // HANDLE GROUP (...)
if (*p == '(') if (*p == '(')
@ -323,8 +329,8 @@ uint32_t PTOE::_count(const char sep, const char * abbrev)
// can be optimized // can be optimized
if ((abbrev == NULL) || (strcmp(elem, abbrev) == 0)) if ((abbrev == NULL) || (strcmp(elem, abbrev) == 0))
{ {
int z = find(elem); int index = find(elem);
if (z == 255) return 0; // fail if (index == 255) return 0; // fail
w = 1; w = 1;
} }
} }

View File

@ -3,7 +3,7 @@
// FILE: AtomicWeight.h // FILE: AtomicWeight.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// DATE: 2022-03-09 // DATE: 2022-03-09
// VERSION: 0.2.1 // VERSION: 0.2.2
// PURPOSE: Arduino library for atomic weights // PURPOSE: Arduino library for atomic weights
// URL: https://github.com/RobTillaart/AtomicWeight // URL: https://github.com/RobTillaart/AtomicWeight
@ -11,7 +11,7 @@
#include "Arduino.h" #include "Arduino.h"
#define ATOMIC_WEIGHT_LIB_VERSION (F("0.2.1")) #define ATOMIC_WEIGHT_LIB_VERSION (F("0.2.2"))
#ifndef ATOMIC_WEIGHT_MAX_SPLIT_LIST #ifndef ATOMIC_WEIGHT_MAX_SPLIT_LIST
@ -41,18 +41,18 @@ public:
// BASIC // BASIC
char * name(const uint8_t el); char * name(const uint8_t element);
uint8_t find(const char * abbrev); uint8_t find(const char * abbrev);
uint8_t electrons(const uint8_t el); uint8_t electrons(const uint8_t element);
uint8_t neutrons(const uint8_t el); uint8_t neutrons(const uint8_t element);
uint8_t protons(const uint8_t el); uint8_t protons(const uint8_t element);
// WEIGHT of one atom // WEIGHT of one atom
float weight(const uint8_t el); float weight(const uint8_t element);
// if (el != NULL) weights one element in a formula, e.g el == "H" // if (element != NULL) weights one element in a formula, e.g element == "H"
// if (el == NULL) weights the whole formula // if (element == NULL) weights the whole formula
float weight(const char * formula, const char * abbrev = NULL); float weight(const char * formula, const char * abbrev = NULL);
// mass percentage of one element in a formula. // mass percentage of one element in a formula.
float massPercentage(const char * formula, const char * abbrev); float massPercentage(const char * formula, const char * abbrev);
@ -65,15 +65,15 @@ public:
// SPLIT FORMULA IN ELEMENTS // SPLIT FORMULA IN ELEMENTS
uint8_t splitElements(const char * formula); uint8_t splitElements(const char * formula);
uint8_t element(uint8_t el); uint8_t element(uint8_t element);
// COUNT // COUNT
// if (el != NULL) count atoms of one element in a formula, e.g el == "H" // if (element != NULL) count atoms of one element in a formula, e.g element == "H"
// if (el == NULL) count all atoms in the whole formula // if (element == NULL) count all atoms in the whole formula
uint32_t count(const char * formula, const char * el = NULL); uint32_t count(const char * formula, const char * element = NULL);
// atom percentage of one element in a formula. // atom percentage of one element in a formula.
float atomPercentage(const char * formula, const char * el); float atomPercentage(const char * formula, const char * element);
// DEBUG // DEBUG
@ -83,9 +83,9 @@ public:
private: private:
uint8_t _size; uint8_t _size;
// if (el == NULL) ==> whole weight otherwise only of element. // if (element == NULL) ==> whole weight otherwise only of element.
float _weight(char sep, const char * abbrev); float _weight(char separator, const char * abbrev);
uint32_t _count(const char sep, const char * abbrev); uint32_t _count(const char separator, const char * abbrev);
char *p; // for _weight() and _count() char *p; // for _weight() and _count()
// for splitElements // for splitElements

View File

@ -6,10 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.2] - 2024-04-07
- update GitHub actions
- refactor internal names for readability
- minor edits
## [0.2.1] - 2023-10-17 ## [0.2.1] - 2023-10-17
- update readme.md - update readme.md
## [0.2.0] - 2023-04-15 ## [0.2.0] - 2023-04-15
- use new weight factor 201.3868 to reduce maximum relative error - use new weight factor 201.3868 to reduce maximum relative error
- create **elements_uint16.h** file. - create **elements_uint16.h** file.

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2022-2023 Rob Tillaart Copyright (c) 2022-2024 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -262,6 +262,8 @@ minimize the memory used for the elements mass lookup table.
- improve documentation - improve documentation
- reorganize. - reorganize.
- float => double
- EV is at the end of range float...
#### Should #### Should
@ -271,7 +273,7 @@ minimize the memory used for the elements mass lookup table.
- **uint32_t neutrons(formula)** uses protons() - **uint32_t neutrons(formula)** uses protons()
- **uint32_t electrons(formula)** uses protons() - **uint32_t electrons(formula)** uses protons()
- add weight of electron as constant. for completeness. - add weight of electron as constant. for completeness.
- functions around **AVOGADRO**, **DALTON** etc - functions around **AVOGADRO**, **DALTON** etc.
- **float weightEV(formula)** - **float weightEV(formula)**
- **float dalton2EV(float Dalton)** to express mass in eV. - **float dalton2EV(float Dalton)** to express mass in eV.
- **float dalton2Joule(float Dalton)** - **float dalton2Joule(float Dalton)**
@ -285,16 +287,18 @@ minimize the memory used for the elements mass lookup table.
- look for optimizations - look for optimizations
- 3x almost same parser - 3x almost same parser
- PROGMEM ? - PROGMEM ?
- state table
- liquid, gas, solid, unknown (2 bits per element) = ~30 bytes
- room temperature + sea level pressure
- separate file like elements_name.h
- performance **find()** - performance **find()**
- alphabetical array? tree? - alphabetical array? tree?
- ==> more memory... - ==> more memory...
- support \[] square brackets too. - support \[] square brackets too.
- (NH4)2\[Pt(SCN)6] - (NH4)2\[Pt(SCN)6]
- add a derived class PERIODIC_TABLE? - add a derived class PERIODIC_TABLE?
- state table
- liquid, gas, solid, unknown (2 bits per element) = ~30 bytes
- room temperature + sea level pressure
- separate file like elements_name.h
#### Wont (unless) #### Wont (unless)

View File

@ -14,9 +14,9 @@ char formula0[24] = "C6H6O6";
char formula1[24] = "((COH)3)2"; char formula1[24] = "((COH)3)2";
char formula2[24] = "H2SO4"; char formula2[24] = "H2SO4";
char formula3[24] = "CuO2"; char formula3[24] = "CuO2";
// char formula4[24] = "(COH)3(COH)2COH"; char formula4[24] = "(COH)3(COH)2COH";
// char formula4[24] = "(CH)6O6"; char formula5[24] = "(CH)6O6";
char formula4[24] = "xH2"; // fails => 0; char formula6[24] = "xH2"; // fails => 0;
@ -85,6 +85,14 @@ void setup()
Serial.print(" \t H \t"); Serial.print(" \t H \t");
Serial.println(ptoe.atomPercentage(formula4, "H")); Serial.println(ptoe.atomPercentage(formula4, "H"));
Serial.print(formula5);
Serial.print(" \t H \t");
Serial.println(ptoe.atomPercentage(formula5, "O"));
Serial.print(formula6);
Serial.print(" \t H \t");
Serial.println(ptoe.atomPercentage(formula6, "H"));
} }

View File

@ -14,7 +14,7 @@ PTOE ptoe;
char formula0[24] = "C6H6O6"; // Benzenehexol char formula0[24] = "C6H6O6"; // Benzenehexol
char formula1[24] = "Al2Si2O5(OH)4"; // kaolin char formula1[24] = "Al2Si2O5(OH)4"; // kaolin
char formula2[24] = "H2SO4"; // sulpheric acid char formula2[24] = "H2SO4"; // sulphuric acid
char formula3[24] = "CuO2"; // CopperOxide char formula3[24] = "CuO2"; // CopperOxide
char formula4[24] = "(COH)3(COH)2COH"; // fake char formula4[24] = "(COH)3(COH)2COH"; // fake
// char formula4[24] = "(CH)6O6"; // fake // char formula4[24] = "(CH)6O6"; // fake

View File

@ -14,9 +14,9 @@ char formula0[24] = "C6H6O6";
char formula1[24] = "((COH)3)2"; char formula1[24] = "((COH)3)2";
char formula2[24] = "H2SO4"; char formula2[24] = "H2SO4";
char formula3[24] = "CuO2"; char formula3[24] = "CuO2";
// char formula4[24] = "(COH)3(COH)2COH"; char formula4[24] = "(COH)3(COH)2COH";
// char formula4[24] = "(CH)6O6"; char formula5[24] = "(CH)6O6";
char formula4[24] = "xH2"; // fails => 0; char formula6[24] = "xH2"; // fails => 0;
void setup() void setup()
@ -62,15 +62,23 @@ void setup()
Serial.print(formula4); Serial.print(formula4);
Serial.print(" \t"); Serial.print(" \t");
Serial.println(ptoe.weight(formula4)); Serial.println(ptoe.weight(formula4));
Serial.print(formula5);
Serial.print(" \t");
Serial.println(ptoe.weight(formula5));
Serial.print(formula6);
Serial.print(" \t");
Serial.println(ptoe.weight(formula6));
Serial.print("()"); Serial.print("()");
Serial.print(" \t"); Serial.print(" \t");
Serial.println(ptoe.weight("()")); Serial.println(ptoe.weight("()"));
Serial.print("(H2O)"); Serial.print("(H2O)");
Serial.print(" \t"); Serial.print(" \t");
Serial.println(ptoe.weight("(H2O)")); Serial.println(ptoe.weight("(H2O)"));
Serial.print("(H2O)255"); Serial.print("(H2O)255");
Serial.print(" \t"); Serial.print(" \t");
Serial.println(ptoe.weight("(H2O)255")); Serial.println(ptoe.weight("(H2O)255"));

View File

@ -14,9 +14,9 @@ char formula0[24] = "C6H6O6";
char formula1[24] = "((COH)3)2"; char formula1[24] = "((COH)3)2";
char formula2[24] = "H2SO4"; char formula2[24] = "H2SO4";
char formula3[24] = "CuO2"; char formula3[24] = "CuO2";
// char formula4[24] = "(COH)3(COH)2COH"; char formula4[24] = "(COH)3(COH)2COH";
// char formula4[24] = "(CH)6O6"; char formula5[24] = "(CH)6O6";
char formula4[24] = "xH2"; // fails => 0; char formula6[24] = "xH2"; // fails => 0;
@ -69,6 +69,14 @@ void setup()
Serial.print(" \t H \t"); Serial.print(" \t H \t");
Serial.println(ptoe.massPercentage(formula4, "H")); Serial.println(ptoe.massPercentage(formula4, "H"));
Serial.print(formula5);
Serial.print(" \t H \t");
Serial.println(ptoe.massPercentage(formula5, "H"));
Serial.print(formula6);
Serial.print(" \t H \t");
Serial.println(ptoe.massPercentage(formula6, "H"));
} }

View File

@ -0,0 +1,16 @@
ATOMIC_WEIGHT_LIB_VERSION: 0.2.2
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.47
((COH)3)2 H 3.47
H2SO4 H 2.06
CuO2 H 0.00
(COH)3(COH)2COH H 3.47
(CH)6O6 H 3.47
xH2 H 0.00

View File

@ -14,9 +14,9 @@ packages:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile: compile:
# Choosing to run compilation tests on 2 different Arduino platforms # Choosing to run compilation tests on 2 different Arduino platforms
platforms: platforms:
# - uno # - uno # memory usage is HIGH for this lib
# - due # - due
# - zero # - zero
# - leonardo # - leonardo

View File

@ -14,7 +14,7 @@
// 0.1.x 222.909 based op mapping max range 65535 == weight(118) // 0.1.x 222.909 based op mapping max range 65535 == weight(118)
// 0.2.x 201.3868 searched for minimal relative error. // 0.2.x 201.3868 searched for minimal relative error.
// const float WEIGHT_FACTOR = 222.909; // const float WEIGHT_FACTOR = 222.909;
const float WEIGHT_FACTOR = 201.3868; const float WEIGHT_FACTOR = 201.3868;

View File

@ -15,7 +15,7 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/AtomicWeight.git" "url": "https://github.com/RobTillaart/AtomicWeight.git"
}, },
"version": "0.2.1", "version": "0.2.2",
"license": "MIT", "license": "MIT",
"frameworks": "*", "frameworks": "*",
"platforms": "*", "platforms": "*",

View File

@ -1,5 +1,5 @@
name=AtomicWeight name=AtomicWeight
version=0.2.1 version=0.2.2
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for atomic weights, calculate massPercentage of elements in a formula. sentence=Arduino library for atomic weights, calculate massPercentage of elements in a formula.