Commit 5dadf385 authored by Berk Onat's avatar Berk Onat

Initial commit of OpenKIM parser in nomad-lab

parents
MIT License
Copyright (c) 2018 Berk Onat
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# OpenKIM Parser
[NOMAD Laboratory CoE](http://nomad-coe.eu) parser for [OpenKIM](https://openkim.org)
## Version 0.0.1
This is the parser for OpenKIM queries at [OpenKIM](https://openkim.org).
The official version lives at:
git@gitlab.mpcdf.mpg.de:nomad-lab/parser-openkim.git
You can browse it at:
https://gitlab.rzg.mpg.de/nomad-lab/parser-openkim
It relies on having the nomad-meta-info and the python-common repositories one level higher.
The simplest way to have this is to check out nomad-lab-base recursively:
git clone --recursive git@gitlab.mpcdf.mpg.de:nomad-lab/nomad-lab-base.git
This parser will be in the directory parsers/openkim of this repository.
## Running and Testing the Parser
### Requirements
The required python packages can be installed with (see [python-common](https://gitlab.rzg.mpg.de/nomad-lab/python-common)):
pip install -r nomad-lab-base/python-common/requirements.txt
### Usage
The query output of OpenKIM simulation results can be parsed with:
python parser-openkim.py test_nomad_id openkim_query_data.json
### Test Files
Example log output files of OpenKIM query can be found in the directory test/examples.
More details about the calculations and files are explained in README file of test/examples.
import requests
from requests.exceptions import Timeout, TooManyRedirects, RequestException, HTTPError
from datetime import datetime
import re
import sys
import numpy as np
import json
from tinydb import TinyDB, Query
dbasefile = "openkim_query_database.json"
kimdb = TinyDB(dbasefile)
atomlabel = 'Al'
structure = 'fcc'
#nomad_space_group_map={
# 'sc' : '221',
# "fcc" : '225',
# 'bcc' : '229',
# 'diamond': '227'
#}
def secondsFromEpoch(date):
epoch = datetime(1970,1,1)
ts=date-epoch
return ts.seconds + ts.microseconds/1000.0
def get_timestep(kim_date):
pdate = None
if kim_date:
pdate = datetime.strptime(kim_date.strip(), "%Y-%m-%d %H:%M:%S.%f")
if pdate:
pdate = secondsFromEpoch(pdate)
return pdate
def OPENKIM_query(atomlabel, structure, properties=None):
"""
atomlabel:
----------
string : element symbol
structure:
----------
string : fcc, bcc, sc, diamond
properties:
-----------
string : lattice_energy, elastic_constants
returns:
--------
dictionary of OpenKIM entry
"""
openkim_query = None
try:
query = requests.post(
url="https://query.openkim.org/api",
data={
'flat' : 'on',
'database': 'data',
'limit' : '0',
'fields' : json.dumps({
#"a.si-value": "1",
#"cohesive-potential-energy.si-value" : "1",
#"meta.subject.kimcode": "1"
}),
'query' : json.dumps({
"meta.type" : "tr",
#"property-id" : {
# "$regex" : ":property/structure-cubic-crystal-npt"
#},
#"meta.runner.kimcode" : {
# "$regex" : "^LatticeConstantCubicEnergy"
#},
#"species.source-value": {
# "$all" : [atomlabel],
# "$not" : {
# "$elemMatch" : {
# "$nin" : [atomlabel]
# }
# }
# },
"short-name.source-value": structure
})
}
)
query.raise_for_status()
openkim_query = query.json()
except(ConnectionError, HTTPError, Timeout, TooManyRedirects, RequestException):
print("OpenKIM request exception: %s" % sys.exc_info()[1])
return openkim_query
openkim_data = OPENKIM_query(atomlabel, structure)
if openkim_data:
now = datetime.today().strftime("%Y-%m-%d %H:%M:%S.%f")
updated_data = []
kim_item = Query()
for item in openkim_data:
kim_instance_id = item["instance-id"]
kim_unique_code = item["meta._id"]
kim_result_code = item["meta.test-result-id"]
kim_unique_code = kim_unique_code + '-' + str(kim_instance_id)
kim_result_code = kim_result_code + '-' + str(kim_instance_id)
kim_create_date = get_timestep(item["meta.created_on"])
kim_insert_date = get_timestep(item["inserted_on"])
db_entry = kimdb.search((kim_item.unique_id == kim_unique_code and
kim_item.result_id == kim_result_code))
if db_entry:
if db_entry[0]["insert_timestep"] < kim_insert_date:
kimdb.update({
'insert_timestep': kim_insert_date,
},
(kim_item.unique_id == kim_unique_code and
kim_item.result_id == kim_result_code))
updated_data.append(item)
else:
kimdb.insert({
'unique_id': kim_unique_code,
'result_id': kim_unique_code,
'insert_timestep': kim_insert_date,
})
updated_data.append(item)
if updated_data:
openkim_query = {}
openkim_query['OPENKIM_QUERY_OUTPUT'] = 'OPENKIM_QUERY_OUTPUT'
openkim_query['QUERY'] = updated_data
with open('data.json', 'w') as outfile:
json.dump(openkim_query, outfile,
sort_keys = True, indent = 4,
ensure_ascii = True)
with open('data.json', 'r') as databfile:
qdata = json.load(databfile)
This diff is collapsed.
import sys
import os
import os.path
basedir = os.path.dirname(os.path.abspath(__file__))
commondir = os.path.normpath(os.path.join(basedir,"../../../../python-common/common/python"))
parserdir = os.path.normpath(os.path.join(basedir, ".."))
if commondir not in sys.path:
sys.path.insert(1, commondir)
if parserdir not in sys.path:
sys.path.insert(1, parserdir)
package eu.nomad_lab.parsers
import eu.nomad_lab
import eu.nomad_lab.DefaultPythonInterpreter
import org.{ json4s => jn }
import eu.{ nomad_lab => lab }
import scala.collection.breakOut
object OpenkimParser extends SimpleExternalParserGenerator(
name = "OpenkimParser",
parserInfo = jn.JObject(
("name" -> jn.JString("OpenkimParser")) ::
("parserId" -> jn.JString("OpenkimParser" + lab.OpenkimVersionInfo.version)) ::
("versionInfo" -> jn.JObject(
("nomadCoreVersion" -> jn.JObject(lab.NomadCoreVersionInfo.toMap.map {
case (k, v) => k -> jn.JString(v.toString)
}(breakOut): List[(String, jn.JString)])) ::
(lab.OpenkimVersionInfo.toMap.map {
case (key, value) =>
(key -> jn.JString(value.toString))
}(breakOut): List[(String, jn.JString)])
)) :: Nil
),
mainFileTypes = Seq("text/.*"),
mainFileRe = """\s*OPENKIM_QUERY_OUTPUT\s*""".r,
cmd = Seq(DefaultPythonInterpreter.pythonExe(), "${envDir}/parsers/openkim/parser/parser-openkim/parser-openkim.py",
"${mainFilePath}", "${mainFilePath}/data.json"),
resList = Seq(
"parser-openkim/parser-openkim.py",
"parser-openkim/setup_paths.py",
"nomad_meta_info/public.nomadmetainfo.json",
"nomad_meta_info/common.nomadmetainfo.json",
"nomad_meta_info/meta_types.nomadmetainfo.json",
"nomad_meta_info/openkim.nomadmetainfo.json"
) ++ DefaultPythonInterpreter.commonFiles(),
dirMap = Map(
"parser-openkim" -> "parsers/openkim/parser/parser-openkim",
"nomad_meta_info" -> "nomad-meta-info/meta_info/nomad_meta_info",
"python" -> "python-common/common/python/nomadcore"
) ++ DefaultPythonInterpreter.commonDirMapping(),
metaInfoEnv = Some(lab.meta.KnownMetaInfoEnvs.openkim)
)
package eu.nomad_lab.parsers
import org.specs2.mutable.Specification
object OpenkimParserTests extends Specification {
"OpenkimParserTest" >> {
"[OpenKIM Query with 3767 entries] test with json-events" >> {
ParserRun.parse(OpenkimParser, "parsers/openkim/test/examples/data.json", "json-events") must_== ParseResult.ParseSuccess
}
"[OpenKIM Query with 3767 entries] test with json" >> {
ParserRun.parse(OpenkimParser, "parsers/openkim/test/examples/data.json", "json") must_== ParseResult.ParseSuccess
}
}
}
# Tests:
### Source of Tests:
This test is genereted with openkim\_tool.py using OpenKIM Query web service.
Please check [OpenKIM Query](https://query.openkim.org)
This diff is collapsed.
Markdown is supported
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