Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix electron representations #11

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/models/Chemistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,9 @@ function checkNodesEqual(test: ASTNode, target: ASTNode, response: CheckerRespon
}
}
else if (isElectron(test) && isElectron(target)) {
// There is no need to check electrons, they are always equal
// Electrons have no properties to test equivalence of, but charge must still be aggregated
response.termChargeCount = (response.termChargeCount ?? 0) - 1;

return response;
}
else if (isTerm(test) && isTerm(target)) {
Expand Down
72 changes: 32 additions & 40 deletions src/models/Nuclear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,40 +130,24 @@ export function augment(ast: NuclearAST): NuclearAST {
}
}

const particleMassAtomic: { [key in ParticleString]: [number, number] } = {
alphaparticle: [4, 2],
betaparticle: [0, -1],
gammaray: [0, 0],
neutrino: [0, 0],
antineutrino: [0, 0],
electron: [0, -1],
positron: [0, 1],
neutron: [1, 0],
proton: [1, 1],
};

function isValidAtomicNumber(test: Particle | Isotope): boolean {
if (isIsotope(test)) {
return chemicalSymbol.indexOf(test.element) + 1 === test.atomic &&
test.mass > test.atomic;
}
switch(test.particle) {
case "alphaparticle":
return test.mass === 4 &&
test.atomic === 2;
case "betaparticle":
return test.mass === 0 &&
test.atomic === -1;
case "gammaray":
return test.mass === 0 &&
test.atomic === 0;
case "neutrino":
return test.mass === 0 &&
test.atomic === 0;
case "antineutrino":
return test.mass === 0 &&
test.atomic === 0;
case "electron":
return test.mass === 0 &&
test.atomic === -1;
case "positron":
return test.mass === 0 &&
test.atomic === 1;
case "neutron":
return test.mass === 1 &&
test.atomic === 0;
case "proton":
return test.mass === 1 &&
test.atomic === 1;
}
return particleMassAtomic[test.particle][0] === test.mass && particleMassAtomic[test.particle][1] === test.atomic;
}

function checkParticlesEqual(test: Particle, target: Particle): boolean {
Expand All @@ -174,6 +158,13 @@ function checkParticlesEqual(test: Particle, target: Particle): boolean {
}
}

function missingMassAtomicError(response: CheckerResponse): CheckerResponse {
response.containsError = true;
response.error = "Check that all atoms have a mass and atomic number!";
response.isEqual = false;
return response;
}

const STARTING_RESPONSE: (options?: ChemistryOptions) => CheckerResponse = (options) => { return {
isNuclear: true,
containsError: false,
Expand All @@ -190,12 +181,16 @@ const STARTING_RESPONSE: (options?: ChemistryOptions) => CheckerResponse = (opti

function checkNodesEqual(test: ASTNode, target: ASTNode, response: CheckerResponse): CheckerResponse {
if (isParticle(test) && isParticle(target)) {
// Answers can be entered without a mass or atomic number. However, this is always wrong so we throw an error
if (test.mass === null || test.atomic === null) {
response.containsError = true;
response.error = "Check that all atoms have a mass and atomic number!"
response.isEqual = false;
return response;
// Answers can be entered without a mass or atomic number. Particles with 0 mass e.g. electrons can be validly represented this way so a conversion is made
if (test.mass === null && test.atomic === null) {
if (particleMassAtomic[test.particle][0] === 0) {
test.mass = particleMassAtomic[test.particle][0];
test.atomic = particleMassAtomic[test.particle][1];
} else {
return missingMassAtomicError(response);
}
} else if (test.mass === null || test.atomic === null) {
return missingMassAtomicError(response);
}

response.validAtomicNumber = (response.validAtomicNumber === true) && isValidAtomicNumber(test);
Expand All @@ -214,12 +209,9 @@ function checkNodesEqual(test: ASTNode, target: ASTNode, response: CheckerRespon

return response;
} else if (isIsotope(test) && isIsotope(target)) {
// Answers can be entered without a mass or atomic number. However, this is always wrong so we throw an error
// Answers can be entered without a mass or atomic number. However, this is always wrong for isotopes so we always throw an error
if (test.mass === null || test.atomic === null) {
response.containsError = true;
response.error = "Check that all atoms have a mass and atomic number!"
response.isEqual = false;
return response;
return missingMassAtomicError(response);
}

response.sameElements = response.sameElements && test.element === target.element;
Expand Down
Loading