Sp: Normalization.Minerals

Calculation form Excel table. To 3 cations with optional Fe_p3_pfu calculation from charge balance.

Details

Reference
Input SiO2_wtp, TiO2_wtp, Al2O3_wtp, Cr2O3_wtp, Fe2O3_wtp, FeO_wtp, MnO_wtp, MgO_wtp
Output Si_pfu, Ti_pfu, Al_pfu, Cr_pfu, Fe_pfu, Fe_p2_wtp, Fe_p3_wtp, Mn_pfu, Mg_pfu

Использовать Проверить

  • заголовок
  • краткое описание
  • статья
  • ссылка
  • карточка
  • подробное описание
  • код
  • тест

Source

recalc.sp_nm <- function (wtp_data, ...) {

    #Names preparation
    ox_tnames <- c('SiO2',    'TiO2',     'Al2O3',     'Cr2O3',     'Fe2O3',     'FeO',       'MnO',     'MgO');
    ox_names <-  c('SiO2_wtp','TiO2_wtp', 'Al2O3_wtp', 'Cr2O3_wtp', 'Fe2O3_wtp', 'FeO_wtp',   'MnO_wtp', 'MgO_wtp');
    pfu_names <- c('Si_pfu',  'Ti_pfu',   'Al_pfu',    'Cr_pfu',    'Fe_p3_pfu', 'Fe_p2_pfu', 'Mn_pfu',  'Mg_pfu');

    #Periodic table load
    elem <- read.csv("constants_nmperiodic.csv",stringsAsFactors=F, dec = ".");
    tmelem <- data.frame(t(elem), stringsAsFactors = F);
    names(tmelem) <- elem$oxide;
    telem  <- selectNames(tmelem,ox_tnames);
    names(telem) <- ox_names;

    #Select columns
    wtp_group <- selectNames(wtp_data,ox_names);

    mole_cations <- rowApply('*', wtp_group, as.numeric(telem['ncations',]));
    mole_cations <- rowApply('/', mole_cations, as.numeric(telem['mole_wt',]));

    mole_oxigen <- rowApply('*', wtp_group, as.numeric(telem['noxygen',]));
    mole_oxigen <- rowApply('/', mole_oxigen, as.numeric(telem['mole_wt',]));

    norm_cations <- mole_cations;

    for(i in 1:nrow(norm_cations)) {
        if(norm_cations[i,]$Fe2O3_wtp>0) {
            norm_cations[i,] = norm_cations[i,] * 4/rowSums(mole_oxigen)[i];
        } else {
            norm_cations[i,] = norm_cations[i,] * 3/rowSums(mole_cations)[i];        
        }
    }
    
    norm_oxygens <- norm_cations;
    norm_oxygens <- rowApply('*', norm_oxygens, as.numeric(telem['noxygen',]));
    norm_oxygens <- rowApply('/', norm_oxygens, as.numeric(telem['ncations',]));

    names(norm_cations) <- pfu_names;
    charge <- 4*(norm_cations$Si_pfu+norm_cations$Ti_pfu)+
              3*(norm_cations$Al_pfu+norm_cations$Cr_pfu+norm_cations$Fe_p3_pfu)+
              2*(norm_cations$Fe_p2_pfu+norm_cations$Mn_pfu+norm_cations$Mg_pfu);
              
    pfu_units <- norm_cations;

    fecharge <- ifelse(8-charge>0,8-charge,0);
    pfu_units$Fe_p3_pfu <- ifelse(pfu_units$Fe_p3_pfu > 0, pfu_units$Fe_p3_pfu, fecharge);
    pfu_units$Fe_p2_pfu <- ifelse(pfu_units$Fe_p3_pfu > 0, pfu_units$Fe_p2_pfu, norm_cations$Fe_p2_pfu - fecharge);

    endcharge <- 4*(pfu_units$Si_pfu+pfu_units$Ti_pfu)+
                 3*(pfu_units$Al_pfu+pfu_units$Cr_pfu+pfu_units$Fe_p3_pfu)+
                 2*(pfu_units$Fe_p2_pfu+pfu_units$Mn_pfu+pfu_units$Mg_pfu);

    endcharge<- 8-endcharge;

    rownames <- data.frame(Name = wtp_data$Name);
    qcheck <- data.frame(ChargeDisbalance = endcharge);
 
    return (cbind(rownames, pfu_units, qcheck));
}

recalc.sp_nm.test <- function () {
    test <- data.frame(Name=c("Sp1","Sp2"), SiO2_wtp=c(0.1,0.1), TiO2_wtp=c(36.14, 36.14), Al2O3_wtp=c(0.98,0.98), FeO_wtp=c(49.38, 49.38), Fe2O3_wtp=c(0, 1), MnO_wtp=c(0.34, 0.34), MgO_wtp=c(8.35, 8.35));
    result <- recalc.sp_nm(test);

    expect_equal(object = result[1,]$Fe_p2_pfu, 1.502, tolerance = 0.001);
    expect_equal(object = result[2,]$Ti_pfu, 0.975, tolerance = 0.001);
}