Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create MongoDB with DMS (data management system) and dot-notation #11

Open
eudoxos opened this issue Jul 27, 2022 · 2 comments
Open

create MongoDB with DMS (data management system) and dot-notation #11

eudoxos opened this issue Jul 27, 2022 · 2 comments

Comments

@eudoxos
Copy link
Member

eudoxos commented Jul 27, 2022

Table Beam {
  id int [unique]
  cs CrossSection
  length double
  height double
  density double
  bc_0 boolean[3]
  bc_l boolean[3]
}

Table CrossSection {
  id int [unique]
  beamId int
  rve ConcreteRVE
  rvePositions double[npoints]
  
}

Table ConcreteRVE {
  id int [unique]
  ct CTScan
  origin position
  size double
  //cs CrossSection // cs to which rve belongs to
  //z double // position in a stack
  vectorizedMicrostructure list_of_particle_gometries
  discretizedMicrostructure mesh
  particleStatistics TBD
  nmat int
  materials int[nmat]
}

Table CTScan {
  id int [unique]
  image varbinary
}

// Creating references
// You can also define relaionship separately
// > many-to-one; < one-to-many; - one-to-one
Ref: ConcreteRVE.ct  > CTScan.id
Ref: CrossSection.beamId - Beam.id
Ref: CrossSection.rve < ConcreteRVE.id

Table BeamState {
  id int [unique]
  beam Beam
  npointz int // number of rve per height
  csState CrossSectionState[npointsz]
  w double(nsteps, npos)
}

Ref: BeamState.beam < Beam.id
Ref: BeamState.csState - CrossSectionState.id

Table CrossSectionState {
  id int [unique]
  cs id
  rveStates ConcreteRVEState[npoints]
  BendingMoment double[t]
  kappa double[t]
  eps_axial double[t]
}
 
Ref: CrossSectionState.cs < CrossSection.id

Table ConcreteRVEState {
  id int [unique] 
  rve ConcreteRVE
  sigmaHom double[t]
  epsHom double[t]
  Stiffness double[3t]
  eps0Hom [t]
}

Ref: ConcreteRVEState.rve < ConcreteRVE.id
Ref: CrossSectionState.rveStates < ConcreteRVEState.id

Table MaterialRecord {
  id int [unique]
  name varcharacter
  type mattype
  props dict
}

Ref: ConcreteRVE.materials < MaterialRecord.id
@eudoxos
Copy link
Member Author

eudoxos commented Aug 2, 2022

For the record, attaching MongoDB dump with these collections
DMS00-dump.zip
. Follows an example on traversing the structure via AST.

# connect to Mongo and open the top-level database
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')['DMS00']
from rich.pretty import pprint
import pymongo
# maps Collection.attribute to another Collection in the same DB (lookup by "id")
structs={
    'Beam.cs':'CrossSection',
    'CrossSection.rve':'ConcreteRVE',
    'ConcreteRVE.ct':'CTScan',
    'ConcreteRVE.materials':'MaterialRecord',
    'BeamState.beam':'Beam',
    'BeamState.csState':'CrossSectionState',
    'CrossSectionState.rveStates':'ConcreteRVEState',
    'CrossSectionState.cs':'CrossSection',
    'ConcreteRVEState.rve':'ConcreteRVE'
}
import typing
import dataclasses
@dataclasses.dataclass
class DotNode:
    what: str
    param: str
    value: typing.Any

import ast
class MupifObjectTraversal(ast.NodeVisitor):
    def __init__(self,db,expr):
        self.tree=ast.parse(expr,filename='<mupif-expr>')
        self.seq=[]
        self.val=None
        self.db=self.curr=db
    def __call__(self): 
        self.generic_visit(self.tree)
        return self.seq
    def visit(self,node):
        if isinstance(node,ast.Load): return # ignored
        allowed=[ast.Expr,ast.Attribute,ast.Name,ast.Subscript,ast.Load,ast.Constant]
        if node.__class__ not in allowed: raise SyntaxError(f'Node {node.__class__.__name__} not allowed (only {",".join([c.__name__ for c in allowed])} are)')
        return super().visit(node)
    def visit_Subscript(self,node):
        'bracketed subscript'
        self.generic_visit(node)
        assert isinstance(self.curr,pymongo.collection.Collection)
        self.val=self.curr.find_one({'id':node.slice.value})
        if self.val is None: raise IndexError(f'Invalid id {node.slice.value} (not found in {self.curr})')
        self.seq.append(DotNode(node.__class__.__name__.upper(),node.slice.value,self.val))
    def visit_Attribute(self,node):
        'dotted attribute'
        self.generic_visit(node)
        # is a pointer to another database
        if (k:=f'{self.curr.name}.{node.attr}') in structs: self.curr=self.db[structs[k]]
        # is a terminal value
        else: self.curr=self.val[node.attr]
        self.seq.append(DotNode(node.__class__.__name__.upper(),node.attr,self.curr))
    def visit_Name(self,node):
        'top named object'
        self.generic_visit(node)
        self.curr=self.curr[node.id]
        self.seq.append(DotNode(node.__class__.__name__.upper(),node.id,self.curr))

pprint(MupifObjectTraversal(db=client,expr='BeamState[0].beam.cs.rve.ct.id')())

pprint(MupifObjectTraversal(db=client,expr='BeamState[0].csState[0].rveStates[1].rve.ct.id')())

pprint(MupifObjectTraversal(db=client,expr='BeamState[0].csState[0].rveStates[1].rve.materials[0].name')())
[
DotNode(
│   │   what='NAME',
│   │   param='BeamState',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'BeamState')
),
DotNode(
│   │   what='SUBSCRIPT',
│   │   param=0,
│   │   value={
│   │   │   '_id': ObjectId('62e9145b9bb766a61709416b'),
│   │   │   'id': 0,
│   │   │   'npointz': 1,
│   │   │   'csState': [0, '1'],
│   │   │   'w': []
│   │   }
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='beam',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'Beam')
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='cs',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CrossSection')
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='rve',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVE')
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='ct',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CTScan')
),
DotNode(what='ATTRIBUTE', param='id', value=0)
]
[
DotNode(
│   │   what='NAME',
│   │   param='BeamState',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'BeamState')
),
DotNode(
│   │   what='SUBSCRIPT',
│   │   param=0,
│   │   value={
│   │   │   '_id': ObjectId('62e9145b9bb766a61709416b'),
│   │   │   'id': 0,
│   │   │   'npointz': 1,
│   │   │   'csState': [0, '1'],
│   │   │   'w': []
│   │   }
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='csState',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CrossSectionState')
),
DotNode(
│   │   what='SUBSCRIPT',
│   │   param=0,
│   │   value={
│   │   │   '_id': ObjectId('62e969d69bb766a6170941bd'),
│   │   │   'id': 0,
│   │   │   'cs': 0,
│   │   │   'rveStates': [0, 1],
│   │   │   'BendingMoment': [0, 0],
│   │   │   'kappa': [1, 1],
│   │   │   'eps_axial': [2, 2, 2]
│   │   }
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='rveStates',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVEState')
),
DotNode(
│   │   what='SUBSCRIPT',
│   │   param=1,
│   │   value={
│   │   │   '_id': ObjectId('62e969c39bb766a6170941b2'),
│   │   │   'id': 1,
│   │   │   'rve': 1,
│   │   │   'sigmaHom': [1, 1, 1],
│   │   │   'epsHom': [1, 1, 1],
│   │   │   'Stiffness': [11, 12, 13, 14, 15, 16, 17, 18, 19],
│   │   │   'eps0Hom': [1, 1, 1]
│   │   }
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='rve',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVE')
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='ct',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CTScan')
),
DotNode(what='ATTRIBUTE', param='id', value=1)
]
[
DotNode(
│   │   what='NAME',
│   │   param='BeamState',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'BeamState')
),
DotNode(
│   │   what='SUBSCRIPT',
│   │   param=0,
│   │   value={
│   │   │   '_id': ObjectId('62e9145b9bb766a61709416b'),
│   │   │   'id': 0,
│   │   │   'npointz': 1,
│   │   │   'csState': [0, '1'],
│   │   │   'w': []
│   │   }
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='csState',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CrossSectionState')
),
DotNode(
│   │   what='SUBSCRIPT',
│   │   param=0,
│   │   value={
│   │   │   '_id': ObjectId('62e969d69bb766a6170941bd'),
│   │   │   'id': 0,
│   │   │   'cs': 0,
│   │   │   'rveStates': [0, 1],
│   │   │   'BendingMoment': [0, 0],
│   │   │   'kappa': [1, 1],
│   │   │   'eps_axial': [2, 2, 2]
│   │   }
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='rveStates',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVEState')
),
DotNode(
│   │   what='SUBSCRIPT',
│   │   param=1,
│   │   value={
│   │   │   '_id': ObjectId('62e969c39bb766a6170941b2'),
│   │   │   'id': 1,
│   │   │   'rve': 1,
│   │   │   'sigmaHom': [1, 1, 1],
│   │   │   'epsHom': [1, 1, 1],
│   │   │   'Stiffness': [11, 12, 13, 14, 15, 16, 17, 18, 19],
│   │   │   'eps0Hom': [1, 1, 1]
│   │   }
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='rve',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVE')
),
DotNode(
│   │   what='ATTRIBUTE',
│   │   param='materials',
│   │   value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'MaterialRecord')
),
DotNode(
│   │   what='SUBSCRIPT',
│   │   param=0,
│   │   value={
│   │   │   '_id': ObjectId('62e914fb9bb766a617094171'),
│   │   │   'id': 0,
│   │   │   'name': 'material0',
│   │   │   'type': None,
│   │   │   'props': {'manufacturer': 'Unemployed Philosophers Guild'}
│   │   }
),
DotNode(what='ATTRIBUTE', param='name', value='material0')
]

@eudoxos
Copy link
Member Author

eudoxos commented Aug 2, 2022

The beanie module might be useful for defining the data structure: as pydantic classes, beanie interfacing with MongoDB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant