Commit f420780f authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Fixed issue in data not being correctly loaded into viewer on changing...

Fixed issue in data not being correctly loaded into viewer on changing archive, fixed issue in fitting structure into canvas.
parent 0f61452d
Pipeline #80732 passed with stages
in 42 minutes and 10 seconds
......@@ -4,7 +4,7 @@
"commit": "e98694e",
"private": true,
"dependencies": {
"@lauri-codes/materia": "0.0.3",
"@lauri-codes/materia": "0.0.4",
"@material-ui/core": "^4.0.0",
"@material-ui/icons": "^4.0.0",
"@material-ui/lab": "^4.0.0-alpha.49",
......
......@@ -12,6 +12,7 @@ import { Matrix, Number } from './visualizations'
import Structure from '../visualization/Structure'
import { StructureViewer } from '@lauri-codes/materia'
import Markdown from '../Markdown'
import { convert } from '../../utils'
export const configState = atom({
key: 'config',
......@@ -22,15 +23,21 @@ export const configState = atom({
}
})
// Context for sharing data that is expensive to create
// Context for sharing data across the browser sections
const ArchiveContext = React.createContext()
// Shared instance of the StructureViewer
const viewer = new StructureViewer()
// Contains details about the currently visualized system. Used to detect if a
// reload is needed for the StructureViewer.
const visualizedSystem = {}
export default function ArchiveBrowser({data}) {
const searchOptions = useMemo(() => archiveSearchOptions(data), [data])
return (
<ArchiveContext.Provider value={{
structure: viewer
id: data.section_metadata.calc_id
}}>
<Browser
adaptor={archiveAdaptorFactory(data)}
......@@ -307,7 +314,26 @@ QuantityValue.propTypes = ({
function getVisualization(section, def, archiveContext) {
// Structure visualization for section_system
if (def.name === 'section_system') {
return <Structure viewer={archiveContext.structure} system={section}></Structure>
// If a structure with the same id is loaded, it is assumed that only the
// positions need to be updated on data change.
let system
let positionsOnly = false
if (archiveContext.id === visualizedSystem.id) {
positionsOnly = true
system = {
positions: convert(section.atom_positions, 'm', 'angstrom')
}
} else {
system = {
'atomicNumbers': section.atom_species,
'cell': convert(section.lattice_vectors, 'm', 'angstrom'),
'positions': convert(section.atom_positions, 'm', 'angstrom'),
'pbc': section.configuration_periodic_dimensions
}
}
visualizedSystem.id = archiveContext.id
return <Structure viewer={viewer} system={system} id={archiveContext.id} positionsOnly={positionsOnly}></Structure>
}
}
......
......@@ -23,11 +23,9 @@ import {
Replay
} from '@material-ui/icons'
import { StructureViewer } from '@lauri-codes/materia'
import { convert } from '../../utils'
export default function Structure(props) {
// States
const [loaded, setLoaded] = React.useState(props.viewer === undefined ? false : props.viewer.structure !== undefined)
const [anchorEl, setAnchorEl] = React.useState(null)
const [fullscreen, setFullscreen] = useState(false)
const [showBonds, setShowBonds] = useState(true)
......@@ -168,23 +166,21 @@ export default function Structure(props) {
if (props.system === undefined) {
return
}
let nAtoms = props.system.atom_species.length
if (nAtoms >= props.sizeLimit) {
setError('Visualization is disabled due to large system size.')
if (props.positionsOnly) {
viewer.current.setPositions(props.system.positions)
return
}
// If a structure has already been loaded and a viewer has been pre-created,
// it is assumed that only the positions need to be updated on data change.
if (props.viewer !== undefined && loaded) {
let positions = convert(props.system.atom_positions, 'm', 'angstrom')
viewer.current.setPositions(positions)
let nAtoms = props.system.atomicNumbers.length
if (nAtoms >= props.sizeLimit) {
setError('Visualization is disabled due to large system size.')
return
}
// Systems with cell are centered on the cell center and orientation is defined
// by the cell vectors.
let cell = props.system.lattice_vectors
let cell = props.system.cell
if (cell !== undefined) {
viewer.current.setOptions({layout: {
viewCenter: 'COC',
......@@ -211,17 +207,10 @@ export default function Structure(props) {
}
}})
}
var system = {
'atomicNumbers': props.system.atom_species,
'cell': convert(props.system.lattice_vectors, 'm', 'angstrom'),
'positions': convert(props.system.atom_positions, 'm', 'angstrom'),
'pbc': props.system.configuration_periodic_dimensions
}
viewer.current.load(system)
viewer.current.load(props.system)
viewer.current.fitToCanvas()
viewer.current.saveReset()
viewer.current.reset()
setLoaded(true)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
......@@ -362,12 +351,14 @@ export default function Structure(props) {
}
Structure.propTypes = {
viewer: PropTypes.object, // Optional shared viewer instance.
id: PropTypes.string, // Id for the visualized structure.
system: PropTypes.object, // The system to display as section_system
options: PropTypes.object, // Viewer options
captureName: PropTypes.string, // Name of the file that the user can download
viewer: PropTypes.any, // An optional shared instance of StructureViewer
aspectRatio: PropTypes.number, // Fixed aspect ratio for the viewer canvas
sizeLimit: PropTypes.number // Maximum number of atoms to attempt to display
sizeLimit: PropTypes.number, // Maximum number of atoms to attempt to display
positionsOnly: PropTypes.bool // Whether to update only positions. This is much faster than loading the entire structure.
}
Structure.defaultProps = {
aspectRatio: 4 / 3,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment