Chemistry Add-In for Microsoft Word

CML is Cool

  • Home
  • History
  • Download
  • Consultancy
  • Sponsors
  • Under the Hood
  • News
  • Shop
    • My account
    • Basket
  • Support ▾
    • Knowledge Base
    • Usage
    • FAQs
    • Tutorials

Under the Hood: 1- Modelling Chemistry

12th October 2022 by Clyde Davies

This is the first in (what I hope will be) a series of occasional postings.  Targeted at developers mainly, these will cover some of the problems we come across, and how we solve them.

Chem4Word is about ten years old.  It started out as a research project.  Such projects typically explore new ways of solving problems.  They don’t set out to optimize established approaches.  When you keep adding new functions at the expense of making existing ones work better, you end up with cruft.

The cruftiest portion of Chem4Word was its in-memory chemistry handling.  Chem4Word’s standard file format is Chemical Markup Language, an XML dialect. There was no logical differentiation between the file containing the information and its in-memory representation.  The code simply loaded the CML as-is into an XDocument.  It then manipulated the XML directly.  This was very wasteful as it required the XML parsed every time we needed to manipulate something.  XDocuments are fine for limited XML editing, but when you’re constantly hitting the document then the conversion overhead becomes huge.

Some CML for a simple molecule!

So, we wrote a completely new, in-memory Model class in C#. The Model in essence represents a chemical drawing.   A Model contains Molecules,  which can contain other Molecule objects or Atom and Bond collections.

Atoms obviously are the building blocks of Molecules.  We store these in Molecules as dictionaries.  These allow us to quickly retrieve an Atom by its label, such as ‘a1’.  Bonds are simple collections, which reference a StartAtom and EndAtom  as a text label.  So Bond ‘b1’ would link Atoms ‘a1’ and ‘a2’, for example.

Atoms obviously have to reference a specific Element. There are obviously a limited number of these, stored in a single global PeriodicTable.

Each Molecule also contains what we term as emergent objects.  So, a Molecule has a collection of Rings, and each Ring collects several Atoms.   Atoms can be shared by more than one ring.

This simple approach allows us to create compact in-memory representations of quite complex molecules.  The XML representation, instead of being central, is now relegated purely to persistence roles.  Previewing, in-document rendering, editing and many other functions now work on the  Model.  A dedicated CMLConverter class handles conversion to and from the CML format, typically when storing this in a Custom XML Part.

No doubt some purists will take issue with this rather literal implementation but we (and you) are already reaping the benefits.   The improved performance is by far the most dramatic effect.  Even on a relatively fast machine, rendering insulin in the document used to take six or seven minutes.  Now it takes a few seconds.  We have seen performance improvements of 400-500% on small molecules and 12,000% on large ones!

One final benefit comes from exposing the atoms and bonds as objects in their own right, and organizing them as collections.  We can now data-bind to them.  Windows Presentation Foundation makes heavy use of data binding.  So, we could theoretically data-bind various visual elements to their corresponding logical ones.  And this is precisely what we do in the FlexDisplay component, which we will cover in a later post.

Filed Under: News, Under the Hood

Recent Posts

  • Chem4Word – V3.3.10 Release 8
  • Why Should I Use Chem4Word?
  • SketchEl file support now included in V3.3
  • Improved Chemical Libraries are live in Chem4Word!
  • Draw Chemical Reactions Easily and Quickly with Chem4Word!

Archives

  • February 2025
  • July 2023
  • June 2023
  • March 2023
  • December 2022
  • November 2022
  • October 2022
  • March 2022
  • September 2020
  • August 2020
  • December 2018
  • April 2017
  • January 2017
  • December 2016
  • September 2016
  • December 2015
  • Privacy Policy – Chemistry Add-In for Microsoft Word
  • About Us
  • Contact Us
  • Cookie Policy (UK)

Copyright © 2025 · "Supported by the .NET Foundation" · Log in

Manage Cookie Consent
We use technologies like cookies to store and/or access device information. We do this to improve browsing experience and to show (non-) personalised ads. Consenting to these technologies will allow us to process data such as browsing behaviour or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}