Commit e051e1eb authored by Matthias Schroeder's avatar Matthias Schroeder

Decoder routine startet for storing stuff in a compact way

parent 8a32e5cf
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <sys/stat.h>
#define limC 3000
#define limH 30000
#define limO 400
#define limN 200
#define limS 120
#define limP 100
#define limCl 10
#define MAX_MASS 2000
typedef struct {
int64_t mass;
int64_t formel;
} Moli;
#define N 1200000000
Moli mols[N];
typedef struct {
char formel[10];
int64_t mass;
int valence;
int spec;
} Element;
Element elements[] = {
{ "C" , 12000000000 , 4, 0 },
{ "C13" , 13003354835 , 4, 2 },
{ "H" , 1007825000 , 1, 0 },
{ "O" , 15994914619 , 2, 0 },
{ "O18" , 17999161000 , 2, 2 },
{ "N" , 14003074004 , 5, 0 },
{ "N15" , 15000108899 , 5, 2 },
{ "S" , 31972072000 , 6, 0 },
{ "S34" , 33967866900 , 6, 2 },
{ "P" , 30973763000 , 5, 0 },
{ "Cl" , 34968852700 , 1, 1 },
{ "CUi" , 62929597720 , 1, 1 },
{ "CUii" , 62929597720 , 2, 1 },
{ "FEii" , 55934936000 , 2, 1 },
{ "FEiii", 55934936000 , 3, 1 },
{ "BR79" , 78918338000 , 1, 1 },
{ "BR81" , 80916900000 , 1, 1 },
{ "NA" , 22989769280 , 1, 1 },
{ "NI.ii", 57935346200 , 2, 1 },
{ "ZNii" , 65380000000 , 2, 1 },
{ "I" , 126904470000 , 1, 1 } };
int nbr_elements= sizeof(elements)/sizeof(Element);
void print_aF(int* aF) {
printf("Array=");
for (int i=0; i<16; i++) {
printf("%6d", aF[i]);
}
printf("\n");
}
/* Takes an Element in aE and number of apperance in card
* and stores it in the respective position in int array aF */
void storeElement(int* aF, char* sE, int card) {
if (strlen(sE)==0) {
return;
}
int i=0;
if ( ! strcmp(sE, "C")) {
aF[i]= card;
}
i+=1;
if ( ! strcmp(sE, "H")) {
aF[i]= card;
}
i+=1;
if ( ! strcmp(sE, "O")) {
aF[i]= card;
}
i+=1;
if ( ! strcmp(sE, "N")) {
aF[i]= card;
}
i+=1;
if ( ! strcmp(sE, "S")) {
aF[i]= card;
}
i+=1;
if ( ! strcmp(sE, "P")) {
aF[i]= card;
}
i+=1;
if ( ! strcmp(sE, "Cl")) {
aF[i]= card;
}
}
/* Takes a formel- string in the form "C_16 H_32 BR79_2",
* so its Element, underscore Abundance Space or \0
* and fills array aF */
void formel2aF(int* aF, char* f) {
char sE[16] = "";
char* pE = sE;
int i=0;
int card=0;
int state = 0;
int len = strlen(f);
for (int i=0; i < 16; i++) {
aF[i]= 0;
}
while (f[i]!='\0' && i<len) {
if ((state == 0 && isalpha(f[i]) || state == 1) && f[i] != '_'){
state = 1;
*pE = f[i++];
pE++;
*pE = '\0';
} else if (f[i] == '_') {
state=2;
i += 1;
} else if (state == 2 && f[i]>='0' && f[i]<='9') {
card=card*10 + f[i++] - 48;
} else if (f[i] == ' ' || f[i] == '\0') {
storeElement(aF, sE, card);
i += 1;
state = 0;
sE[0]= '\0';
card=0;
pE= sE;
} else {
printf("Some error in %s", f);
exit(9);
}
}
storeElement(aF, sE, card);
}
int aF2formel(char* f, int* aF) {
int i=0;
f[0]= '\0';
if (aF[i])
sprintf(f+strlen(f), "C_%d ", aF[i]);
i += 1;
if (aF[i])
sprintf(f+strlen(f), "H_%d ", aF[i]);
i += 1;
if (aF[i])
sprintf(f+strlen(f), "O_%d ", aF[i]);
i += 1;
if (aF[i])
sprintf(f+strlen(f), "N_%d ", aF[i]);
i += 1;
if (aF[i])
sprintf(f+strlen(f), "S_%d ", aF[i]);
i += 1;
if (aF[i])
sprintf(f+strlen(f), "P_%d ", aF[i]);
i += 1;
if (aF[i])
sprintf(f+strlen(f), "Cl_%d ", aF[i]);
i += 1;
}
void int2aF(int* aF, int64_t iF) {
int i;
for (i=0; i < 16; i++) {
aF[i]= 0;
}
i=0;
aF[i++]= iF % limC;
iF = iF / limC;
aF[i++]= iF % limH;
iF = iF / limH;
aF[i++]= iF % limO;
iF = iF / limO;
aF[i++]= iF % limN;
iF = iF / limN;
aF[i++]= iF % limS;
iF = iF / limS;
aF[i++]= iF % limP;
iF = iF / limP;
aF[i++]= iF % limCl;
iF = iF / limCl;
}
int64_t aF2int(int* aF) {
int64_t r = 0;
int64_t m = 1LL;
int i = 0;
r += (int64_t)aF[i++] * m;
m *= limC;
printf("\t%ld, %ld\n", r, m);
r += (int64_t)aF[i++] * m;
m *= limH;
printf("\t%ld, %ld\n", r, m);
r += (int64_t)aF[i++] * m;
m *= limO;
printf("\t%ld, %ld\n", r, m);
r += (int64_t)aF[i++] * m;
m *= limN;
printf("\t%ld, %ld\n", r, m);
r += (int64_t)aF[i++] * m;
m *= limS;
printf("\t%ld, %ld\n", r, m);
r += (int64_t)aF[i++] * m;
m *= limP;
printf("\t%ld, %ld\n", r, m);
r += (int64_t)aF[i++] * m;
m *= limCl;
printf("\t%ld, %ld\n", r, m);
return r;
}
/*
double mass_mol() {
int64_t m= 0;
for (int i = 0; i < nbr_counts; ++i) {
if (counts[i].cur>0) {
m = m + counts[i].cur*counts[i].e.mass;
}
}
return m;
}
char* format_mol(char* f) {
f[0] = '\0';
for (int i = 0; i < nbr_counts; ++i) {
if (counts[i].cur>0) {
sprintf(f+strlen(f), "%s_%d ", counts[i].e.formel, counts[i].cur);
}
}
return f;
}
*/
// Mol* check_mol() {
// double
// cC=0,
// cH=0,
// cO=0,
// cN=0,
// cS=0,
// cP=0,
// cCl=0,
// cNa=0,
// db;
// double mass;
// int massidx;
// Mol* mol;
// for (int i = 0; i < nbr_counts; ++i) {
// if (0 == strcmp(counts[i].e.formel, "C")) cC= (double)counts[i].cur;
// else if (0 == strcmp(counts[i].e.formel, "H")) cH= (double)counts[i].cur;
// else if (0 == strcmp(counts[i].e.formel, "O")) cO= (double)counts[i].cur;
// else if (0 == strcmp(counts[i].e.formel, "N")) cN= (double)counts[i].cur;
// else if (0 == strcmp(counts[i].e.formel, "S")) cS= (double)counts[i].cur;
// else if (0 == strcmp(counts[i].e.formel, "P")) cP= (double)counts[i].cur;
// else if (0 == strcmp(counts[i].e.formel, "Cl")) cCl= (double)counts[i].cur;
// else if (0 == strcmp(counts[i].e.formel, "Na")) cNa= (double)counts[i].cur;
// }
// db= 1 + 0.5 * (cC *2 - cH - cCl + cN + cP);
// if (db < 0) return 0;
// if (cO / cC > 2.1) return 0;
// if (cH / cC > 8) return 0;
// if (cS > cC) return 0;
// if (cP > cC) return 0;
// if (cN > 1.25 * cC) return 0;
// mass= mass_mol();
// if (mass>(int64_t)MAX_MASS*1000000000LL) return 0;
// return 1;
// }
void main(int argc, char* *argv) {
int aF[16];
int64_t iF;
char* f="C_2999 P_99 H_29999 Cl_9 O_399 N_199 S_119";
char rf[256];
formel2aF(aF, f);
print_aF(aF);
aF2formel(rf, aF);
iF= aF2int(aF);
printf("res=%s i=%ld\n", rf, iF);
int2aF(aF, iF);
print_aF(aF);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment