libAtoms / abcd

1 stars 4 forks source link

Query for Filtering Between Force Thresholds #85

Open htunstall opened 4 years ago

htunstall commented 4 years ago

When doing some database querying I found it rather convoluted to filter by a min/max force component (so that you select all documents that have no forces outside these criteria) using standard mongodb terms.

The following query will only return documents with all its force components contained within the bounds of force_lim:

{"$and" : [
    { "dft_forces" : { "$not" : { "$elemMatch" : { "$elemMatch" : { "$gt" :   force_lim }}}}},
    { "dft_forces" : { "$not" : { "$elemMatch" : { "$elemMatch" : { "$lt" : - force_lim }}}}}
]}

You could also create an absolute max force component field and query it as such:

{"abs_max_fc"   : {"$lte" : force_lim}}

However creating this involves the following code:

for d in db.atoms.find({}):
    dft_forces = np.concatenate(d["dft_forces"], axis=0).flatten()

    new_data = np.amax(np.abs(dft_forces))
    new_tag  = "abs_max_fc"

    db.atoms.update({"_id": d["_id"]}, {"$set": {new_tag : new_data}})
    db.atoms.update_one({"_id": d["_id"]}, {"$push" : {"derived.derived_keys" : new_tag}})

A potential feature could be to make this more streamlined in future.

gabor1 commented 3 years ago

same as #79