Skip to content

Commit

Permalink
coincided atoms handling
Browse files Browse the repository at this point in the history
fix #42
  • Loading branch information
stan-donarise committed Jul 17, 2024
1 parent b4c9bb2 commit 1f71eae
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 66 deletions.
16 changes: 2 additions & 14 deletions matinfio/matinfio.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace $ {
x: number,
y: number,
z: number,
},
} | null,
x: number,
y: number,
z: number,
Expand Down Expand Up @@ -60,19 +60,7 @@ namespace $ {
symlabel?: string,
},
overlayed: Record< string, string >,
atoms: {
fract: {
x: number,
y: number,
z: number,
} | null,
x: number,
y: number,
z: number,
c: string, //color
r: number, //radius
overlays: Record< string, string | number >,
}[],
atoms: $optimade_cifplayer_matinfio_internal_obj_atom[]
sg_name: string,
ng_name: number,
info: string,
Expand Down
122 changes: 76 additions & 46 deletions matinfio/player/player.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,63 +37,87 @@ namespace $ {
mpds_data: crystal.mpds_data,
mpds_demo: crystal.mpds_demo
}
const pos2els: any = {}
const hashes: any = {}

const in_pos: Map< string, $optimade_cifplayer_matinfio_internal_obj_atom[] > = new Map

for( let i = 0; i < crystal.atoms.length; i++ ) {
const pos = [ crystal.atoms[ i ].x, crystal.atoms[ i ].y, crystal.atoms[ i ].z ]
const hash = pos.map( function( item ) { return item.toFixed( 2 ) } ).join( ',' )
const atom = crystal.atoms[ i ]

const pos = [ atom.x, atom.y, atom.z ]

// CIF has fractional positions
// OPTIMADE has cartesian positions
// POSCAR may have either of two
let cpos = pos
let fpos: number[] | null = null
if( !crystal.cartesian ) {
fpos = pos.map( fract_cord_norm )
cpos = math.multiply( fpos, cell_matrix )

} else if( cell_matrix ) {
fpos = math.divide( pos, cell_matrix ).map( fract_cord_norm )
}

const pos_hash = ( fpos ?? cpos ).map( c => c.toFixed( 2 ) ).join( ',' )

// make atoms unique, i.e. remove collisions;
// makes special sense for partial occupancies
if( hashes.hasOwnProperty( hash ) ) {
var update = ""
for( let oprop in render.atoms[ hashes[ hash ] ].overlays ) {
if( in_pos.get( pos_hash )?.length! > 0 ) {

const first = in_pos.get( pos_hash )![0]

for( let oprop in first.overlays ) {

if( oprop == 'S' ) {
if( pos2els[ hash ].indexOf( crystal.atoms[ i ].symbol ) == -1 ) {
update = " " + crystal.atoms[ i ].symbol
pos2els[ hash ].push( crystal.atoms[ i ].symbol )
if( in_pos.get( pos_hash )?.every( a => a.symbol != atom.symbol ) ) {
first.overlays[ oprop ] += ' ' + atom.symbol
}

} else if( oprop == 'N' ) {
first.overlays[ oprop ] += ', ' + ( i + 1 )

} else if( oprop == '_atom_site_occupancy' ) {
first.overlays[ oprop ] += '+' + atom.overlays[ oprop ]

} else {
first.overlays[ oprop ] += ' ' + atom.overlays[ oprop ]
}
else if( oprop == 'N' )
update = ", " + ( i + 1 )
else if( oprop == '_atom_site_occupancy' )
update = "+" + crystal.atoms[ i ].overlays[ oprop ]
else
update = " " + crystal.atoms[ i ].overlays[ oprop ]

render.atoms[ hashes[ hash ] ].overlays[ oprop ] += update

}
} else {
const color = ($optimade_cifplayer_matinfio_chemical_elements.JmolColors as any)[ crystal.atoms[ i ].symbol ] || '#FFFF00'
const radius = ($optimade_cifplayer_matinfio_chemical_elements.AseRadii as any)[ crystal.atoms[ i ].symbol ] || 0.66

const overlays: Record< string, string | number > = {
"S": crystal.atoms[ i ].symbol,
"N": i + 1,
}
for( let oprop in crystal.atoms[ i ].overlays ) {
overlays[ oprop ] = crystal.atoms[ i ].overlays[ oprop ]
}
continue
}

const overlays: Record< string, string | number > = {
"S": atom.symbol,
"N": i + 1,
}
for( let oprop in atom.overlays ) {
overlays[ oprop ] = atom.overlays[ oprop ]
}

// CIF has fractional positions
// OPTIMADE has cartesian positions
// POSCAR may have either of two
const cpos = crystal.cartesian ? pos : math.multiply( pos, cell_matrix )
const fpos = !crystal.cartesian ? pos : cell_matrix ? math.divide( pos, cell_matrix ) : null
const fract = fpos ? { 'x': fpos[ 0 ], 'y': fpos[ 1 ], 'z': fpos[ 2 ] } : null

render.atoms.push( {
'fract': fract,
'x': cpos[ 0 ],
'y': cpos[ 1 ],
'z': cpos[ 2 ],
'c': color,
'r': radius,
'overlays': overlays
} )
hashes[ hash ] = render.atoms.length - 1
pos2els[ hash ] = [ crystal.atoms[ i ].symbol ]
const color = ($optimade_cifplayer_matinfio_chemical_elements.JmolColors as any)[ atom.symbol ] || '#FFFF00'
const radius = ($optimade_cifplayer_matinfio_chemical_elements.AseRadii as any)[ atom.symbol ] || 0.66
const atom_result = {
fract: fpos ? {
x: fpos[ 0 ],
y: fpos[ 1 ],
z: fpos[ 2 ],
} : null,
x: cpos[ 0 ],
y: cpos[ 1 ],
z: cpos[ 2 ],
c: color,
r: radius,
overlays,
symbol: atom.symbol,
label: atom.label,
}

render.atoms.push( atom_result )

in_pos.get( pos_hash )?.push( atom_result ) ?? in_pos.set( pos_hash, [ atom_result ] )

}

for( let oprop in crystal.atoms.at(-1)!.overlays ) {
Expand All @@ -102,5 +126,11 @@ namespace $ {

return render
}


function fract_cord_norm( cord: number ){
const res = cord % 1
return res > 0 ? res : res + 1
}

}
11 changes: 5 additions & 6 deletions matinfio/spacegroup/spacegroup.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ namespace $ {
const spans = symmetry.split( ',' )

const fract = {
x: fract_cord_norm( calc_symmetry_span( spans[ 0 ], atom.fract ) ),
y: fract_cord_norm( calc_symmetry_span( spans[ 1 ], atom.fract ) ),
z: fract_cord_norm( calc_symmetry_span( spans[ 2 ], atom.fract ) ),
x: fract_cord_norm( calc_symmetry_span( spans[ 0 ], atom.fract! ) ),
y: fract_cord_norm( calc_symmetry_span( spans[ 1 ], atom.fract! ) ),
z: fract_cord_norm( calc_symmetry_span( spans[ 2 ], atom.fract! ) ),
}

const [ x, y, z ] = math.multiply( [ fract.x, fract.y, fract.z ], cell )
Expand Down Expand Up @@ -96,9 +96,8 @@ namespace $ {
}

function fract_cord_norm( cord: number ){
let res = cord % 1
if( res < 0 ) res = res + 1
return res
const res = cord % 1
return res > 0 ? res : res + 1
}

}

0 comments on commit 1f71eae

Please sign in to comment.