Commit debc4bee authored by Dorchies David's avatar Dorchies David
Browse files

Merge branch '160-ajout-du-module-mathematique-y-a-x-b' into 'master'

Resolve "Ajout du module mathématique "y = a.x + b""

Closes #160

See merge request cassiopee/jalhyd!49
parents 3f52eaab fabb7fed
import { CalculatorType } from "../src/compute-node";
import { Grille, Session } from "../src/index";
import { Grille, ParamDomainValue, Session, SPP, YAXN, YAXNParams } from "../src/index";
import { MacrorugoCompound } from "../src/macrorugo/macrorugo_compound";
import { Nub } from "../src/nub";
import { CloisonAval } from "../src/pab/cloison_aval";
......@@ -9,6 +9,7 @@ import { Props } from "../src/props";
import { SectionNub } from "../src/section/section_nub";
import { CreateStructure } from "../src/structure/factory_structure";
import { ParallelStructure } from "../src/structure/parallel_structure";
import { Trigo, TrigoOperation } from "../src/trigo";
import { checkResultConsistency, SetJasmineCurrentSpec } from "./test_func";
/**
......@@ -38,7 +39,8 @@ const nubsNotTested: CalculatorType[] = [
CalculatorType.Structure,
CalculatorType.Section,
CalculatorType.CloisonAval,
CalculatorType.Solveur
CalculatorType.Solveur,
CalculatorType.YAXN
];
const nubsWithStructures: CalculatorType[] = [
......@@ -60,15 +62,34 @@ const calTypes =
).map((e) => +e);
function randomizeParameter(p: ParamDefinition) {
p.singleValue = Math.max(-1E6, p.domain.minValue)
let min = p.domain.minValue;
let max = p.domain.maxValue;
// special case for SPP to avoid +/-Infinity with high powers
if (p.parentNub instanceof YAXN && p.symbol === "N") {
min = -10;
max = 10;
}
// special case for Trigo to avoid +/-Infinity with cosh/sinh
if (
p.parentNub instanceof Trigo
&& p.symbol === "X"
&& [ TrigoOperation.COSH, TrigoOperation.SINH ].includes((p.parentNub as Trigo).operation)
) {
min = -500;
max = 500;
}
p.singleValue = Math.max(-1E6, min)
+ Math.random()
* (
Math.min(1E6, p.domain.maxValue)
- Math.max(-1E6, p.domain.minValue)
Math.min(1E6, max)
- Math.max(-1E6, min)
);
if (Math.random() < fuzzyCfg.propInvertedPrm) {
p.singleValue = 1 / p.currentValue;
}
if (p.domain.domain === ParamDomainValue.INTEGER) {
p.singleValue = Math.floor(p.singleValue);
}
}
function printPrms(n: Nub): string {
......@@ -100,6 +121,32 @@ function setRandomSection(sn: SectionNub) {
sn.setSection(newSect);
}
function setRandomTrigoOperation(sn: Trigo) {
const op = Math.floor(Math.random() * 6);
sn.operation = op;
}
function setRandomTrigoUnit(sn: Trigo) {
const un = Math.floor(Math.random() * 2);
sn.unit = un;
}
function setRandomSppOperation(sn: SPP) {
const op = Math.floor(Math.random() * 2);
sn.operation = op;
}
function addRandomYAXNs(n: SPP, nYMax: number = 3) {
const nY = Math.floor(Math.random() * nYMax) + 1;
for (let i = 0; i < nY; i++) {
n.addChild(
new YAXN(
new YAXNParams(Math.random() * 10, Math.random() * 10, Math.floor(Math.random() * 10))
)
);
}
}
function setPab(pab: Pab, nClMax = 30, nStMax = 3) {
const nCl = Math.floor(Math.random() * nClMax) + 1;
for (let i = 0; i < nCl; i++) {
......@@ -149,6 +196,14 @@ function CreateTestNub(iCalType: number): Nub {
if (iCalType === CalculatorType.Bief) {
setRandomSection(n as SectionNub);
}
if (iCalType === CalculatorType.Trigo) {
setRandomTrigoOperation(n as Trigo);
setRandomTrigoUnit(n as Trigo);
}
if (iCalType === CalculatorType.SPP) {
addRandomYAXNs(n as SPP);
setRandomSppOperation(n as SPP);
}
for (const p of n.parameterIterator) {
if (p.visible) {
randomizeParameter(p);
......@@ -175,8 +230,7 @@ describe("Fuzz testing", () => {
}
});
for (const iCalType of calTypes) {
// if ([CalculatorType.MacroRugoCompound].includes(iCalType)) {
if (!nubsNotTested.includes(iCalType)) {
if (!nubsNotTested.includes(iCalType) /* && iCalType === CalculatorType.Trigo */) {
describe(CalculatorType[iCalType], () => {
for (let i = 0; i < fuzzyCfg.nTests; i++) {
describe(`Test ${i}`, () => {
......
import { Session } from "../src/session";
import { SPP, SPPOperation } from "../src/spp";
import { SPPParams } from "../src/spp_params";
import { CreateStructure } from "../src/structure/factory_structure";
import { ParallelStructure } from "../src/structure/parallel_structure";
import { ParallelStructureParams } from "../src/structure/parallel_structure_params";
import { LoiDebit } from "../src/structure/structure_props";
import { MessageCode } from "../src/util/message";
import { YAXN } from "../src/yaxn";
import { YAXNParams } from "../src/yaxn_params";
describe("Class SPP: ", () => {
describe("Calc Y (sum): ", () => {
it("Y = sum([ 3*2^4, 7*1^0 ]) should be 55", () => {
const nub = new SPP(new SPPParams(1));
nub.addChild(new YAXN(new YAXNParams(3, 2, 4)));
nub.addChild(new YAXN(new YAXNParams(7, 1, 0)));
nub.operation = SPPOperation.SUM;
nub.CalcSerie();
expect(nub.result.vCalc).toBe(55);
});
});
describe("Calc Y (product): ", () => {
it("Y = product([ 3*2^4, 7*1^0 ]) should be 336", () => {
const nub = new SPP(new SPPParams(1));
nub.addChild(new YAXN(new YAXNParams(3, 2, 4)));
nub.addChild(new YAXN(new YAXNParams(7, 1, 0)));
nub.operation = SPPOperation.PRODUCT;
nub.CalcSerie();
expect(nub.result.vCalc).toBe(336);
});
});
describe("Calc some X (sum): ", () => {
it("sum([ 3*X^4, 7*1^0 ]) = 55 - X should be 2", () => {
const nub = new SPP(new SPPParams(55));
nub.addChild(new YAXN(new YAXNParams(3, 5, 4)));
nub.addChild(new YAXN(new YAXNParams(7, 1, 0)));
nub.operation = SPPOperation.SUM;
const c = nub.getChildren()[0] as YAXN;
nub.calculatedParam = c.prms.X;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(2, 3);
});
});
describe("Calc some Y (product): ", () => {
it("product([ 3*X^4, 7*1^0 ]) = 336 - X should be 2", () => {
const nub = new SPP(new SPPParams(336));
nub.addChild(new YAXN(new YAXNParams(3, 17, 4)));
nub.addChild(new YAXN(new YAXNParams(7, 1, 0)));
nub.operation = SPPOperation.PRODUCT;
const c = nub.getChildren()[0] as YAXN;
nub.calculatedParam = c.prms.X;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(2, 3);
});
});
describe("Link: ", () => {
it("all parameters must be linkable to Y and children A, X, N in both ways", () => {
const spp = new SPP(new SPPParams(1));
spp.addChild(new YAXN(new YAXNParams(3, 666, 4)));
spp.addChild(new YAXN(new YAXNParams(7, 1, 0)));
spp.addChild(new YAXN(new YAXNParams(5, 4, 3)));
const lo = new ParallelStructure(new ParallelStructureParams(1.2, 102, 101));
lo.addChild(CreateStructure(LoiDebit.GateCem88d, lo));
Session.getInstance().clear();
Session.getInstance().registerNubs([ spp, lo ]);
// SPP.Y should be linkable to all ParallelStructures params
const lpY = Session.getInstance().getLinkableValues(spp.prms.Y);
expect(lpY.length).toBe(7);
// all 3 YAXN.X should be linkable to all ParallelStructures params
for (const c of spp.getChildren()) {
const lp = Session.getInstance().getLinkableValues((c as YAXN).prms.X);
expect(lp.length).toBe(13); // 7, plus siblings params
}
// each ParallelStructures param should be linkable to SPP.Y and all 3 YAXN.X
for (const p of lo.parameterIterator) {
const lpLO = Session.getInstance().getLinkableValues(p);
expect(lpLO.length).toBe(10);
}
});
});
it("Calc(X) find negative C for integer N", () => {
const spp = new SPP(new SPPParams(-27));
const c1 = new YAXN(new YAXNParams(1, 666, 3));
spp.addChild(c1);
spp.calculatedParam = c1.prms.X;
expect(spp.CalcSerie().vCalc).toBeCloseTo(-3, 3);
});
describe("error cases − ", () => {
it("Calc(Y), non-integer N on negative X in child should lead to error log", () => {
const spp = new SPP(new SPPParams(666));
const c1 = new YAXN(new YAXNParams(1, -4, 3.2));
spp.addChild(c1);
spp.calculatedParam = spp.prms.Y;
const res = spp.CalcSerie();
expect(res.vCalc).toBeUndefined();
expect(res.log.messages.length).toBe(2);
expect(res.log.messages[0].code).toBe(MessageCode.ERROR_SOMETHING_FAILED_IN_CHILD);
expect(c1.result.log.messages.length).toBe(1);
expect(c1.result.log.messages[0].code).toBe(MessageCode.ERROR_NON_INTEGER_POWER_ON_NEGATIVE_NUMBER);
});
it("Calc(X), non-integer N on negative X in non-calculated child should lead to error log", () => {
const spp = new SPP(new SPPParams(536636.0711158155));
const c1 = new YAXN(new YAXNParams(-495973.326926908, 986950.0910993216, 3.8095699869980315));
const c2 = new YAXN(new YAXNParams(694194.424256592, -985042.6786260167, -0.22419216942446418));
spp.addChild(c1);
spp.addChild(c2);
spp.calculatedParam = c1.prms.X;
const res = spp.CalcSerie();
expect(res.vCalc).toBeUndefined();
expect(res.ok).toBe(false);
expect(res.log.messages.length).toBe(2);
expect(res.log.messages[0].code).toBe(MessageCode.ERROR_SOMETHING_FAILED_IN_CHILD);
expect(c2.result.log.messages.length).toBe(1);
expect(c2.result.log.messages[0].code).toBe(MessageCode.ERROR_NON_INTEGER_POWER_ON_NEGATIVE_NUMBER);
});
it("Calc(X), non-integer N on negative X in calculated child should lead to error log", () => {
const spp = new SPP(new SPPParams(536636.0711158155));
const c1 = new YAXN(new YAXNParams(-495973.326926908, 986950.0910993216, 3.8095699869980315));
const c2 = new YAXN(new YAXNParams(694194.424256592, -985042.6786260167, -0.22419216942446418));
spp.addChild(c1);
spp.addChild(c2);
spp.calculatedParam = c2.prms.X;
const res = spp.CalcSerie();
expect(res.vCalc).toBeUndefined();
expect(res.ok).toBe(false);
expect(res.log.messages.length).toBe(1);
expect(res.log.messages[0].code).toBe(MessageCode.ERROR_NON_INTEGER_POWER_ON_NEGATIVE_NUMBER);
expect(c2.result.log.messages.length).toBe(1);
expect(c2.result.log.messages[0].code).toBe(MessageCode.ERROR_NON_INTEGER_POWER_ON_NEGATIVE_NUMBER);
});
it("division by zero in parent should lead to error log", () => {
const spp = new SPP(new SPPParams(1));
spp.operation = SPPOperation.PRODUCT;
const c1 = new YAXN(new YAXNParams(2, 666, 3));
spp.addChild(c1);
const c2 = new YAXN(new YAXNParams(0, 1, 1));
spp.addChild(c2);
spp.calculatedParam = c1.prms.X;
const res = spp.CalcSerie();
expect(res.vCalc).toBeUndefined();
expect(res.log.messages.length).toBe(1);
expect(res.log.messages[0].code).toBe(MessageCode.ERROR_DIVISION_BY_ZERO);
});
});
});
import { Session } from "../src/session";
import { CreateStructure } from "../src/structure/factory_structure";
import { ParallelStructure } from "../src/structure/parallel_structure";
import { ParallelStructureParams } from "../src/structure/parallel_structure_params";
import { LoiDebit } from "../src/structure/structure_props";
import { Trigo, TrigoOperation, TrigoUnit } from "../src/trigo";
import { TrigoParams } from "../src/trigo_params";
describe("Class Trigo: ", () => {
describe("Calc Y with degrees: ", () => {
it("Y = cos(X) should be 0.985", () => {
const nub = new Trigo(new TrigoParams(1.5, 10));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.DEG;
nub.operation = TrigoOperation.COS;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(0.985, 3);
});
it("Y = sin(X) should be 0.174", () => {
const nub = new Trigo(new TrigoParams(1.5, 10));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.DEG;
nub.operation = TrigoOperation.SIN;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(0.174, 3);
});
it("Y = tan(X) should be 0.176", () => {
const nub = new Trigo(new TrigoParams(1.5, 10));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.DEG;
nub.operation = TrigoOperation.TAN;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(0.176, 3);
});
it("Y = cosh(X) should be 1.015", () => {
const nub = new Trigo(new TrigoParams(1.5, 10));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.DEG;
nub.operation = TrigoOperation.COSH;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(1.015, 3);
});
it("Y = sinh(X) should be 0.175", () => {
const nub = new Trigo(new TrigoParams(1.5, 10));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.DEG;
nub.operation = TrigoOperation.SINH;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(0.175, 3);
});
it("Y = tanh(X) should be 0.173", () => {
const nub = new Trigo(new TrigoParams(1.5, 10));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.DEG;
nub.operation = TrigoOperation.TANH;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(0.173, 3);
});
});
describe("Calc Y with radians: ", () => {
it("Y = cos(X) should be 0.540", () => {
const nub = new Trigo(new TrigoParams(1.5, 1));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.RAD;
nub.operation = TrigoOperation.COS;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(0.540, 3);
});
it("Y = sin(X) should be 0.841", () => {
const nub = new Trigo(new TrigoParams(1.5, 1));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.RAD;
nub.operation = TrigoOperation.SIN;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(0.841, 3);
});
it("Y = tan(X) should be 1.557", () => {
const nub = new Trigo(new TrigoParams(1.5, 1));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.RAD;
nub.operation = TrigoOperation.TAN;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(1.557, 3);
});
it("Y = cosh(X) should be 1.543", () => {
const nub = new Trigo(new TrigoParams(1.5, 1));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.RAD;
nub.operation = TrigoOperation.COSH;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(1.543, 3);
});
it("Y = sinh(X) should be 1.175", () => {
const nub = new Trigo(new TrigoParams(1.5, 1));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.RAD;
nub.operation = TrigoOperation.SINH;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(1.175, 3);
});
it("Y = tanh(X) should be 0.762", () => {
const nub = new Trigo(new TrigoParams(1.5, 1));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.RAD;
nub.operation = TrigoOperation.TANH;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(0.762, 3);
});
});
describe("Calc X: ", () => {
for (const un of [ TrigoUnit.DEG, TrigoUnit.RAD ]) {
for (const op of [
TrigoOperation.COS, TrigoOperation.SIN, TrigoOperation.TAN,
TrigoOperation.COSH, TrigoOperation.SINH, TrigoOperation.TANH
]) {
it("X = acos(Y = cos(X)) should be X", () => {
const nub = new Trigo(new TrigoParams(0, 1.5));
nub.calculatedParam = nub.prms.Y;
nub.unit = un;
nub.operation = op;
nub.CalcSerie();
nub.prms.Y.singleValue = nub.result.vCalc;
nub.prms.X.singleValue = 666;
nub.calculatedParam = nub.prms.X;
nub.CalcSerie();
expect(nub.result.vCalc).toBeCloseTo(1.5, 5);
});
}
}
});
describe("Link: ", () => {
it("all parameters must be linkable to Y and X in both ways", () => {
const trig = new Trigo(new TrigoParams(1.5, 10));
const lo = new ParallelStructure(new ParallelStructureParams(1.2, 102, 101));
lo.addChild(CreateStructure(LoiDebit.GateCem88d, lo));
Session.getInstance().clear();
Session.getInstance().registerNubs([ trig, lo ]);
// each Trigo param should be linkable to all ParallelStructures params
expect(Session.getInstance().getLinkableValues(trig.prms.Y).length).toBe(7);
expect(Session.getInstance().getLinkableValues(trig.prms.X).length).toBe(7);
// each ParallelStructures param should be linkable to all Trigo params
for (const p of lo.parameterIterator) {
expect(Session.getInstance().getLinkableValues(p).length).toBe(2);
}
});
});
it("Calculated param should be Y", () => {
const trig = new Trigo(new TrigoParams(1.5, 10));
expect(trig.calculatedParam.symbol).toBe("Y");
});
it ("definition domains depending on operation", () => {
const nub = new Trigo(new TrigoParams(1.5, 1));
nub.calculatedParam = nub.prms.Y;
nub.unit = TrigoUnit.RAD;
// RAD / COS (COS is default operation)
expect(nub.prms.Y.domain.interval.min).toBe(-1);
expect(nub.prms.Y.domain.interval.max).toBe(1);
// RAD / TAN
nub.operation = TrigoOperation.TAN;
expect(nub.prms.Y.domain.interval.min).toBe(-Infinity);
expect(nub.prms.Y.domain.interval.max).toBe(Infinity);
// RAD / SIN
nub.operation = TrigoOperation.SIN;
expect(nub.prms.Y.domain.interval.min).toBe(-1);
expect(nub.prms.Y.domain.interval.max).toBe(1);
// RAD / SINH
nub.operation = TrigoOperation.SINH;
expect(nub.prms.Y.domain.interval.min).toBe(-Infinity);
expect(nub.prms.Y.domain.interval.max).toBe(Infinity);
// RAD / TANH
nub.operation = TrigoOperation.TANH;
expect(nub.prms.Y.domain.interval.min).toBe(-0.99999999);
expect(nub.prms.Y.domain.interval.max).toBe(0.99999999);
// RAD / COSH
nub.operation = TrigoOperation.COSH;
expect(nub.prms.Y.domain.interval.min).toBe(1);
expect(nub.prms.Y.domain.interval.max).toBe(Infinity);
});
});
import { Session } from "../src/session";
import { CreateStructure } from "../src/structure/factory_structure";
import { ParallelStructure } from "../src/structure/parallel_structure";
import { ParallelStructureParams } from "../src/structure/parallel_structure_params";
import { LoiDebit } from "../src/structure/structure_props";
import { MessageCode } from "../src/util/message";
import { YAXB } from "../src/yaxb";
import { YAXBParams } from "../src/yaxb_params";
describe("Class YAXB: ", () => {
describe("Calc: ", () => {
it("Y should be 10", () => {
const nub = new YAXB(new YAXBParams(666, 2, 3, 4));
nub.calculatedParam = nub.prms.Y;
nub.CalcSerie();
expect(nub.result.vCalc).toBe(10);
});
it("A should be 2", () => {
const nub = new YAXB(new YAXBParams(10, 666, 3, 4));
nub.calculatedParam = nub.prms.A;
nub.CalcSerie();
expect(nub.result.vCalc).toBe(2);
});
it("X should be 3", () => {
const nub = new YAXB(new YAXBParams(10, 2, 666, 4));
nub.calculatedParam = nub.prms.X;
nub.CalcSerie();
expect(nub.result.vCalc).toBe(3);
});
it("B should be 4", () => {
const nub = new YAXB(new YAXBParams(10, 2, 3, 666));
nub.calculatedParam = nub.prms.B;
nub.CalcSerie();
expect(nub.result.vCalc).toBe(4);
});
it("calc A with X = 0 should trigger error", () => {
const nub = new YAXB(new YAXBParams(10, 666, 0, 4));
nub.calculatedParam = nub.prms.A;
nub.CalcSerie();
expect(nub.result.ok).toBe(false);
expect(nub.result.resultElement.log.messages.length).toBe(1);
expect(nub.result.resultElement.log.messages[0].code).toBe(MessageCode.ERROR_DIVISION_BY_ZERO);
});
it("calc X with A = 0 should trigger error", () => {
const nub = new YAXB(new YAXBParams(10, 0, 666, 4));
nub.calculatedParam = nub.prms.X;
nub.CalcSerie();
expect(nub.result.ok).toBe(false);
expect(nub.result.resultElement.log.messages.length).toBe(1);
expect(nub.result.resultElement.log.messages[0].code).toBe(MessageCode.ERROR_DIVISION_BY_ZERO);
});
});
describe("Link: ", () => {
it("all parameters must be linkable to Y,A,X,B in both ways", () => {
const yaxb = new YAXB(new YAXBParams(10, 2, 3, 4));
const lo = new ParallelStructure(new ParallelStructureParams(1.2, 102, 101));
lo.addChild(CreateStructure(LoiDebit.GateCem88d, lo));
Session.getInstance().clear();
Session.getInstance().registerNubs([ yaxb, lo ]);
// each YAXB param should be linkable to all ParallelStructures params
expect(Session.getInstance().getLinkableValues(yaxb.prms.Y).length).toBe(7);
expect(Session.getInstance().getLinkableValues(yaxb.prms.A).length).toBe(7);
expect(Session.getInstance().getLinkableValues(yaxb.prms.X).length).toBe(7);
expect(Session.getInstance().getLinkableValues(yaxb.prms.B).length).toBe(7);
// each ParallelStructures param should be linkable to all YAXB params
for (const p of lo.parameterIterator) {
expect(Session.getInstance().getLinkableValues(p).length).toBe(4);
}
});
});
});