Skip to content

Commit

Permalink
Merge pull request #6 from enflujo/simulacion_avances
Browse files Browse the repository at this point in the history
Simulacion avances
  • Loading branch information
1cgonza authored Mar 20, 2024
2 parents 3f9b181 + 6ef509a commit 3e0874b
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 74 deletions.
Binary file not shown.
12 changes: 8 additions & 4 deletions aplicaciones/procesador/fuente/aplicacion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getXlsxStream } from 'xlstream';
import { guardarJSON } from './utilidades/ayudas';
import { DatosInclusion } from '../../../tipos/compartidos';

const nombreArchivo = 'Inclusion scores nationwide';
const nombreArchivo = 'Inclusion scores nationwide180324';
const nombreArchivoPoblacion = 'Censo_nacional_de_poblacion_2018_mun';

type Fila = [
Expand All @@ -16,8 +16,10 @@ type Fila = [
departamento: string,
/** Código del departamento */
codigoDep: number,
/** Ranking de inclusión */
valorRank: number,
/** Índice de inclusión */
valor: number,
valorIndice: number,
/** Índice de encuestados */
indiceEncuestado: number,
];
Expand Down Expand Up @@ -67,6 +69,7 @@ async function inicio() {
});

flujo.on('data', (fila) => {
console.log(fila);
if (numeroFila === 1) {
total = fila.totalSheetSize;
}
Expand All @@ -92,7 +95,7 @@ async function inicio() {
}

function procesarFila(fila: Fila, numeroFila: number) {
const [nombreMun, codMun, nombreDep, codDep, valor, indiceEncuestado] = fila;
const [nombreMun, codMun, nombreDep, codDep, valorRank, valorIndice, indiceEncuestado] = fila;
const mun = municipios.datos.find((municipio) => +municipio[3] === codMun);

if (!mun) {
Expand All @@ -110,7 +113,8 @@ async function inicio() {
datos.push({
nombre: mun[1],
dep: dep[1],
valor,
valorRank,
valorIndice,
encuestado: !!indiceEncuestado,
latitud: dep[2],
longitud: dep[3],
Expand Down
2 changes: 1 addition & 1 deletion aplicaciones/www/estaticos/inclusion-municipios.json

Large diffs are not rendered by default.

41 changes: 22 additions & 19 deletions aplicaciones/www/fuente/Bola.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class Bola {
largoMuro: number;
//rotacion: number;
parabola: number;
corono: boolean;

constructor(
posX: number,
Expand Down Expand Up @@ -47,6 +48,8 @@ export default class Bola {
// this.posX = 250;
// }
// }

this.corono = false;
}

// mover/dibujar básico
Expand All @@ -71,7 +74,7 @@ export default class Bola {

this.parabola += 0.01;
}
this.color = 'rgb(4, 149, 168)';
this.color = 'rgb(4, 149, 168)'; // Color de las bolas cuando pasan al otro lado
}
}

Expand Down Expand Up @@ -108,18 +111,18 @@ export default class Bola {
if (this.equals(bola)) {
return Number.POSITIVE_INFINITY;
}
var dpx = bola.p.x - this.p.x;
var dpy = bola.p.y - this.p.y;
var dvx = bola.v.x - this.v.x;
var dvy = bola.v.y - this.v.y;
var dpdv = dvx * dpx + dvy * dpy;
let dpx = bola.p.x - this.p.x;
let dpy = bola.p.y - this.p.y;
let dvx = bola.v.x - this.v.x;
let dvy = bola.v.y - this.v.y;
let dpdv = dvx * dpx + dvy * dpy;
if (dpdv > 0) {
return Number.POSITIVE_INFINITY;
}
var dvdv = dvx * dvx + dvy * dvy;
var dpdp = dpx * dpx + dpy * dpy;
var R = bola.r + this.r;
var D = dpdv * dpdv - dvdv * (dpdp - R * R);
let dvdv = dvx * dvx + dvy * dvy;
let dpdp = dpx * dpx + dpy * dpy;
let R = bola.r + this.r;
let D = dpdv * dpdv - dvdv * (dpdp - R * R);
if (D < 0) {
return Number.POSITIVE_INFINITY;
}
Expand Down Expand Up @@ -149,15 +152,15 @@ export default class Bola {

// Resolución de la colisión
bounceOff(bola: Bola) {
var dpx = bola.p.x - this.p.x;
var dpy = bola.p.y - this.p.y;
var dvx = bola.v.x - this.v.x;
var dvy = bola.v.y - this.v.y;
var dpdv = dpx * dvx + dpy * dvy;
var R = this.r + bola.r;
var J = (2 * this.m * bola.m * dpdv) / ((this.m + bola.m) * R);
var Jx = (J * dpx) / R;
var Jy = (J * dpy) / R;
let dpx = bola.p.x - this.p.x;
let dpy = bola.p.y - this.p.y;
let dvx = bola.v.x - this.v.x;
let dvy = bola.v.y - this.v.y;
let dpdv = dpx * dvx + dpy * dvy;
let R = this.r + bola.r;
let J = (2 * this.m * bola.m * dpdv) / ((this.m + bola.m) * R);
let Jx = (J * dpx) / R;
let Jy = (J * dpy) / R;
this.v.x += Jx / this.m;
this.v.y += Jy / this.m;
bola.v.x -= Jx / bola.m;
Expand Down
61 changes: 40 additions & 21 deletions aplicaciones/www/fuente/Sim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export default class Sim {
bolas: Bola[];
pq: MinPQ;
cl: number;
cantidadMuros: number;
bolasCoronadas: number;

constructor(bolas: Bola[], cl: number) {
constructor(bolas: Bola[], cl: number, cantidadMuros: number) {
if (bolas == null) {
throw new Error('Sim constructor requires array of balls');
}
for (var i = 0; i < bolas.length; i++) {
for (let i = 0; i < bolas.length; i++) {
if (bolas[i] == null) {
throw new Error('Invalid ball passed to Sim constructor');
}
Expand All @@ -25,8 +27,10 @@ export default class Sim {
this.bolas = bolas;
this.pq = new MinPQ();
this.cl = cl;
this.cantidadMuros = cantidadMuros;
this.bolasCoronadas = 0;

for (var i = 0; i < bolas.length; i++) {
for (let i = 0; i < bolas.length; i++) {
this.predecirTodo(this.bolas[i]);
}
}
Expand All @@ -35,8 +39,8 @@ export default class Sim {
if (bola == null) {
return;
}
var dt;
for (var i = 0; i < this.bolas.length; i++) {
let dt;
for (let i = 0; i < this.bolas.length; i++) {
//
//
// Uncomment this once the wall collisions are working,
Expand Down Expand Up @@ -66,8 +70,8 @@ export default class Sim {
if (bola == null) {
return;
}
var dt;
for (var i = 0; i < this.bolas.length; i++) {
let dt;
for (let i = 0; i < this.bolas.length; i++) {
//
//
// Uncomment this once the wall collisions are working,
Expand All @@ -87,7 +91,7 @@ export default class Sim {
if (bola == null) {
return;
}
var dt = bola.timeToHitVerticalWall(this.cl);
let dt = bola.timeToHitVerticalWall(this.cl);
if (isFinite(dt) && dt > 0) {
//console.log('Vert event inserted');
this.pq.insertar(new SimEvent(this.tiempo + dt, null, bola));
Expand All @@ -97,7 +101,7 @@ export default class Sim {
if (bola == null) {
return;
}
var dt = bola.timeToHitHorizontalWall(this.cl);
let dt = bola.timeToHitHorizontalWall(this.cl);
if (isFinite(dt) && dt > 0) {
//console.log('Horiz event inserted');
this.pq.insertar(new SimEvent(this.tiempo + dt, bola, null));
Expand All @@ -106,20 +110,35 @@ export default class Sim {

redibujar(ctx: CanvasRenderingContext2D) {
const cl = this.cl;

ctx.clearRect(0, 0, cl, cl);
for (var i = 0; i < this.bolas.length; i++) {
this.bolas[i].dibujar(ctx);
for (let i = 0; i < this.bolas.length; i++) {
const bola = this.bolas[i];
bola.dibujar(ctx);
}

this.bolasCoronadas = this.bolas.reduce(
(conteo, bola) => {
if (!bola.activo) conteo += 1;
return conteo;
},
0 - Math.ceil(this.cantidadMuros)
);

if (this.bolasCoronadas > 100 - this.cantidadMuros) {
console.log('Fin de la simulación');
}
//console.log(this.bolasCoronadas);
}

// 'Increment' the simulation by time dt
simulate(dt: number) {
var simLog = 'Start time: ' + this.tiempo + '\n';
var end = this.tiempo + dt;
var minEvent;
var inc;
let simLog = 'Start time: ' + this.tiempo + '\n';
let end = this.tiempo + dt;
let minEvent;
let inc;

var counter = 0;
let counter = 0;
while (!this.pq.isEmpty()) {
// Check min event time. If outside time window, break.
// Otherwise, delete it. If not valid, continue.
Expand All @@ -139,13 +158,13 @@ export default class Sim {
simLog += 'Evento válido: ' + minEvent.type() + '; ';
inc = minEvent.time - this.tiempo;

for (var i = 0; i < this.bolas.length; i++) {
for (let i = 0; i < this.bolas.length; i++) {
this.bolas[i].mover(inc);
}
this.tiempo = minEvent.time;

var a = minEvent.a;
var b = minEvent.b;
let a = minEvent.a;
let b = minEvent.b;
if (a !== null && b !== null) {
a.bounceOff(b);
simLog += 'Bounced off particle\n';
Expand Down Expand Up @@ -174,11 +193,11 @@ export default class Sim {
}

inc = end - this.tiempo;
for (var i = 0; i < this.bolas.length; i++) {
for (let i = 0; i < this.bolas.length; i++) {
this.bolas[i].mover(inc);
}
this.tiempo = end;

// console.log(this.tiempo);
//console.log(simLog);
}
}
2 changes: 1 addition & 1 deletion aplicaciones/www/fuente/SimEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class SimEvent {

isValid(simTime: number, longitudCanvas: number) {
// Nota: se usa toFixed(4) para evitar posibles errores de exactitud de floating-point
var log = '';
let log = '';
// Note: this check forces only one event at a given instant
if (this.time < simTime) {
log += 'Event precedes simulation time';
Expand Down
8 changes: 4 additions & 4 deletions aplicaciones/www/fuente/mapa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { DatosInclusion } from '../../../tipos/compartidos';

export default async function mapa() {
const datos = await pedirDatos<DatosInclusion[]>('/inclusion-municipios.json');
console.log(datos);
// console.log(datos);
const geoJson: FeatureCollection = { type: 'FeatureCollection', features: [] };

datos.forEach((lugar) => {
geoJson.features.push({
type: 'Feature',
properties: { ranking: lugar.valor, poblacion: lugar.poblacionTotal, mun: lugar.nombre, dep: lugar.dep },
properties: { ranking: lugar.valorRank, poblacion: lugar.poblacionTotal, mun: lugar.nombre, dep: lugar.dep },
geometry: { type: 'Point', coordinates: [lugar.longitud, lugar.latitud] },
});
});
console.log(geoJson);
// console.log(geoJson);
let map: Map;
const contenedorMapa = document.getElementById('contenedorMapa') as HTMLDivElement;
const estilo = 'mapbox://styles/enflujo/cltixf9jp000h01pfdd2oby94';
Expand Down Expand Up @@ -109,7 +109,7 @@ export default async function mapa() {
const ranking = punto.properties.ranking as number;
const municipio = punto.properties.mun;
const departamento = punto.properties.dep;
console.log(punto);
// console.log(punto);
new mapboxgl.Popup()
.setLngLat(coords)
.setHTML(
Expand Down
4 changes: 2 additions & 2 deletions aplicaciones/www/fuente/programa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fetch('/inclusion-municipios.json')
inicio(datos);
});

function inicio(datos: { nombre: string; dep: string; valor: number; encuestado: boolean }[]) {
function inicio(datos: { nombre: string; dep: string; valorRank: number; encuestado: boolean }[]) {
const contenedorLista = document.getElementById('contenedorLista');
const lista = document.createElement('ul');

Expand All @@ -27,7 +27,7 @@ function inicio(datos: { nombre: string; dep: string; valor: number; encuestado:
departamento.className = 'departamento';

const valor = document.createElement('span');
valor.innerText = `${lugar.valor}`;
valor.innerText = `${lugar.valorRank}`;
valor.className = 'valor';

elemento.appendChild(municipio);
Expand Down
9 changes: 9 additions & 0 deletions aplicaciones/www/fuente/scss/estilos.scss
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ button {
margin: 0px 20px 0px 20px;
}


#contador {
border: solid 2px;
padding: 0.5em;
width: fit-content;
margin: 1em auto;
}

#contenedorIntroduccion {
height: 100vh;
background-color: var(--naranja);
Expand Down Expand Up @@ -119,4 +127,5 @@ button {
}
}
}

}
Loading

0 comments on commit 3e0874b

Please sign in to comment.