-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SimpleMaterialBuilder.cpp
373 lines (310 loc) · 13.1 KB
/
SimpleMaterialBuilder.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// File modified by Deepak Samuel on 25 Sep 2019
#include "SimpleMaterialBuilder.h"
#include "ui_SimpleMaterialBuilder.h"
#include <QMessageBox>
#include <G4NistManager.hh>
#include <G4SystemOfUnits.hh>
SimpleMaterialBuilder *SimpleMaterialBuilder::instance = nullptr;
SimpleMaterialBuilder::SimpleMaterialBuilder(QWidget *parent) :
QWidget(parent),
ui(new Ui::SimpleMaterialBuilder)
{
ui->setupUi(this);
// dbManager = dbM;
// ui->g4materiallist->insertItems(0,dbManager->GetListOfMaterials());
// ui->g4elementlist->insertItems(0,dbManager->GetListOfElementSymbols());
setWindowTitle("New material");
ui->material_temp->setVisible(false);
ui->material_pressure->setVisible(false);
ui->temp_label->setVisible(false);
ui->press_label->setVisible(false);
ui->g4elementlist->hide();
ui->number_of_atoms->hide();
ui->element_label->hide();
ui->frac_mass->hide();
ui->frac_mass->setChecked(true);
}
SimpleMaterialBuilder::~SimpleMaterialBuilder()
{
delete ui;
}
void SimpleMaterialBuilder::SetDataBaseManager(SimpleDatabaseManager *dbM)
{
dbManager = dbM;
ui->g4materiallist->insertItems(0,dbManager->GetListOfMaterials());
ui->g4elementlist->insertItems(0,dbManager->GetListOfElementSymbols());
}
void SimpleMaterialBuilder::ShowUI()
{
ui->g4materiallist->clear();
ui->g4elementlist->clear();
ui->g4materiallist->insertItems(0,dbManager->GetListOfMaterials());
ui->g4elementlist->insertItems(0,dbManager->GetListOfElementSymbols());
ui->materialList->clear();
mat_names.clear();
mat_fractions.clear();
totalFrac=0.000000;
show();
}
void SimpleMaterialBuilder::SetBuildForElements(bool state)
{
// ui->build_from_elements->setChecked(state);
ui->g4materiallist->setVisible(!state);
ui->add_material->setVisible(!state);
}
void SimpleMaterialBuilder::on_build_from_elements_toggled(bool checked)
{
SetBuildForElements(checked);
}
void SimpleMaterialBuilder::on_material_stp_toggled(bool checked)
{
ui->material_temp->setVisible(!checked);
ui->material_pressure->setVisible(!checked);
ui->temp_label->setVisible(!checked);
ui->press_label->setVisible(!checked);
}
void SimpleMaterialBuilder::on_add_material_clicked()
{
//create a string of the form
if(ui->mat_composition->isChecked())
mat_names.append(ui->g4materiallist->currentText());
else {
mat_names.append(ui->g4elementlist->currentText());
}
mat_fractions.append(ui->material_fraction->value());
totalFrac=0.00000;
foreach(double d, mat_fractions)
totalFrac+=d;
if(ui->mat_composition->isChecked() | ui->frac_mass->isChecked())
ui->totfrac_label->setText(QString("Total fraction: %1").arg(totalFrac));
else {
ui->totfrac_label->setText(QString("Total atoms: %1").arg(totalFrac));
}
if(ui->elementalcomposition->isChecked() && ui->number_of_atoms->isChecked())
ui->materialList->append(QString("%2*%1").arg(mat_names.last()).arg(int(mat_fractions.last())));
else {
ui->materialList->append(QString("%2*%1").arg(mat_names.last()).arg((mat_fractions.last())));
}
}
void SimpleMaterialBuilder::on_add_element_clicked()
{
//create a string of the form
// "(name:n;density:d;temp:t;press:p; mat1*frac1 + mat2*frac + ....)"
}
void SimpleMaterialBuilder::on_clear_clicked()
{
mat_names.clear();
mat_fractions.clear();
totalFrac=0.000000;
ui->totfrac_label->setText(QString("Total: %1").arg(totalFrac));
ui->materialList->clear();
}
void SimpleMaterialBuilder::on_create_material_clicked()
{
mat_names.clear();
mat_fractions.clear();
QStringList sl = ui->materialList->toPlainText().split("\n");
foreach(QString str,sl)
{
mat_fractions.append(str.split("*").at(0).toDouble());
mat_names.append(str.split("*").at(1));
}
if(mat_names.count()<2){
QMessageBox::warning(this,"Insufficient materials to build" , "Please enter atleast 2 materials to build a composite");
return;
}
QString name = ui->material_name->text();
int state = ui->material_state->currentIndex(); //kStateUndefined kStateSolid kStateLiquid kStateGas
if(name==""){
QMessageBox::warning(this,"Empty name field" , "Please enter a name for the material");
return;
}
if(name.contains("G4_")){
QMessageBox::warning(this,"Invalid name" , "Custom material names should not contain G4_");
return;
}
double density = ui->material_density->value(); //g/cm3
double temp = -999999; // dummy temp for STP
double press = -999999; // dummy press for STP
if(!ui->material_stp->isChecked()){
temp = ui->material_temp->value(); // c
press = ui->material_pressure->value(); // atm
}
int ii=0;
QStringList mats;
foreach(QString material, mat_names)
{
mats.append(QString("%1*%2").arg(mat_fractions.at(ii++)).arg(material));
}
QString type;
if(ui->mat_composition->isChecked())
{
type="material";
}
else {
type = "element-natoms";
if(ui->frac_mass->isChecked()){
type="element-fracmass";
}
}
//"(type: t;state: s; density:d;temp:t;press:p; frac1*mat1 + frac2*mat + ....)"
QString final_command = QString("(type:%6;state:%1;density:%2;temp:%3;press:%4;%5)").arg(state).arg(density).arg(temp).arg(press).arg(mats.join("+")).arg(type);
qDebug()<<"Inserting into material database:"<<final_command;
dbManager->InsertNewMaterial(name,final_command);
close();
}
QString SimpleMaterialBuilder::GetMaterialFormula(QString name)
{
return dbManager->GetMaterialFormula(name);
}
G4Material* SimpleMaterialBuilder::GetMaterial(QString name, QString formula)
{
//format: "(type:mat;state:s; density:d;temp:t;press:p; frac1*mat1 + frac2*mat + ....)"
G4NistManager* nist = G4NistManager::Instance();
formula = formula.remove("(").remove(")");
QStringList s = formula.split(";");
qDebug()<<"New material creation from user string"<<s;
int state; //kStateUndefined kStateSolid kStateLiquid kStateGas
double density, temp, press;
int nComponents =0;
QString type;
G4Material* new_material = nullptr;
bool modify_prop_only = false;
if(s.count()!=6){
//if the material has modified property, it will have its formula as a G4Material. Check that
if(s.count()==1 && s.at(0).contains("G4_")){
modify_prop_only =true;
new_material = nist->FindOrBuildMaterial(s.at(0).toLatin1().data());
qDebug()<<"Found G4 material with property formula "<<s.at(0);
}
else{
qDebug()<<"[1] Error creating material from formula. Incorrect number of parameters. Setting material to G4_AIR";
return nist->FindOrBuildMaterial("G4_AIR");
}
}
if(!modify_prop_only){
if(s.at(0).split(":").count()==2){
type =s.at(0).split(":").at(1);
}
else{qDebug()<<"[2] Error creating material from formula (Unknown type). Setting material to G4_AIR"; return nist->FindOrBuildMaterial("G4_AIR");}
if(s.at(1).split(":").count()==2){
state =s.at(1).split(":").at(1).toInt();
}
else {qDebug()<<"[3] Error creating material from formula. Incorrect number of parameters. Setting material to G4_AIR"; return nist->FindOrBuildMaterial("G4_AIR");}
if(s.at(2).split(":").count()==2){
density =s.at(2).split(":").at(1).toDouble();
}
else{qDebug()<<"[4] Error creating material from formula. Incorrect number of parameters. Setting material to G4_AIR"; return nist->FindOrBuildMaterial("G4_AIR");}
if(s.at(3).split(":").count()==2){
temp =s.at(3).split(":").at(1).toDouble();
}
else{qDebug()<<"[5] Error creating material from formula. Incorrect number of parameters. Setting material to G4_AIR"; return nist->FindOrBuildMaterial("G4_AIR");}
if(s.at(4).split(":").count()==2){
press =s.at(4).split(":").at(1).toDouble();
}
else{qDebug()<<"[6] Error creating material from formula. Incorrect number of parameters. Setting material to G4_AIR"; return nist->FindOrBuildMaterial("G4_AIR");}
G4State g4_state= G4State::kStateUndefined;
if(state==1) g4_state = G4State::kStateSolid;
if(state==2) g4_state = G4State::kStateLiquid;
if(state==3) g4_state = G4State::kStateGas;
QStringList comps = s.at(5).split("+");
nComponents =comps.count();
QStringList mats;
QList <double> fracs;
//format: "(type:mat;state:s; density:d;temp:t;press:p; frac1*mat1 + frac2*mat + ....)"
foreach(QString comp, comps){ // gets the fracs and the mats
if(comp.split("*").count()==2){
fracs.append(comp.split("*").at(0).toDouble());
mats.append(comp.split("*").at(1));
}
else {
qDebug()<<"[7] Error creating material from formula. Incorrect number of parameters. Setting material to G4_AIR"; return nist->FindOrBuildMaterial("G4_AIR");
}
}
if(state==0 && temp<-999998.0)
new_material = new G4Material(name.toLatin1().data(), density*g/cm3, nComponents);
else if(state!=0 && temp<999998.0){
new_material = new G4Material(name.toLatin1().data(), density*g/cm3, nComponents, g4_state);
}
else {
new_material = new G4Material(name.toLatin1().data(), density*g/cm3, nComponents,g4_state,temp*kelvin, press*atmosphere);
}
for(int ii=0; ii<nComponents;ii++)
{
if(!mats.at(ii).contains("G4_")){ // this is not a standard G4 material
if(type=="material"){
QString s = QString("adding custom material %1 (%2) to main material %3").arg(mats.at(ii)).arg(fracs.at(ii)).arg(name);
qDebug()<<s;
new_material->AddMaterial(GetMaterial(mats.at(ii),dbManager->GetMaterialFormula(mats.at(ii))),fracs.at(ii));
}
else if (type=="element-fracmass") { // element
QString s = QString("adding G4Element %1 (%2) to main material %3").arg(mats.at(ii)).arg(fracs.at(ii)).arg(name);
qDebug()<<s;
new_material->AddElement(nist->FindOrBuildElement(mats.at(ii).toLatin1().data()),fracs.at(ii));
}
else if (type=="element-natoms") { // element
QString s = QString("adding G4Element %1 (%2 atoms) to main material %3").arg(mats.at(ii)).arg(fracs.at(ii)).arg(name);
qDebug()<<s;
new_material->AddElement(nist->FindOrBuildElement(mats.at(ii).toLatin1().data()),int(fracs.at(ii)));
}
}
else{
QString s = QString("adding G4Material %1 (%2) to main material %3").arg(mats.at(ii)).arg(fracs.at(ii)).arg(name);
qDebug()<<s;
new_material->AddMaterial(nist->FindOrBuildMaterial(mats.at(ii).toLatin1().data()),fracs.at(ii));
}
}
qDebug()<<"Created new material "<<new_material->GetName();
}
// check if material property exists
if(dbManager->GetMaterialProperty(name)!=""){
qDebug()<<"Updating material properties...";
SimpleMaterialPropertyBuilder::getInstance()->SetMaterialProperties(new_material,dbManager->GetMaterialProperty(name));
}
return new_material;
}
void SimpleMaterialBuilder::on_material_fraction_valueChanged(double arg1)
{
if(ui->elementalcomposition->isChecked()&& ui->number_of_atoms->isChecked())
ui->totfrac_label->setText(QString("Total atoms: %1").arg(totalFrac+ui->material_fraction->value()));
else
ui->totfrac_label->setText(QString("Total fraction: %1").arg(totalFrac+ui->material_fraction->value()));
}
void SimpleMaterialBuilder::on_addElement_clicked()
{
}
void SimpleMaterialBuilder::on_elementalcomposition_toggled(bool checked)
{
ui->g4elementlist->setVisible(checked);
ui->number_of_atoms->setVisible(checked);
ui->frac_mass->setVisible(checked);
ui->element_label->setVisible(checked);
if(checked==true)
ui->add_material->setText("Add element");
else {
ui->add_material->setText("Add Material");
}
ui->g4materiallist->setVisible(!checked);
ui->materialList->clear();
}
void SimpleMaterialBuilder::on_frac_mass_toggled(bool checked)
{
}
void SimpleMaterialBuilder::on_number_of_atoms_toggled(bool checked)
{
if(checked){
ui->fraction_label->setText("No. of atoms");
ui->material_fraction->setMinimum(1);
ui->material_fraction->setDecimals(0);
ui->material_fraction->setMaximum(99);
ui->totfrac_label->setText("Total atoms: ");
}
else{
ui->fraction_label->setText("Fraction");
ui->material_fraction->setMinimum(0.000001);
ui->material_fraction->setDecimals(6);
ui->material_fraction->setMaximum(1);
ui->totfrac_label->setText("Total fraction: ");
}
ui->materialList->clear();
}