aboutsummaryrefslogtreecommitdiff
path: root/headers/baseElement.h
blob: 42fb8e5f0615756cfd0ce4ee076ca99a110f3027 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
86
87
88
89
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef BASEELEMENT_H_INCLUDED
#define BASEELEMENT_H_INCLUDED

#include <iostream>
#include <math.h>
#ifdef _USE_GMP
#   include <gmp.h>
#   include <gmpxx.h>
#endif // _USE_GMP
#include "readElementState.h"
using namespace std;

#ifdef _USE_GMP
    typedef mpf_class bigfloat;
#   define bigfloat_unwrap(X) (bigfloat(X)).get_d()
#else
    typedef double bigfloat;
#   define bigfloat_unwrap(X) (X)
#endif // _USE_GMP

class baseElement
{
private:
    string elementName;
    bigfloat latentHeatOfFusion;
    bigfloat latentHeatOfVaporization;
    bigfloat specificHeatSolid;
    bigfloat specificHeatLiquid;
    bigfloat specificHeatGas;
    bigfloat meltingPoint;
    bigfloat boilingPoint;
    string initialState = "";
    string finalState = "";

protected:
    void setElementName(string value) { elementName = value; }
    void setLatentHeatOfFusion(double value) { latentHeatOfFusion = value; }
    void setLatentHeatOfVaporization(double value) { latentHeatOfVaporization = value; }
    void setSpecificHeatSolid(double value) { specificHeatSolid = value; }
    void setSpecificHeatLiquid(double value) { specificHeatLiquid = value; }
    void setSpecificHeatGas(double value) { specificHeatGas = value; }
    void setMeltingPoint(double value) { meltingPoint = value; }
    void setBoilingPoint(double value) { boilingPoint = value; }

public:
    string getElementName() { return elementName; }
    bigfloat getLatentHeatOfFusion() { return latentHeatOfFusion; }
    bigfloat getLatentHeatOfVaporization() { return latentHeatOfVaporization; }
    bigfloat getSpecificHeatSolid() { return specificHeatSolid; }
    bigfloat getSpecificHeatLiquid() { return specificHeatLiquid; }
    bigfloat getSpecificHeatGas() { return specificHeatGas; }
    bigfloat getMeltingPoint() { return meltingPoint; }
    bigfloat getBoilingPoint() { return boilingPoint; }

    bigfloat totalHeatNeeded(double mass, double fromTemp, double toTemp)
    {
        if (fromTemp < 0 || toTemp < 0)
        {
            cout << "Temperature cannot be less than 0 K" << endl;
            return 0;
        }

        bigfloat totalHeat = 0;
        if (fromTemp == meltingPoint || fromTemp == boilingPoint)
        {
            cout << "What is the state of the element at the initial temperature?" << endl;
            initialState = readElementState();
        }
        if (toTemp == meltingPoint || toTemp == boilingPoint)
        {
            cout << "What is the state of the element at the final temperature?" << endl;
            finalState = readElementState();
        }

        if (toTemp <= meltingPoint)
        {
            totalHeat += mass * specificHeatSolid * (toTemp - fromTemp);
            if (toTemp == meltingPoint && finalState != "Solid")
                totalHeat += mass * latentHeatOfFusion;
        }
        else if (toTemp <= boilingPoint)
        {
            if (fromTemp < meltingPoint)
                totalHeat += mass * specificHeatSolid * (meltingPoint - fromTemp);
            if (fromTemp == meltingPoint && initialState != "Liquid")
                totalHeat += mass * latentHeatOfFusion;
            if (fromTemp <= meltingPoint)
                totalHeat += mass * specificHeatLiquid * (toTemp - meltingPoint);
            else
                totalHeat += mass * specificHeatLiquid * (toTemp - fromTemp);
            if (toTemp == boilingPoint && finalState != "Liquid")
                totalHeat += mass * latentHeatOfVaporization;
        }
        else
        {
            if (fromTemp < meltingPoint)
                totalHeat += mass * specificHeatSolid * (meltingPoint - fromTemp);
            if (fromTemp == meltingPoint && initialState != "Liquid")
                totalHeat += mass * latentHeatOfFusion;

            if (fromTemp < boilingPoint)
                totalHeat += mass * specificHeatLiquid * (fromTemp - meltingPoint);
            if (fromTemp == boilingPoint && initialState != "Gas")
                totalHeat += mass * latentHeatOfVaporization;

            if (fromTemp > boilingPoint)
                totalHeat += mass * specificHeatGas * (toTemp - fromTemp);
            else
                totalHeat += mass * specificHeatGas * (toTemp - boilingPoint);
        }
        return totalHeat;
    }

    bigfloat totalEntropyChange(double mass, double fromTemp, double toTemp)
    {
        if (fromTemp < 0 || toTemp < 0)
        {
            cout << "Temperature cannot be less than 0 K" << endl;
            return 0;
        }

        bigfloat totalEntropy = 0;
        if (fromTemp == meltingPoint || fromTemp == boilingPoint)
        {
            cout << "What is the state of the element at the initial temperature?" << endl;
            if (initialState != "")
                initialState = readElementState();
        }
        if (toTemp == meltingPoint || toTemp == boilingPoint)
        {
            cout << "What is the state of the element at the final temperature?" << endl;
            if (finalState != "")
                finalState = readElementState();
        }

        if (toTemp <= meltingPoint)
        {
            totalEntropy += mass * specificHeatSolid * log(bigfloat_unwrap(toTemp / fromTemp));
            if (toTemp == meltingPoint && finalState != "Solid")
                totalEntropy += mass * latentHeatOfFusion / meltingPoint;
        }
        else if (toTemp <= boilingPoint)
        {
            if (fromTemp < meltingPoint)
                totalEntropy += mass * specificHeatSolid * log(bigfloat_unwrap(meltingPoint / fromTemp));
            if (fromTemp == meltingPoint && initialState != "Liquid")
                totalEntropy += mass * latentHeatOfFusion / meltingPoint;
            if (fromTemp <= meltingPoint)
                totalEntropy += mass * specificHeatLiquid * log(bigfloat_unwrap(toTemp / meltingPoint));
            else
                totalEntropy += mass * specificHeatLiquid * log(bigfloat_unwrap(toTemp / fromTemp));
            if (toTemp == boilingPoint && finalState != "Liquid")
                totalEntropy += mass * latentHeatOfVaporization / boilingPoint;
        }
        else
        {
            if (fromTemp < meltingPoint)
                totalEntropy += mass * specificHeatSolid * log(bigfloat_unwrap(meltingPoint / fromTemp));
            if (fromTemp == meltingPoint && initialState != "Liquid")
                totalEntropy += mass * latentHeatOfFusion / meltingPoint;

            if (fromTemp < boilingPoint)
                totalEntropy += mass * specificHeatLiquid * log(bigfloat_unwrap(fromTemp / meltingPoint));
            if (fromTemp == boilingPoint && initialState != "Gas")
                totalEntropy += mass * latentHeatOfVaporization / boilingPoint;

            if (fromTemp > boilingPoint)
                totalEntropy += mass * specificHeatGas * log(bigfloat_unwrap(toTemp / fromTemp));
            else
                totalEntropy += mass * specificHeatGas * log(bigfloat_unwrap(toTemp / boilingPoint));
        }
        return totalEntropy;
    }
};
#endif // BASEELEMENT_H_INCLUDED